nm

11523 Reputation

20 Badges

13 years, 108 days

MaplePrimes Activity


These are questions asked by nm

Maple help does not have an example I could find showing the right way to call method, which is meant to be called only from the constructor the object, before the object is fully build.

Help page does not have such example.

This worksheet shows 3 possible ways to do this. All seem to work fine. I am sure one can come up with more variations.

The question is, which one of these calls is the "right" way?

The method called from the constructor in this example is called process_age().

If someone knows an official Maple documentation showing one example of calling  a proc from constructor that will also help.

Version 1

 

restart;

module person()
   option object;

   local _name::string;
   local _age::posint;

   export ModuleCopy::static := proc( _self, proto::person,
        name::string,age::posint,$)
        _name:=name;
        _age:=process_age(age);
   end proc;

   local process_age:=proc(age::posint,$)::posint;
         age+1;
   end proc;

   export get_age::static:=proc(_self,$)::posint;
          _age;
   end proc;
end module;

module person () local _name::string, _age::posint, process_age; option object; end module

o:=Object(person,"me",99);
o:-get_age()

module person () local _name::string, _age::posint, process_age; option object; end module

100

 

 

Version 2

 

restart;

module person()
   option object;

   local _name::string;
   local _age::posint;

   export ModuleCopy::static := proc( _self, proto::person,
        name::string,age::posint,$)
        _name:=name;
        _age:=process_age(age);
   end proc;

   local process_age::static:=proc(age::posint,$)::posint;
         age+1;
   end proc;

   export get_age::static:=proc(_self,$)::posint;
          _age;
   end proc;
end module;

module person () local _name::string, _age::posint; option object; end module

 

o:=Object(person,"me",99);
o:-get_age()

module person () local _name::string, _age::posint; option object; end module

100

 

 

Version 3

 

restart;

module person()
   option object;

   local _name::string;
   local _age::posint;

   export ModuleCopy::static := proc( _self, proto::person,
        name::string,age::posint,$)
        _name:=name;
        _age:=_self:-process_age(age);
   end proc;

   local process_age::static:=proc(_self,age::posint,$)::posint;
         age+1;
   end proc;

   export get_age::static:=proc(_self,$)::posint;
          _age;
   end proc;
end module;

module person () local _name::string, _age::posint; option object; end module

 

 

o:=Object(person,"me",99);
o:-get_age()

module person () local _name::string, _age::posint; option object; end module

100

 

 

Download correct_way_to_call_method_from_constructor_oct_17_2025.mw

Help page for structured types shows examples where they add delayed quotes around things. For example

So I started writing  'specfunc(anything,y)' instead of just  specfunc(anything,y). Only to find this breaks things when used inside a proc.

Here is an example.

restart;

#here it worksheetdir
IC:=y(x)=0;
indets(IC,'specfunc(anything,y)');      

y(x) = 0

{y(x)}

#inside proc, same code fail
foo:=proc(func::function(name),IC::`=`)
    local y::symbol:=op(0,func);
    indets(IC,'specfunc(anything,y)');
end proc:
 

foo(y(x),y(x)=0)

{}

 

 

Removing the quotes, it now works

restart;

#here it worksheetdir
IC:=y(x)=0;
indets(IC,'specfunc(anything,y)');      

y(x) = 0

{y(x)}

#inside proc, same code fail
foo:=proc(func::function(name),IC::`=`)
    local y::symbol:=op(0,func);
    indets(IC,specfunc(anything,y));
end proc:
 

foo(y(x),y(x)=0)

{y(x)}

 

 

Download delayed_vs_not_v2.mw

Download delayed_vs_not.mw

It took me 2 hrs debugging to find this, as when I copied code which was working in my worksheet to my function, it stopped working and did not know why.

So for now, I will not use quotes around specfunc or any others like the help page shows in its examples.

Is there a rule of thumb to follow here? The help page is very inconsistent. It shows some examples with quotes around and some without. Makes it hard to know why and when to add the delayed quotes. It seems arbitrary.

I gave up on this. I know how to find all normal derivatives such as diff(y(x),x) and diff(y(x),x$2) so on in expression. 

But now I want to find all those used for initial conditions, which do not have x in them. This makes it harder.

These have the form   D(y)(0) or (D@@2)(y)(1) and so on.

I tried indets and select and many other things, but can't figure how to tell Maple to find these in expression. Here is an example.

Given

expr:=y(3)+y(8)+5*(D@@2)(y)(4)+Pi+3*exp(2)+77*D(y)(0)+1/(D@@4)(y)(7);

I'd like to get list that has in it only the derivatives and nothing else, anywhere they show, which will be

{(D@@2)(y)(4) , D(y)(0) , (D@@4)(y)(7) }

