nm

8552 Reputation

19 Badges

12 years, 346 days

MaplePrimes Activity


These are replies submitted by nm

@Carl Love 

Thanks for the info, I read the help page you refer to. But after 1 hr trying, I given up. I can't get it to work.

i.e. I tried calling the parent's ModuleCopy from the child using the syntax parent:-ModuleCopy() but when later on I look at the fields, I see there are still not initialized. 

May be you can see what is the correct way to do this. Here is a small example I made. The parent is a bank_account and the child is saving_account.

I also found that the child ModuleCopy can not have ::static added to it, once kernelopts('assertlevel'=2): is added. Maple complain that type static does not exist.

So I removed ::static from the ModuleCopy on the child and kept ::static only on the parent ModuleCopy. This is new change from the example I gave originally, because I did not have kernelopts('assertlevel'=2): before.

I learn better by trying things and using examples. Maple help is not good at all for learning Maple as it lacks many examples. 

If one picture is worth 1,000 words, then one example is worth one million words. Maplesoft should improve its help system.
 

8632

restart;

8632

interface(version);

`Standard Worksheet Interface, Maple 2023.0, Windows 10, March 6 2023 Build ID 1689885`

interface(warnlevel=4);
kernelopts('assertlevel'=2):

3

module bank_account()
  option object;
  local account_id::integer:=0;
  local amount::float:=0.0;

  export ModuleCopy::static:=proc(_self, proto::bank_account, account_id::integer, amount::float,$)
         print("enter bank_account ModuleCopy");
         if nargs=2 then
            _self:-account_id := proto:-account_id;
            _self:-amount     := proto:-amount;
         else
             print("Not proto case. account_id=",account_id);
            _self:-account_id := account_id;
            _self:-amount     := amount;
         fi;
         NULL;
  end proc;

  export get_id::static:=proc(_self,$)
         print("enter bank_account::get_id, _self:-account_id=",_self:-account_id);
         RETURN(_self:-account_id);
  end proc;

  export get_amount::static:=proc(_self,$)
         RETURN(_self:-amount);
  end proc;

end module;

module bank_account () local account_id::integer, amount::float; option object; end module

module saving_account()
  option object(bank_account);
  local interest_rate::float:=0.0;

  #do not use export. do not use ::static either ! If using ::static then Maple
  #gives error when kernelopts('assertlevel'=2): may be a bug?

  ModuleCopy := proc(_self, proto::saving_account, account_id::integer, interest_rate::float, amount::float,$)
         print("enter saving_account ModuleCopy");
         if nargs=2 then
            _self:-account_id := proto:-account_id;
            _self:-amount     := proto:-amount;
            _self:-interest_rate := proto:-interest_rate;
         else
            #call parent constructor. Notice, must use _self as first argument ! else
            #will not work
            #but this is not working at all. It is not initializing the fields as expected.
            #bank_account:-ModuleCopy(account_id, amount);  does not work

            #call is made but fields are not initialized
            bank_account:-ModuleCopy(_self,account_id, amount);
  
            #finish child constructor
            _self:-interest_rate := interest_rate;
         fi;
         NULL;
  end proc;

  export get_interest_rate::static:=proc(_self,$)
         RETURN(_self:-interest_rate);
  end proc;
end module;

module saving_account () local account_id::integer, amount::float, interest_rate::float; option object(bank_account); end module

account_id := 903432; interest_rate:=5.4; initial_amount:=100.0;
my_bank_account:=Object(bank_account,account_id,initial_amount);
my_bank_account:-get_id();
my_bank_account:-get_amount();

903432

5.4

100.0

"enter bank_account ModuleCopy"

"Not proto case. account_id=", 903432

module bank_account () local account_id::integer, amount::float; option object; end module

"enter bank_account::get_id, _self:-account_id=", 903432

903432

100.0

my_saving_account:=Object(saving_account,account_id,interest_rate,initial_amount);
my_saving_account:-get_interest_rate();
my_saving_account:-get_id();  #this returns 0 which is wrong
my_saving_account:-get_amount(); #this returns 0 which is wrong

"enter saving_account ModuleCopy"

"enter bank_account ModuleCopy"

"Not proto case. account_id=", 903432

module saving_account () local account_id::integer, amount::float, interest_rate::float; option object(bank_account); end module

5.4

"enter bank_account::get_id, _self:-account_id=", 0

0

0.


 

Download calling_parent_constructor.mw

 

@Carl Love 

Thanks. So having "export" on ModuleCopy in the child was the problem. 

But I modified your solution as below.

I do not want to duplicate the work done in base constructor also in the child constructor. This was a very basic example. But imagine the base class doing lots of work in its constructor. Then the child constructor (since it overwrote the base class constuctor method) has now to duplicate all this work. right?

