Code Generation - Maple Help

 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

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

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$
 > $R\left(\sqrt{{a}^{2}+{b}^{2}+{c}^{2}}\right)$
 cg <- sqrt(a ^ 2 + b ^ 2 + c ^ 2)

You can also translate procedures and larger programs.

 > $R\left(m\to \mathrm{add}\left(i,i=1..m\right)\right)$
 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.

 > $R\left(\left[\begin{array}{rr}1& 2\\ 3& 4\end{array}\right]\right)$
 cg1 <- matrix(c(1,3,2,4),nrow=2,ncol=2)
 > $R({\begin{array}{cc}-{v}^{3}+3v+1& v<1\\ 2{v}^{3}-9{v}^{2}+12v-2& v<2\\ -{v}^{3}+9{v}^{2}-24v+22& \mathrm{otherwise}\end{array})$
 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))
 >
 cg3 <- function(M,n) M - x * diag(n)
 > $R\left(\mathrm{_C1}\mathrm{BesselJ}\left(\mathrm{\nu },x\right)+\mathrm{_C2}\mathrm{BesselY}\left(\mathrm{\nu },x\right)\right)$
 cg4 <- _C1 * besselJ(x, nu) + _C2 * besselY(x, nu)

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

 > $R\left('\mathrm{Statistics}:-\mathrm{Mean}\left(\left[5,2,1,4,3\right]\right)'\right)$
 cg5 <- mean(c(5,2,1,4,3))
 > $R\left('\mathrm{Statistics}:-\mathrm{Median}\left(\left[5,2,1,4,3\right]\right)'\right)$
 cg6 <- median(c(5,2,1,4,3))
 > $R\left('\mathrm{Statistics}:-\mathrm{StandardDeviation}\left(\left[5,2,1,4,3\right]\right)'\right)$
 cg7 <- sd(c(5,2,1,4,3))
 > $R\left('\mathrm{Statistics}:-\mathrm{FivePointSummary}\left(\left[5,2,1,4,3\right]\right)'\right)$
 cg8 <- fivenum(c(5,2,1,4,3))
 >
 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:

 >
 cg10 <- hist(c(1,3,5,7,8,4,4), axes = FALSE, col = "Red", freq = TRUE, main = "Histogram")
 > $R\left('\mathrm{Statistics}:-\mathrm{BoxPlot}\left(\left[4,8,15,16,23,42\right],\mathrm{color}="Orange",\mathrm{title}="BoxPlot",\mathrm{notched}=\mathrm{true},\mathrm{orientation}=\mathrm{horizontal}\right)'\right)$
 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.

 > $R\left(\mathrm{Statistics}:-\mathrm{PDF}\left(\mathrm{LogNormal}\left(0,1\right),x\right)\right)$
 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\left('\mathrm{Statistics}:-\mathrm{PDF}\left(\mathrm{LogNormal}\left(0,1\right),1\right)'\right)$
 cg13 <- dlnorm(1, meanlog = 0, sdlog = 1)

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

 > $R\left('\mathrm{Statistics}:-\mathrm{ProbabilityFunction}\left(\mathrm{Poisson}\left(2\right),1\right)'\right)$
 cg14 <- dpois(1,2)
 > $R\left('\mathrm{Statistics}:-\mathrm{CDF}\left(\mathrm{ChiSquare}\left(1\right),1\right)'\right)$
 cg15 <- pchisq(1,1)

In addition, samples and quantiles can be returned:

 > $R\left('\mathrm{Statistics}:-\mathrm{Sample}\left(\mathrm{Uniform}\left(1,10\right),10\right)'\right);$
 cg16 <- runif(0.10e2, min = 1, max = 10)
 > $R\left('\mathrm{Statistics}:-\mathrm{Quantile}\left(\mathrm{Weibull}\left(3,5\right),0.5\right)'\right);$
 cg17 <- qweibull(0.5e0,5, scale = 3)

Time series objects can also be created:

 > $R\left('\mathrm{TimeSeriesAnalysis}:-\mathrm{TimeSeries}\left(\left[10,25,30,55\right]\right)'\right)$
 cg18 <- ts(c(10,25,30,55))
 >
 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:

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$
 > $\mathrm{JavaScript}\left(\sqrt{{a}^{2}+{b}^{2}+{c}^{2}}\right)$
 cg0 = Math.sqrt(a * a + b * b + c * c);

You can also translate procedures and larger programs:

 > $\mathrm{JavaScript}\left(m\to \mathrm{add}\left(i,i=1..m\right)\right)$
 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. $\mathrm{CodeGeneration}:-\mathrm{Python}\left(\left(x,y\right)\to \mathrm{sin}{\left(x\right)}^{2}+\mathrm{cos}{\left(x\right)}^{2},\mathrm{output}=\mathrm{embed}\right)$