[go: up one dir, main page]

Items tagged with overload

Feed App Center

The procedure is to plot text points and lines. so it has three procedures in it. the 1st and 2nd work to plot text and pooints. But the 2nd is blocking access to the 3rd to plot lines. If I swap the 2nd and 3rd the problem reverse. I use typ and tyl to try to focre points or lines. What other check could if use  here?


 

restart

#with(LinearAlgebra)


RTPlot:=overload([

#1 plots text Matrix(1,3) are projective points. Matrix(3,1) are projective lines

proc(p1::{list,Vector[row],Matrix(1,3)},p2::{list,Vector[row],Vector[column],Matrix(1,3)},{Txt::{list,string}:="Zebra"})
option overload;
  description "plots text";
uses plots, plottools, LinearAlgebra;
local ang,pc1, pc2, td:=2,txt, f, plt;
if p1::{list,Vector[row]} and p2::{list,Vector[row]} then
   pc1:=p1;
    if numelems(p2)=2 then
      ang:=evalf(VectorAngle(p2-~p1,[1,0]));
     elif numelems(p2)=3 then
      td:=3;
      ang:=evalf(VectorAngle(p2-~p1,[1,0,0]));
     end if;
elif p1::list and p2::{Vector[column] }then
  pc1:=p1;
   if numelems(p2)=2 then
      ang:=evalf(VectorAngle(p2,<1,0>));
   elif numelems(p2)=3 then
      td:=3;
      #ang:=evalf(VectorAngle(p2,<1,0,0>));
  end if;
elif p1::'Matrix'(1,3) and p2::'Matrix'(1,3) then
  if p2[1,3]=0 then
     pc1:=LPproj(p1);
     ang:=evalf(VectorAngle(p2,<1,0,0>));
   else
     pc2,pc1:=LPproj~([p2,p1],false)[];  
     ang:=evalf(VectorAngle(pc2-pc1,[1,0]));
  end if;
end if;
if Txt::string then
 txt:=Txt;
else
 txt:=Txt[];
end if;
if td=2 then
    display(point(pc1),textplot([op(pc1),txt,'rotation'=ang]));
 else
    display(point(pc1),textplot3d([op(pc1),txt]));
end if;
end proc,

#2 plots points. Matrix(1,3) are projective points


proc(p1::{list,vector[row],Matrix(1,3)},{dta:=[]},{typ::string:="y"})
option overload;
description "plots points";
uses plots, plottools, LinearAlgebra;
local pt,pl1,n,i  ;
if typ="pnt" then
    if p1::list and p1[1]::{list,vector[row],Matrix(1,3)} then
      n:=nops(p1);
      pl1:=p1;
     else
      n:=1;
      pl1:=[p1];
    end if;
  pt:=[];#print(pt,n);print(pl1);
    for i to n do
      if pl1[i]::list then
        pt:=[pt[],pl1[i]];
       elif pl1[i]::'Vector'[row] then
        #print(pl1[i]);
        pt:=[pt[],convert(pl1[i],list)];
       elif pl1[i]::'Matrix'(1,3) then
        pt:=[pt[],[pl1[i][1,1]/pl1[i][1,3],pl1[i][1,2]/pl1[i][1,3]] ];
      end if;
    end do;
  plots:-display(plottools:-point(pt,dta[]));
end if;
end proc,

#3 plots lines

proc(l1::{list,Vector[column],Matrix(3,1)},{tyl::string:="x"})
option overload;
description "plots lines";
uses plots, plottools, LinearAlgebra;
local xmin:=-3, xmax:=3, ymin:=-3, ymax:=3 ;
print("yyy");
if tyl="lnn" then
print("xxxxx");
  #if  l1::list and l1[1]::list and l1[2]::list then
    #plt1:=plottools:-line(l1[],color=black, thickness=4);
  #end if;
  plots:-display(plottools:-line(l1[],color=black, thickness=4));
end if;
end proc

]):

 

 

#maplemint(RTPlot)

RTPlot([1,2],<5|-6>,Txt=['typeset'("Local Minima ", [-Pi/2, -1]),colour=blue,'font' = ["times", "roman", 20],align=right]);

 

