updates/Maple18/CodeGeneration - Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : updates/Maple18/CodeGeneration

Code Generation

 

The updated CodeGeneration package offers new support for translating to Python and Perl, expanded support for MATLAB®, and more options for controlling output.

The new Code Generation Assistant offers a convenient graphical interface to the CodeGeneration package.  

CodeGeneration[Python]

The new Python translator generates code in the Python 3 language for standalone execution. Similar to CodeGeneration's other translators, with CodeGeneration[Python], you can translate expressions to code fragments:

withCodeGeneration:

Pythona2+b2+c2

cg = math.sqrt(a ** 2 + b ** 2 + c ** 2)

 

You can also translate procedures and larger programs.

Pythonmaddithprimei,i=1..m

import sympy


def cg0 (m):
    r = 0
    for i in range(1, m + 1):
        r = r + sympy.prime(i)
    return(r)


In addition to supporting the core Python 3 language, CodeGeneration[Python] also translates many Maple data structures and functions, including many routines for linear algebra and special functions, to equivalents in the popular numpy and scipy libraries for scientific computation.

Maple Object

Generated Python Output

5968169512679206222992533146051

cg25 = numpy.mat([[-59,-68,16,-95],[12,-67,9,-20],[-62,22,99,-25],[-33,14,60,51]])

&lcub;v3&plus;3v&plus;1v<12v39v2&plus;12v2v<2v3&plus;9v224v&plus;22otherwise

cg26 = -v ** 3 + 3 * v + 1 if v < 1 else 2 * v ** 3 - 9 * v ** 2 + 12 * v - 2 if v < 2 else -v ** 3 + 9 * v ** 2 - 24 * v + 22

M&comma;n&rarr;MxLinearAlgebra:-IdentityMatrixn

import numpy

cg27 = lambda M,n: M - x * numpy.eye(n)

_C1BesselJ&nu;&comma;x&plus;_C2BesselY&nu;&comma;x

cg28 = _C1 * scipy.special.jv(nu, x) + _C2 * scipy.special.yv(nu, x)

LambertW100&comma;&pi;&plus;&ExponentialE;

cg29 = scipy.special.lambertw(math.pi + math.e, 100)

CodeGeneration[Perl]

The new Perl translator allows you to prototype code in Maple for later inclusion in existing code in the Perl 5 language.
Since Maple, Perl, and Python are all loosely-typed interpreted programming languages, Maple programs can often be expressed very naturally in either Perl or Python.

withCodeGeneration&colon;

Perlb24 a c

$cg30 = sqrt(-4 * $a * $c + $b ** 2);

In particular, many of the advanced tools for string processing in Maple can be translated to Perl equivalents.  The following Maple code uses the StringTools[RegMatch] command for string matching with regular expressions.

replaceFoo procs     if StringTools:-RegMatchN[aeiou]*dl[ae]*&comma;s then         Match     else         catNo match: &comma;s     end if&semi;end proc

procsifStringTools:-RegMatchN[aeiou]*dl[ae]*&comma;sthenMatchelsecatNo match: &comma;send ifend proc

(2.1)

replaceFooNeedle&semi; replaceFooHaystack

No match: Haystack

(2.2)

The Perl language also has extensive support for regular expressions and CodeGeneration[Perl] makes use of this fact in its translation:

PerlreplaceFoo

#!/usr/bin/perl


sub replaceFoo
{
  local($s) = @_;
  if (($s =~ m/N[aeiou]*dl[ae]*/)) {
    return("Match");
  } else {
    return("No match: " . $s);
  }
}

Additional improvements

In addition to supporting new language targets, Maple 18 includes expanded support for some existing targets and new options for controlling output.

The Matlab translator now provides MATLAB® equivalents for a larger set of Maple functions, including commands in the LinearAlgebra and ArrayTools packages.

DimensionOfCharacteristicMatrix procM&comma;n&comma; &lambda;     local A&semi;     A M&lambda;LinearAlgebra:-IdentityMatrixn&comma;n&semi;     return LinearAlgebra:-RowDimensionAend proc&colon; 

MatlabDimensionOfCharacteristicMatrix

function cg = DimensionOfCharacteristicMatrix(M, n, lambda)

  A = M - lambda * eye(n, n);
  cg = size(A,1);

In addition, the new libraryorder option allows the user to specify a preference on which standard libraries are used for translation in the case when matching translations are available from multiple libraries.  This example from Python shows first a translation using both numpy.linalg and scipy.linalg libraries.

HilbertDeterminant  procM&comma;n uses LinearAlgebra&semi;   return Determinant HilbertMatrix n  &semi;end proc&colon;

PythonHilbertDeterminant

import numpy.linalg

import scipy.linalg

def HilbertDeterminant (M, n):
    return(numpy.linalg.det(scipy.linalg.hilbert(n)))

Typically, translating the determinant function  to numpy.linalg is the preferable choice.
In this case, because scipy.linalg library is already being used and contains its own implementation of the determinant function, it is preferable to use that implementation. This may be achieved using the libraryorder option to specify the relative weight to assign to each library.

PythonHilbertDeterminant&comma; libraryorder&equals;scipy.linalg&comma; numpy.linalg

import scipy.linalg


def HilbertDeterminant (M, n):
    return(scipy.linalg.det(scipy.linalg.hilbert(n)))

See Also

CodeGeneration[Perl], CodeGeneration[Python], Language and Programming Enhancements in Maple 18


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam