Maple 2018 Questions and Posts

These are Posts and Questions associated with the product, Maple 2018

Dear sir / madam

Hi
I'm a student of mechanical engineering and looking for some HPM ( homotopy perturbation method) maple codes with the related article to practice more in this field.
can I ask you for help?
unfortunately, I couldn't find any articles with maple solution about HPM.

sincerely
Aidin

As an exercise to gain familiarity with the maple programming platform, I am attempting to repackage a geometric algebra program written for Maple V as a module. I have attached file NewGlyph.mpl containing the initialization code for a module to support the GA routines in the package. The text file was created in the Maple code editor (Maple 2018) and loaded by reading it into a worksheet (configured for maple input instead of 2D math). I have two questions: one: how to get the GAinit procedure to execute completely, and two: to understand the puzzling warnings produced by the code editor.  The behaviour I observe when the module is loaded into a worksheet is: NewGlyph:= a maple id.  At this point the load=setup procedure has registered type blade, but not executed the remaining procedures in the setup routine. When I execute with(NewGlyph) the exported procedures are displayed properly.  However, I still have to execute the GAversion procedure and
the GA_addmetric procedure manually to properly initialize the metric table in the module. It would appear that the module load process permits the execution of the addtype procedure but not the output of the version procedure or the metric table constructor. Is there a way to autoexecute these procedures when the NewGlyph module is invoked by the with(NewGlyph) command? Are there any criteria for the kinds of processes that can be included using option load=setup? My second question concerns the compiler warning raised by the code editor.  It warns that "These names were used as global names but were not declared: blade, sig. Also "These local variable were assigned a value but not otherwise used: cleanup.

NewGlyph := module ()
description "Basic Geometric Algebra Functions";
local setup, cleanup;
global e;
export GAversion,GAinit, GAadd_metric,GAmetric,GAmv;
option package, load = setup, unload = cleanup;

setup := proc ()
TypeTools:-AddType(`blade`, proc (a) type(a, indexed) and op(0, a) = 'e' end proc);
GAversion();
GAinit();
end proc;
setup();
cleanup:=proc()
TypeTools:-RemoveType(`blade`);
end proc;

GAinit := proc()
global `&@`, `&.`, `&^`, `&x`, `&s` , `&/`, `&l`, `&v`;
protect('`&@`','`&.`', '`&^`', '`&x`', '`&s`', '`&/`', '`&l`', '`&v`');
GAadd_metric('default', signum, -infinity, infinity);
GAadd_metric('SA', proc () 1 end proc, 1, 3);
GAadd_metric('STA', proc (i) `if`(i = 0, 1, -1) end proc, 0, 3);
GAadd_metric('MSTA', proc (i) `if`(modp(i, 4) = 0, 1, -1) end proc, 0, infinity);
GAadd_metric('Homogeneous', proc () 1 end proc, 0, infinity);
GAadd_metric('Conformal', proc (i) `if`(i = -1, -1, 1) end proc, -1, infinity);
GAmetric('default');
end proc:

GAversion := proc ()
print(`GA Package`);
print(`Version 2.0`);
print(`Written by: Mark Ashdown, maja1@mrao.cam.ac.uk`);
print(`Extended by: Alan Macdonald, macdonal@luther.edu`);
print(`Re-Packaged for Maple 2018 by: Ian McCreath`);
end proc;

GAadd_metric := proc (fred::name, signature::procedure, minind::{integer, infinity}, maxind::{integer, infinity})
global MetricTable;
unprotect('MetricTable');
MetricTable['fred'][sig] := eval(signature);
if maxind < minind then
ERROR(`The minimum index is greater than the maximum index`)
end if;
MetricTable['fred'][min] := minind;
MetricTable['fred'][max] := maxind;
protect('MetricTable');
print(`The GA package knows about metric ` . fred . `.`)
end proc:

GAmetric := proc (a::name)
global Metric, Sig, MetricTable, MinIndex, MaxIndex;
if member([a], [indices(MetricTable)]) then
unprotect('Metric', 'Sig', 'MinIndex', 'MaxIndex');
Metric := a;
Sig := MetricTable[Metric][sig];
MinIndex := MetricTable[Metric][min];
MaxIndex := MetricTable[Metric][max];
protect('Metric', 'Sig', 'MinIndex', 'MaxIndex');
print(`Metric is now set to: ` . Metric)
else
ERROR(`The metric ` . a . ` is unknown to the GA package`)
end if
end proc:

# Creates multivector with name x of with grade or set of grades g with
# indices ind
GAmv := proc(x::name,g::{nonnegint,set(nonnegint)},ind::set(integer))
local i;
if g::set then
add(procname(x,i,ind), i=g)
elif g > nops(ind) then
ERROR(`grade too large for number of indices.`)
elif g=0 then
x[NULL]
else
add(x[op(i)]*e[op(i)], i=combinat:-choose(sort([op(ind)]),g))
end if
end:

end module;

Download NewGlyph.txt

 

When running this code in Maple 13 and 16 this works just fine,

GenMs:= (k::posint)-> assign(m||(1..2*k) =~ seq([dx||i, dy||i][], i= 1..k)):
GenMs(k), m || (1 .. 2*k);
gln := evalDG(l1 &s n1+`&s`(l2, n2)+sum('epsilon || i'*(cat(m, 2*i-1) &s cat(m, 2*i-1)+cat(m, 2*i) &s cat(m, 2*i)), i = 1 .. k));

But running the same code in Maple 2018 I get the following error:

Error, (in DifferentialGeometry:-Tensor:-SymmetrizeIndices) expected 1st argument to be a tensor. Received: `m2*i-1`^2

I don't know if it's related to Maple 2018 screwing with my tensor formatting:
https://gyazo.com/88eaeceda2e36cf411df44dbc4aa3ab6

(compare https://gyazo.com/b33396131165d66bbcf16e45d20cc579)

For whatever reason I just noticed that my inline &t's have been turned into `&t` but that's a separate issue.

I am learning how to do parsing in Maple. 

I want to check that a user supplied an expression with correct argument to y(*) from some complicated expression. So I need to find all instanced of y() to check that its agument is only y(x) and nothing else.  For example, given this 

restart;
expr:=y(x)^2+x+y(x)+2*1/y(z)+sin(x)+sin(y(x))+y+f(z)/Int(sin(y(x)),x)+y(x,y,z);

I need to obtain all these y(anything), like this

No matter where they show up in the expression. The above is just some made up example. The actual input will be a differential equation, and I want to check that the dependent variable y(x) has only x as its argument.

So I did the following

candidates:=convert(select(has,expr,y),list);

The problem now, is how to scan this list and check that each entry in it, the "y" in there has form y(x) and nothing else, so I can reject or accept the input. For example, the first one above is Ok, so the second one, but the third is not, since it function of z and not x. #4 is OK, #5 is not OK, since it has y without (x), and the last one is no OK, since it has 3 arguments, and so on.

I am not good in pattern matching in Maple. do I need to check match() for this? or patmatch()? If given single expresion like y(x), then I can handle it. I do something like

expr:=y(x);
if type(expr,'function') and nops(expr)=1 and op(0,expr)=y and op(1,expr)=x then
   print("OK");
else
   print("not ok");
fi;

But when the expression gets more complicated, like 1/y(z), then I need to check other things, and things gets complicated quickly. I think pattern matching is needed? or is there a better approach to do this that works in general? 

How does Maple do it internally? When I type

   dsolve(diff(y(x),x)+x+y(z)=0,y(x));

Error, (in dsolve) found the indeterminate function y with different arguments {y(x), y(z)}

dsolve(diff(y(x),x)+x+sin(y(z))=0,y(x));

Error, (in dsolve) found the indeterminate function y with different arguments {y(x), y(z)}

dsolve(diff(y(x),x)+x+y()=0,y(x));

So I need to do the same thing as Maple does. I looked at dsolve() code, but did not understand it how or where it does the parsing. 

I am using this proc by Carl Love posted here

https://www.mapleprimes.com/questions/211401-How-Do-I-Print-Text-Followed-By-Math

TSprintf:= proc() 
   local e;
   uses T= Typesetting; 
   T:-mrow(seq(`if`(e::string, T:-mn(e), T:-Typeset(T:-EV(e))), e= [args])) 
end proc:

when I do 

         TSprintf("Solving ", diff(y(x),x)=x);   

it works fine and it prints on the screen as expected. 

The problem is that inside a proc, if I use an error() later on, the message do not show up

foo:=proc()
   TSprintf("Solving ", _passed);   
   error "opps"
end proc;

And now when I call it like this 

  foo(diff(y(x),x)=x);
      Error, (in foo) opps   # Where the message "solving...." gone?? it does not show on the screen

I only see the "opps" and never see the message.  If I remove error(), then it shows up. But with standard printf, both show up

foo:=proc()   
   printf("Solving %a", _passed); 
   error "opps";
end proc;


foo(diff(y(x),x)=x);
    Solving diff(y(x),x) = x  # printf message shows OK
    Error, (in foo) opps      # from error 
 

Why TSprintf message do not show up if there is an error() after it?

I am learning to use module().  If one has a local private proc() inside  a module, then on calling this local proc from inside the module itself, does one need to call it using module_name:-local_proc() or is it safe  to just call it using local_proc()?

i.e. will Maple always look to resolve this name inside the module first, before looking outside? What order Maple uses to resolve names? 

Here is an example

restart;
private_proc:= proc()
    print("Opps, should not be calling this, global copy");
end proc;

foo :=module()
    local private_proc;
    export public_proc;

    private_proc:=proc()
        print("inside private");
    end proc;

   public_proc:= proc()
        private_proc(); #will this always call foo:-private_proc() and not
                        #any other global proc with that name?                 
   end proc;

end module;

foo:-public_proc();

gives "inside private". So it seems to work without having to use foo:-private_proc(). But I thought to ask if the above will always work like this. 

I only just noticed the Start.mw file being always number 1 in my Recent document list.  I don't recall it being there, ever, and I don't think it should.  Why is it there now?  Anyone else have this?

Hi All,

I recently switched from Maple 18 to Maple 2018. I was trying to execute and old worksheet created with Maple 18 in which a DAE have to be solved numerically. In Maple 18 the worksheet works flawlessly whereas Maple 2018 throws 'Error, (in dsolve/numeric/process_input) invalid specification of initial conditions...'

Initial conditions are given in the form {x1(0) = 1, x2(0) = 2, D(x1)(0) = 1, D(x2)(0) = 0 ....} and are the same in both cases ( I merely executed without any modification the same worksheet in the two Maple versions).

I feel I've missed out something in the changelog... any idea on what is happening?

PS. call to dsolve

dsolve(dsys_numeric, numeric, implicit = true, stiff = true, optimize = true, compile = true):
 

 

 

Hi Guys :)

 

