Redefining the _mult command in muPad (MATLAB symbolic engine)
Show older comments
I have created a user-defined domain in muPad called Bx. Each object in this domain is defined by two variables: n and k.
Bx := newDomain("Bx"):
Bx::new:=
proc(n,k)
begin
if args(0)<>2 then
error("There must be exactly two arguments")
end_if;
new(dom,n,k)
end_proc:
Bx::print:=
proc(x)
begin
"Bx(".expr2text(op(x,1),op(x,2)).")";
end_proc:
If at this point, muPad is instructed 2*Bx(2,3) then the output will look like:
2 Bx(2,3)
If muPad is instructed 2*k*Bx(2,3) then the output will look like:
2 k Bx(2,3)
This is how I would like my domain to behave under multiplication. The exception is when two objects of the Bx domain are multiplied together. If, for example, I instruct Bx(2,3)*Bx(2,3) then I would like to see:
Bx(4,6)
The multiplication rule for two Bx objects is Bx(n1,k1)*Bx(n2,k2)=Bx(n1+n2,k1+k2). How can I redefine _mult for the domain Bx so that it multiplies in this way iff (if and only if) both arguments are Bx objects? If either argument is not Bx, then the multiplication should occur as it would without me having redefined _mult, demonstrated in the examples above.
The current revised definition I have for _mult on Bx is the following, and it works when multiplying two Bx objects, but it messes up when either argument does not belong to Bx, e.g. it does nothing for 2*Bx(1,1):
Bx::_mult:=
proc()
local type1,type2,n,k;
begin
type1:=type(args(1));
type2:=type(args(2));
n:=map(args(),op,1);
k:=map(args(),op,2);
if type1=type2
then dom(_plus(n),_plus(k));
end_if;
end_proc:
I have revised this question many times over the past few days and have really reduced it to its essence. I hope it is clear and that someone can help me with my special _mult definition.
Answers (1)
Categories
Find more on Library Domains in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!