Joe Riel

9660 Reputation

23 Badges

20 years, 5 days

MaplePrimes Activity


These are answers submitted by Joe Riel

When ?assuming is used, the "assumee" (the operand to the left of the keyword assuming) is not evaluated until all assumptions have been applied. That is achieved by using the parameter modifier uneval (see ?parameter_modifiers).  To see that, do

 > print(`assuming`);
                                                        proc(x::uneval, a)  ...  end proc

The x parameter, which has the uneval modifier, is the "assumee".  When the assumption n :: negative (or n :: negint) is used, Maple correctly picks the proper branch of the piecewise and so GAMMA is never evaluated with x=-1.

The usual way to delay evaluation of a Maple expression is to surround it in single forward quotes (see ?').

Do not use ?with inside a procedure.  It doesn't work.  Instead uses ?uses.  That is,

stableMatching := proc(...)
local ... ;
uses ArrayTools;
    ...
end proc:

 

The error occurs with the call test(-1,n).  Because n is symbolic, the piecewise cannot be evaluated, so it computes piecewise(0<n, 1/GAMMA(-1), n<=0, sin(-Pi)).  The evaluation of GAMMA(-1) raises the divide-by-zero error.  One way to avoid this is to prevent the evaluation of the piecewise unless n is numeric.  That can be achieved with

test := proc(x,n) 
    if n :: numeric then
         piecewise(...);
    else
         'procname'(args);
    end if;
end proc:

This isn't ideal; if n is numeric then there is little point in using a piecewise.  That is, you might as well use a conditional.  A somewhat crude way to workaround that is to use a try/catch.

test := proc(x,n)
    try
        piecewise(...);
    catch:
        'procname'(args);
   end try;
end proc:

Not really.  The usual way to do this is with the `if` function: `if`(something, 1, 0).  If the condition is evaluable in evalhf, you can use it:

evalhf(3 < Pi);
                          1.

Note that the output is a float and not an integer.

Use ?add instead of ?sum.  The procedure sum is intended for symbolic summations, that is, when the limits are undefined.  The reason that sum fails is that the summand is evaluated before the summing begins.  That means the inner sum is evaluated before j is assigned a value.  That doesn't happen with add, which uses special evaluation rules.

The problem with assigning is that that effectively removes the symbolic variable from further use.  I generally prefer to assign the entire output of fsolve to a variable, and then use ?eval to get the value of a portion of it.  For example,

fsol := fsolve(...):
a5 := eval(a[5], fsol);

The difficulty is keeping R1/r0 together.  The simplest way may be to substitute for it; here I use the empty function, ``(), to group the product.

(**) y := R1^2/r0^2-2*R1/r0+1;
                                     2
                                   R1    2 R1
                              y := --- - ---- + 1
                                     2    r0
                                   r0

(**) subs(R1=``(R1/r0)*r0, y);
                              R1  2        R1
                            (----)  - 2  (----) + 1
                              r0           r0

(**) factor(%);
                                /   R1      \2
                                | (----) - 1|
                                \   r0      /

The command expand will expand the empty function (``(...)), which returns the original:

(**) expand(%);
                                  2
                                R1    2 R1
                                --- - ---- + 1
                                  2    r0
                                r0

Your latest error, "illegal use of a formal parameter" is usually caused (and is here) by attempting to assign to a formal parameter of a procedure. In this case, the statement p := g(p) is a attempting to assign to p, which is a formal parameter of fixe.  That is not allowed here.  One way to avoid it is to first assign the argument p (i.e., the value of the formal parameter) to a local variable, and then use that local variable inside the procedure.  That is, do

fixe := proc(p, g)
local pp;
pp := p;
...
pp := g(pp);
...
end proc

When the while condition is evaluated the first time, n is not assigned a value.  So the condition n < nmax cannot be evaluated to true or false. Note that the call to evalb is not needed.

Duncan's suggestion to use the CodeTools package is a good one.  To use these tools, the code needs to be in form of executable procedures, rather than top-level commands.  If the current code is one large worksheet/document, you might find it convenient to export it as Maple input, then wrap the contents of that text file into a Maple procedure.  Depending on how the orginal worksheet was coded, the top-level variables may have to be declared as globals, but if you can avoid doing so it may run faster since global variabled are fully evaluated each time they are accessed. 

As an example, suppose you started with a simple worksheet that looked like

L := NULL:  # this is a lousy way to create the sequence 1,4,9,16,...,100
for i to 10 do
    L := L, i^2;
end do;

After exporting to a text file and wrapping in a procedure you might have a file named createL.mpl with the content

CreateL := proc()
local i, L; # declare these variables as locals
   L := NULL;
   for i to 10 do
      L := L, i^2;
   end do;
   return L;
end proc:

Inside of Maple you would then do

read "createL.mpl":
with(CodeTools:-Profiling):
Profile(CreateL):
L := CreateL();
                   L := 1, 4, 9, 16, 25, 36, 49, 64, 81, 100

PrintProfiles();
CreateL
CreateL := proc()
local i, L;
     |Calls Seconds  Words|
PROC |    1   0.000    174|
   1 |    1   0.000      0| L := NULL;
   2 |    1   0.000      0| for i to 10 do
   3 |   10   0.000    174|   L := L, i^2
                            end do;
   4 |    1   0.000      0| return L
end proc

stackmatrix and augment are part of the deprecated linalg package, they do not work with rtable based matrices (created with Matrix). They aren't needed with rtables.  You can do, for example,

 

(**) A := Matrix(2, symbol='a');
                                                             [a[1, 1]    a[1, 2]]
                                                        A := [                  ]
                                                             [a[2, 1]    a[2, 2]]

(**) B := Matrix(2, symbol='b');
                                                             [b[1, 1]    b[1, 2]]
                                                        B := [                  ]
                                                             [b[2, 1]    b[2, 2]]

(**) <A|B>;                     
                                               [a[1, 1]    a[1, 2]    b[1, 1]    b[1, 2]]
                                               [                                        ]
                                               [a[2, 1]    a[2, 2]    b[2, 1]    b[2, 2]]

(**) <A,B>;                     
                                                          [a[1, 1]    a[1, 2]]
                                                          [                  ]
                                                          [a[2, 1]    a[2, 2]]
                                                          [                  ]
                                                          [b[1, 1]    b[1, 2]]
                                                          [                  ]
                                                          [b[2, 1]    b[2, 2]]

What's the problem?

As an example, suppose you are looking for all variables of the form x(t), y(t), etc.  One way to do that with ?indets is

(**) ex := diff(x(t),t,t) + sin(y(t)) + cos(t);
                                             / 2      \
                                             |d       |
                                       ex := |--- x(t)| + sin(y(t)) + cos(t)
                                             |  2     |
                                             \dt      /

(**) indets(ex, 'And(anyfunc(identical(t)),Not(known))');
                                                   {x(t), y(t)}

Note the use of the structured type (see ?type,structured) to select the appropriate variables.  The type known, alas, is not documented, but you can see its assignment by doing

(**) `type/known`;
                                      And(function, PDEtools/known_function)

The purpose of it here is to eliminate cos(t), which is a known function and so generally not suitable as a variable.

How about using ?LinearAlgebra[JordanForm]? Hmm. I wasn't paying attention, you only want to permute columns...

The limit depends on the sign of T.  Try

limit(exp(x/T),x=infinity) assuming T>0;
First 61 62 63 64 65 66 67 Last Page 63 of 114