5

A Maple expression (for example, x^3+x*y) can be converted to Matlab by

with(CodeGeneration):
Matlab(x^3+x*y);

However, in Matlab, there are two kinds of product: A*B and A.*B. The above way will give x^3+x*y. Is there a convenient way to get the result x.^3+x.*y?

renphysics
  • 269
  • 1
  • 3
  • 7
  • I suppose you already know the difference between `*`, `^` and `.*`, `.^`right? – fpe Jan 24 '13 at 07:58
  • Yes. If both `x` and `y` are vectors (lists), only `x.*y` makes sense. If they are matrices, both `x.*y` and `x*y` may make sense. It seems that we need to declare the type of variables. I do not know whether the CodeGeneration package has taken this into account. – renphysics Jan 24 '13 at 14:57
  • 1
    It may be possible to extend `CodeGeneration[Matlab]` so that the elementwise Maple commands `x^~3` and `x*~y` get printed by the translator as `x.^3` and `x.*y` resp. It might only be possible if insides a translated whole procedure, as opposed to just translated expressions on their own. The extended Printer defn might have to handle calls of the form `~`[:-`^`](x,y) which seems to be what the elementwise calls become when such a proc body gets defined. No time to try that right now, sorry. – acer Jan 24 '13 at 18:49

3 Answers3

2

The language definition for Maple's CodeGeneration[Matlab] can be extended to handle various instances of the elementwise tilde (~) operator.

Since 'x*~y' seems to automatically simplify to `~`[`*`](x, ` $`, y), and since there appears to be a hard-coded error emitted by the presence of the name " $", then that name is substituted by NULL in the usage code below.

> restart:

> with(CodeGeneration): with(LanguageDefinition):

> LanguageDefinition:-Define("NewMatlab", extend="Matlab",
>   AddFunction("`~`[`^`]", [Vector,integer]::Vector,
>               proc(X,Y)
>                  Printer:-Print(X,".^",Y)
>              end proc,
>               numeric=double),
>   AddFunction("`~`[`*`]", [Vector,integer]::Vector,
>               proc(X,Y)
>                  Printer:-Print(X,".*",Y)
>               end proc,
>               numeric=double));

> expr:=''x^~y + x^~3 + x*~y'':

> Translate(subs(` $`=NULL, expr ), language="NewMatlab");

cg = x.^y + x.^3 + x.*y;

> p := proc(x,y)
>         x^~y + x^~3 + x*~y;
>      end proc:

> f := subs(` $`=NULL, eval(p) ):
> Translate(f, language="NewMatlab");

function freturn = f(x, y)
  freturn = x.^y + x.^3 + x.*y;
acer
  • 6,671
  • 15
  • 15
1

For whatever it's worth, Maple 2015 can do this translation directly, without the extra help kindly provided by acer:

> f := (x,y)->x^~y + x^~3 + x*~y:
> CodeGeneration:-Matlab(f);
function freturn = f(x, y)
  freturn = x .^ y + x .^ 3 + x .* y;
Community
  • 1
  • 1
saforrest
  • 196
  • 3
0

If the Matlab(x^3+x*y) expression gives out the code x^3+x*y in written format, then you can simply convert it into x.^3+x.y , just by using "Find & Replace" option of any notepad application. Just find all "" and "^" , and then replace them with ".*" and ".^" .

Hope this helps.

Omid1989
  • 121
  • 1
  • 5
  • 13