---
title: "knitr engine test page"
output: html_document
---

```{r options-init}
options(width = 80)
maxima.options(engine.format = "latex", 
	       engine.label = TRUE,
	       inline.format = "inline", 
	       inline.label = FALSE)
```

```{maxima first-example}
L: sqrt(1 - 1/R^2);
assume(R > 0);
'integrate(x, x, 0, L) = integrate(x, x, 0, L);
```

```{maxima second-example}
'L = L;
'integrate(x, x, 0, 'L) = integrate(x, x, 0, L);
```

This is an inline test: `r maxima.inline("'L = L;")`.


```{maxima square-root}
sqrt(3/4);
```

```{maxima output-var, output.var = "moo"}
f(x) := e^(x^2)$
diff(f(x), x);
```

```{maxima repeat}
%;
```

```{maxima output-reference-label, output.var = "moo"}
log(%o1);
```

```{r using-output-var}
rim_global()$moo
eval(rim_global()$moo[[1]], list(R = 12))
```

```{maxima comments}
/* aa is a variable of interest */  aa : 1234;
bb : aa^2; /* Value of bb depends on aa */ 
/* User-defined infix operator */  infix ("Q");
/* Parses same as a b c, not abc */  a/* foo */Q/* bar */c;
/* Comments /* can be nested /* to any depth */ */ */  1 + xyz;
```

```{maxima logical-list-parsing, output.var = "boollist"}
load(fourier_elim)$
fourier_elim([x^2-1>0], [x]);
```

```{r, eval = TRUE}
# from previous chunk: output.var = "boollist"
rim_global()$boollist
eval(rim_global()$boollist[["o18"]], list(x = seq(-10, 10, 0.5)))
```


# Plots

```{maxima plot2d, fig.cap = "plot2d()", fig.align="center", fig.show='hide'}
r: (exp(cos(t))-2*cos(4*t)-sin(t/12)^5)$
plot2d([parametric, r*sin(t), r*cos(t), [t,-8*%pi,8*%pi]]);
```

```{maxima plot3d, fig.cap = "plot3d()", fig.align="center", fig.show='hide', eval = FALSE}
plot3d(log (x^2*y^2), [x, -2, 2], [y, -2, 2],[grid, 29, 29],
       [palette, [gradient, red, orange, yellow, green]],
       color_bar, [xtics, 1], [ytics, 1], [ztics, 4],
       [color_bar_tics, 4]);
```

```{maxima draw, fig.cap = "draw()", fig.align="center", fig.show='hide'}
example1:
  gr3d (title          = "Controlling color range",
        enhanced3d     = true,
        color          = green,
        cbrange        = [-3,10],
        explicit(x^2+y^2, x,-2,2,y,-2,2)) $

example2:
  gr3d (title          = "Playing with tics in colorbox",
        enhanced3d     = true,
        color          = green,
        cbtics         = {["High",10],["Medium",05],["Low",0]},
        cbrange = [0, 10],
        explicit(x^2+y^2, x,-2,2,y,-2,2))$

example3:
  gr3d (title      = "Logarithmic scale to colors",
        enhanced3d = true,
        color      = green,
        logcb      = true,
        logz       = true,
        palette    = [-15,24,-9],
        explicit(exp(x^2-y^2), x,-2,2,y,-2,2))$

draw(
  dimensions = [500,1500],
  example1, example2, example3)$
```

```{maxima draw2d, fig.cap = "draw2d()", fig.align="center", fig.show='hide'}
draw2d(
  dimensions = [1000, 1000],
  proportional_axes = xy,
  fill_color        = sea_green,
  color             = aquamarine,
  line_width        = 6,
  ellipse(7,6,2,3,0,360))$
``` 

```{maxima draw3d, fig.cap = "draw3d()", fig.align="center", fig.show='hide'}
draw3d(
   dimensions = [1000, 1000],
   surface_hide      = true,
   axis_3d           = false,
   proportional_axes = xyz,
 
   color             = blue,
   cylindrical(z,z,-2,2,a,0,2*%pi), 
 
   color            = brown,
   cylindrical(3,z,-2,2,az,0,%pi),
 
   color            = green,
   cylindrical(sqrt(25-z^2),z,-5,5,a,0,%pi))$
```


```{r image-count, warning=FALSE, results="markup"}
pft <- list.files(pattern = "(?:plot|draw)(2d|3d)?-[[:print:]]{6}\\.png", full.names = TRUE)

if(length(pft) == 4L)  {
  paste0("OK")
} else {
  paste0("Error: Unexpected number of Maxima plots: ", 
         paste0(pft, collapse = ", "))
}

if(length(pft)) {
  if(all(as.logical(file.size(pft)))) {
    paste0("OK")
  }
  else {
    errfiles <- pft[file.size(pft) == 0]
    paste0("Error: Maxima plot file(s) ", paste0(errfiles, collapse = ", "),
           "are empty.")
  }
}
```

# Normal Distribution

```{maxima summary-functions}
area(dist) := integrate(dist, x, minf, inf)$
mean(dist) := area(dist*x)$
EX2(dist) := area(dist*x^2)$
variance(dist) := EX2(dist) - mean(dist)^2$
mgf(dist) := area(dist*%e^(x*t))$
```

```{maxima normal}
normal(x) := 
      (2*%pi*sigma^2)^(-1/2) * 
      exp(-(x-mu)^2/(2*sigma^2));

assume(sigma > 0)$

area(normal(x));
mean(normal(x));
variance(normal(x));
mgf(normal(x));
```

# Matrices

```{maxima matrices}
m: matrix([0, 1, a], [1, 0, 1], [1, 1, 0]);
transpose(m);
determinant(m);
f: invert(m), detout;
m . f;
expand(%);
factor(%);
```

# If-then-else

```{maxima x-y}
x: 1234;
y: 2345;
```

```{maxima if-then-else}
if x > y
  then x
  else y;
```
