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.

 

R

JavaScript

Other updates

R

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

withCodeGeneration:

Ra2+b2+c2

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

 

You can also translate procedures and larger programs.

Rmaddi&comma;i&equals;1..m

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

    r <- r + i
  return(r)
}

 

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

R1234

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

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

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)

R_C1BesselJν&comma;x&plus;_C2BesselYν&comma;x

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

 

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

R&apos;Statistics:-Mean5&comma;2&comma;1&comma;4&comma;3&apos;

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

R&apos;Statistics:-Median5&comma;2&comma;1&comma;4&comma;3&apos;

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

R&apos;Statistics:-StandardDeviation5&comma;2&comma;1&comma;4&comma;3&apos;

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

R&apos;Statistics:-FivePointSummary5&comma;2&comma;1&comma;4&comma;3&apos;

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")

R&apos;Statistics:-BoxPlot4&comma;8&comma;15&comma;16&comma;23&comma;42&comma;color&equals;Orange&comma;title&equals;BoxPlot&comma;notched&equals;true&comma;orientation&equals;horizontal&apos;

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.

RStatistics:-PDFLogNormal0&comma;1&comma;x

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.

R&apos;Statistics:-PDFLogNormal0&comma;1&comma;1&apos;

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:

R&apos;Statistics:-ProbabilityFunctionPoisson2&comma;1&apos;

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

cg14 <- dpois(1,2)

R&apos;Statistics:-CDFChiSquare1&comma;1&apos;

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

cg15 <- pchisq(1,1)

 

In addition, samples and quantiles can be returned:

R&apos;Statistics:-SampleUniform1&comma;10&comma;10&apos;&semi;

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

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

R&apos;Statistics:-QuantileWeibull3&comma;5&comma;0.5&apos;&semi;

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:

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

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.

JavaScript

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

withCodeGeneration&colon;

JavaScripta2&plus;b2&plus;c2

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

 

You can also translate procedures and larger programs:

JavaScriptmaddi&comma;i&equals;1..m

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

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

Other updates

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

CodeGeneration:-Pythonx&comma;ysinx2&plus;cosx2&comma;output&equals;embed