Joe Riel

9590 Reputation

23 Badges

19 years, 134 days

MaplePrimes Activity


These are answers submitted by Joe Riel

A nicer way to pass-on a keyword parameter is to use _options['debug']. See the help page using_parameters.

An alternative approach is to use MapleSim:-Tools:-MapleToModelica, which can produce a Modelica block suitable for use in MapleSim.

A better way to do this is something like
 

U := simplify((A+1/A).B);
for s to n do
    V[s] := eval(U, t=s);
end do:

 

Here's a somewhat crude approach:

AssocPower := proc(ex? :: string)
    local ex := StringTools:-SubstituteAll(ex?, "^", "&&^");
    ex := parse(ex);
    eval(ex, `&&^` = `^`);
end proc:

AssocPower("a^b^c");
                                         a^(b^c)

That works because the &&- operators (see operators,precedence) are right-associative, unlike the &- operators, which are left-associative. 

Alas this simple approach, which calls parse, won't handle general Mathematica expressions, so doesn't really help you. Also, as the precedence of &&- operators is slightly greater than the ^ operator, in Maple, there can be other issues.  For example
 

AssocPower("a^b!");   #--> (a^b)!
parse("a^b!"); #--> a^(b!)

 

Presumably a bug in the external routine.  Here's a simple workaround,

SplitString := proc(s :: string, len :: posint)
local i;
    seq(s[i..i+len], i=1..length(s), len);
end proc:

 

ModuleLoad is executed when the code is loaded from a Maple archive (mla file).  It won't be executed if the module has been assigned in another way (say by entering it as text).  A workaround is to modify your source to call ModuleLoad when it is evaluated:

foo := module()
option package;
export ModuleLoad := proc() .... end proc;
...
ModuleLoad(); # call ModuleLoad when evaluating the source for foo
end module:

 

A workaround is to introduce another parameter, say sqrt3 = sqrt(3) and assign a = L/sqrt3.  You can then backsubstitute sqrt3 = sqrt(3) in the extracted equations.

Followup That doesn't address your request to keep a.  Because L only enters through a (I believe, haven't carefully checked your model), you should be able to do subs(L = a*sqrt3, sqrt3 = sqrt(3), %);

I have written a package, Bark, available on the Maple Cloud, that partially addresses this issue.  I use it to convert Maple programs into command line tools. Because I work almost exclusively on Linux, it hasn't been tested much on Windows, though I did so originally.  However, on Windows it requires Cygwin (it creates a small bash file that calls maple).  It possibly could be extended to work from PowerShell, however, I didn't know enough about PowerShell to do so.

There are quite a few ways to do this in Maple.  Here's one. 

M := Matrix([[1,2,"A"],[2,3,"B"],[3,4,"A"]]):

# create list of indices of the desired rows
rows := [seq(ifelse(M[i,3]="A", i, NULL), i=1..upperbound(M,1))]:

# create submatrix that contains the desired rows
M[rows,..];

 

Function calls use parentheses, not square brackets.  Also, the exponent should be applied after the parentheses.  That is, change f^2[n-2] to f(n-2)^2 and f[n-1] to f(n-1).  To compute the derivative, do eval(diff(f(18),x), x=2*Pi).

Am not certain, but believe there is not a way to do this, at least if you want to refer to the object itself from a procedure in a submodule. You could, of course, do
 

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

    export ModuleCopy::static:= proc( self, proto, name::string, $)
         self:-name := name;
    end proc;

    export process_1::static:=proc(_self,$)
       B:-process_2(name);
    end proc;

    #method process_2 is now inside a module.
    local B :: static := module()
    export
        process_2::static:=proc(nm :: string)
            printf("in B:-process_2: %s\n",nm);
        end proc;
    end module;

end module:

o:=Object(A,"me"):
o:-process_1();

 

This looks like a bug.  I'll report it.  Note that the help page for copy does not state that it is intended for copying objects, however, it doesn't say what it does if the input is other than a table, rtable or record and the deep option is used.

What were you intending? 

