nm

11353 Reputation

20 Badges

13 years, 18 days

MaplePrimes Activity


These are questions asked by nm

In Mathematica, one can define a function 2 ways. Using delayed evaluation of its RHS (which is same as proc() in Maple) but also as immediate evaulation of its RHS.

In the immediate evaluation, what happens is that the RHS is evaulated first using normal evaluations, then the result of this evaluation becomes the new body of the function.

This can be very useful sometimes. For examle, if the RHS was a complicated integral, which can be evaluated immediatly and gives a result, which still depends on a parameter to fully evaluate, then this method saves having it to evaluate the full integral each time as with the case of delayed evaluation.

I do not know how to emulate immediate evaluation, but using a proc() in Maple. Here is a simple example to explain.

restart;
foo:=proc(n::integer)
     local r;
     r:=int(x*sin(n*x),x=0..Pi);
     r;
end proc;

(ps. I added the extra `r` there just for debuging. They are not needed)

Now when doing foo(3), then the integral will have to be computed each time for each `n`.

But If the integral was evaluated at time of the function definition, it will have the result of -(-1)^n*Pi/n and now when the function is called, then it will be much faster, since in effect the calling the function would be as if one typed

restart;
foo:=proc(n::integer)
     local r;
     r:=-(-1)^n*Pi/n;
     r;
end proc;

In Mathematica, I can do the above by defining a function using `=` instead of the delayed `:=`

foo[n] = Assuming[Element[n, Integers], Integrate[x Sin[n*x], {x, 0, Pi}]]

Now when I do f[3], it will actually use -(-1)^n*Pi/n as the body of the function since the RHS side of the function was evaluated immediatly at time the function was defined. This saves having to do the integral each time.

To make it work like in Maple, the one must make it delayed, like this

foo[n] := Assuming[Element[n, Integers], Integrate[x Sin[n*x], {x, 0, Pi}]]

How can one emulate the immediate evaluation of a proc() in Maple? If not the whole body, but may be a statment? as if one can do

restart; foo:=proc(n::integer) 
      local r; 
      r:=eval_now(int(x*sin(n*x),x=0..Pi)); #result of int is used in definition
      r; 
end proc;

so that the body of the proc will be evaluated as much as possible at time of definition? This can be much more efficient in some cases.

I know ofocurse I could write

foo:=int(x*sin(n*x),x=0..Pi) assuming n::integer;

and then use subs() to evaluate for different `n`. But I wanted to use proc().

 

To make animations, one must generate many plots. The following are two methods I know about. Which would be better? And is there a more efficient way than any of these?

This one pre-allocates an Array of the correct size needed, then fills it in in the loop. But then one has to convert the whole Array back to a list in order to animate it

restart;
nFrames := 10:
frames  := Array([seq(0,i=1..nFrames)]):
w       := 0:
for i from 1 to nFrames do
      frames[i] := plot(sin(w*t),t=-2*Pi..2*Pi);
      w         := w+1;
od:
plots:-display(convert(frames,list),insequence=true);

 

This method does not need to convert an Array to a list. But it does not pre-allocate memory needed before and has to dynamically grow the list each time, which might not be efficient

restart;
nFrames :=10:
frames  := NULL:
w       := 0:
for i from 1 to nFrames do
      frames := frames , plot(sin(w*t),t=-2*Pi..2*Pi);
      w      := w+1;
od:
plots:-display(frames,insequence=true);

For very large number of frames, I am not sure which is better. It is always best to pre-allocate memory to avoid dynamic growing list, which can be costly. But on the other hand, the first method requires converting the whole Array to a list, and I was not sure if that is done in-place or if Maple will have to copy the whole thing again to make a list.

Are there better and more efficient ways to do the above?

I am learning how to do animations in Maple, and I need to export an animation I made to animated gif file.

nTerms := 20:
lam    := evalf([BesselJZeros(0,1..nTerms)]):
c      := seq(1/lam[n]^2*BesselJ(1,lam[n]/2)/BesselJ(1,lam[n])^2,n=1..nTerms):
mySol  := proc(r,t)
   local n;
   4/Pi*sum(c[n]*BesselJ(0,lam[n]*r)*sin(lam[n]*t),n=1..nTerms);
end proc:

maxTime := 5: (*seconds*)
delay   := 0.03:
nFrames := round(maxTime/delay):
frames  := Array([seq(0,i=1..nFrames)]):

frames  := [seq( plot3d([ r, theta, mySol(r,(i*delay)) ],
                   r      = 0..1,
                   theta  = 0..2*Pi,
                   coords = cylindrical,
                   axes   = none,                    
                   title  = sprintf("%s %3.2f %s","time ",(i*delay),"seconds")
                ),
             i=0..nFrames-1)
           ]:
plots:-display(convert(frames,list),insequence=true);

And the above makes

 

I have few questions which I could not find an answer for.

How to save the above sequence of images frames (in the list frames) to an animated gif file programmatically? Currently, I use plots:-display(frames,insequence=true); and then right-click on the screen out, select export->GIF  and this does save the file as animated file.

The problem with the above method, is that I have no control on telling Maple some options. For example, I want to control the amount of delay between each frame when it is played in the web page. I also want to tell Maple for example to play this once in the gif file.

These options are suppored in Mathematica. But I do not see how to do them in Maple. In Mathematica, given a list of frames (generted from a Plot command), which is the variable "frames" in the above Maple code, then one can do this

Export["anim.gif",frames,"DisplayDurations"->Table[.2,{Length@frames}]]

Now when loading anim.gif in a browser, the delay between each frame is 0.2 seconds. The "DisplayDurations" set the delay between each frame when played in the browser  automatically.

One can also add the option

"AnimationRepetitions" -> 1

and this will make the animation play one time in the browser when first loaded. Now Maple generated animation gif file plays continuously which I do not want to, this is even though inside the notebook, Maple says it will play "single" time. But this option does not seem to be exported to the animation gif file

 

 

Is there a way to configure these options? When I right-click, and export the animation to GIF file, I see no export options to change.

I could not use the export("anim.gif",frames)  command in Maple, the generated GIF file is not animated.

thank you

I am looking at the help pages, and I see Maple code using symbols never seen in Maple language before.

I tried to do ?&under to get help on this new Maple language command/symbol, but help does not show it. I read help Neutral Operators but do not understand it.

What does the following mean in plain Maple code?

And what does the following mean using Plain Maple code:

 

Could the above be written without using these `&` things?  I am having hard time understanding what the code is doing because of these. Never used them before.

These are from help on "Definition of a Structured Type in Maple"

On the maple cloud, I see that current version is 38 for Physics 

After I installed, I typed

Physics:-Version();

It gives 
  "......\maple\toolbox\2018\Physics Updates\lib\Physics Updates.maple", 2018, May 8, 17:49 hours

I was expecting to see "38".  Why does Physics:-Version(); does not give the version number as shown on Maple cloud? Using only a date for a version number is not a good idea. There should be a number there. (date can also be included, but a number should be the official version number).

Is there another command to use to obtain Version number "38"?

Does each package shown on cloud support packageName:-Version()? I tried this command on another package I have, but I got an error saying it does not Version() 

OrthogonalExpansions:-Version();
Error, Version is not a command in the OrthogonalExpansions package
 

Yet, on cloud, it says the version number is "1" for the above package. What does the version number shown on the cloud then really mean?

First 160 161 162 163 164 165 166 Last Page 162 of 199