Ronan

1157 Reputation

14 Badges

12 years, 136 days
East Grinstead, United Kingdom

MaplePrimes Activity


These are questions asked by Ronan

How should optional input parameters be handled for procedures. The example has three optional inputs 

vars:=[x,y]   ,  clr:="b"   and prnt:="y"  . if one wants to change prnt to"n", vars and clr values must be entered.

{vars:=[x,y]  } , { clr:="b" }  and{ prnt:="y"}  this is a good method because one just enteres prnt="n".  But have to remember the input parameter name prnt.

I have a 3rd option, but it is to complicated and probably unreliable to use in practice.  and with more than 3 optional inputs too difficult to code.

I am wondering  are the other approaches and what is the prefered methodology. I have about 30+ procedures to apply this to in a package.

Edit:-  I can change the prnt to boolean true, false instead of "y" , "n".  That would make the all the optional inputs different types.

In Test1 its should be vars::list:=[x,y] not vars::{list , `string`}:=[x,y]

restart

Geomclr := "b"

"b"

(1)

NULL

NULL

NULL

Test1 := proc (A, B, vars::{list, string} := [x, y], clr::string := Geomclr, prnt::string := "y") if vars::string then clr := vars; vars := [x, y] end if; if clr = "y" or clr = "n" then prnt := clr; clr := Geomclr end if; print(clr); if prnt = "y" then print("good") end if; A*vars[1]+B*vars[2] end proc

NULL

Test1(A, B)

A*x+B*y

(2)

Test1(A, B, [r, s], "n")

Error, (in Test1) invalid left hand side in assignment

 

Test1(A, B, "g")

Error, (in Test1) invalid left hand side in assignment

 

Test1(A, B, "n")

Error, (in Test1) invalid left hand side in assignment

 

Test1(A, B, [x, y], "b", "n")

A*x+B*y

(3)

 

 

NULL

``

NULL

 

NULL

Test2 := proc (A, B, { vars::{list} := [x, y], clr::string := Geomclr, prnt::string := "y" }) print(clr); if prnt = "y" then print("good") end if; A*vars[1]+A*vars[2] end proc

NULL

NULL

Test2(A, B)

A*x+A*y

(4)

Test2(A, B, clr = "r")

A*x+A*y

(5)

Test2(A, B, prnt = "n")

A*x+A*y

(6)

Test2(A, B, prnt = "n")

A*x+A*y

(7)

Test2(A, B, prnt = "n", clr = "green", l = [r, s])

A*x+A*y

(8)

``

 

# 3 This is possible but is a very complicated method of handling the optional inputs and difficult the handle altered sequence on inputs.

NULL

Test3 := proc (A, B, vars::{list, string} := [x, y], clr::string := Geomclr, prnt::string := "y") local varsl, clrl, prntl; global Geomclr; varsl := vars; clrl := clr; prntl := prnt; if vars::string then varsl := [x, y]; if vars = "y" or vars = "n" then prntl := vars; clrl := Geomclr elif vars = "r" or vars = "g" or vars = "b" then clrl := vars end if elif vars::list and clr = "y" or clr = "n" then prntl := clr; clrl := Geomclr end if; if clr = "y" or clr = "n" and vars::string then prntl := clr; clrl := vars end if; print("Colour print out ", clrl); if prntl = "y" then print("This is a test Message") end if; A*varsl[1]+B*varsl[2] end proc

NULL

Test3(A, B)

A*x+B*y

(9)

Test3(A, B, [r, s])

A*r+B*s

(10)

Test3(A, B, [r, s], "n")

A*r+B*s

(11)

Test3(A, B, "n")

A*x+B*y

(12)

Test3(A, B, [r, s], "r", "n")

A*r+B*s

(13)

Test3(A, B, "r", "n")

A*x+B*y

(14)

Test3(A, B, "n", "r")

A*x+B*y

(15)

NULL


 

Download Q_2024-02-25_Test_proc_Args.mw

Using textplot  is there anything that can be done to to increase the offest of alignI would like to raise  P3 at top of graph up a bit and S3 down. they are {centre, above} and {centre,below}. I know I can tie them to a different point  coordinate to begin with but that is messy to control inside a procedure.

I need to produce some diagrams for the help pages of my package. I trying to typeset the dot product "." on a plot but keep getting a % symbol. I have tried without typesetting but makes no differance.

restart

 

 

Sdim:=proc(A,B,C,clr::string,S:=NULL)
    description"plots spread symbol";
    uses plots,plottools;
    local aa,bb,c,d,e,f,g,h,k,p1,v1,v2,vab,vac;
    vab:=Vector(B-A);
    vac:=Vector(C-A);
    aa:=(B-A)/5;
    bb:=(C-A)/5;
    p1:=plottools:-point([A,B,C],symbol=solidcircle);
    c:= plottools:-line(A+aa,A+bb);
    d:=plottools:-line(A,B);
    e:=plottools:-line(A,C);
    f:=plots:-textplot([op(1.1*(A+(aa+bb)/2)+[1.5,0]), typeset(S)]);   #print S
    g:=plots:-textplot([op(.9*A), typeset([x[1],y[1]])]);  #[x1,y1]
    h:=plots:-textplot([op(1.1*B), typeset([x[2],y[2]])]);  #x2,y2]
    k:=plots:-textplot([op(1.05*C), typeset([x[3],y[3]])]);  #x3,y3]
    v1:=plots:-textplot([op(A+(B-A)/2+[.2,-.2]), typeset(`#mover(mi("v"),mo("⇀"))`[a])]);   # vector symbol
    v2:=plots:-textplot([op(A+(C-A)/2+[-.2,.2]), typeset(`#mover(mi("v"),mo("⇀"))`[b])]);   #vector symbol
    display(p1,c,d,e,f,g,h,k,v1,v2,axes=none,scaling=constrained,caption="Spread between two vectors");    #place title under diagram
