Question: How to setup special type check in a procedure?

 About 2 years ago I asked this question Application of how to test types in a module? - MaplePrimes on type checking and @CarlLove provided a great answer for inputs to a procedure in that package.

In the package I have a procdure to plot/draw lines. They require sorting out when there is a list of them The procedure recognises the different possible input formats. Could the types be defined in the procdure body. It would make sorting out a lot easier. I could possibly add them to Module load at the start of the package.

restart

 

# Line type
#type _TNL1  expression type {`+`,`*`,`=`, `symbol`,procedure}                         2D line or any expression function equation

#type _TNL2  listlist [[a,b],[c,d]] or [[a,b,c],[d,e,f]]                               2D  line just draws a line between the 2 points

#type _TNL4  listlist [[a,b,c],[d,e,f]]                                                3D line just draws a line between the 2 points

#type _TNL4  list of list & vector 2D [[a,b],<c,d>]  or possibly  [<c,d>,[a,b]]        2D generates line A*x+B*y+C                        

#type _TNL5  list of list & vector 3D [[a,b,c],<d,e,f>] or possibly [<d,e,f>,[a,b,c]]  3D line <a+alpha*d, b+alpha*e, c+alpha*f>

#type _TNL6  vector 3D <a+alpha*d, b+alpha*e, c+alpha*f>                               3D line <a+alpha*d, b+alpha*e, c+alpha*f> checks indets to know

#type _TNL7  list of vector[row] and vector[row]  [<a|b|c>, <d|e|f>]                   projective points are converted to [a/c,b/c] and [d/f,e/f] and line drawn between the points

 

 

 

sorts:=proc(l1::{list,set,Vector[column],`+`,`*`,`=`, `symbol`,procedure},
{range::list:=[-5,5,-4,4]},
{vars::list:=[':-x',':-y']},
{rangep::list:=[-3,3]},
{plopts::list:=[':-colour'=':-blue,magenta']})

description " sort the list of line formats and converts to plotable format  ";

local i,n,Listsystem,subplt, subL, tmp, vv,ll,subpltemp;

      #single line;
if l1::{'Vector[column]',`+`,`*`,`=`, `symbol`,procedure} then
   n:=1;
   subplt:=[{}];
   if l1::'Vector[column]'(3) and indets(l1)={} then
      subL:=[l1[1]*vars[1]+l1[2]*vars[2]+l1[3]];
    else
      subL:=[l1];
   end if;
 elif l1::list and nops(l1)= 2 and not(hastype(l1,set))then
    #for j to 2 do
    #   if  l1[j][1]::listlist or l1[j][2]::listlist  then

                                                        # this section not finished supposed
                                                        # to detect is the list is two line without extra plopts, range, rangexy                                                  
   #   end if;
   # end do;
   n:=1;
   subplt:=[{}];
    #print("here now");
   if hastype(l1,'Vector[column]'(3)) and hastype(l1,list) then  
      #print("in  here");
      vv:=(select(type,l1,('Vector[column]'(3)))[]);
      ll:=select(type,l1,list)[];
      #print("vv " ,vv, "ll",ll);
      subL:=[ll+~alpha*vv];  
    elif hastype(l1,'Vector[column]'(2)) and hastype(l1,list)  then
      vv:=(select(type,l1,('Vector[column]'(2)))[]);
      ll:=select(type,l1,list)[];
      subL:=[ (ll[2] - vars[2])*vv[1] - vv[2]*(ll[1] - vars[1])];    
    else
      subL:=[l1];
  end if;
  
      #single line with own sub-options plopts range rangep bundled as a set
elif l1::set then
   n:=1;
   subpltemp,tmp:=selectremove(has,l1,{':-rangep',':-range',':-plopts',':-rangep'});
   #print("00",subpltemp,tmp);
   subplt:=`if`(has(subpltemp,{':-plopts',':-range',':-rangep'}),[subpltemp],[{}]);
   tmp:=op(tmp);
   #print("TMP ",tmp ,nops(tmp));
   if tmp::{'Vector[column]',`+`,`*`,`=`, `symbol`,procedure} then
  
     if tmp::'Vector[column]'(3) and indets(tmp)={} then
        subL:=[tmp[1]*vars[1]+tmp[2]*vars[2]+tmp[3]];
      elif hastype(tmp,'Vector[column]'(2)) and hastype(tmp,list)  then
        vv:=select(type,tmp,'Vector[column]'(2))[];
        ll:=select(type,tmp,list)[];
        subL:= [(ll[2] - vars[2])*vv[1] - vv[2]*(ll[1] - vars[1])];  
     else
       subL:=[tmp];
   end if;
 elif nops(tmp)= 2  then
   #print("0th nops(tmp)",nops(tmp),tmp);
   # print("here here now");
   if hastype(tmp,'Vector[column]'(3)) and hastype(tmp,list) then  
      #print("in  here here");
      vv:=(select(type,tmp,('Vector[column]'(3)))[]);
      ll:=select(type,tmp,list)[];
      #print("vv " ,vv, "ll",ll);
      subL:=[ll+~alpha*vv];
     elif hastype(tmp,'Vector[column]'(2)) and hastype(tmp,list)  then
      vv:=(select(type,tmp,('Vector[column]'(2)))[]);
      ll:=select(type,tmp,list)[];
      subL:=[ (ll[2] - vars[2])*vv[1] - vv[2]*(ll[1] - vars[1])];         
    else
      subL:=[tmp];
   end if;
   end if;

    #a list of lines with possible sub-options if
 else
   n:=nops(l1);
   subplt:=[];
   subL:=[];
   for i to n do
     subpltemp:=select(has,l1[i],{':-plopts',':-range',':-rangep'});
     #print("subpltemp",subpltemp);
     #print("subpltemp",has(subpltemp,{':-plopts',':-range',':-rangep'}));
     if has(subpltemp,{':-plopts',':-range',':-rangep'})then subplt:=  [subplt[],subpltemp]else subplt:=[subplt[],{}] end if;
     tmp:=select(not(has),l1[i],{':-plopts',':-range',':-rangep'});
     if type(tmp,set)then tmp:=op(tmp)end if;
     #print("TMP ",tmp ,nops(tmp));
     #print("1st nops(tmp)",nops(tmp),tmp);
     if tmp::{'Vector[column]',`+`,`*`,`=`, `symbol`,procedure} then
  
     if tmp::'Vector[column]'(3) and indets(tmp)={} then
        subL:=[subL[],tmp[1]*vars[1]+tmp[2]*vars[2]+tmp[3]];
      else
        subL:=[subL[],tmp];
     end if;
 elif nops(tmp)= 2  then
   #print("nops(tmp)",nops(tmp),tmp);
   #print("2nd here here now");
   if hastype(tmp,'Vector[column]'(3)) and hastype(tmp,list) then  
      #print("in  here here");
      vv:=(select(type,tmp,('Vector[column]'(3)))[]);
      ll:=select(type,tmp,list)[];
      #print(tmp,"vv " ,vv, "ll",ll);
      subL:=[subL[],ll+~alpha*vv];
    elif hastype(tmp,'Vector[column]'(2)) and hastype(tmp,list)  then
      vv:=(select(type,tmp,('Vector[column]'(2)))[]);
      ll:=select(type,tmp,list)[];
      #print(tmp,"vv " ,vv, "ll",ll);
      subL:= [subL[],(ll[2] - vars[2])*vv[1] - vv[2]*(ll[1] - vars[1])];           
    else
      subL:=[subL[],tmp];
  end if;