So I made a new method in base class called base_ode_class_constructor to do the actual initialization of the base class. This will be called from both the base class as well from the child class. 

So this way I can have ModuleCopy in both the base and the child, but not duplicate the code that does base class initialization.

I think now it is an OK setup. It would be better if Maple OOP allowed child class constructor to call base class constructor directly as in Java and C++ and all other OOP languages. But I think this workaround is OK for now.
 

restart;

30160

interface(version)

`Standard Worksheet Interface, Maple 2023.0, Windows 10, March 6 2023 Build ID 1689885`

module ode_class()
   option object;
   local ode::`=`;
   local x::symbol;
   local y::symbol;

   #constructor
   export ModuleCopy::static := proc( _self, proto::ode_class, ode, func, $ )           
       print("base class constructor");
       if nargs=2 then
          _self:-ode := proto:-ode;
          _self:-y   := proto:-y;
          _self:-x   := proto:-x;
       else
          _self:-base_ode_class_constructor(ode, func);
       fi   
   end proc;

   #this will be called by child class to initlize base
   local base_ode_class_constructor::static :=proc(_self,ode,func,$)
         print("base class constructor helper");
        _self:-ode := ode;
        _self:-y   := op(0,func);
        _self:-x   := op(1,func);
   end proc;

   export get_ode::static:=proc(_self,$)
     return _self:-ode;
   end proc;

   export get_x::static:=proc(_self,$)
     return _self:-x;
   end proc;

   export get_y::static:=proc(_self,$)
     return _self:-y;
   end proc;


end module;

module ode_class () local ode::`=`, x::symbol, y::symbol; option object; end module

module first_order_ode_class()
   option object(ode_class);
   local is_linear_ode::truefalse;

   #constructor
   ModuleCopy::static := proc( _self, proto::first_order_ode_class, ode, func, $ )
       if nargs=2 then
          _self:-ode := proto:-ode;
          _self:-y   := proto:-y;
          _self:-x   := proto:-x;
       else
          _self:-base_ode_class_constructor(ode, func);
       fi;

       #rest of child constructor here
       print("Finished base class constructor. Now doing child");
       _self:-is_linear_ode := false;
   end proc;

   export is_linear::static:=proc(_self,$)
          return _self:-is_linear_ode;
   end proc;
          
end module;

 

module first_order_ode_class () local ode::`=`, x::symbol, y::symbol, is_linear_ode::truefalse; option object(ode_class); end module

ode:=Object(first_order_ode_class,diff(y(x),x)=sin(x),y(x));
ode:-is_linear();
ode:-get_ode();

"base class constructor helper"

"Finished base class constructor. Now doing child"

module first_order_ode_class () local ode::`=`, x::symbol, y::symbol, is_linear_ode::truefalse; option object(ode_class); end module

false

diff(y(x), x) = sin(x)

 

 

Download OOP_with_base_class_call.mw

can't give you a definite answer as it can depend on many things, but with my limited experience with Maple is that if Maple does not solve an equation in few minutes, (assuming you have fast enough CPU and sufficient memory) then waiting many hours is not going to make any difference. 

But why not try and find out yourself? leave it running overnight on one of the problems you have which takes long time and find out?

Also you should really use timelimit() on all your calls to Maple functions. No need to wait and manually stop Maple yourself. timelimit() can also hang easily and not stop as asked, but at least it is better than nothing.

@acer 

Using Array is also fast

p1 := proc(N) local i;
  [seq(i^2, i=1..N)];
end proc:

p2 := proc(N) local i,T;
  T := table():
  for i from 1 to N do
    T[i] := i^2;
  end do:
  convert(T,list);
end proc:

p3 := proc(N) local i,R;
  R := []:
  for i from 1 to N do
    R := [op(R), i^2];
  end do:
  R;
end proc:
 
p4 := proc(N) 
    local T:=Array(0..1);
    local i;
    for i from 1 to N do
        T ,= i^2;
    od;
    convert(T,list);
end proc:
	
n := 10^5: 	
	
CodeTools:-Usage( p1(n) ):
CodeTools:-Usage( p2(n) ):
CodeTools:-Usage( p3(n) ):
CodeTools:-Usage( p4(n) ):

Gives on my PC

 

There is normally 2 updates per major version. So 2022.1 and 2022.2

This will be same for 2023. There will be 2023.1 and 2023.2

This is how maplesoft been doing for the last few years. I do not remember a time when there was more than two updates within one major version.

To get 2023 you can go to MapleStore and either do an upgrade or buy new version depending on what you qualify for. Upgrade is less expensive.

 

@Rouben Rostamian  

