Ronan

1042 Reputation

14 Badges

12 years, 42 days
East Grinstead, United Kingdom

MaplePrimes Activity


These are replies submitted by Ronan

@dharr The types are used in many of the modules. I just didn't include them here. Scoping could be the issue. I had a scoping problem back in December when I was last looking at this. I will check it out tonight.

@dharr I see. So i tried something probably dangerous so I could apply a series of mutations from a list.

L:=[w,y,w,y,w,y]
                    L := [w, y, w, y, w, y]

newPopvs:=[Px,Py,Pz,Pw]:
for i in L do
newPopvs:=Mutate1(i,newPopvs);
end do
                        [Px, Py, Pz, Pw]

 

@Carl Love That wotks very well for me. I created a proc to mutate the population at a vertex called Mutate. Then I tried to make it more generat by including the Population list. Called Mutate1, This is giving an error "illegat use of formal parameter". What am a doing wrong here?
 

restart:

GT:= GraphTheory:

Vs:= [x,y,z,w]:

n:= [$nops(Vs)];

[1, 2, 3, 4]

V:= table(Vs=~ n);

table( [( y ) = 2, ( x ) = 1, ( z ) = 3, ( w ) = 4 ] )

V[y]

2

Popvs:= [4, 3, -1, 5]:

newPopvs:=Popvs

[4, 3, -1, 5]

Nbs:= Array([{2}, {1,3,4}, {2,4}, {2,3}]);

Vector[row](4, {(1) = {2}, (2) = {1, 3, 4}, (3) = {2, 4}, (4) = {2, 3}})

X:= GT:-Graph(Vs, Nbs);

GRAPHLN(undirected, unweighted, [x, y, z, w], Array(1..4, {(1) = {2}, (2) = {1, 3, 4}, (3) = {2, 4}, (4) = {2, 3}}), `GRAPHLN/table/1`, 0)

 

GT:-SetVertexPositions(X, [[0,0], [1,0], [1.5,1], [2,0]]):

GT:-DrawGraph(X)

X1:=GT:-RelabelVertices( X, [seq](cat(Vs[i], "=",Popvs[i]),i=n )) ;

GRAPHLN(undirected, unweighted, [`x=4`, `y=3`, `z=-1`, `w=5`], Array(1..4, {(1) = {2}, (2) = {1, 3, 4}, (3) = {2, 4}, (4) = {2, 3}}), `GRAPHLN/table/1`, 0)

GT:-DrawGraph(X1)

newX:= GT:-RelabelVertices(X, [seq](cat(Vs[i], "=", add(Popvs[[i, Nbs[i][]]])), i= n));

GRAPHLN(undirected, unweighted, [`x=7`, `y=11`, `z=7`, `w=7`], Array(1..4, {(1) = {2}, (2) = {1, 3, 4}, (3) = {2, 4}, (4) = {2, 3}}), `GRAPHLN/table/1`, 0)

GT:-DrawGraph(newX);

Mutate := proc(vtx)
description " Mutates population at a vertex";
 local i;
 global Vs, Popvs, Nbs,n,newPopvs;
 
 print(vtx, "Pop at Vertex =  ",newPopvs[V[vtx]]);
 newPopvs[V[vtx]] := -newPopvs[V[vtx]] + add(newPopvs[i],i=[op(Nbs[V[vtx]])]);
print(vtx,"Mutated Pop =  ", newPopvs[V[vtx]]);
 end proc

proc (vtx) local i; global Vs, Popvs, Nbs, n, newPopvs; description " Mutates population at a vertex"; print(vtx, "Pop at Vertex =  ", newPopvs[V[vtx]]); newPopvs[V[vtx]] := -newPopvs[V[vtx]]+add(newPopvs[i], i = [op(Nbs[V[vtx]])]); print(vtx, "Mutated Pop =  ", newPopvs[V[vtx]]) end proc

``

Mutate1 := proc(vtx,poplist)
description " Mutates population at a vertex";
 local i;
 global Vs, Popvs, Nbs,n,newPopvs;
 
 print(vtx, "Pop at Vertex =  ",poplist[V[vtx]]);
 poplist[V[vtx]] := -poplist[V[vtx]] + add(poplist[i],i=[op(Nbs[V[vtx]])]);
