Skip to content(if available)orjump to list(if available)

Maxima in the browser using Embedded Common Lisp on WASM

r85804306610

because nobody knows maxima, some things to try. also plot2d/plot3d work, so that's pretty neat. the whole thing is powered by https://ecl.common-lisp.dev and the op announcement is here https://mailman3.common-lisp.net/hyperkitty/list/ecl-devel@c...

  :lisp (+ 2 2)
  solve(f(x)^2-1,x);
  integrate(x^2,x);
  2^1024;
  factor(30!);
  a:1
  b:2
  a+b;
  sqrt(a^2+b^2);
  expr: log((x+2)*(x-2))+log(x);
  ratsimp(expr);
  fullratsimp(expr);
  trigsimp(2*cos(x)^2 + sin(x)^2);
  solve(x^3=1,x);
  diff(sin(x), x);
  float([%e,%pi,%phi,%gamma]);
  f(x):=x^2;
  f(10);
  taylor(sin(x),x,0,5);
  plot2d(x^2-x+3,[x,-10,10]);
  plot2d([x^2, x^3, x^4 -x +1] ,[x,-10,10]);
  f(x,y):= sin(x) + cos(y);
  plot3d(f(x,y), [x,-5,5], [y,-5,5]);

seanhunter

One thing I would add for people who are new to this is you can do

??something; to get the internal help. So say I want to know how to solve ODEs in maxima, do

??ode; …and you’ll get help. It generally does a search and presents a numbered list of options and you’ll want to type a number and a semicolon.

seanhunter

Some other useful things:

1)solve to solve a system and “variable: expression” will assign that expression to that variable. You can then substitute the results into a later expression using a “,”. So you might do

   solns:solve([x+3*y=5,5*x-2*y=4],[x,y]);
   z:%e^(x,y),solns;
That will solve the system and then substitute those solutions into the expression for z.

2)pi, e, and the imaginary unit are called %pi, %e and %i respectively.

3)The symbolic solver is a lot less powerful than mathematica but you can do

   load(“to_poly_solve”);
To get a polynomial solver that will at least get you all the roots of polynomials and does better on most trigonometric equations.

   load(“drawdf”);
Is useful for drawing vector fields, phase portraits and that type of thing.

4) you can use a single quote to delay evaluation of something. Like say you want to write a differential equation and you don’t want it to actually try to evaluate the derivative inline, you can write something like

   osc:‘diff(x,t,2) + r*’diff(x,t) + k*x = p*cos(omega\*t);
…for a forced harmonic oscilator. Or whatever. This is useful if you want to mess with the expression a bit before you try to solve the expression (eg doing substitutions or whatnot).*

null

[deleted]

michaelsbradley

Thank you! As someone with no prior Maxima experience, those were some great starting points for me to learn and explore!

Note `plot3d(...);` doesn't seem to work: the output image is missing/broken.

r85804306610

you probably didn't define the function, f(x,y), which is the line before plot3d. it's a slightly buggy behavior

williamstein

plot3d did work for me (on Chrome). Impressive.

unusual-name

We used maxima extensively in high school. I remember that I forgot to bring my laptop when we had our first maxima lesson. My teachers understandably scolded me for not bringing my laptop to school, but to their surprise I found an online version of maxima. [1] While it was good enough for this period, in contrast to this implementation it was truly awful.

I just think it's kinda sad that a lot of people never heard of maxima, as it is a pretty decent tool once you learn how to use it.

[1] http://www.dma.ufv.br/maxima/index.php

forgotpwd16

>scolded me for not bringing my laptop to school

That sounds stupid. What if someone doesn't have a laptop? Even if do, cannot see why one's obligated to bring it along.

>in contrast to this implementation it was truly awful

It surely has the advantage of interactivity. The version you linked seems to restart the session in every query. On the other hand, seems more stable. Both `invert(matrix([2,3,1], [a,0,0], [1,4,8]));` and `draw3d(explicit(x^2+y^2,x,-1,1,y,-1,1));` that run fine in UFV's DMa version fail to run in the submitted one.

aidenn0

> That sounds stupid. What if someone doesn't have a laptop? Even if do, cannot see why one's obligated to bring it along.

Where I live, the school issues laptops to students, so not bringing the laptop is today's not bringing your textbook. (As a point of fact, my daughter is in 10th grade and has had exactly one physical textbook so far; the rest have been digital)

veryveryold

(to_lisp); (loop for i below 1000000 count t) takes 0.34 seconds on my system with vanilla maxima (on gcl). In the browser it takes about 7 seconds, so it must be a factor of 21 in computer time. Using sbcl outside maxima it takes 0.002 seconds. So one can get some idea about performance.

Perhaps it could be combined with J (array language), like in the playground https://code.jsoftware.com/wiki/Playground that is using webassembly

It seems to work very well locally without connection to the web.

disentanglement

Apart from the restriction to bytecode interpretation already mentioned, one reason for the slowness is that the sort of C with garbage collection that ECL needs is quite difficult to do in Webassembly. There is no way to scan previous stack frames for pointers in wasm, so all pointers to heap objects (or everything that looks like it might be one) have to be kept around somewhere in the heap where the GC can find them. This is really expensive and slows down the code a lot.