I need some Help with solving a nonlinear system. 

I have 3 equations.

These 3 equations have 7 unknown variables in it. But 5 of them are constants which I don't want to solve. I just want to solve three of them.

I tried it with fsolve but I always get the error :

fsolve({eq1, eq3, eq5}, {a, b, c});
%;
Error, (in fsolve) {eq1, eq3, eq5} are in the equation, and are not solved for

 

i tried to define the variables as Parameters but then i get this error: 

Parameters(t1, t2, t3, t4, t5, t6, t7, b1, t, v1, v2, s1, s2, j, a, b, c);

fsolve({eq1, eq3, eq5}, {ca, cb, cc});
%;
Error, (in fsolve) {b1, eq3, eq5, j, t, t1, t2, t3, t4, t5, t6, t7} are in the equation, and are not solved for


Here is the script : Non-Linear_system.mw

 has sb an Idea?

 

result3.mw
 

NULL

with(LinearAlgebra); restart; delta := 0.407e-9; C := 2023.2; L := 5*delta

0.2035e-8

(1)

eq := (1/12)*delta^2*S^4+S^2+omega^2/C^2

0.1380408333e-19*S^4+S^2+0.2442993814e-6*omega^2

(2)

solve(eq, S)

0.7244233290e-6*(-0.6902041665e32+2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2), -0.7244233290e-6*(-0.6902041665e32+2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2), 0.7244233290e-6*(-0.6902041665e32-2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2), -0.7244233290e-6*(-0.6902041665e32-2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2)

(3)