print(vtx,"Mutated Pop =  ", poplists[V[vtx]]);
 end proc

proc (vtx, poplist) local i; global Vs, Popvs, Nbs, n, newPopvs; description " Mutates population at a vertex"; print(vtx, "Pop at Vertex =  ", poplist[V[vtx]]); poplist[V[vtx]] := -poplist[V[vtx]]+add(poplist[i], i = [op(Nbs[V[vtx]])]); print(vtx, "Mutated Pop =  ", poplists[V[vtx]]) end proc

newPopvs

[4, 3, -1, 5]

Mutate(x);
Mutate(z);
newPopvs

x, "Pop at Vertex =  ", 4

x, "Mutated Pop =  ", -1

z, "Pop at Vertex =  ", -1

z, "Mutated Pop =  ", 9

[-1, 3, 9, 5]

``

Mutate1(x,newPopvs)

x, "Pop at Vertex =  ", 4

Error, (in Mutate1) illegal use of a formal parameter

newPopvs:=Popvs;
Mutate(y);
newPopvs;
Mutate(w);
newPopvs

[4, 3, -1, 5]

y, "Pop at Vertex =  ", 3

y, "Mutated Pop =  ", 5

[4, 5, -1, 5]

w, "Pop at Vertex =  ", 5

w, "Mutated Pop =  ", -1

[4, 5, -1, -1]

newPopvs:=Popvs

[4, 3, -1, 5]

Mutate(w);
newPopvs;
Mutate(y);
newPopvs;
Mutate(w);
newPopvs

w, "Pop at Vertex =  ", 5

w, "Mutated Pop =  ", -3

[4, 3, -1, -3]

y, "Pop at Vertex =  ", 3

y, "Mutated Pop =  ", -3

[4, -3, -1, -3]

w, "Pop at Vertex =  ", -3

w, "Mutated Pop =  ", -1

[4, -3, -1, -1]

NULL

newPopvs:=Popvs

[4, 3, -1, 5]

``

Mutate(y);
newPopvs;
Mutate(w);
newPopvs;
Mutate(y);
newPopvs

y, "Pop at Vertex =  ", 3

y, "Mutated Pop =  ", 5

[4, 5, -1, 5]

w, "Pop at Vertex =  ", 5

w, "Mutated Pop =  ", -1

[4, 5, -1, -1]

y, "Pop at Vertex =  ", 5

y, "Mutated Pop =  ", -3

[4, -3, -1, -1]

NULL


 

Download Q_24-12-22_NeighborArray.mw

@dharr  Thank you.  I really like the approach.  I thought I just broke your answer. Then when I looked the table mapping is doing something odd. It changed order to x,w,y,z  This then messes up the the populations displayed.

@Carl Love  @Joe Riel  Thank you.

@Joe Riel So is that all I need to change? Not a computer till later tonight. Will check then.

Do I make it local to MyModule or RT-local (outside of Mymodule)?

@rlopez Thank you 

@C_R So simple when one knows!

   @Carl Love

The overloaded routine Spreadw has one procedure that is being skipped. I can't figure out why?   Section highlighted in blue.

The apparently same concept works in routine f2 highlighted in green.

Any insights here?

restart


rt:=module()
option package;
export f2,Spreadw;
local MyModule;
MyModule:= module()
uses TT= TypeTools;
global _T1, _T2L, _T2V, _T3L, _T3V, _MyType;
local 
     MyTypes:= {_T1, _T2L, _T2V, _T3L, _T3V},
     AllMyTypes:= MyTypes union {_MyType},
     ModuleLoad,
      ModuleUnload:= proc()
     local T;
          for T in AllMyTypes do if TT:-Exists(T) then TT:-RemoveType(T) end if; end do;
          return
     end proc;

     ModuleLoad:= proc()
     local
          g, #iterator over module globals
          e
     ;
          ModuleUnload();
          #op([2,6], ...) of a module is its globals.
          for g in op([2,6], thismodule) do
               e:= eval(g);
               #print("e",e);
               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);
          return
     end proc;