RTPlot([1,2,1],[5,6,11],Txt=['typeset'("Local Minima ", [-Pi/2, -1]),colour=blue,'font' = ["times", "roman", 20],align=right]);

 

RTPlot([1,2],<-5|-6>,Txt=['typeset'("Local Minima ", [-Pi/2, -1]),colour=blue,'font' = ["times", "roman", 20],align=right]);

 

#RTPlot([1,2,4],<5,6,1>,Txt=['typeset'("Local Minima ", [-Pi/2, -1]),colour=grey,'font' = ["arial",  16],align=right]);

RTPlot(<<-1|-1|2>>,<<1|-1|1>>,Txt=["Big word",colour=blue,'font' = ["times", "roman", 20],align=below])

 

RTPlot([[1,2],<3|-1>,[4,5],<<4|1|7>>],dta=[color=[red,green,blue,black],symbol=solidcircle,symbolsize=12],typ="pnt")

 

RTPlot([[1,2,1],<3|-1|4>,[4,5,6],[4,1,3],<7|0|2>],dta=[color=[red,green,blue,black,orange],symbol=solidcircle,symbolsize=18],typ="pnt")

 

RTPlot([[1,2],[3,-1]],tyl="lnn")

l1:=[[1,2],[3,-1]]

[[1, 2], [3, -1]]

(1)

RTPlot(l1,tyl="lnn")

#plots:-display(plottools:-line(l1[],color=black, thickness=4));

 


 

Download Q_2024-08-29_Plot_in_RT.mw

I use a procedure called Isee  to print to screen procedures from my packages.

However if the procedure is overloaded it Isee doesn't print it. Is there a way around this.
I have inserted screen shots to show the outputs for the package.

restart

Test:=proc(a::{vector})
print(a);
end proc;

proc (a::{vector}) print(a) end proc

(1)

Test1:=overload([ proc(a::{vector})
                  option overload;
                  print(a);
                  end proc,

                  proc(a::{Matrix})
                  option overload;
                  print(a);
                  end proc
                ])

proc () option overload; [proc (a::{vector}) option overload; print(a) end proc, proc (a::{Matrix}) option overload; print(a) end proc] end proc

(2)

Isee := proc(a)
interface(verboseproc = 3);
printf("%P", eval(a));
end proc;

proc (a) interface(verboseproc = 3); printf("%P", eval(a)) end proc

(3)

Isee(Test)

proc(a::{vector})
    print(a);
end proc

 

Isee(Test1)

proc()
    option overload;
    [proc(a::{vector}) option overload; print(a); end proc,
        proc(a::{Matrix}) option overload; print(a); end proc];
end proc

 

#with(Routines);
#

#An non overloaded procedure in a package

Isee(ConicMatrix);
#

 

                                  ConicMatrix

 

#An overloaded procecure in a package

Isee(FactReduce);
#

                                  FactReduce

 
 

 

Download 2024-08-12_print_overloaded_procedure.mw

The overloaded  procedure here test returns based on 2 lists or 3 lists entered. The two list has a mixed input type with a default value. The default value of "a" can cause a problem if an explicit value in not entered for "a" in foo1. I not sure is the mixed input type is adding to the problem.
By changing the order of the procedures the problem is avoidable here. But this just a simple example. When there are 6 or so procedures it can be very difficult to select a correct ordering.

Is there a way around this, apart from don't have default values?

I could experiment with changing the input order in each proc but that would break up some logical input sequences on me.

restart

 

foo:=overload([
                        proc(P1::list,P2::list,a::algebraic:=4,$)
                         option overload;
                         print("2 lists");
                          end proc,

                       proc(P1::list,P2::list,P3::list,$)
                         option overload;
                         print("3 lists");
                          end proc
                       ]);

proc () option overload; [proc (P1::list, P2::list, a::algebraic := 4, ` $`) option overload; print("2 lists") end proc, proc (P1::list, P2::list, P3::list, ` $`) option overload; print("3 lists") end proc] end proc

(1)

foo([1,2],[3,4])

"2 lists"

(2)

foo([1,2],[3,4],[4,7])

"3 lists"

(3)

 

 

 

