How to convert Maple x * y expression to Matlab for result x. * Y?

Maple expression (e.g. x ^ 3 + x * y) can be converted to matlab on

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

But in Matlab there are two types of product: A Bed and * and A. The Bed and * . The above method will give x ^ 3 + x * y . Is there any convenient way to get the result of x. ^ 3 + x. * Y ?

+5
source share
3 answers

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

'x*~y', , `~` [`*`] (x, `$`, y), , , , "$", NULL .

> 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;
+2

, Maple 2015 , , 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;
+1

If the Matlab expression (x ^ 3 + x * y) yields the code x ^ 3 + x * y in writing, you can simply convert it to x. ^ 3 + xy, just using the "Find and Replace" of any notepad application. Just find all "and" ^ "and then replace them with". * "And." ^ ".

Hope this helps.

0
source

All Articles