export
     WhichMyType:= proc(X)
     local S:= select(T-> X::T, MyTypes), n:= nops(S);
         printf("%a is ", X);
         if n=0 then printf("not any of the special types.\n")
         else printf("type %a.\n", `if`(n=1, S[], Amd(S[])))
         fi
      end proc;
     
     ModuleLoad()    
     end module;
#Proceduews for export
     
    

     f2:=overload([
          proc(A::_T2L,B::_T1)
               option overload;
               local s;
               MyModule:-WhichMyType~([A,B]);
               s:=A*B^2;
          end proc,

          proc(A::_T3L,B::_T1,C::_T1,E::_T1,clr)
               option overload;
               local s,t;
               MyModule:-WhichMyType~([A,B,C,E]);
               s:=A*B^3;
               t:=2*B+sqrt(C)-sin(E);
               s-t;
          end proc,

          proc(A::_T2L)
               option overload;
               local s;
               MyModule:-WhichMyType(A);
               s:=A*6;
          end proc
     ]);

     Spreadw :=overload([
         
          # s0 from 3 Quadrances
          
          proc(p0::_T1,p1::_T1,p2::_T1)
           option overload;
               1/4*((p0 + p1 + p2)^2 - 2*p0^2 - 2*p1^2 - 2*p2^2)/(p2*p1),  "3 Quadrances";
          end proc,

          # 2 Points wrt origin
          proc(p0::_T2L,p1::_T2L,clr := GeomClr)
           option overload;
           if clr=b or clr=B or clr=blue or clr=Blue then
                1 - LinearAlgebra:-BilinearForm(p0, p1, conjugate = false)^2/(LinearAlgebra:-BilinearForm(p0, p0, conjugate = false)*LinearAlgebra:-BilinearForm(p1, p1, conjugate = false)), "2 Points Blue";
           elif clr=g or clr=G or clr=green or clr=Green then
               -1/4*(p0[1]*p1[2] - p0[2]*p1[1])^2/(p0[1]*p0[2]*p1[1]*p1[2]), "2 Points Green";
           elif clr=r or clr=R or clr=red or clr=Red then
               -(p0[1]*p1[2] - p0[2]*p1[1])^2/((p0[1]^2 - p0[2]^2)*(p1[1]^2 - p1[2]^2)), "2 Points Red";
          end if;
          end proc,

          #3 Points wrt p0 
          proc(p0::_T2L, p1::_T2L, p2::_T2L, clr:=GeomClr)
           option overload;
               local P0,P1; 
               P0 := p0 - p2; 
               P1 := p1 - p2; 
               print(P0,P1);
               if clr=b or clr=B or clr=blue or clr=Blue then
                   1 - LinearAlgebra:-BilinearForm(P0, P1, conjugate = false)^2/(LinearAlgebra:-BilinearForm(P0, P0, conjugate = false)*LinearAlgebra:-BilinearForm(P1, P1, conjugate = false)), "3 Points Blue";
                elif clr=g or clr=G or clr=green or clr=Green then
                   -1/4*(P0[1]*P1[2] - P0[2]*P1[1])^2/(P0[1]*P0[2]*P1[1]*P1[2]), "3 Points Green";
                elif clr=r or clr=R or clr=red or clr=Red then
                   -(P0[1]*P1[2] - P0[2]*P1[1])^2/((P0[1]^2 - P0[2]^2)*(P1[1]^2 - P1[2]^2)), "3 Points Red";
               end if;
               end proc,

          # 2 Vectors
          proc(p0::_T2V,p1::_T2V,clr:=GeomClr)
               if clr=b or clr=B or clr=blue or clr=Blue then
                    1 - LinearAlgebra:-BilinearForm(p0, p1, conjugate = false)^2/(LinearAlgebra:-BilinearForm(p0, p0, conjugate = false)*LinearAlgebra:-BilinearForm(p1, p1, conjugate = false)), "Vectors Blue";
               elif clr=g or clr=G or clr=green or clr=Green then
                    -1/4*(p0[1]*p1[2] - p0[2]*p1[1])^2/(p0[1]*p0[2]*p1[1]*p1[2]), "Vectors Green";
               elif clr=r or clr=R or clr=red or clr=Red then
                    -(p0[1]*p1[2] - p0[2]*p1[1])^2/((p0[1]^2 - p0[2]^2)*(p1[1]^2 - p1[2]^2)), "Vectors Red";
               end if;
          end proc

]);
end module:
#maplemint(rt)
#GeomClr:=Blue

