Joe Riel

9530 Reputation

23 Badges

20 years, 29 days

MaplePrimes Activity


These are answers submitted by Joe Riel

selectremove( z -> Re(z) >= 0, ... );

Applying an assumption to a variable creates an assignment that assigns a new variable (theta~) to the original variable (theta). However, the left side of a Maple assignment is not evaluated, so

  theta := 0

assigns a new value to theta that replaces the theta~.   There are two way around this.  One is to use the assign procedure to assign the value:

  assign(theta,0);

That works because theta is evaluated (to theta~) before the assignment is made. The alternative---my preference---is to avoid assigning to the variable and use eval:

  eval(A, theta=0);

Its not clear to me what you are asking.  Regardless, using variables that start with underscores in not a good idea. See ?underscore:

Names Beginning with the Underscore Character

Description

o Any symbol beginning with an underscore (_) is effectively reserved for use
  only by library code. It is not available to users. Failure to observe this
  rule can lead to unexpected results.

I'm not suggesting that that is causing whatever problem you are having, just warning against the practice.

Another possibility is

add(x, x in L);

As a procedure:

f := proc(L) 
local x;
    add(x, x in L);
end proc:

Function composition can also be used

f := `+`@op:
f([a,b,c]);
                  a + b + c

 

From your examples I deduced that you wanted the first element in a list to be s if the number of elements in the list is odd, otherwise the first element is t. After that, the elements alternate between s and t.  This was an easy way to both alternate parity (with j) and ensure that the first element of each sublist was the appropriate term.  Your explanation is correct. 

(**) f := k -> x -> k*x:
(**) g := f(3):
(**) g(a); 
                                                  3 a

The proc form is more general.  It permits multiple statements and optional arguments.

 

[seq([seq(`if`((j+k)::even,s,t),j=1..k)],k=1..5)];
                         [[s], [t, s], [s, t, s], [t, s, t, s], [s, t, s, t, s]]

There are several problems with this

  1. L is reassigned to an empty array in place
  2. L is local to queens, so even if the above were corrected there is an issue.
  3. The inner while loop in queens should negate the result of place.

Here is a modified version.  I made L a parameter to place.

place := proc(k,L)
local i;
    for i to k-1 do
        if L(i)=L(k) # two in the same column
        or abs(L(i)-L(k)) = abs(i-k) # in the same diagonal
        then
            return false;
        fi:
    end do;
    return true;
end proc:

queens := proc(n)
local k,L;
    L := Array(1..n);
    k := 1; # k is the current row
    while k > 0 do
        L(k) := L(k)+1; # move to the next column
        while L(k) <= n and not place(k,L) do # can this queen be placed
            L(k) := L(k) + 1;
        end do;
        if L(k)<=n # a position is found
        then if k = n # the solution has been found
             then print(L);
             else
                 k := k+1;
                 L(k) := 0;
             fi:
        else
            k := k-1; # backtrack
        end if;
    end do;
    return NULL;
end proc:

Why not use ?ListTools:-MakeUnique, as John suggested?  Of course, learning to roll your own solution is a useful exercise.  Here's an interesting approach described by Roman Pearce.

Once you have created the procedure f, as Georgios described, you can plot it with

plot(f, 0..1);

where the 0..1 specifies the domain over which to evaluate f.

An alternative is to forego creating a procedure and instead use an expression.  In that case you have to tell Maple what variable to vary:

ex := x^2+1:
plot(ex, x = 0..1);

It's not the only way, but it is probably the simplest. An alternative, which can be useful in some situations, is to use the `[]` procedure:

`[]`(a,b,c);
                         [a,b,c]

 

Maybe this will help:

plots[odeplot](sol1, [z, denom(rhs(eqn3))], 0 .. 1);

It might be more natural to swap the polarity, but here is a routine you can use to rotate left/right.

Rotate := proc(L, i::integer) 
local k;
    k := modp(i,nops(L)); 
   [L[k+1..][], L[..k][]]; 
end proc:
L := [1,2,3,4,5]:
Rotate(L, 2);
                                [3, 4, 5, 1, 2]
Rotate(L, -2);
                                [4, 5, 1, 2, 3]



That helps, but I'm still not sure I understand.  That is, if E is an nxn matrix, why are you accessing it with one index (E[i])?

I'm going to assume that E is actually a list.  Here are two implementation, which may or may not do what you want.  The first is a straightforward implementation of your original idea (as I understand it), but using seqs.  The algorithm is not particularly efficient.  The second procedure uses a method I described here to first create a permutation list that is used to sort the list.  That permits looking at adjacent values to find the minimum, which significantly improves the speed.  After computing the values, it applies the inverse permutation to return the final list in the desired order.

slow := proc(E :: list)
local i,j,n;
    n := nops(E);
    [seq(min(seq(abs(E[i]-E[j]),j=1..i-1),
             seq(abs(E[i]-E[j]),j=i+i..n)
            )
         , i = 1..n)
    ];
end proc:

fast := proc(E :: list(numeric))
local i, n, P, L, A;
    n := nops(E);
    # compute permutation that sorts E
    P := map(attributes, sort([seq(setattribute(SFloat(E[i]),i), i=1..n)]));
    # apply permutation to sort E
    L := [seq(E[i], i in P)];
    # compute adjacent differences
    L := L[2..] - L[..-2];
    # compute minimum adjacencies
    L := [L[1], seq(min(L[i],L[i+1]), i=1..n-2), L[n-1]];
    # apply inverse permutation to restore order
    A := Array(1..n);
    for i to n do
        A[P[i]] := L[i];
    end do;
    [seq(A[i], i=1..n)];
end proc:

n := 1000:
L := RandomTools:-Generate(list(integer(range=0..5),n)):
time(slow(L));
                                     0.504

time(fast(L));
                                     0.008

You can reassign the index variable in the loop, however, to get it to restart from the beginning you have assign it the initial value minus the step value because the index is incremented by each time at the top of the loop, except for the first time. I generally don't reassign a loop counter and instead would follow Roman's advice to use a while loop.

First 71 72 73 74 75 76 77 Last Page 73 of 114