nm

10758 Reputation

19 Badges

12 years, 253 days

MaplePrimes Activity


These are questions asked by nm

I am trying maplemint for first time, but some of the messages it generates do not seem to make sense to me and they all seem to be false alarms.

And not sure how make maplemint generate true warnings to make it easier to filter the real problems from the not real ones. For example, I made some module to try

my_module:=module()

export foo;
local  f,A,n,x;

#private stuff here
f:= x -> x^2:

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

#public stuff here
foo:= proc()
        A;
end proc;     
    
end module;

maplemint(my_module) generates

Module my_module() 
  These local variables were never used:  x
  These local variables were used but never assigned a value:  n
  These local variables were assigned a value, but otherwise unused:  f

Well, "x" is clearly used. It is the integration variable?  And I can't assign value to "n", it is just a symbol used in the symbolic integration and assumed to be integer.

It also says "f" is not used. But "f" is used in definition of "A" inside the integrand.

So all these messages are not really needed. Is there a way to make maplemint not generate these? I do not see how I could change the code to remove these messages. Is something wrong with my code above?

Code works as expected

my_module:-foo();  gives (-Pi^2*(-1)^n*n^2+2*(-1)^n-2)/n^3

Here is another simpler example of where maplemint messages can't be removed no matter what.

restart;
foo:= proc()
	 local x;
	 plot(sin(x),x=-Pi..Pi);
end proc;     

and maplemint(foo) gives

Procedure foo()
  These local variables were used but never assigned a value:  x

restart;
boo:= proc()	
	 plot(sin(x),x=-Pi..Pi);
end proc;

And now

maplemint(boo);
Procedure boo()
  These names were used as global names but were not declared:  x

Here is another example where maplemint complains about option names for plot3 being undeclared

restart;
foo:= proc()	
    local p,x,y;
    p:=plot3d(sin(x)*cos(y),x=0..Pi,y=0..Pi,
              axes = none, projection=0.9, 
              orientation=[-30,55,0], scaling=unconstrained
              ):
    p:
end proc:

And

maplemint(foo);
Procedure foo()
  These names were used as global names but were not declared:  
     axes, none, orientation, projection, scaling, unconstrained

  These local variables were used but never assigned a value:  
       x, y

If one has to go each time through 100's of messages like these in order to find 1 or 2 real ones which indicate real problems, then using maplemint is not going to an effective way to find problems in code.

Basic question on Maple scoping, having hard time finding it doing search.

I noticed when I do this

get_plot:= proc()     
    plot(sin(x),x=-Pi..Pi);
end proc:

Maple did not complain that `x` inside the proc() was implicitly declared. So this tells me that `x` is set local in scope to the body of the plot() itself and this is done automatically. right?  This is same as in Mathematica actually.

But when I did this

get_plot := proc()  
    local x; 
    x:=10;     
    plot(sin(x),x=-Pi..Pi);
end proc:
get_plot();

I got an error that Error, (in plot) unexpected option: 10 = -Pi .. Pi. So my theory was wrong.

While in Mathematica, one can do the above and it will work

getPlot[]:=Module[{x},
  x=10;
  Plot[Sin[x],{x,0,10}]
];
getPlot[]

It works, becuase the x inside plot have local scope for the Plot command only and it is not the same as the x outside the plot.

But in Maple, it seems once I declared x to be local, then the plot will use that local x. 

So the question is, why did Maple not complain in the first example above that x is implicilty declared as normally happen when one does something like

foo := proc()     
    x:=10;     
end proc:

The 'x' is either local or not. which is it? Why above gives warning but not

foo:= proc() 
  plot(sin(x),x=-Pi..Pi); 
end proc:

Basically, I wanted to know if I should write like this

foo:= proc() 
  local x;
  plot(sin(x),x=-Pi..Pi); 
end proc:

or without the local x if not needed.

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