Of course, another approach would be to use the new wasm GC interface. But that requires defining a new ABI for garbage collected C, writing a new backend for LLVM, etc. So that would also be a lot of work to implement. Right now, there just is no efficient way to run programs that depend on bdwgc on wasm.

whitten

What is bdwgc ? gc==garbage collection ?

shawn_w

The Boehm-Demers-Weiser garbage collecting memory allocation library.

https://www.hboehm.info/gc/

jackdaniel

Web version of ecl when compiler is invoked at runtime, uses one-pass bytecodes compiler, so things tend to lack optimization unless compiled beforehand.

ted_dunning

It feels faster than the other web-hosted maxima instances out there and if you take into account the cost of installing common lisp and maxima (I only need to use this about twice a year so I don't keep them live), it can be much faster than a local instance.

vindarel

I'll add options to run Maxima:

* [Maxima on Android](https://play.google.com/store/apps/details?id=jp.yhonda), built with ECL.

* [Maxima on Jupyter](https://github.com/robert-dodier/maxima-jupyter)

* Maxima can be used via [SageMath](https://www.sagemath.org/) and [KDE Cantor](https://apps.kde.org/cantor/).

* Of course, with Emacs: [maxima-mode](https://gitlab.com/sasanidas/maxima) ([screenshot](https://community.linuxmint.com/img/screenshots/maxima-emacs...))

and [maxima-interface](https://github.com/jmbr/maxima-interface) to ease the interface between Maxima and Common Lisp.

* it can be used [from a Lisp REPL](https://mahmoodsheikh36.github.io/post/20230510181916-maxima...).

(and [wxMaxima](https://wxmaxima-developers.github.io/wxmaxima/): a graphical frontend as mentioned earlier)

(find the links and more scientific libraries for CL on https://github.com/CodyReichert/awesome-cl)

phoe-krk

It's called "Embeddable Common Lisp", not "Embedded Common Lisp".

https://ecl.common-lisp.dev/

v9v

Raymond Toy posted this on the ECL mailing list: https://mailman3.common-lisp.net/hyperkitty/list/ecl-devel@c...

magicalhippo

I've tried Maxima on a few occasions but the syntax somehow fails to agree with me.

Been looking for a self-hosted alternative to Wolfram Alpha, and just stumbled over Mathics, which has a Django front-end[1]. Rough but usable Mathematica alternative, at least for basic use.

[1]: https://github.com/Mathics3/mathics-django

pkaye

There is also Expreduce which is a CAS written in Go that follows Mathematica syntax.

https://corywalker.github.io/expreduce-docs/

trott

How do Maxima and SymPy compare in terms of capability, features and speed (native, not WASM)?

fsckboy

tangential: can Macsyma/Maxima do symbolic boolean algebra, reducing formulae to bog standard "OR of ANDs" form?

(I've tried looking at the doc, and it's clearly not a feature they showcase, and as others have said, the way it works is not intuitive to me. I really need this, but trying to do it in lisp is becoming its own project and I want to get back to my project. I found some really nice online reducers, but the UIs are not conducive to pasting/putting in simultaneous equations, and using those outputs as inputs again)

r85804306610

maxima has logic package, written in 2009 by alexey beshenov. it lives in share/logic which is also where the texi docs are. it's included with the op version,

  load(logic);
  logic_simp (a or (b or false or (a or b)));
  characteristic_vector (a xor b);
  zhegalkin_form ((a implies b) or c);
  e : ((a or b) xor c) and d$
  logic_equiv (e, zhegalkin_form (e));
  logic_diff (a or b or c, a);
  demorgan (boolean_form (a nor b nor c));
etc. there's more stuff in docs

azakai

It looks like the wasm file here is not fully optimized. Reading the wasm and JS, I'd guess -O1 perhaps? Linking with -O3 would make it smaller and possibly faster (10% smaller binary in a quick local test, and it removes 90% of locals, which should speed things up too)

But it is possible that it's already fast enough for the purposes here, and this doesn't matter.

jackdaniel

It's worse, it is -O0 -- this is because of the GC and binaryen/llvm interaction. For GC to work we need to spill stack call pointers (and binaryen has such a flag!), but for the optimization level 1 and above said pointers are sometimes optimized away :3

I'm experimenting with WASI and the GC extension for WASM, but that's months from today if we speak about complete port (given my time capacity at the moment).

azakai

Interesting, thanks for the details!

WasmGC would be the best solution here, yeah, then the VM handles pointers for you.

Otherwise, I could look into the SpillPointers issue for you if you want - optimizations should not remove GC pointers, so that sounds like a bug. If so feel free to file an issue with a testcase. (But WasmGC would be best, avoiding all that.)

disentanglement

As far as I know, optimization levels higher than -O0 work fine with SpillPointers. But at least in a cursory first look I had a while ago, the optimizations made things slower overall. I guess they might lead actually to more "moving pointers in and out of the heap" since the SpillPointers pass is done at the very end. But this should all be investigated more thoroughly.

gtpedrosa

This brings back memories. We used this in my mechanical engineering mechanisms class. It was cool to have the (simplified!) symbolic formulas describing the movements we were designing.