That is correct, the code does chop and not round, it comes from tihis line 

L[2]:=L[2][1..k];

It is easy to add code to make it do rounding instead. Will leave that to the OP as an exercise if they want to use it, but I suspect they will use your version as it is shorter and does rounding already.

 

 

have a look at  GraphTheory:-ShortestPath

@Ronan 

 This is a Maple specific problem

This is what I also think, since it is only the Maple application that this happens to. And only in Maple 2023. I have not seen this in Maple 2022.2. 

@alexsid 

I'll try to upgrade nvidia driver and see if this fixes this. I have to find how to do that. My windows 10 is all up-to-date.

But the question is, why is it only Maple that have its window content disappear like this?

I have many other windows apps open and none of them have this issue. But none of those other apps use Java for the front end (which is a good thing they do not. Java is horrible for GUI).

I do not know if it is related to Java or not, but that is the main difference I see now.

@sand15 

nice touch with the change to 

`#mo("ℤ")`

This makes it look nice in Latex.

@Preben Alsholm 

thanks. This works better. But it overlooked some case Where it keeps _Z as is and not change it to Z like the others. Your code is supposed to change all "_Znnnn~" to "Z" right? not to "_Z" ? Here is an example where "_Z" remained as "_Z"

restart;
S4:=singular(1/(cos(x)*sin(x)*Psi(1/x)),x);
#S4:=singular(1/(cos(x))+Psi(1/x),x); 
#S4:=singular(1/cos(x),x); 
idts:=indets([S4],`local`);
S:=convert~(idts,string);
idts;

resZ,resNoZ:=selectremove(s->evalb(s[2]="Z"),S);
resN,resNoN:=selectremove(s->evalb(s[2]="N"),resNoZ);
resNN,resNoNN:=selectremove(s->evalb(s[2..3]="NN"),resN);
numZ:=numelems(resZ);
numNN:=numelems(resNN);
numN:=numelems(resNoNN);
`union`(idts[1..numN]=~N,idts[numN+1..numNN]=~NN,idts[numN+numNN+1..numZ+numNN]=~Z) ;
assign(%);
S4;

The input is 

Notice that last _Z has no ~ after it, like all the others,. May that is why it was not changed by your code. Here is the output of your code

But this is not a big deal. Just thought to mention it. Will test your code more on other examples. I do not know what Maple does not have a ~ after that last Z like with all the other ones.

@Preben Alsholm 

Ah! But now your new code does not work any more on a little different example. I think it is because you hardcoded [-2..1] in there for the specific example we were talking about? 

restart;

#S4:=singular(1/(cos(x)*sin(x))+Psi(1/x),x);
S4:=singular(1/(cos(x))+Psi(1/x),x);  #try on this one
idts:=indets([S4],`local`);

idts[1]=~NN;

assign(%);

idts;

idts[-2..-1]=~Z; # Don't use _Z

assign(%);

idts;

S4;

For the above we see it changes

To

So the problem is still there. 

I think a StringTools:-RegSubs might be the easier way to do this, i.e. convert each output to string (we only need RHS) and make a regular expression which changes  _Z*anything* to _Z and also change _N*anything* to _N.  

My regular expression skill is a little rusty, but will try to see if I ca do it later on.

Btw, your example above will work if I change idts[-2..-1]=~Z;  to idts[-1..-1]=~Z;  

If you could automate this part, then I think it will work.  I can't hardcode [-2..1] ofcourse, it has to be something decided at run-time.

This is not an easy problem, all becuase Maple likes to change _Z on its own for some reason instead of using just one _Z

@Preben Alsholm 

I was also looking at StringTools:-RegSubs to do this. But I tried your code. But it seems to change _N to _Z?

I am just now interested in single example, The union part I can always do at the very end, so it is not important. I tried your code on this

S4:=singular(1/(cos(x)*sin(x))+Psi(1/x),x);

Which gives

So it should have changed _Z2 to _Z and changed _Z1 to _Z, which is does. But why it changed _NN to _Z also? DId I not use your code correctly?

restart;



S4:=singular(1/(cos(x)*sin(x))+Psi(1/x),x);
idts:=indets([S4],`local`);
idts=~NN;
assign(%);
idts;
idts=~Z; # Don't use _Z
assign(%);
idts;
S4

try with timelimit

@C_R 

Any other mserver.exe corresponds to the tabs as you open files in Maple.

But how to know which mserver.exe is connected to which worksheet?? That is the whole point of this question. I can have 10 worksheets open at once. Also many times there could be an mserver.exe  in separate places in the task manager. I've seen mserver.exe sitting on its own outside the main group of mservers.  This method of counting is not practical.

4 5 6 7 8 9 10 Last Page 6 of 71