rt:-Spreadw(1,1,1);

rt:-Spreadw([1,1],[1,0],b);
rt:-Spreadw([1,2],[1,0],r);
rt:-Spreadw([1,1],[1,2],g);


rt:-Spreadw([0,0],[1,1],[1,0],b);
rt:-Spreadw([0,0],[1,2],[1,0],r);
rt:-Spreadw([0,0],[1,1],[1,2],g);


rt:-Spreadw(<1,1>,<1,0>,b);
rt:-Spreadw(<1,2>,<1,0>,r);
rt:-Spreadw(<1,1>,<1,2>,g);
rt:-f2([2,3],5);
rt:-f2([2,3,8],19);#should produce an exception
rt:-f2([2,3,8],19,7,3,g);
rt:-f2([2,3]);

@Carl Love 

A follow up question. Why does a:: [algebraic $ 3]  not work on a procedure input usless it is added to Types?

It works for  is(a:: [algebraic $ 3])  and if.....then without having been addet to Types.

restart

 

a:=[2,5,2]

[2, 5, 2]

 
 

is(a:: [algebraic $ 3]) ;

is(a:: [algebraic $ 2])

true

FAIL

tst:=proc(A:: [algebraic $ 3])   
 local r:= "A is T3L";


   print(r);
       
end proc;

proc (A::[`$`(algebraic, 3)]) local r; r := "A is T3L"; print(r) end proc

 

tst(a)

is(a:: [algebraic $ 3]) ;

 

 

Error, invalid input: tst expects its 1st argument, A, to be of type [algebraic $ 3], but received [2, 5, 2]

 

tst1:=proc(A)   
 local r:= "A is T3L";
if   is(a:: [algebraic $ 3])then

   print(r);
end if;        
end proc;

proc (A) local r; r := "A is T3L"; if is(a::[`$`(algebraic, 3)]) then print(r) end if end proc

tst1(a)

"A is T3L"

 


 

Download Q_24-11-2022_Type_Test_on_input.mw

@Carl Love I have it working probable on a crude basis. Ichanged your MyDispatch to f1 The exports are now f1 and f2. Both run.  I have moved the code around to make things clearer.
Q1. if A :=[2,3] how do I get the procedure to detect and print that it is _T2L etc?  See f2

Edit

Q2. why does it need ? Highlighted in green.

ModuleLoad()    

     end module;

restart

 

rt:=module()
option package;
export f1,f2;
local MyModule;

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);
               #print("e",e);
               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;
      ModuleLoad()   # ???? 
      end module;



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

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

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

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

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

          proc(A::_T2L, B::_T3L,$)
          option overload;
          local r:= "A, B, are mixed.";#I added this
               #
          end proc
     ]);

     
     f2:=overload([
          proc(A::_T2L,B::_T1)
               option overload;
               local s;
               print(AllMyTypes(A));#should be_T2L
               s:=A*B^2;
          end proc,

          proc(A::_T3L,B::_T1)
               option overload;
               local s;
               print(AllMyTypes(A));#should be_T3L
               s:=A*B^3-2*B;
          end proc
     ]);
          
end module;

#maplemint(rt);
#Example usage:
 

Warning, previous type for `_T1` has been overwritten.

Warning, previous type for `_T2V` has been overwritten.

Warning, previous type for `_T2L` has been overwritten.

Warning, previous type for `_T3V` has been overwritten.

Warning, previous type for `_T3L` has been overwritten.

Warning, previous type for `_MyType` has been overwritten.

_m2590059755904

x:=[9,4]:
y:=[5,67]:
z:=[1,2]:                                 

rt:-f1(x,y,z);
rt:-f1(2,y,s);  #should produce an exception
x:=<9,4,r>:
y:=<5,6,r>:
z:=<1,2,w>:                                 
rt:-f1(x,y,z);
x:=[9,4]:
y:=[5,6,7]:
z:=[1,2]:
rt:-f1(x,y);
rt:-f2([2,3],5);
rt:-f2([2,3,8],19);