foo1:=overload([
                        

                       proc(P1::list,P2::list,P3::list,$)
                         option overload;
                         print("3 lists");
                          end proc,

                        proc(P1::list,P2::list,a::algebraic:=4,$)
                         option overload;
                         print("2 lists");
                          end proc
                       ]);

proc () option overload; [proc (P1::list, P2::list, P3::list, ` $`) option overload; print("3 lists") end proc, proc (P1::list, P2::list, a::algebraic := 4, ` $`) option overload; print("2 lists") end proc] end proc

(4)

foo1([1,2],[3,4]); #incorrect output

"3 lists"

(5)

foo1([1,2],[3,4],4)

"2 lists"

(6)

foo1([1,2],[3,4],[4,7])

"3 lists"

(7)

 

Download 2024-02-3_Q_Overload_proc_.mw

I have this overloaded function foo(). 

restart;

foo:= overload(  
        [
        proc(A::integer,$)option overload;
            print("first one, _passed = ", _passed," _npassed = ",_npassed);             
        end proc,

        proc(A::integer,B::integer,$)option overload;
            print("second one  _passed = ", _passed," _npassed = ",_npassed);
        end proc
       ]);

Which works OK. so I can now do 

foo(1)
foo(1,2)

But I wanted to indicate that the proc returns say ::integer , and this where I am stuck, I do not know where to add this ::integer

With non-overloaded proc's, this is the syntax

foo:=proc(A::integer,$)::integer;
     ......
end proc;

but I can't do this with the overloaded function. If I type

foo:= overload(  
        [
        proc(A::integer,$)option overload;
            print("first one, _passed = ", _passed," _npassed = ",_npassed);             
        end proc,

        proc(A::integer,B::integer,$)option overload;
            print("second one  _passed = ", _passed," _npassed = ",_npassed);
        end proc
       ])::integer;

Maple simply does not like it. typing foo(1) now it just echos the definition back.

And these give syntax errors

foo:= overload(  
        [
        proc(A::integer,$)::integer option overload;
            print("first one, _passed = ", _passed," _npassed = ",_npassed);    
        end proc,

        proc(A::integer,B::integer,$)::integer option overload;
            print("second one  _passed = ", _passed," _npassed = ",_npassed);
        end proc
       ]);

Tried many other variations and looked at help but see nothing to far.

Can one add ::type to indicate type proc returns with overloaded proc?

Maple 2023.2.1

Maple 2023.1 will not let me overload a method for an object when using _self.  Is there a way around this?

module person()
   option object;
   local the_name::string:="*",the_age;

   export set_info::static:= overload(
   [
      proc(_self,the_name::string,$) option overload;
          _self:-the_name:=the_name;
      end proc,

      proc(_self,the_name::string,the_age::integer, $) option overload;
          _self:-the_name:=the_name;
          _self:-the_age:=the_age;
      end proc
   ]);

   export get_name::static:=proc(_self,$)
      RETURN(_self:-the_name);
   end proc;
end module;

And now when I do 

o:=Object(person);
o:-set_info("me");
o:-set_info("me",20)

Error, (in person:-set_info) `me` does not evaluate to a module
Error, invalid input: no implementation of person:-set_info 
matches the arguments in call, 'person:-set_info("me",20)'

The problem goes away by removing `_self` as first argument in signature of the overloaded method. But then I will not be able to use _self any more inside the methods.

There is no problem overloading the method when using normal module, or one that does not use _self. For example this works

restart;

module person() 
   export set_info:= overload(
   [
      proc(the_name::string,$) option overload;
          print(the_name);
      end proc,

      proc(the_name::string,the_age::integer, $) option overload;
          print(the_name);
          print(the_age);
      end proc
   ]);
end module;

person:-set_info("me");
person:-set_info("me",20)

How to make the first example above work? I need to overload a method inside an module with option object that uses _self

update

I found a workaround. But it is not good, but for now. For the overloaded method, instead of using 
           o:-set_info("me");

This works instead

         set_info(o,"me");

So the following now works

o:=Object(person);

#o:-set_info("me");   #do not use with overloaded
#o:-set_info("me",20); #do not use with overloaded

set_info(o,"me");  #now works OK with no error
set_info(o,"me",20); #now works OK with no error

