Ronan

762 Reputation

14 Badges

10 years, 272 days
East Grinstead, United Kingdom

MaplePrimes Activity


These are questions asked by Ronan

In 1D worksheet mode I can't figure out how to insert a line between a and c. 

restart

a:=1
                             a := 1

c:=3
                             c := 3


 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

Procedure returns two values. I would like to print a comment after them if possible, without it being a returned value


foo := proc(a, b) 
local x, y; 
x := a^2 - b; 
y := b^2 - a; 
# return x,y, "test on foo"  # returns 3 values
return x, y, print(" tests on return"); end proc;

A, B := foo(6, 3);
                       " tests on return"

                         A, B := 33, 3  #would like "tests on return here"

 

The procedures "getCP" which uses "cpsub" (not exported) acts differently inside the package as opposed to them outside it.
In the package it returns a column matrix which is incorrect. Should return a 2 row matrix. They dismantle a polynomial into it's powers and coefficients. I know one cant run the package here. Included are the two procedures and the text of the notepad file to make the package.

restart

NULL

with(AlgCalc)

[`&oplus;`, FundThrm, SignedArea, diag, diftab, faulD, faulS, getCP]

f := sort(2+x^7+5*x^2-7*x^a+k*x^2, [x], ascending)

2-7*x^a+5*x^2+k*x^2+x^7

getCP(f, x)

Matrix(%id = 36893490212391698668)

NULL

g := sort(5*x^8+k*x^2-7*x^3+x+2, [x], ascending)

2+x+k*x^2-7*x^3+5*x^8

CP := getCP(g, x)

Matrix(%id = 36893490212391688180)

NULL

restart

with(AlgCalc)

[`&oplus;`, FundThrm, SignedArea, diag, diftab, faulD, faulS, getCP]

NULL

f := sort(2+x^7+5*x^2-7*x^a+k*x^2, [x], ascending)

2-7*x^a+5*x^2+k*x^2+x^7

getCP(f, x)

Matrix(%id = 36893490212390043884)

g := sort(5*x^8+k*x^2-7*x^3+x+2, [x], ascending)

2+x+k*x^2-7*x^3+5*x^8

getCP(g, x)

Matrix(%id = 36893490212390032796)

 

restart

 

Dismantlimg a polynomial  uses the following 2 procedures

" cpsub:= proc(t, var:=alpha)  description "used by getCP";       local i,cf,varpwr;                   if   not has(t, var)                  then return < 0,t>:               elif whattype(t)=`^` then return <op(t)[2],1>;                  else varpwr,cf:= selectremove(has,[op(t)],var);                       if op(varpwr)=var then                        return <1, `*`(op(cf))> ;                   end if;                     for i from 1 to nops(varpwr) do                    if numer(varpwr[i])=1  then if op((1)/(varpwr[i]))=alpha then varpwr[i]:=-1 else  varpwr[i] :=-op((1)/(varpwr[i]))[2] end if else  varpwr[i]:=op(varpwr[i])[2]end if                        end do;                   return <`+`(op(varpwr)),`*`(op(cf))>;                   #print(op(t));                                   end if ;            end proc:"

NULL

``

getCP := proc (f, var := alpha) local t; description "Dissmantles a polynomial into powers and coefficients"; if not has(f, var) then return `<,>`(0, f) elif whattype(f) = `^` then return `<,>`(op(f)[2], 1) elif whattype(f) = `*` then return `<|>`(`~`[cpsub]([f], var)[]) else `<|>`(`~`[cpsub]([op(f)], var)[]) end if end proc

NULL

f := sort(2+x^7+5*x^2-7*x^a+k*x^2, [x], ascending)

2-7*x^a+5*x^2+k*x^2+x^7

cp := getCP(f, x)

Matrix(%id = 36893490212391322676)

whattype(cp)

Matrix

NULL

Notepad file to Create AlgCalc.

 

AlgCalc := module ()
export SignedArea,
           `&oplus;`,

             getCP,

             diag,
             diftab,
             faulD,
             faulS,
             FundThrm;
        

 option package;

$include "SignedArea.mm"
$include "`&oplus;`.mm"
$include "cpsub.mm"
$include "getCP.mm"
$include "diag.mm"
$include "diftab.mm"
$include "faulD.mm"
$include "faulS.mm"
$include "FundThrm.mm"

end module;

Download 6-11-22_Procedures_acting_differently_in_Package.mw

I have 5 procedures saved in my library. Say Proc1, Proc2, Proc3, Proc4 and Proc5. They all work ok.

Proc5 calls Proc4 which calls Proc3 which calls Proc2 which calls Proc1. That is fine if I read them all in to the dodument.

Is it ok ok to put read commands in procedures 5..2 so I don't have to manually load them all myself into a new document or is there a better way to do this. These procedures are not part of a package.
I hace set 

currentdir();
      "C:\Users\Ronan\Documents\maple\Alibrary\Procedures"

which is where the procedures are stored. If I just load Proc5 it doesn't find the other procedures

1 2 3 4 5 6 7 Last Page 1 of 21