"A, B, C are T2L."

Error, invalid input: no implementation of f1 matches the arguments in call, 'f1(2,y,s)'

"A, B, C are T3V."

"A, B, are mixed."

AllMyTypes([2, 3])

[50, 75]

AllMyTypes([2, 3, 8])

[13718, 20577, 54872]-38

Download Q_22-11-22_rev_1_Module_Test_Types_and_Exports.mw

@Carl Love 

The above works nicely. Yes I had noticed the $ as a terminator. I was already experimenting with how to use this as a package with exports. I have highlighted the code below. Exported MyModule anf f1 as a test. f1 not working

restart

 

rt:=module()
option package;
export MyModule,f1;# I don't actually want to export MyModule . Just it works for the demonstrastion of an export


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);
               #print("e",e);
               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,
#All of the MyDispatch procedures is what I would have had as one procedure of the #package
#containing a very complicated set of "if   then" statements

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

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

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

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

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

          proc(A::_T2L, B::_T3L,$)
          option overload;
          local r:= "A, B, are mixed.";#I added this
               #
          end proc

     ]),

     ModuleApply:= proc(
          #A::And(
             #  _MyType,
            #   satisfies(A-> andmap(T-> A::T implies B::T and C::T, MyTypes) )
          #),
          #B::_MyType, C::_MyType
     )
          MyDispatch(args)
     end proc
;
     ModuleLoad()    
end module;

f1:=module()
     proc(A::_T2L,B::_T1)

     option overload;
     local s;
     print(AllMyTypes(A));#should be_T2L
     s:=A*B^2;
     end proc;

     proc(A::_T3L,B::_T1)

     option overload;
     local s;
     print(AllMyTypes(A));#should be_T3L
     s:=A*B^3-2*B;
     end proc;

   end module;  

end module;

#maplemint(rt);

_m2186886803680

#Example usage:

 

x:=[9,4]:
y:=[5,67]:
z:=[1,2]:                                 

rt:-MyModule(x,y,z);
x:=<9,4,r>:
y:=<5,6,r>:
z:=<1,2,w>:                                 
rt:-MyModule(x,y,z);
x:=[9,4];
y:=[5,6,7];

rt:-MyModule(x,y);

"A, B, C are T2L."

"A, B, C are T3V."

[9, 4]

[5, 6, 7]

"A, B, are mixed."

rt:-f1([2,3],5);

 

f1([2, 3], 5)

Download Q_22-11-22_Module_Test_Types_and_Exports.mw

@zenterix I installed this last night. IMO it seems much better that jEdit Sublime and Notepad++. I interface is much more professional. Though I have'nt figured out how to launch Maple from it yet.

Some of the nice features are 

if.....then

end if 

inserts automatically. same for   For...do ...end do and other loops. Also nested parenthesis are auto coloured differently

@zenterix I am new to code editors. Had tried about 5 years ago. Sublime Text:- I had difficulty installing the Maple plugin because I didnt know what I was doing. Somehow it installed. GitHub - johnpmay/SublimeTextMaple: Sublime Text syntax and configuration files for Maplesoft's Maple language. It is straight forward to use. I saved a couple of procedures and a module/package with $include <xyadsddd.mpl> commands all as .mpl files. Then read in the module from Maple. That worked fine. I dont know anything about how to do code error checking in Sublime or it it can be done.  Don't know it Maple can be launched from it.

I found Notepad ++ easier to set up. To me it is much the same as Sublime. BTW I am on windows.

jEdit. Has native maple support. and the Project manager plugin is good. Irrating trying to download the plugins though and the mirror sites often fail.  Might be possible to launch Maple from it but I lack programming education.

That VScode for Maple looks really good. Will have to try it.

@ecterrab Is there a Komodo addon for Maple? have been investigating a few Code editors over the past week or so. Looked at Sublime text, Notepad++  and jEdit which does have a default Maple package. I so far like jEdit. I would like something that could flash up bugs in the code as it's hard to tell what is wrong when Maple reads in an *.mpl and doesn't like it.

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