o:-get_name();  #OK since this method  is not overloaded
o:-get_age();  #OK since this method  is not overloaded

I do not understand why _self can't be used with overloaded methods and if this a bug or by design. 

Maple 2023.1 on windows 10

67588

interface(version);

`Standard Worksheet Interface, Maple 2023.1, Windows 10, July 7 2023 Build ID 1723669`

restart;

67588

module person()
   option object;
   local the_name::string:="*",the_age;

   export set_info::static:= overload(
   [
      proc(_self,the_name::string,$) option overload;
           #print("Inside first overloaded set_info");
          _self:-the_name:=the_name;
      end proc,

      proc(_self,the_name::string,the_age::integer, $) option overload;
          #print("Inside second overloaded set_info");
          _self:-the_name:=the_name;
          _self:-the_age:=the_age;
      end proc
   ]);

   export get_name::static:=proc(_self,$)
      RETURN(_self:-the_name);
   end proc;

   export get_age::static:=proc(_self,$)
      RETURN(_self:-the_age);
   end proc;
end module;

module person () local the_name::string, the_age; option object; end module

o:=Object(person);
#o:-set_info("me");
#o:-set_info("me",20);

set_info(o,"me"):
set_info(o,"me",20):

o:-get_name();
o:-get_age();

module person () local the_name::string, the_age; option object; end module

"me"

20

 

Download self_with_overloaded.mw

 About 6 years ago I aked  a question on testing types in a package. How to Test Input Typed to a Procedure to Determine what to do? - MaplePrimes.  @Carl Love supplied the very good code below. I never used the answer back then as I really didn't understand what to do with it. I have a somewhat of a better idea now and I really need to be able to use this in general..

 Basically the code checks that all the inputs are of the same type and order (which is what the original question specified).

 Q1:- I now need to check  a mixed input type

         A:: a 2 list , B a 3 list   

          or 

           A:: a 2 Vector, and B a 3 Vector.

         etc

       I tried modifying the ModuleApply part of the code at the end, but that either causes errors or kernal looses connection.

Q2:-  What is a good way to handle different numbers of inputs to procedures (exported) here eg Proc1(a), Proc2(a,b), ....,                   ProcN(a1,...,aN)

Q3:-    Would the package exports of the module be the procedures inside the Mydispatch? Or does all of MyModule sit inside                the  package module?

  I have three different packages I would like to apply this to in general.

restart

 

MyModule:= module()
uses TT= TypeTools;
global _T1, _T2L, _T2V, _T3L, _T3V, _MyType;
local
     MyTypes:= {_T1, _T2L, _T2V, _T3L, _T3V},
     AllMyTypes:= MyTypes union {_MyType},

     ModuleLoad:= proc()
     local
          g, #iterator over module globals
          e
     ;
          #op([2,6], ...) of a module is its globals.
          for g in op([2,6], thismodule) do
               e:= eval(g);
               if g <> e and e in AllMyTypes then
                    error "The name %1 must be globally available.", g
               end if
          end do;
          TT:-AddType(_T1, algebraic);
          TT:-AddType(_T2V, 'Vector(2, algebraic)');
          TT:-AddType(_T2L, [algebraic $ 2]);
          TT:-AddType(_T3V, 'Vector(3, algebraic)');
          TT:-AddType(_T3L, [algebraic $ 3]);
          TT:-AddType(_MyType, MyTypes)
     end proc,

     ModuleUnload:= proc()
     local T;
          for T in AllMyTypes do TT:-RemoveType(T) end do
     end proc,

     MyDispatch:= overload([
          proc(A::_T1, B, C)
          option overload;
          local r:= "A, B, C are T1."; #unnecessary; just an example.
               #statements to process this type
          end proc,

          proc(A::_T2L, B, C)
          option overload;
          local r:= "A, B, C are T2L.";
               #
          end proc,

          proc(A::_T2V, B, C)
          option overload;
          local r:= "A, B, C are T2V.";
               #
          end proc,

          proc(A::_T3L, B, C)
          option overload;
          local r:= "A, B, C are T3L.";
               #         
          end proc,

          proc(A, B, C)
          local r:= "A, B, C are T3V.";
               #
          end proc,
#
#I added this
#
          proc(A, B)
          local r:= "A, B, are mixed.";
               #
          end proc
     ]),