some of the things I tried are

indets(expr,'satisfies'(s->op(0,s)=(D@@n)(y) and n::integer) );
indets(expr,'specfunc(anything, D)');
indets(expr,'specfunc(anything, (D@@anything)(y))');

And many more. I still struggle with structured types in Maple.

In the above, we can assume the dependent variable is always same which is the 'y' symbol.

I can do the above using patmatch. But I am trying to move away from that in Maple and use types.

For reference, this is what I do using another software. I am trying to do the same in Maple, but using structured types and not patmatch.

Iam sure there is a way to do this in Maple using structured types, but so far not able to figure the syntax needed.

Maple 2025.1

I have more examples like this. Sometimes odetest give false negative result. dsolve solution is actually correct, but odetest does not return all zeros as result of its checking.

Example 1

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

Physics:-Version();

`The "Physics Updates" version in the MapleCloud is 1881 and is the same as the version installed in this computer, created 2025, October 7, 16:4 hours Pacific Time.`

SupportTools:-Version();

`The Customer Support Updates version in the MapleCloud is 29 and is the same as the version installed in this computer, created June 23, 2025, 10:25 hours Eastern Time.`

ode:=x^2*diff(y(x),x)+sin(2*y(x)) = 1;
IC:=y(infinity)=11*Pi/4;
maple_sol:=dsolve([ode,IC]);
odetest(maple_sol,[ode,IC])

x^2*(diff(y(x), x))+sin(2*y(x)) = 1

y(infinity) = (11/4)*Pi

y(x) = -arctan((x+2)/(x-2))+3*Pi

[0, undefined]

#but the IC above should not be undefined. This belows shows it is correct.
limit(maple_sol,x=infinity);

limit(y(x), x = infinity) = (11/4)*Pi

#which is what the IC is.


Download odetest_problem_oct_8_2025.mw

Would you agree that odetest in the above should have returned [0,0] instead? Should this result be considered a bug?

Example 2

More examples where odetest gives false negative. The solution by dsolve is correct but odetest says IC is off but it is not. 

 

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

restart;

ode:=x^2*diff(y(x),x)+cos(2*y(x))=1;
IC:=y(infinity)=10/3*Pi;
sol:=dsolve([ode,IC]);

x^2*(diff(y(x), x))+cos(2*y(x)) = 1

y(infinity) = (10/3)*Pi

y(x) = (7/2)*Pi-arctan((1/3)*(3^(1/2)*x+6)/x)

odetest(sol,[ode,IC]);

[0, -(1/6)*Pi]

limit(sol,x=infinity);

limit(y(x), x = infinity) = (10/3)*Pi

 


 

Download odetest_problem_oct_11_2025.mw

Example 3

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

ode:=x^2*diff(y(x),x)*cos(y(x))+1=0;
IC:=y(infinity)=16/3*Pi;

x^2*(diff(y(x), x))*cos(y(x))+1 = 0

y(infinity) = (16/3)*Pi

sol:=dsolve([ode,IC]);

y(x) = arcsin((1/2)*(3^(1/2)*x-2)/x)+5*Pi

odetest(sol,[ode,IC])

[0, (1/3)*Pi]

limit(sol,x=infinity)

limit(y(x), x = infinity) = (16/3)*Pi

 

 

Download odetest_problem_oct_11_2025_V2.mw

Example 4

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

restart;

ode:=diff(y(x),x)*sin(x)-y(x)*cos(x)=-sin(x)^2/x^2;
IC:=y(infinity)=0;

(diff(y(x), x))*sin(x)-y(x)*cos(x) = -sin(x)^2/x^2

y(infinity) = 0

sol:=dsolve([ode,IC]);

y(x) = sin(x)/x

odetest(sol,[ode,IC]);

[0, undefined]

limit(sol,x=infinity)

limit(y(x), x = infinity) = 0

 

 

Download odetest_problem_oct_11_2025_V3.mw

Example 5


 

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

restart;

ode:=x*y(x)^2+exp(x)*diff(y(x),x)=0;
IC:=y(infinity)=1/2;
sol:=dsolve([ode,IC]);

x*y(x)^2+exp(x)*(diff(y(x), x)) = 0

y(infinity) = 1/2

y(x) = -1/(x*exp(-x)+exp(-x)-2)

odetest(sol,[ode,IC])

[0, undefined]

limit(sol,x=infinity)

limit(y(x), x = infinity) = 1/2

 

 

Download odetest_problem_oct_11_2025_V4.mw

Will add more examples as I find them....

 

Any one could find why dsolve can't solve this first order ode when adding IC,  but Student:-ODEs:-ODESteps can?

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

Physics:-Version();

`The "Physics Updates" version in the MapleCloud is 1878 and is the same as the version installed in this computer, created 2025, September 28, 11:35 hours Pacific Time.`

