 AddTranslator - Maple Help

Matlab

 add a custom MATLAB(R) to Maple function translator method Calling Sequence AddTranslator(fname,handler) Parameters

 fname - string handler - procedure Description

 • AddTranslator is an extension mechanism that allows anyone to supply a function that will translate a MATLAB® command string into an equivalent Maple command string.  The handler must accept as arguments the parameters to the MATLAB® function fname.  Each argument passed to the handler has already been translated to parsable Maple strings.
 • For example, AddTranslator("eye",handler1); will define a translation method for the MATLAB® eye command.  handler1 will be invoked when translating the MATLAB® command, eye(2,3). handler1 will be called with two string arguments; handler1("2", "3") The handler should not execute any code (ie. it should not create an identity matrix), rather it should return a string that contains Maple syntax for performing the equivalent operation in Maple.  Something like the string "LinearAlgebra[IdentityMatrix](2,3)".
 • In many cases the MATLAB® argument sequence will not exactly match the equivalent Maple command.  For example, the MATLAB® eye command accepts either integer dimensions, or an array of dimensions.  Maple's IdentityMatrix command only accepts integer dimensions.  So, translating eye(dims) to LinearAlgebra[IdentityMatrix](dims) is not correct when dims is an array. In these cases it is usually easiest to write a Maple procedure that does the argument conversion.  For example, write:$\mathrm{maple_eye}≔\mathbf{proc}\left(m,n≔m\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}m::\mathrm{rtable}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{LinearAlgebra}[\mathrm{IdentityMatrix}]\left(\mathrm{entries}\left(m,'\mathrm{nolist}'\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{else}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{LinearAlgebra}[\mathrm{IdentityMatrix}]\left(m,n\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end if}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end proc}$ Then write a handler that translates eye(dims) to maple_eye(dims).  In this case, where the handler simply needs to rename the function while keeping all the arguments as is, use StringTools[Join] to join all the given arguments together into a string with commas in between.  eye(a,b) becomes "maple_eye(a,b)". The handler will be:$\mathrm{translate_eye_handler}≔\mathbf{proc}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{cat}\left("maple_eye\left(",\mathrm{StringTools}[\mathrm{Join}]\left(\left[\mathrm{args}\right],","\right),"\right)"\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end proc}$
 • Most built-in translations handlers try not to map to special procedures that just massage their arguments.  The intent is to make it obvious what the target function is.  In the examples above you really should see LinearAlgebra[IdentityMatrix] in the translated result, rather than a call to maple_eye.  However, this sometimes makes the translated code more verbose than it would normally be if you simply started writing the code in Maple.  Translated code should usually be reviewed and optimized by hand to remove extra cases that don't need to be handled.
 • When translating directly to Maple statements, keep in mind that the arguments can themselves be expressions. To avoid multiple evaluations, reference each argument only once.  For example, when translating eye(1+n), the first argument given to the handler will be "1+n".  So, a translation to $"IdentityMatrix\left(if\left(args\left[1\right]::rtable, entries\left(args\left[1\right], \text{'}nolist\text{'}\right), args\left[1\right]\right)\right)"$ will cause $1+n$ to be evaluated at least twice.  Inline procedures can help.  Rewrite the above to $"IdentityMatrix\left(A->\left(if\left(A::rtable,entries\left(A,\text{'}nolist\text{'}\right),A\right)\right)\left(args\left[1\right]\right)"$, or, more explicitly $"IdentityMatrix\left(proc\left(A\right) if A::rtable then entries\left(A,\text{'}nolist\text{'}\right) else A end if; end proc\left(args\left[1\right]\right)"$.
 • Similarly the function you are translating could be contained inside another expression.  To support embedded expressions make sure the resulting translation is a single expression (ie don't translate to multiple statements).  As described above, a procedure can be defined and invoked in the same statement -- proc(x) x^2 end(3) is another way to write 9. Examples

 > $\mathrm{with}\left(\mathrm{Matlab}\right):$
 > translate_brick_handler := proc( ) cat( "maple_brick(", StringTools[Join]([args],", "), ")" ); end proc;
 ${\mathrm{translate_brick_handler}}{≔}{\mathbf{proc}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{cat}}{}\left({"maple_brick\left("}{,}{\mathrm{StringTools}}{[}{\mathrm{Join}}{]}{}\left(\left[{\mathrm{args}}\right]{,}{","}\right){,}{"\right)"}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)
 > $\mathrm{AddTranslator}\left("brick",\mathrm{translate_brick_handler}\right)$
 > $\mathrm{FromMatlab}\left("brick\left(1,2\right)"\right)$
 Evaluating: maple_brick(1, 2);
 ${\mathrm{maple_brick}}{}\left({1}{,}{2}\right)$ (2)
 > $\mathrm{FromMatlab}\left("brick\left(\left[1 2 ; 3 4\right]\right)"\right)$
 Evaluating: maple_brick(Matrix([[1, 2], [3, 4]] ));
 ${\mathrm{maple_brick}}{}\left(\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]\right)$ (3)