#
# I have have  added  'Or'(....(A::_T2L,B::_T3L)
#
     ModuleApply:= proc(
          A::'Or'(And(
               _MyType,
               satisfies(A-> andmap(T-> A::T implies B::T and C::T, MyTypes) )
          ),

          (A::_T2L,B::_T3L)),
          B::_MyType, C::_MyType
     )
          MyDispatch(args)
     end proc
;
     ModuleLoad()    
end module:
 

#Example usage:
#

x:=[9,4];
y:=[5,7];
z:=[1,9];                                 
MyModule(x,y,z);

[9, 4]

[5, 7]

[1, 9]

"A, B, C are T2L."

x:=[9,4];
y:=[5,6,7];
z:=p;                                 
MyModule(x,y);  #Looses kernel connection

[9, 4]

[5, 6, 7]

p

Download Q_19-11-22_Module_Test_Types.mw

I have some of my main functions defined to take input in different formats. Some have required  parameters and also optional parameters. Similar to how say dsolve can take one ode, or a list of ode's or set of ode's and also other additional arguments.

Currently I use something like  ode::{`=`,set(`=`),list(`=`)} in the signature of the proc to indicate this input can be any of these types. There are also optional aguments.

So inside the proc, it does lots and lots of if this then do such else do such type of logic in order to determine which case/combination of input it is called with.

I am thinking of rewriting all of the API to use overload. Yes, it means I will have lots of copies of the same proc, each to handle specific case of the signature. But it also mean it will be now much simpler inside each of the overloaded proc's to determine which case of call it is processing.

I'd like to ask, is there anything to be aware of before making this change? does it affect performance much? It seems to me it will make the logic and the program simpler.  Here is a very simple example to illustrate.

Which you think is better? I like the overload version more. But I am worried that I will end up with too many versions of the API since I need one for each possible combination and may be this will slow Maple down? 

The following is the worksheet also.

restart;

interface(warnlevel=4);
kernelopts('assertlevel'=2):
foo:=overload(
[
   proc(A::`=`) option overload;
       print("single equation");
   end,
  
   proc(A::set(`=`)) option overload;
       if nops(A)=1 then
          print("single eq in a set");
       else
          print("more than one eq in a set");
       fi;
   end,

   proc(A::list(`=`)) option overload;
       if nops(A)=1 then
          print("single eq in a list");
       else
          print("more than one eq in a list");
       fi;
   end

]):

 

3

foo(x=1);
foo([x=1]);
foo([x=1,y=2]);
foo({x=1});
foo({x=1,y=2})

"single equation"

"single eq in a list"

"more than one eq in a list"

"single eq in a set"

"more than one eq in a set"

restart;

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

foo:=proc(A::{`=`,set(`=`), list(`=`)})

    if type(A,`=`) then
        print("single equation");
    elif type(A,set) then
          if nops(A)=1 then
             print("single eq in a set");
          else
             print("more than one eq in a set");
          fi;
    else #must be list
       if nops(A)=1 then
          print("single eq in a list");
       else
          print("more than one eq in a list");
       fi;
    fi;
end proc:

4

foo(x=1);
foo([x=1]);
foo([x=1,y=2]);
foo({x=1});
foo({x=1,y=2})

"single equation"

"single eq in a set"

"more than one eq in a set"

"single eq in a set"

"more than one eq in a set"

 

Download overload.mw

How does one undo an overload?                                                                        

The suggested solution in the answer https://mapleprimes.com/questions/234325-Use-Of-self--Inside-Object-Constructor  worked OK in the setup shown in that question.

But it does not work when putting the constructor inside overload

Here is an example where it works (i.e. by removing the type from _self as suggested in the above answer)

restart;

person:=module()
    option object;
    local name::string:="";

    #notice no _self::person, just _self
    export ModuleCopy::static:= proc( _self, proto::person, the_name::string, $ )            
           name:= the_name;
    end proc;

end module;


p:=Object(person,"me")

The above works. But since I have different constructors, once I put the above inside overload, using the same exact syntax, now the error comes back

restart;