SupportTools:-Version();

`The Customer Support Updates version in the MapleCloud is 29 and is the same as the version installed in this computer, created June 23, 2025, 10:25 hours Eastern Time.`

restart;

ode:=diff(m(t),t) = -k/m(t)^2;
ic:=m(0) = m__0;
DEtools:-odeadvisor(ode);

diff(m(t), t) = -k/m(t)^2

m(0) = m__0

[_quadrature]

sol:=dsolve(ode)

m(t) = (-3*k*t+c__1)^(1/3), m(t) = -(1/2)*(-3*k*t+c__1)^(1/3)-((1/2)*I)*3^(1/2)*(-3*k*t+c__1)^(1/3), m(t) = -(1/2)*(-3*k*t+c__1)^(1/3)+((1/2)*I)*3^(1/2)*(-3*k*t+c__1)^(1/3)

sol:=dsolve([ode,ic])

Student:-ODEs:-ODESteps([ode,ic])

"[[,,"Let's solve"],[,,[(ⅆ)/(ⅆt) m(t)=-k/((m(t))^2),m(0)=`m__0`]],["•",,"Highest derivative means the order of the ODE is" 1],[,,(ⅆ)/(ⅆt) m(t)],["•",,"Solve for the highest derivative"],[,,(ⅆ)/(ⅆt) m(t)=-k/((m(t))^2)],["•",,"Separate variables"],[,,((ⅆ)/(ⅆt) m(t)) (m(t))^2=-k],["•",,"Integrate both sides with respect to" t],[,,∫((ⅆ)/(ⅆt) m(t)) (m(t))^2 ⅆt=∫-k ⅆt+`c__1`],["•",,"Evaluate integral"],[,,((m(t))^3)/3=-k t+`c__1`],["•",,"Solve for" m(t)],[,,{m(t)=(-3 k t+3 `c__1`)^(1/3),m(t)=-((-3 k t+3 `c__1`)^(1/3))/2-(ⅈ sqrt(3) (-3 k t+3 `c__1`)^(1/3))/2,m(t)=-((-3 k t+3 `c__1`)^(1/3))/2+(ⅈ sqrt(3) (-3 k t+3 `c__1`)^(1/3))/2}],["•",,"Simplify"],[,,{m(t)=(-3 k t+3 `c__1`)^(1/3),m(t)=((-3 k t+3 `c__1`)^(1/3) (-1+ⅈ sqrt(3)))/2,m(t)=-((-3 k t+3 `c__1`)^(1/3) (1+ⅈ sqrt(3)))/2}],["•",,"Redefine the integration constant(s)"],[,,{m(t)=(-3 k t+`c__1`)^(1/3),m(t)=((-3 k t+`c__1`)^(1/3) (-1+ⅈ sqrt(3)))/2,m(t)=-((-3 k t+`c__1`)^(1/3) (1+ⅈ sqrt(3)))/2}],["•",,"Use initial condition" m(0)=`m__0`],[,,`m__0`=`c__1`^(1/3)],["•",,"Solve for" `c__1`],[,,`c__1`=`m__0`^3],["•",,"Substitute" `c__1`=`m__0`^3 "into general solution and simplify"],[,,m(t)=(`m__0`^3-3 k t)^(1/3)],["•",,"Use initial condition" m(0)=`m__0`],[,,`m__0`=(`c__1`^(1/3) (-1+ⅈ sqrt(3)))/2],["•",,"Solve for" `c__1`],[,,`c__1`=`m__0`^3],["•",,"Substitute" `c__1`=`m__0`^3 "into general solution and simplify"],[,,m(t)=((`m__0`^3-3 k t)^(1/3) (-1+ⅈ sqrt(3)))/2],["•",,"Use initial condition" m(0)=`m__0`],[,,`m__0`=-(`c__1`^(1/3) (1+ⅈ sqrt(3)))/2],["•",,"Solve for" `c__1`],[,,`c__1`=`m__0`^3],["•",,"Substitute" `c__1`=`m__0`^3 "into general solution and simplify"],[,,m(t)=-((`m__0`^3-3 k t)^(1/3) (1+ⅈ sqrt(3)))/2],["•",,"Solutions to the IVP"],[,,[m(t)=(`m__0`^3-3 k t)^(1/3),m(t)=((`m__0`^3-3 k t)^(1/3) (-1+ⅈ sqrt(3)))/2,m(t)=-((`m__0`^3-3 k t)^(1/3) (1+ⅈ sqrt(3)))/2]]]"

Download why_dsolve_cant_solve_ode_oct_4_2025_maple_2025_1.mw

1 2 3 4 5 6 7 Last Page 2 of 204