Code Generation - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim

Code Generation

The CodeGeneration package offers new support for translating Maple code to R and JavaScript . In addition, the output option for Code Generation has been enhanced to display formatted output inside an embedded text field with the option output=embed.




Other updates


With CodeGeneration[R], you can translate expressions to code fragments:



cg <- sqrt(a ^ 2 + b ^ 2 + c ^ 2)


You can also translate procedures and larger programs.


cg0 <- function(m)
  r <- 0
  for(i in 1 : m)

    r <- r + i


CodeGeneration[R] also translates many Maple data structures and functions, including many routines for linear algebra and special functions, to equivalents in R.


cg1 <- matrix(c(1,3,2,4),nrow=2,ncol=2)


cg2 <- ifelse(v < 1, -v ^ 3 + 3 * v + 1, ifelse(v < 2, 2 * v ^ 3 - 9 * v ^ 2 + 12 * v - 2, -v ^ 3 + 9 * v ^ 2 - 24 * v + 22))

RM&comma;nM  x LinearAlgebra:-IdentityMatrixn

cg3 <- function(M,n) M - x * diag(n)


cg4 <- _C1 * besselJ(x, nu) + _C2 * besselY(x, nu)


Code Generation for R in Maple can also translate some key commands from Statistics:


cg5 <- mean(c(5,2,1,4,3))


cg6 <- median(c(5,2,1,4,3))


cg7 <- sd(c(5,2,1,4,3))


cg8 <- fivenum(c(5,2,1,4,3))

R &apos;Statistics:-Scale 5&comma;2&comma;1&comma;4&comma;3&comma; center &equals; 2&comma; scale &equals; 1 &apos; &semi;

cg9 <- scale(c(5,2,1,4,3), center = 2, scale = 1)


There is limited support for plotting routines from Statistics. Several visualizations such as Histogram and BoxPlot are supported:

R&apos;Statistics:-Histogram1&comma;3&comma;5&comma;7&comma;8&comma;4&comma;4&comma; color&equals;Red&comma; title &equals; Histogram&comma; frequencyscale &equals; absolute&comma; axes&equals;none&apos;

cg10 <- hist(c(1,3,5,7,8,4,4), axes = FALSE, col = "Red", freq = TRUE, main = "Histogram")


cg11 <- boxplot(c(4,8,15,16,23,42), col = "Orange", notch = TRUE, horizontal = TRUE, main = "BoxPlot")


Maple will attempt to return an equivalent command when possible to commands dealing with many distributions. In this example, the evaluated probability density function is translated.


cg12 <- ifelse(x < 0, 0, 0.1e1 / x * sqrt(0.2e1) * pi ^ (-0.1e1 / 0.2e1) * exp(-log(x) ^ 2 / 0.2e1) / 0.2e1)


However, there are differences in how Maple and R treat named probability distributions. In Maple, a distribution can be referenced by the distribution name directly inside of Statistics commands, for example, Statistics:-CDF( Normal(..), .. ). In R, distributions are included in the containing command itself, pnorm(..). A warning message will always be returned when CodeGeneration[R] attempts to translate an expression that contains a reference to a named distribution. When possible, Maple will still attempt to translate the containing expression to an equivalent command in R.

For example, to find the value of the probability density function for the LogNormal distribution with parameters mean = 0 and standard deviation = 1 at the point x = 1 in Maple, one can use PDF(LogNormal(0,1), 1) where the PDF value is taken directly from the LogNormal distribution rather than from a random variable based on a distribution. This is equivalent to a similar command in R, and as such, the equivalent R translation is returned.


Warning, the function names {LogNormal} are not recognized in the target language

cg13 <- dlnorm(1, meanlog = 0, sdlog = 1)


It is possible to translate commands such as PDF, ProbabilityFunction, and CDF in some cases:


Warning, the function names {Poisson} are not recognized in the target language

cg14 <- dpois(1,2)


Warning, the function names {ChiSquare} are not recognized in the target language

cg15 <- pchisq(1,1)


In addition, samples and quantiles can be returned:


Warning, the function names {Uniform} are not recognized in the target language

cg16 <- runif(0.10e2, min = 1, max = 10)


Warning, the function names {Weibull} are not recognized in the target language

cg17 <- qweibull(0.5e0,5, scale = 3)


Time series objects can also be created:


cg18 <- ts(c(10,25,30,55))

R&apos;TimeSeriesAnalysis:-TimeSeriesPlot TimeSeriesAnalysis:-TimeSeries10&comma;25&comma;30&comma;55 &apos;

cg19 <- plot.ts(ts(c(10,25,30,55)))


A list of statistics commands with limited support for code generation can be viewed in the RDetails page.


With CodeGeneration[JavaScript], you can translate expressions to code fragments:



cg0 = Math.sqrt(a * a + b * b + c * c);


You can also translate procedures and larger programs:


function cg1(m) {
  var r;
  r = 0;

  for (i in 1...m)
    r = r + i;

Other updates

The new option output=embed allows CodeGeneration output to be produced inside a Maple text region.