person:=module()
    option object;
    local name::string:="";

    export ModuleCopy::static:= overload( 
    [
       proc( _self, proto::person, the_name::string, $ ) option overload;           
           name:= the_name;
      end proc
    ]);

end module;
             

p:=Object(person,"me")

Error, static procedure `ModuleCopy` refers to non-static local or export `name::string` in surrounding scope

I did not expect that using overload will cause any change to how it behaves. Is this a bug?

Below is worksheet also.

interface(version);

`Standard Worksheet Interface, Maple 2022.1, Windows 10, May 26 2022 Build ID 1619613`

restart;

person:=module()
    option object;
    local name::string:="";

    export ModuleCopy::static:= proc( _self, proto::person, the_name::string, $ )            
           name:= the_name;
    end proc;

end module;

_m2950059551392

p:=Object(person,"me")

_m2950172920000

restart;

person:=module()
    option object;
    local name::string:="";

    export ModuleCopy::static:= overload(
    [
       proc( _self, proto::person, the_name::string, $ ) option overload;           
           name:= the_name;
      end proc
    ]);

end module;

_m2950059551392

p:=Object(person,"me")

Error, static procedure `ModuleCopy` refers to non-static local or export `name::string` in surrounding scope

 

Download OO_version.mw

It would seem that if a package is loaded at the "top-level" via with() and the worksheet also has a procedure definition which contains a 'uses' statement for the same package (OK, admittedly a bit redundant), the commands from the relevant package actually "go missing".

See the output from test4() in the attached.

I can't believe that this is deliberate, because when trying to 'compartmentalise' code, then a 'uses' statement in a procedure seems like a good idea. To have this fail because the "top-level" worksheet contains a with() statement loading the same package seems perverse.

Please don't post workarounds - I already know several ways to do achieve it. I'm trying to find out if this behaviour is "deliberate" or a "bug". If the latter, it has been around for a long time because I have checked all the way back to Maple 18: every version exhibits the same behaviour

  restart:

  kernelopts(version);
  test1:= proc(M::Matrix)
               uses LinearAlgebra:
               return MatrixInverse(M):
          end proc:
  test2:= proc(M::Matrix)
               return LinearAlgebra:-MatrixInverse(M):
          end proc:
  with(LinearAlgebra):
  test3:= proc(M::Matrix)
               return MatrixInverse(M):
          end proc:
  test4:= proc(M::Matrix)
               uses LinearAlgebra:
               return MatrixInverse(M):
          end proc:
  test1( Matrix( [[1,2],[3,4]]));
  test2( Matrix( [[1,2],[3,4]]));
  test3( Matrix( [[1,2],[3,4]]));
  test4( Matrix( [[1,2],[3,4]]));

`Maple 2020.0, X86 64 WINDOWS, Mar 4 2020, Build ID 1455132`

 

Matrix(2, 2, {(1, 1) = -2, (1, 2) = 1, (2, 1) = 3/2, (2, 2) = -1/2})

 

Matrix(2, 2, {(1, 1) = -2, (1, 2) = 1, (2, 1) = 3/2, (2, 2) = -1/2})

 

Matrix(2, 2, {(1, 1) = -2, (1, 2) = 1, (2, 1) = 3/2, (2, 2) = -1/2})

 

MatrixInverse(Matrix(%id = 18446744074373391174))

(1)

 

Download usewith.mw

I was wondering if there was any way to reassign the "||" operator to calculate parallel resistors?

like this:

R1 || R2 = (1/R1+1/R2)^(-1)

 

Hi,

I have some matrix valued functions that that I can only define piecewise. Unfortunately I have some problems to manipulate those functions. Piecewise sees to do the job for  scalar valued functions. There I can easily differentiale and add results. For Matrix valued functions however, those features seem to not be availible.
 

I can work around these limitations by manually applying all operations to the operands of the piecewise function. Like I show here:


 

restart:

# a piecewise function

p1:=piecewise(a(t)^2=0,<cos(a(t))^2+sin(a(t))^2,0>,<1,1/a(t)>)

p1 := piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)}))

(1)

# differentiation does not work

diff(p1,t)

diff(piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)})), t)

(2)

# selecting individual entries does not work

r:=p1(1)+1

r := (piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)})))(1)+1

(3)

