Development version of the CRAN package. rim provides an interface to the powerful and fairly complete maxima computer algebra system

This repository is a fork from the original RMaxima created by Kseniia Shumelchyk and Hans W. Borcher shumelchyk/rmaxima, which is currently not maintained.

# Requirements

• Maxima (>= 5.42.1), this package has been tested with 5.42.1 and 5.45.0, but may also work with older versios

# Installation

To install the current release version from CRAN:

install.packages("rim")

If you want to install the latest version the easiest way is to install the R package drat first and add this github account as a repo:

install.packages("drat")
drat::addRepo("rcst")

Now you can easily install it the usual way:

install.packages("rim")

# Usage

If you want to learn how to use Maxima you can check out a number of ressources from it’s project site.

## RMarkdown

This section demonstrates the use of rim via it’s knitr engine. Alternatively, you can run each line of the Maxima code chunks via maxima.get().

This page has been generated using rim’s knitr engine itself.

Note that you can set the output format to LaTeX maxima.engine.format("latex") to get LaTeX-style expression rendering, when using pandoc as well as MathJax is loaded or embedded. Alternatively, expression outputs can be printed in MathML directly using maxima.engine.format("mathml").

When attaching the package and Maxima is installed already, then it will be set up and started in the background automatically, when a command is send. In addition, if you have installed the R-package knitr (which get’s installed by default with this package as well), it will register maxima as a knitr engine. If this worked, maxima code can be used and rendered in RMarkdown documents.

library(rim)
maxima.engine.format("latex")
## [1] "latex"

For example, to generate this page you can download it’s source index.Rmd from here and then use rmarkdown::render to generate it.

download.file(url = "https://raw.githubusercontent.com/rcst/rim/master/docs/index.Rmd",
destfile = "index.Rmd")
maxima.engine.format("latex")
rmarkdown::render("index.Rmd")

Now we can enter Maxima expression inside code chunks. Note that we need to end each line by ; or $ (suppressing output). For example we can type the following code chunk into our RMarkdown document file:  {maxima} L: sqrt(1 - 1/R^2); assume(R > 0)$
'integrate(x, x, 0, L) = integrate(x, x, 0, L);



In the above code chunk we define a variable L (that depends on another variable R). We tell Maximas data base to assume R being larger than zero and suppress any output resulting from this command. The last line prints an unevaluated integral on the left-hand side of the equal sign and on the right-hand side evaluates that same definit integral. The tick quotation mark tells Maxima not to evaluate the suceeding expression. This, when rendered into a document will be printed as

(%i1) L: sqrt(1 - 1/R^2);

$\sqrt{1-\frac{1}{R^2}}$

(%i2) assume(R > 0)$(%i3) 'integrate(x, x, 0, L) = integrate(x, x, 0, L); $\int_{0}^{\sqrt{1-\frac{1}{R^2}}}{x\;dx}=\frac{R^2-1}{2\,R^2}$ The resulting output (if not suppressed) is printed after each line of code, and thus apprears as several. Input and output reference labels that are assign by Maxima are printed into he code chunk. Those can be used to refer to previous commands in other code chunks. For example (%i4) sqrt(rhs(%o3)); $\frac{\sqrt{R^2-1}}{\sqrt{2}\,R}$ takes the right-hand side of the result of the equation from input label %i3, which is assigned to output label %o3 and computes the square-root of it. Of course exercising on such simple computations is of little benefit. The real benefit comes from more complicated expression and the effort that we would need to put if we wanted to typeset the result, such as this (%i5) integrate(1 / (1 + x^4), x); $\frac{\log \left(x^2+\sqrt{2}\,x+1\right)}{2^{\frac{5}{2}}}-\frac{\log \left(x^2-\sqrt{2}\,x+1\right)}{2^{\frac{5}{2}}}+\frac{\arctan \left(\frac{2\,x+\sqrt{2}}{\sqrt{2}}\right)}{2^{\frac{3}{2}}}+\frac{\arctan \left(\frac{2\,x-\sqrt{2}}{\sqrt{2}}\right)}{2^{\frac{3}{2}}}$ pandoc automatically renders the LaTeX output format from Maxima by including MathJax JavaScript script. In general, pandoc takes care of how mathematical equations delimited by $$ are rendered. However, we can also change Maxima’s output format to MathML, which works if the output document is a HTML document. maxima.engine.format("mathml") [1] “mathml” (%i6) sqrt(3/4); $\frac{\sqrt{3}}{2}$ (%i7) f(x) := e^(x^2)$
(%i8) diff(f(x), x);

$2\,e^{x^2}\,\log e\,x$