end proc

 

proc (A, B, C, clr::string, S := NULL) local aa, bb, c, d, e, f, g, h, k, p1, v1, v2, vab, vac; description "plots spread symbol"; vab := Vector(B-A); vac := Vector(C-A); aa := (1/5)*B-(1/5)*A; bb := (1/5)*C-(1/5)*A; p1 := plottools:-point([A, B, C], symbol = solidcircle); c := plottools:-line(A+aa, A+bb); d := plottools:-line(A, B); e := plottools:-line(A, C); f := plots:-textplot([op(1.1*(A+(1/2)*aa+(1/2)*bb)+[1.5, 0]), typeset(S)]); g := plots:-textplot([op(.9*A), typeset([x[1], y[1]])]); h := plots:-textplot([op(1.1*B), typeset([x[2], y[2]])]); k := plots:-textplot([op(1.05*C), typeset([x[3], y[3]])]); v1 := plots:-textplot([op((1/2)*A+(1/2)*B+[.2, -.2]), typeset(`#mover(mi("v"),mo("⇀"))`[a])]); v2 := plots:-textplot([op((1/2)*A+(1/2)*C+[-.2, .2]), typeset(`#mover(mi("v"),mo("⇀"))`[b])]); plots:-display(p1, c, d, e, f, g, h, k, v1, v2, axes = none, scaling = constrained, caption = "Spread between two vectors") end proc

(1)

Spr:=S=1-(`#mover(mi("v"),mo("⇀"))`[b-a]%.`#mover(mi("v"),mo("⇀"))`[b-a])^2/(`#mover(mi("v"),mo("⇀"))`[a]%.`#mover(mi("v"),mo("⇀"))`[a])/(`#mover(mi("v"),mo("⇀"))`[b]%.`#mover(mi("v"),mo("⇀"))`[b]);

S = 1-`%.`(`#mover(mi("v"),mo("⇀"))`[b-a], `#mover(mi("v"),mo("⇀"))`[b-a])^2/(`%.`(`#mover(mi("v"),mo("⇀"))`[a], `#mover(mi("v"),mo("⇀"))`[a])*`%.`(`#mover(mi("v"),mo("⇀"))`[b], `#mover(mi("v"),mo("⇀"))`[b]))

(2)

Sdim([1,2],[5,1],[4,7],"blue",Spr);

 

 

 

Download Q_2024-02-15_Help_Spread_diagram.mw

is there a way to set conjugate=true  to false as a default for BilinearForm?  This would be used inside a package.

restart;
with(LinearAlgebra);

v := <x, y>;
BilinearForm(v, v);
BilinearForm(v, v, conjugate = false);

 

This is something I use a fair bit. I have procedures with alternative spelling options for the colours Red Green and Blue.
Have shown a single example copied from  an overloaded procedure. It there a nicer way of handling this than what I am doing?
There is a section in help under "Procedure Parameter Declarations" on "Indexed Keyword Parameters"  but I don't see how to use it here. These procedures are used inside a package.

restart

 

GeomClr:="Blue";  # can be "Blue", "blue", "B", "b"  or;
                  #        "Green", "green", "G", "g"  or;
                  #        "Red2, "red", "R", "r";

Prntmsg:="y" ; #  or anything that is not"y"

 

"Blue"

 

"y"

(1)

spread:=proc(p0::{satisfies(s -> type(s, [algebraic $ 2])),'Vector[row]'(2, algebraic)},
              p1::{satisfies(s -> type(s, [algebraic $ 2])),'Vector[row]'(2, algebraic)},
              clr::`string`:= GeomClr,
              prnt::`string`:=Prntmsg)
           option overload;
           uses LinearAlgebra;
           #print(clr,p0,p1);
           if clr="b" or clr="B" or clr="blue" or clr="Blue" then
              if prnt="y" then
                print("Spread 2 [x,y] Points/Vectors wrt origin Blue");
              end if;
               return 1 - BilinearForm(p0, p1, conjugate = false)^2/(BilinearForm(p0, p0, conjugate = false)*BilinearForm(p1, p1, conjugate = false));
           elif clr="g" or clr="G" or clr="green" or clr="Green" then
              if prnt="y" then
               print( "Spread 2 [x,y] Points/Vectors wrt origin Green");
              end if;
               return -1/4*(p0[1]*p1[2] - p0[2]*p1[1])^2/(p0[1]*p0[2]*p1[1]*p1[2]);
           elif clr="r" or clr="R" or clr="red" or clr="Red" then
              if prnt="y" then
               print( "Spread 2 [x,y] Points/Vectors wrt origin Red");
               end if;
               return -(p0[1]*p1[2] - p0[2]*p1[1])^2/((p0[1]^2 - p0[2]^2)*(p1[1]^2 - p1[2]^2));
          end if;
          end proc:

sb:=spread(<3|2>,<4|-5>);

"Spread 2 [x,y] Points/Vectors wrt origin Blue"

 

529/533

(2)

sg:=spread(<3|2>,<4|-5>,"g");

"Spread 2 [x,y] Points/Vectors wrt origin Green"

 

529/480

(3)

sr:=spread(<3|2>,<4|-5>,"r");

"Spread 2 [x,y] Points/Vectors wrt origin Red"

 

529/45

(4)

1/sb+1/sr+1/sg

2

(5)

sr:=spread(<3|2>,<4|-5>,"r","n");

529/45

(6)

 


 

Download Q_2024-02-09_Alternative_Spelling_in_Proc.mw

5 6 7 8 9 10 11 Last Page 7 of 31