# strange simplification behaviour

simplify(p1)

piecewise(t = RootOf(a(_Z)), _z1(RootOf(a(_Z))), Vector(2, {(1) = 1, (2) = 1/a(t)}))

(4)

# current workaround, define new piecewise function by using op

r:=piecewise(op(1,p1),op(2,p1)(1)+1,op(3,p1)(1)+1)

r := piecewise(a(t)^2 = 0, cos(a(t))^2+sin(a(t))^2+1, 2)

(5)

 


 

Download Scratch.mw

 

I think my workaround is very cumbersome and error prone. Is there a better way to tackle this probem? Or should I try to overload all needed operations, and is this even possible?

Thanks for all suggestions!

Honigmelone

Hello,

I have two simple module's:

1) ------------------------------------------------------------------------

Point := proc(xx::float,yy::float)
    return module()
        local x := xx,y := yy;
        export ShowPoint,GetX,GetY;
        
        ShowPoint := proc()
            printf("Point X,Y -> [%f,%f]",x,y);
        end proc;

        GetX := proc()
            return x;
        end proc;        
        GetY := proc()
            return y;
        end proc;    
    end module:
end proc:

2) ------------------------------------------------------------------------

PointMath := module()
    option package;  
    export `+`;
    
    `+` := proc(a::Point(float,float),b::Point(float,float))
        option overload;
        Point(a:-GetX()+b:-GetX(),a:-GetY()+b:-GetY());
    end proc;
end module:

------------------------------------------------------------------------

Next I use first module:

p1:=Point(1.2,1.4);

p2:=Point(1.0,2.0);

Finally I want to add above two points:

with(PointMath)

p2:=p1+p2

The results is:

p2:=p1+p2

Why is not called operator '+' and two points are not added?

Best,

Rariusz

 

 

 

I have run into a "funny" feature of 2-D input: It seems to convert something like k/2 into this k*`^`(2,-1). While this would often not be an issue (it is correct after all), it becomes a problem when used in an argument list to a procedure. It becomes even more of a problem when, by chance, I have overloaded `^` to act on specific types that I have defined.

Let me try to explain briefly. I have a package called "Lattice" that does whatever it does (not of relevance here). I am writing a little manual for this package, for which I use 2-D input so I can write it in Maple and have the examples right in it and "live".

Here is what happens:

with(Lattice) # load the package

QFh:=Quad(0,kf/2) # Define an element for Lattice

Error, invalid input: Lattice:-`^` expects its 1st argument, element, to be of type Element, but received 2

Copy-pasting kf/2 into a 1-d worksheet, I get

QFh := Quad(0, kf*Lattice[`^`](2, -1));

So it uses Lattice[`^`] which actually appears to bypass the overload I have in the Lattice package. `^` is defined like this in Lattice:

`^`:=proc(element::Element,n::algebraic) option overload; # Element is a defined type in Lattice
...
end proc;

How can I possibly rewrite `^` to fall-back to Maple's ^ operator when called as Lattice[`^`] ?? I know there is a function overload() but have no experience with it. Would it even help?

Or am I missing something completely here? I do not use 2-D input for my usual work, but in this case I want and need to use it. The reason for its bizarre rewrite of "/2" is beyond me. Note that I can replace /2 by *0.5; but that causes problems later on for algebraic work as 1/2 is not 0.5 in Maple. I tried *1/2 but that has the same problem.

Has anyone a clean solution for this? I assume this effect is not limited to my own package but would affect others as well.

M.D.

PS: I ran into this using Maple 15 but I doubt it is specific to this particular version.

MatrixOperation := module() option package;  export `+`, LinearAlgebra;
    `+` := proc(a::float, b::float) option overload;
 :-`+`(map(x->x^2,a),map(x->x^2,b));
    end proc;
end module;


with(MatrixOperation);
with(LinearAlgebra):
m := Matrix([[1,2],[3,4]]);
L := MatrixMatrixMultiply(m,m);

1 2  1 2
3 4  3 4

1*1+2*3 = 1 + 6 = 1 after overload + with a+b-a*b
1*2+2*4 = 2 + 8 = -6

L should be Matrix([[1, -6],[....]])

1 2 Page 1 of 2