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
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.
Rm→addi,i=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({−v3+3⁢v+1v<12⁢v3−9⁢v2+12⁢v−2v<2−v3+9⁢v2−24⁢v+22otherwise)
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,n→M − x LinearAlgebra:-IdentityMatrixn
cg3 <- function(M,n) M - x * diag(n)
R_C1⁢BesselJ⁡ν,x+_C2⁢BesselYν,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'Statistics:-Mean5,2,1,4,3'
cg5 <- mean(c(5,2,1,4,3))
R'Statistics:-Median5,2,1,4,3'
cg6 <- median(c(5,2,1,4,3))
R'Statistics:-StandardDeviation5,2,1,4,3'
cg7 <- sd(c(5,2,1,4,3))
R'Statistics:-FivePointSummary5,2,1,4,3'
cg8 <- fivenum(c(5,2,1,4,3))
R 'Statistics:-Scale 5,2,1,4,3, center = 2, scale = 1 ' ;
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'Statistics:-Histogram1,3,5,7,8,4,4, color=Red, title = Histogram, frequencyscale = absolute, axes=none'
cg10 <- hist(c(1,3,5,7,8,4,4), axes = FALSE, col = "Red", freq = TRUE, main = "Histogram")
R'Statistics:-BoxPlot4,8,15,16,23,42,color=Orange,title=BoxPlot,notched=true,orientation=horizontal'
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,1,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'Statistics:-PDFLogNormal0,1,1'
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'Statistics:-ProbabilityFunctionPoisson2,1'
Warning, the function names {Poisson} are not recognized in the target language
cg14 <- dpois(1,2)
R'Statistics:-CDFChiSquare1,1'
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'Statistics:-SampleUniform1,10,10';
Warning, the function names {Uniform} are not recognized in the target language
cg16 <- runif(0.10e2, min = 1, max = 10)
R'Statistics:-QuantileWeibull3,5,0.5';
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'TimeSeriesAnalysis:-TimeSeries10,25,30,55'
cg18 <- ts(c(10,25,30,55))
R'TimeSeriesAnalysis:-TimeSeriesPlot TimeSeriesAnalysis:-TimeSeries10,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.
With CodeGeneration[JavaScript], you can translate expressions to code fragments:
JavaScripta2+b2+c2
cg0 = Math.sqrt(a * a + b * b + c * c);
You can also translate procedures and larger programs:
JavaScriptm→addi,i=1..m
function cg1(m) { var r; r = 0;
for (i in 1...m) { r = r + i; } return(r); }
The new option output=embed allows CodeGeneration output to be produced inside a Maple text region.
CodeGeneration:-Pythonx,y→sinx2+cosx2,output=embed
Download Help Document