Joe Riel

9660 Reputation

23 Badges

20 years, 6 days

MaplePrimes Activity


These are answers submitted by Joe Riel

A somewhat more general approach, say if B included sin(u) into which you wanted to subsitute, is

frontend(curry(subs, A), [B], [{Not(specfunc(anything, {f,D(f)}))},{}]);

 

The solution that Doug gave, in the original post, was close to optimal Maple technique; it was short, efficient, and clear, once you understand how lists can be indexed from either end (which he explained).  But, if you don't understand it, or cannot explain it adequately, then you should try something else. 

Here's another approach. I won't write any code, so you cannot be accused of cutting and pasting.  Split the sorted list into two equal length lists (if there is an odd number of elements, the second list should be one smaller than the first. You can use ?op or list indexing to do so.  Use ?ListTools[Reverse] to reverse the order of the second list.  Then use ?ListTools[Interleave] to interleave the contents of the two lists.  That should give you what you want.  It isn't as nice as Doug's solution, but if you are able to implement it, then it should suffice.  As an example, you would do

[1, 2, 3, 4, 5, 6, 7]     # start with this
[1, 2, 3, 4], [5, 6, 7]   # split
[1, 2, 3, 4], [7, 6, 5]   # reverse second list
[1, 7, 2, 6, 3, 5, 4]     # interleave results

 

Here's one approach.  It runs in about 0.1 seconds and is reasonably short.  Each line of output consists of the rows in which to place a queen.

queens := module()

export ModuleApply;

local guess
    , init
    , place
    , Solve
    , N,cnt,board
    ;

    ModuleApply := Solve;

    init := proc(n)
        N := n;
        board := Array(1..N,1..N);
        cnt := 0;
        NULL;
    end proc;

    Solve := proc(n)
        init(n);
        guess(1);
        return cnt;
    end proc;

    guess := proc(col)
    local row;
        for row to N do
            if board[row,col] = 0 then
                place(row,col,+1);
                if col = N then
                    cnt := cnt+1;
                    print(_rest,row);
                else
                    procname(col+1,_rest,row)
                end if;
                place(row,col,-1);
            end if;
        end do;
    end proc;

    place := proc(row,col,val)
    local i;
        board[row,col] := -val-1;
        for i from col+1 to N do
            board[row,i] := board[row,i]+val;
        end do;
        for i to min(row-1,N-col) do
            board[row-i,col+i] := board[row-i,col+i] + val;
        end do;
        for i to min(N-row,N-col) do
            board[row+i,col+i] := board[row+i,col+i] + val;
        end do;
    end proc;

end module:
queens(8);

 

You might upload the file so we could take a look at it. If large, consider zipping it first.

I don't understand the difficulty.  You can put as many commands as you want in the loop.

Here's what I meant to post,

with(LinearAlgebra):
M := <<m+lambda|m>,<m|2*m>>:
ans := solve(Determinant(M),[m]);
                       ans := [[m = 0], [m = -2 lambda]]


M2 := map(subs, ans, M);
                       [lambda    0]  [ -lambda     -2 lambda]
                M2 := [[           ], [                      ]]
                       [  0       0]  [-2 lambda    -4 lambda]



for l from 1 to 2 by 0.1 do
    lambda=l, subs(lambda = l, M2);
end do;
                                   [1    0]  [-1    -2]
                      lambda = 1, [[      ], [        ]]
                                   [0    0]  [-2    -4]

                                 [1.1    0]  [-1.1    -2.2]
                  lambda = 1.1, [[        ], [            ]]
                                 [ 0     0]  [-2.2    -4.4]

...

Use ?evalf to evaluate them to floating-points.  You also might consider using ?fsolve rather than solve.

There are a few problems with your computation.  First, you solve for m, using the determinant of M, then plug into MM, which contains m[i] rather than m; that doesn't make sense to me.  Second, at the exit of the for loop, lambda is assigned a numerical value (it is the loop counter).  I'm thinking you don't want that.  Here is what I might do,
 

with(LinearAlgebra):
M := <<m+1|m>,<m|2*m>>:
ans := solve(Determinant(M),[m]);
                          ans := [[m = 0], [m = -2]]


for ans in ans do
    subs(ans, M);
end do;
                                   [1    0]
                                   [      ]
                                   [0    0]

                                  [-1    -2]
                                  [        ]
                                  [-2    -4]

Note the bit of trickery using ans as the loop counter. Normally I wouldn't do that, but here it illustrates that it can be done, the original value is independent of the loop value, though at the conclusion ans is modified (equal to the last value, [m=-2]). Instead of a loop, you could do

map(subs, ans, M);
                             [1    0]  [-1    -2]
                            [[      ], [        ]]
                             [0    0]  [-2    -4]

It means that there are two instances of the C component of Turbo in the model. That shouldn't happen, though its hard to say what went wrong. Could you upload the model?

One way to accomplish is to use Maple's ?setattribute function to tag each function value with its corresponding domain point.

X := [3,4,-1,2,4]:
f := x -> x^2:

Evaluate f at each x in X, and attribute the value with x. ?SFloat is needed because attributes cannot be applied to integers, but can be to floats


fX := [seq(setattribute(SFloat(f(x)),x), x in X)];
                         fX := [9., 16., 1., 4., 16.]

Compute the min and max values of fX, then get the attributes, which are the corresponding x values.


map(attributes, [min,max](fX));
                                    [-1, 4]

I don't know the purpose of SortX, but if you just want the 10 largest values you may be better off sorting the entire Matrix and picking off the largest 10 elements.  That can be done in one line, using ?ArrayTools[Alias] and the builtin ?sort procedure:

 sort(ArrayTools:-Alias(Ans,[nstock^2]),`>`)[1..10];

Note that ?dsolve permits defining parameters and then reassigning them.  For example


(**) sys := diff(x(t),t) = a*x(t) + b:
(**) 
(**) integ := dsolve({ sys, x(0) = x0}
(**)                 , numeric
(**)                 , 'parameters' = [a,b,x0]
(**)                ):
(**) 
(**) integ('parameters' = [1,2,3]):
(**) integ(10);
                                              [t = 10., x(t) = 110130.139885515]

(**) # Now reassign the parameters
(**) integ('parameters' = [1,1,0]);
                                                  [a = 1., b = 1., x0 = 0.]

(**) integ(10);
                                              [t = 10., x(t) = 22025.4312857109]

You could check the time-stamp.  That is, save the old one, then look to see if it changes. Use ?FileTools[ModificationTime] to get the time-stamp.

You probably should use ?irem and check for remainder 0. Once you find a factor of n, you should exit the loop. Also, rather than printing a value, return true or false.

NaivePrimeTest := proc(n)
local k;
    for k from 2 to isqrt(n) do
        if irem(n,k) = 0 then
           return false;
       end if;
   end do;
   return true;
end proc:

There probably is a nice way that I cannot immediately find.  Here is a less nice way,

J := frontend(Student:-VectorCalculus:-Jacobian
              , [map(rhs,[rossler_sys]), [x,y,z](t)]
              , [{`*`,`+`,list},{}]
             );
                              [ 0      -1       -1   ]
                              [                      ]
                         J := [ 1      a        0    ]
                              [                      ]
                              [z(t)    0     x(t) - c]
First 74 75 76 77 78 79 80 Last Page 76 of 114