end if;
    
end do;
end if:
return subplt, subL, n
end proc:

 

L1:=2*x+3*y-4;
L1ext:={L1, range=[-5,2,-4,4],plopts=[colour=red ,linestyle=dot] };
L2:=<5,-1,3>:
L2ext:={L2,plopts=[linestyle=dash, thickness=4]}:
L3:=[[2,1],[4,-5]]:
L3ext:={L3,plopts=[color=red,thickness=1]}:
L4:=[[2,1,-3],[4,-5,-2]]:
L4ext:={L4,plopts=[color=green]}:
L5:=[[2,3],<-1,4>]:
L5ext:={L5,plopts=[color=green,thickness=2],range=[-6,6,-6,6]}:
L6:=[[1,2,-3],<-3,1,4>]:
L6ext:={L6,rangep=[-4,1],plopts[linestyle=dash]}:
L7:=<1+3*alpha,4-1*alpha,-1+2*alpha>:
L7ext:={L7,rangep=[-4,1],plopts=[colour=purlpe,thickness=3]}:
L8:=[<2|3|1>,<4|7|2>]:
L8ext:={L8,plopts=[colour=red]}:

2*x+3*y-4

 

{2*x+3*y-4, plopts = [colour = red, linestyle = dot], range = [-5, 2, -4, 4]}

(1)

 

Lst:=[L1,L1ext,L2,L2ext,L3,L3ext,x^2+2*y^2+3-4*x*y,L4,L4ext,L5,L5ext,L6,L6ext,L7,L7ext,L8,L8ext]:

A,B,c:=sorts(Lst):

for i to c do
print(i,"  A= ",A[i],"    B= ",B[i]);
end do

1, "  A= ", {}, "    B= ", 2*x+3*y-4

 

2, "  A= ", {plopts = [colour = red, linestyle = dot], range = [-5, 2, -4, 4]}, "    B= ", 2*x+3*y-4

 

3, "  A= ", {}, "    B= ", 5*x-y+3

 

4, "  A= ", {plopts = [linestyle = dash, thickness = 4]}, "    B= ", 5*x-y+3

 

5, "  A= ", {}, "    B= ", [[2, 1], [4, -5]]

 

6, "  A= ", {plopts = [color = red, thickness = 1]}, "    B= ", [[2, 1], [4, -5]]

 

7, "  A= ", {}, "    B= ", x^2-4*x*y+2*y^2+3

 

8, "  A= ", {}, "    B= ", [[2, 1, -3], [4, -5, -2]]

 

9, "  A= ", {plopts = [color = green]}, "    B= ", [[2, 1, -3], [4, -5, -2]]

 

10, "  A= ", {}, "    B= ", -11+y+4*x

 

11, "  A= ", {plopts = [color = green, thickness = 2], range = [-6, 6, -6, 6]}, "    B= ", -11+y+4*x

 

12, "  A= ", {}, "    B= ", Vector[column](%id = 36893490129638901388)

 

13, "  A= ", {plopts[linestyle = dash], rangep = [-4, 1]}, "    B= ", Vector[column](%id = 36893490129638901628)

 

14, "  A= ", {}, "    B= ", Vector[column](%id = 36893490129638901988)

 

15, "  A= ", {plopts = [colour = purlpe, thickness = 3], rangep = [-4, 1]}, "    B= ", Vector[column](%id = 36893490129661594308)

 

16, "  A= ", {}, "    B= ", [Vector[row](%id = 36893490129661594428), Vector[row](%id = 36893490129661594548)]

 

17, "  A= ", {plopts = [colour = red]}, "    B= ", [Vector[row](%id = 36893490129661594428), Vector[row](%id = 36893490129661594548)]

(2)
 

 

Download 2024-11-09_A_Line_Type.mw

Please Wait...