Joe Riel

9530 Reputation

23 Badges

20 years, 25 days

MaplePrimes Activity


These are answers submitted by Joe Riel

Since you've already written a procedure to compute A - B, how about computing the intersection using that procedure and some basic set theory.

As Robert hints, it is hard to give advice without knowing what is allowed.  His suggestion of using member and remove is good; they permit an efficient and compact procedure.  My advice, however, is to not use those two and see what you can accomplish with a more traditional (but not Maple-esque) style of programming.  Actually, the best approach would be to develop a few procedures, one using remove and member, another using loops and conditionals, and a third using the builtin in minus operator, then compare their performance and capabilities.  You can use the Maple time function to measure the evaluation time.  For example, if minus were allowed

SetDiff1 := proc(A,B) A minus B end proc:
S := {seq(1..1000)}: # create a set of integers from 1 to 1000
time(SetDiff1(S,S);
                                             0.

Most procedures are likely to run considerably slower, so if you do time one, start with a relatively small set. Remember, too, that Maple is a math tool.  There are some (too) clever algebraic approaches that can solve this problem.

There might be such a command in the GraphTheory package, but one doesn't jump out at me.  Writing a recursive procedure that solves this is simple enough; whether there is a more efficient technique is the real question.  Here's one way:

FindPaths := module()
export ModuleApply, FindPaths;
local cnt :: nonnegint
    , paths :: table
    ;

    ModuleApply := proc(G, u, v)
        cnt := 0;
        paths := table();
        FindPaths(G,u,v,{},[u]);
        convert(paths,'list');
    end proc;

    FindPaths := proc(G, u, v, visited :: set, path :: list)
    local deps, n, newpath;
    uses GraphTheory;
        deps  := convert(Departures(G,u),'set') minus visited;
        for n in deps do
            newpath := [path[],n];
            if n = v then
                cnt := cnt+1;
                paths[cnt] := newpath;
            else
                procname(G,n,v, visited union {n}, newpath);
            end if;
        end do;
        NULL;
    end proc:

end module:

G := GraphTheory:-Digraph({[1,2],[2,3],[1,3],[2,4],[4,2],[4,3]}):
FindPaths(G,1,3);
                                           [[1, 2, 3], [1, 2, 4, 3], [1, 3]]



What do you expect to learn?  Using recursion is simple enough, the best way is through practice.  More complicated is learning when and how to avoid recursion when it is inefficient.

When using recursion with Maple, one can frequently use the remember/cache option to significantly speed up a compuation.  For example, consider the classic Fibonacci sequence:

f(0) = 0:  f(1) = 1;  f(n) = f(n-1) + f(n-2):
fib := proc(n::nonnegint)
   if n = 0 then return 0
   elif n = 1 then return 1
   else return fib(n-1) + fib(n-2);
   end if:
 end proc:

This is quite inefficient:

time(fib(30));
                                 8.150

However, by adding the cache option, so that previous calls are cached, it becomes significantly faster

fib := proc(n::nonnegint)
option cache;
   if n = 0 then return 0
   elif n = 1 then return 1
   else return fib(n-1) + fib(n-2);
   end if:
end proc:

time(fib(1000));
                                     0.012

It is also possible to explicitly store results without using a cache/remember table.  That can be done with an assignment statement, fib(1) := 1. 

fib := proc(n::nonnegint)
   fib(n) := fib(n-1) + fib(n-2);
end proc:
fib(0) := 0:
fib(1) := 1:

time(fib(1000));
                                   0.012

You can pass a procedure to a Matrix constructor that initializes the Matrix:

c := LinearAlgebra:-RowDimension(d):
U := Matrix(3,4, (s,t) -> add((d[s,t]/d[j,t])^2, j=1..c));

Probably not a concern, but slightly more efficient is

U := Matrix(3,4, (s,t) -> d[s,t]*add(1/d[j,t]^2, j=1..c));

If efficiency were an actual concern, you could first create a Vector and then reuse it
V := Vector(c, t -> add(1/d[j,t]^2, j=1..c):
U := Matrix(3,4, (s,t) -> d[s,t]*V[t]));

Your code is missing some steps, in particular, it doesn't indicate how the values in the first column are assigned. Probably just as well, since once you get the general idea you can fill that in.  You should probably make y a procedure (operator).  Also, you might use a Matrix (or Array) rather than the deprecated array structure (your code assign P, but then uses p; be aware that Maple is case-sensitive). Note also that Maple's assignment operator is `:=', not `='.

P := Matrix(51,3):
y := s -> evalf((-1)*0.6e-1*(arctan(10*s/1.3+(-10)*.3)/Pi+1/2)*cos(6*Pi*s/1.3));
for i to 51 do
    P[i, 2] := y(0.26e-1*i);
end do:
 

Simpler is to use seq:

 seq('(k,k+1)', k=0..20,4);
                   0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21

The usual method, after the search returns, is to change to the "Table of Contents" tab.  If there is an entry for the topic in the Table of Contents, it should be highlighted. Is that not happening for MTM?  How 'bout for other topics?

The VectorCalculus package can be used here:

Y := (x/2)^(3/2):
len := VectorCalculus:-ArcLength(<x,Y>, x=A..B);
                                       3/2              3/2
                            (64 + 18 A)      (64 + 18 B)
                   len := - -------------- + --------------
                                 216              216


The usual way to do this is to substitute the variables you want, say

dsol := dsolve(diff(y(t),t) = y(t) + sin(t), y(t));
             dsol := y(t) = -1/2 cos(t) - 1/2 sin(t) + exp(t) _C1
subs(_C1=C1,dsol);
                  y(t) = -1/2 cos(t) - 1/2 sin(t) + exp(t) C1


If you had to do this a lot, you could hack up a procedure:

remove_leading_underscore := proc(expr)
local v,vars;
    vars := indets(dsol,'symbol');
    vars := select(v -> substring(v,1..1)=`_`, vars);
    subs([seq(v = substring(v,2..-1), v in vars)], expr);
end proc:

remove_leading_underscore(dsol);
                  y(t) = -1/2 cos(t) - 1/2 sin(t) + exp(t) C1

You should be able to use the VectorCalculus package to solve these.  I'll show you how to do two of them

with(VectorCalculus):

#2:
The domain is the portion of the unit-disk in the first quadrant.  Use Sector, with a Circle, to specify the domain:

int(ln(1+x^2+y^2), [x,y]=Sector(Circle(<0,0>,R),0,Pi/2));

#4:
You need to figure out what the domain is.  Remember completing the square:

dom := x^2+y^2=3*x:
Student:-Precalculus:-CompleteSquare((lhs-rhs)(dom));
                                      2    2
                             (x - 3/2)  + y  - 9/4
# You should recognize that as a circle with center at [3/2,0] and radius 3/2.
PathInt(x-y, [x,y]=Circle(<3/2,0>,3/2));

Note that if you are using 2D Math input you can enter this directly by typing

v''+v'+v=0

However, if you want the independent variable to be t (time) rather than x, you may need to modify the Typesetting rules. Click on View -> Typesetting Rules, and in the lower left corner of the pop up dialog, change the variable for "Prime Derivatives" to t.  If the "Prime Derivatives" box is not checked than check.  Click "Done".  Now you can type

v''+v'+v=0

Type "Enter", right-click on the result, and select either "Solve DE" for "Solve DE Interatively".  The latter allows you to easily specify boundary conditions so that the constants can be removed.

Equations 1.2  to 1.4 are wrong.  Because the pullies are assumed massless, the force balance on them is zero.  The diff-eq for m1 should be

m1*diff(x1(t),t,t) = m1*g - 2*T1

Eq. 1.6 is also wrong.  From the arrangement, 2*x1 + x2 = 0 (arbitrary any constant).

Use map:

alias(Q=Q(x,y,t),H=H(x,y,t)):
A := <Q/H|Q^2+H>:
map(diff, A, x);
                                             [d        /d   \                     ]
                                             [-- Q   Q |-- H|                     ]
                                             [dx       \dx  /      /d   \   /d   \]
                                             [---- - --------, 2 Q |-- Q| + |-- H|]
                                             [ H         2         \dx  /   \dx  /]
                                             [          H                         ]


 

Your third (first) graph is of the identity function, use z -> z in the list:

plot([z->z, ff, fr], 0..1, legend = [ideal,front,rear]);

or use an explicit independent variable:

plot([z, ff(z), fr(z)], z = 0..1, legend = [ideal,front,rear]);

The reason you cannot use sets is that Maple may reorder them, so there would be no way for Maple to associate a particular graph with a particular legend.

First 94 95 96 97 98 99 100 Last Page 96 of 114