Joe Riel

9660 Reputation

23 Badges

20 years, 3 days

MaplePrimes Activity


These are answers submitted by Joe Riel

A more mathematical approach is

q := 01100101:
ListTools:-Reverse([seq(irem(q,10,'q'), i=1..length(q))]);
                                    [1, 1, 0, 0, 1, 0, 1]

Be aware that that reassigns q.

Simpler is

q := 01100101:
ListTools:-Reverse(convert(q,'base',10));
                                    [1, 1, 0, 0, 1, 0, 1]

The loop is squaring and adding 1. By the 10th iteration the integer has 91 digits. Memory usage will quickly get out of hand. 

Consider using Threads:-Task for the real problem.

Note, too, that, after correcting the errors that Doug pointed out, the assignment is to a table and not a Matrix because you never assigned a to be a Matrix.  You can do that with

  a := Matrix(4): # only one dimension is necessary for a square matrix

Do so before the loop. Or use the initializer method that Kitonum demonstrated; when usable it is convenient. Here is the corrected version.

a := Matrix(4):
for i to 4 do
   for j to 4 do
       a[i,j] := 2*psi[i]*psi[j];
   end do;
end do:

Using the the AllPairsDistance procedure in GraphTheory almost works here; the problem is that your b node doesn't really fit with the scheme because it is equivalent to a node with a branch to itself and Maple's undirected graphs do not allow that. The following procedure uses MultiSet to store the nodes with self-loops, then, after computing the Matrix, assigns 1 on the diagonal to the nodes with self loops. Note the hackish use of assign to return NULL. The resulting Matrix, which contains the minimum distance between any two vertices, along with the list of vertices whose position in the list corresponds to the indices of the Matrix, can be used to easily construct the desired result

Distance := proc(neighbors :: seq(name=set(name)))
local G,M,S,T,V,eq,i;
uses GT = GraphTheory;
    S := MultiSet();
    G := GT:-Graph({seq(proc(eq)
                        local x, i := lhs(eq);
                            seq(`if`(i=j
                                     , assign('x',Insert(S,i))
                                     , {i,j}
                                    ), j = rhs(eq))
                        end proc(eq)
                        , eq = neighbors)});
    M := GT:-AllPairsDistance(G);
    V := GT:-Vertices(G);
    T := table([seq(V[i]=i, i=1..numelems(V))]);
    for i in S do
        i := T[i];
        M[i,i] := 1;
    end do;
    M,eval(V);
end proc:
(dist,verts) := Distance(a = {b, c, d}, b = {a, b, e}, c = {a, d, f});

Here's a rewrite that may be easier to comprehend.

Distance := proc(neighbors :: seq(name=set(name)))
local G,M,S,T,V,eq,i,j;
uses GT = GraphTheory;
    S := MultiSet();
    V := convert(indets([neighbors]),'list');
    G := GT:-Graph(V);
    for eq in neighbors do
        i := lhs(eq);
        for j in rhs(eq) do
            if i=j then
                Insert(S,i);
            else
                GT:-AddEdge(G, {i,j});
            end if;
        end do;
    end do;
    M := GT:-AllPairsDistance(G);
    T := table([seq(V[i]=i, i=1..numelems(V))]);
    for i in S do
        i := T[i];
        M[i,i] := 1;
    end do;
    (M,V);
end proc:
(dist,verts) := Distance(a = {b, c, d}, b = {a, b, e}, c = {a, d, f});
                    [0    1    1    1    2    2]
                    [                          ]
                    [1    1    2    2    1    3]
                    [                          ]
                    [1    2    0    1    3    1]
     dist, verts := [                          ], [a, b, c, d, e, f]
                    [1    2    1    0    3    2]
                    [                          ]
                    [2    1    3    3    0    4]
                    [                          ]
                    [2    3    1    2    4    0]

I don't get any syntactic error. What input are you using, and what result do you expect?  

Rather than doing with(LinearAlgebra), consider using

 

Karmarkar := proc(A,c)
uses LinearAlgebra;
   ...
end proc;

Actually, I prefer uses LA = LinearAlgebra and then prefixing the LinearAlgebra calls with LA:-.

A way to combine the operations (I assume you plan to replace the inert functions of t with variables) is to use subsindets:

subsindets(eq, function(identical(t)), f -> cat(op(0,f),'_'));

Probably you will need to make that more robust, frequently one is interested in converting only unknown functions of t, that is, sin(t) should remain as is.  That can be accomplished by modifying the type expression used to match the functions of t. For example, use

And(function(identical(t)), Not(known))

where Not(known) means the function is not known to Maple.

 

Map, over the list, a procedure that extracts the name of the function and catenates an underscore to it. The function name is extracted via the op procedure, e.g. op(0,f(a,b,c)) evaluates to f. Thus 

map(f->cat(op(0,f),_),T);

Note that this fails if the name of a function is indexed, e.g. f[2](a). No error is generated but the catenation doesn't evaluate to a symbol. Presumably that isn't an issue here.  One way to handle that case is to first convert the indexed name to a symbol, then catenate the underscore.  So a more robust version is

map(f->cat(convert(op(0,f),'symbol'),_),T);

However, if you are actually dealing with indexed names you might want a different result. Another way to do the conversion, and combine it with the catenation, is to use nprintf, which generates a name (symbol). Thus

map(f -> nprintf("%a_", op(0,f)),T);

 

The curly braces designate a set, not a list. An easy way to flatten a set is

s := {a,b,c, {d, e, {f, g}}}:
{subsindets(s, set, op)};
                      {a,b,c,d,e,f,g}

The same could be done for nested lists (which use square brackets rather than curly brackets). 

Followup:

ListTools:-Flatten works for nested lists, but not sets.  

I don't believe this is possible from the Maple worksheet (GUI). You can use command-line maple.

A superior alternative to either is to use the Emacs Maple debugger, the latest version of which is now at my github site, however, that requires a bit of work to install, configure, and learn.

Yes, I actively update maplev-mode.  A newer version is on github, however, I really need to push the latest, which is at 2.31. Multiline comments have been supported pretty much since they were introduced in Maple.

I recommend upgrading Emacs, preferably to 24.4, which is what I'm now using, though I've only recently switched so the released versions of maplev-mode will work with 23.4.

You can copy the code and paste it into the code region in a Modelica Custom Component Template. You can then edit it there and save it as a custom component.

One way is to attach it to a fixed frame that has a specified direction, set either via Euler angles or a rotation matrix. The prismatic joint's direction is relative to the orientation of the fixed frame

For this particular model, the easy way to do what you want is to attach a probe to the output of the constant torque driver; it is turning the revolute joint, so the angle there is monotonic and equals, mod 2*Pi, the periodic signal you are seeing.

Just do op([2,i,2], ifactors(n))

As a general rule you should not be using % inside a Maple procedure.  Also, to access the i-th element of a list, you should not first convert it to a sequence, which is what your assignment a := op(%) is trying to do. Just access it directly. For example

a := ifactors(n)[2];
b := a[i];
c := b[2];

The approach I suggested combines all the subelement selections into one call to op. For example, op([1,2,3], ... ) means select the third operand of the second operand of the first operand of the second argument (not shown here) to op.

A useful answer, without some context, is hard to come by. Is this a question about Maple?

First 25 26 27 28 29 30 31 Last Page 27 of 114