The following extension may be helpful.  It adds a call to FixID in the ModuleConstructor for person.  The definition of FixID is reassigned in employee, the inheriting class.  If you want the FixID of the inheriting object to be used, then you'll need to use (as shown) either function or the parameter name, self, as the namespace (prepended with colon-dash) for FixID; omitting it will use FixID from the base class (person).  This is discussed in the help page object,function_mechanism.

person := module()
option object;
local idnumber :: integer := 0;
local name :: string := "";
export
    ModuleCopy :: static:= proc( self :: person
                               , proto :: person
                               , name :: string
                               , idnumber :: integer
                               , $)
        self:-idnumber := idnumber;
        self:-name := name;
        function:-FixID(self);
    end proc;
export
    FixID :: static := proc(_self)
        idnumber += 1;
    end proc;
export
    GetID :: static := proc(_self)
        idnumber;
    end proc;
end module;

employee := module()
option object(person);
local salary :: integer := 0;
    ModuleCopy :: static := proc( self :: employee
                                  , proto :: employee
                                  , name :: string
                                  , idnumber :: integer
                                  , salary :: integer
                                  , $)
        person:-ModuleCopy(self, proto, name, idnumber);
        self:-salary := salary;
    end proc;

    FixID := proc(_self)
        idnumber += 100;
    end proc;

end module:

bob   := Object(employee, "Bob", 1, 100):
GetID(bob);

There is another subtlety.  A procedure like FixID should probably be a local rather than an export.  However, if it is declared as a local, then the call function:-FixID(self) in ModuleConstructor will always call the version assigned in the person object.  To ensure that it calls the modified version, change the call to self:-FixID(self).

I just realized that what you are looking for can be readily accomplished by calling the ModuleConstructor of the base object from the ModuleConstructor of the inheriting object using the base classname to reference it. In your example,

person := module()
option object;

local idnumber::integer:=0;
local name::string:="";

export
    ModuleCopy::static:= proc( self :: person
                               , proto :: person
                               , name::string
                               , idnumber::integer
                               , $)
        self:-idnumber := idnumber;
        self:-name := name;
    end proc;

end module;

employee:=module()
option object(person);
local salary::integer:=0;

    ModuleCopy :: static := proc( self :: employee
                                  , proto :: employee
                                  , name::string
                                  , idnumber::integer
                                  , salary::integer
                                  , $)
        person:-ModuleCopy(self, proto, name, idnumber);
        self:-salary := salary;

    end proc;

end module:


bob   := Object(employee, "Bob", 1, 100):
alice := Object(employee, "Alice", 2, 80):

 

The inheritance of Maple's objects is limited; I have yet to use it in an actual application.  I believe that you will have to effectively duplicate the code in the ModuleCopy of the original class.  However, if it were more complex than simple assignments, you could make that part an external method and call it from the ModuleCopy of the base class and the inherited class.  Here is a toy example (it's only simple assignments, but shows the idea).
 

person := module()
option object;

local masterid :: static := 0;
local idnumber;
export name :: string := "";

export
    ModuleCopy :: static:= proc( _self
                                 , proto
                                 , nm :: string
                                 , $
                               )
        SetName(_self, nm);
        SetID(_self);
    end proc;

export
    SetName :: static := proc(_self, nm)
        name := nm;
    end proc;

export
    SetID :: static := proc(_self)
        idnumber := ++masterid;
    end proc;

export
    GetID :: static := proc(_self)
        idnumber;
    end proc;

export
    GetName :: static := proc(_self)
        name;
    end proc;

end module;

#---- extend the above class

employee := module()
option object(person);
export salary::integer:=0;

    ModuleCopy :: static := proc( _self
                                  , proto
                                  , nm :: string
                                  , salary :: integer
                                  , $
                                )
        SetName(_self, nm);
        SetID(_self);
        SetSalary(_self, salary);

    end proc;

export
    SetSalary :: static := proc(_self, sal :: positive)
        salary := sal;
    end proc;

export
    GetSalary :: static := proc(_self);
        salary;
    end proc;
end module;

alice := Object(employee, "alice", 80);
bob   := Object(employee, "bob", 100);

GetID(bob);
GetSalary(alice);

 

2 3 4 5 6 7 8 Last Page 4 of 114