(%i9) %;

$2\,e^{x^2}\,\log e\,x$

Notice, that we can use the symbol % to refer to the last expression and that this works across code chunks.

You can also replay other previous expressions by referring to their output labels. Output labels are printed in the default maxima output format "linear".

(%i10) log(%o1);

$\frac{\log \left(1-\frac{1}{R^2}\right)}{2}$

# Demos

## Distributions

This is basically a reproduced demo for Sympy from Bryan Zhang’s Blog.

First we define some helper functions:

(%i11) area(dist) := integrate(dist, x, minf, inf)$(%i12) mean(dist) := area(dist*x)$
(%i13) EX2(dist) := area(dist*x^2)$(%i14) variance(dist) := EX2(dist) - mean(dist)^2$
(%i15) mgf(dist) := area(dist*%e^(x*t))$ ### Normal Distribution (%i16) normal(x) := (2*%pi*sigma^2)^(-1/2) * exp(-(x-mu)^2/(2*sigma^2)); ${\it normal}\left(x\right):=\left(2\,\pi\,\sigma^2\right)^{\frac{-1}{2}}\,\exp \left(\frac{-\left(x-\mu\right)^2}{2\,\sigma^2}\right)$ (%i17) assume(sigma > 0)$
(%i18) area(normal(x));

$1$

(%i19) mean(normal(x));

$\mu$

(%i20) variance(normal(x));

$\frac{2^{\frac{3}{2}}\,\sqrt{\pi}\,\sigma^3+2^{\frac{3}{2}}\,\sqrt{\pi}\,\mu^2\,\sigma}{2^{\frac{3}{2}}\,\sqrt{\pi}\,\sigma}-\mu^2$

(%i21) mgf(normal(x));

$e^{\frac{\sigma^2\,t^2+2\,\mu\,t}{2}}$

### Laplace Distribution

(%i22) laplace(x) := (2*b)^-1 * exp(-abs(x - mu)/b);

${\it laplace}\left(x\right):=\left(2\,b\right)^ {- 1 }\,\exp \left(\frac{-\left| x-\mu\right| }{b}\right)$

(%i23) load("abs_integrate")$(%i24) assume(b > 0)$
(%i25) area(laplace(x));

$1$

(%i26) mean(laplace(x));

$\mu$

(%i27) variance(laplace(x));

$\frac{2\,b\,\mu^2+4\,b^3}{2\,b}-\mu^2$

### Exponential Distribution

(%i28) expo(x) := unit_step(x) * lambda * exp(-lambda * x);

${\it expo}\left(x\right):={\it unit\_step}\left(x\right)\,\lambda\,\exp \left(\left(-\lambda\right)\,x\right)$

(%i29) assume(lambda > 0)\$
(%i30) area(expo(x));

$1$

(%i31) mean(expo(x));

$\frac{1}{\lambda}$

(%i32) variance(expo(x));

$\frac{1}{\lambda^2}$

## Matrices

(%i33) m: matrix([0, 1, a], [1, 0, 1], [1, 1, 0]);

$\begin{pmatrix}0 & 1 & a \\ 1 & 0 & 1 \\ 1 & 1 & 0 \\ \end{pmatrix}$

(%i34) transpose(m);

$\begin{pmatrix}0 & 1 & 1 \\ 1 & 0 & 1 \\ a & 1 & 0 \\ \end{pmatrix}$

(%i35) determinant(m);

$a+1$

(%i36) f: invert(m), detout;

$\frac{\begin{pmatrix}-1 & a & 1 \\ 1 & -a & a \\ 1 & 1 & -1 \\ \end{pmatrix}}{a+1}$

(%i37) m . f;

$\begin{pmatrix}0 & 1 & a \\ 1 & 0 & 1 \\ 1 & 1 & 0 \\ \end{pmatrix}\cdot \left(\frac{\begin{pmatrix}-1 & a & 1 \\ 1 & -a & a \\ 1 & 1 & -1 \\ \end{pmatrix}}{a+1}\right)$

(%i38) expand(%);

$\begin{pmatrix}\frac{a}{a+1}+\frac{1}{a+1} & 0 & 0 \\ 0 & \frac{a}{a+1}+\frac{1}{a+1} & 0 \\ 0 & 0 & \frac{a}{a+1}+\frac{1}{a+1} \\ \end{pmatrix}$

(%i39) factor(%);

$\begin{pmatrix}1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}$

## If-then-else

(%i40) x: 1234;

$1234$

(%i41) y: 2345;

$2345$

(%i42) if x > y
then x
else y;

$2345$