U := _C1*exp(sqrt(-2*C*(3*C-sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))+_C2*exp(-sqrt(-2*C*(3*C-sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))+_C3*exp(sqrt(-2*C*(3*C+sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))+_C4*exp(-sqrt(-2*C*(3*C+sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))

U2 := diff(U, x)

1214414.026*_C1*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(1214414.026*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)-1214414.026*_C2*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(-1214414.026*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)+1214414.026*_C3*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(1214414.026*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)-1214414.026*_C4*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(-1214414.026*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)

(4)

A := simplify(Matrix(4, 4, [[coeff(subs(x = 0, U), _C1), coeff(subs(x = 0, U), _C2), coeff(subs(x = 0, U), _C3), coeff(subs(x = 0, U), _C4)], [coeff(subs(x = L, U), _C1), coeff(subs(x = L, U), _C2), coeff(subs(x = L, U), _C3), coeff(subs(x = L, U), _C4)], [coeff(subs(x = L, U2), _C1), coeff(subs(x = L, U2), _C2), coeff(subs(x = L, U2), _C3), coeff(subs(x = L, U2), _C4)], [coeff(subs(x = 0, U2), _C1), coeff(subs(x = 0, U2), _C2), coeff(subs(x = 0, U2), _C3), coeff(subs(x = 0, U2), _C4)]]))

Matrix(%id = 18446746958277761134)

(5)

solve(Determinant(A) = 0)

Warning,  computation interrupted

 

NULL

NULL

``


 

Download result3.mw
 

NULL

with(LinearAlgebra); restart; delta := 0.407e-9; C := 2023.2; L := 5*delta

0.2035e-8

(1)

eq := (1/12)*delta^2*S^4+S^2+omega^2/C^2

0.1380408333e-19*S^4+S^2+0.2442993814e-6*omega^2

(2)

solve(eq, S)

0.7244233290e-6*(-0.6902041665e32+2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2), -0.7244233290e-6*(-0.6902041665e32+2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2), 0.7244233290e-6*(-0.6902041665e32-2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2), -0.7244233290e-6*(-0.6902041665e32-2760816666.*(-0.8430822546e19*omega^2+0.6250000000e45)^(1/2))^(1/2)

(3)

U := _C1*exp(sqrt(-2*C*(3*C-sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))+_C2*exp(-sqrt(-2*C*(3*C-sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))+_C3*exp(sqrt(-2*C*(3*C+sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))+_C4*exp(-sqrt(-2*C*(3*C+sqrt(-3*delta^2*omega^2+9*C^2)))*x/(C*delta))

U2 := diff(U, x)

1214414.026*_C1*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(1214414.026*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)-1214414.026*_C2*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(-1214414.026*(-24560029.44+4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)+1214414.026*_C3*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(1214414.026*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)-1214414.026*_C4*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*exp(-1214414.026*(-24560029.44-4046.4*(-0.496947e-18*omega^2+36840044.16)^(1/2))^(1/2)*x)

(4)

A := simplify(Matrix(4, 4, [[coeff(subs(x = 0, U), _C1), coeff(subs(x = 0, U), _C2), coeff(subs(x = 0, U), _C3), coeff(subs(x = 0, U), _C4)], [coeff(subs(x = L, U), _C1), coeff(subs(x = L, U), _C2), coeff(subs(x = L, U), _C3), coeff(subs(x = L, U), _C4)], [coeff(subs(x = L, U2), _C1), coeff(subs(x = L, U2), _C2), coeff(subs(x = L, U2), _C3), coeff(subs(x = L, U2), _C4)], [coeff(subs(x = 0, U2), _C1), coeff(subs(x = 0, U2), _C2), coeff(subs(x = 0, U2), _C3), coeff(subs(x = 0, U2), _C4)]]))

Matrix(%id = 18446746958277761134)

(5)

solve(Determinant(A) = 0)

Warning,  computation interrupted

 

NULL

NULL

``


 

Download result3.mw

 

 

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.

Dear Friends, 

I would appreciate your help in resolving some issues. Let me describe my dummy code and the issues I am having. 

I want to know the proper value of two parameters beta and `&Delta;A` in a tripe integral function  by the NonlinearFit command.The triple integral function is complex.

The code of function is below:

int(tan(beta)^2*exp(-Pi*tan(beta)^2*((x-varepsilon)^2+(0-varsigma)^2)/eta^2)/eta^2, [eta = 22.83-sqrt((5.83+`&Delta;A`)^2-varepsilon^2) .. 22.83+sqrt((5.83+`&Delta;A`)^2-varepsilon^2), varepsilon = -5.83-`&Delta;A` .. 5.83+`&Delta;A`, varsigma = -1 .. 1])-(int(tan(beta)^2*exp(-Pi*tan(beta)^2*((x-varepsilon)^2+(0-varsigma)^2)/eta^2)/eta^2, [eta = 22.83-sqrt(5.83^2-varepsilon^2) .. 22.83+sqrt(5.83^2-varepsilon^2), varepsilon = -5.83 .. 5.83, varsigma = -1 .. 1])) 

And this is the complete code:

datax := [-8, -4.5, -.5, 4.5, 8, 11.5, 14.5];

datay := [0.287e-2, 0.266e-2, 0.259e-2, 0.199e-2, 0.164e-2, 0.113e-2, 0.78e-3];

f := NonlinearFit(int(tan(beta)^2*exp(-Pi*tan(beta)^2*((x-varepsilon)^2+(0-varsigma)^2)/eta^2)/eta^2, [eta = 22.83-sqrt((5.83+`&Delta;A`)^2-varepsilon^2) .. 22.83+sqrt((5.83+`&Delta;A`)^2-varepsilon^2), varepsilon = -5.83-`&Delta;A` .. 5.83+`&Delta;A`, varsigma = -1 .. 1])-(int(tan(beta)^2*exp(-Pi*tan(beta)^2*((x-varepsilon)^2+(0-varsigma)^2)/eta^2)/eta^2, [eta = 22.83-sqrt(5.83^2-varepsilon^2) .. 22.83+sqrt(5.83^2-varepsilon^2), varepsilon = -5.83 .. 5.83, varsigma = -1 .. 1])), datax, datay, x)

I try to run it and get the value of parameters beta and `&Delta;A` ,but I keep getting this error,

Error, (in Statistics:-NonlinearFit) integration range or variable must be specified in the second argument, got HFloat(1.0) = HFloat(16.073603031200726) .. HFloat(29.58639696879927)
Does someone how to deal with this problem?

I would sincerely appreciate any inputs in this regard. 

 

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?

First 55 56 57 58 59 60 61 Page 57 of 61