# [Maxima] Writing a new module ?

Akshay Srinivasan akshaysrinivasan at gmail.com
Sat Mar 17 12:39:54 CDT 2012

Yes it will probably be much easier (not to mention far more elegant)
to write the whole thing in Maxima. At the time I wrote the thing, I
hardly knew any lisp (much less about the internals of Maxima), so the
whole thing was hacked up to work.

The purpose of the project is to actually generate the C code; I need
to get the jacobian of the vector field governing the dynamics, which
is feasible only using AD for large systems.

Here's an example of the maxima (predominant) part of an example:
https://github.com/enupten/Classical-Mechanic/blob/master/tests/double-pendulum/double-pendulum.mac
---------------------------------------------------------------------------

L_double_pendulum(t, q, q1, p) := block([theta_1, theta_2, thetadot_1,
theta_1: q[1][1],
theta_2: q[2][1],
m: p[1][1],
thetadot_2 * cos(theta_1 - theta_2) - (1 + m) * sin(theta_1)
-sin(theta_2),
return(L))\$

/*^^q*/
q: transpose(matrix([phi, theta]));
/*^^v*/
/*^^u*/
u: transpose(matrix([tau_theta]));
/*^^t*/
t: t;
/*^^p*/
p: transpose(matrix([m]));

S : generate_eom(L_double_pendulum, t, q, qdot, transpose(matrix([-
u[1][1], u[1][1]])), p);

save_system(S, "expr/pend");
---------------------------------------------------------------------------
"S" here is a list of
(d^2 L)/(dq' dq'), (d^2 L)/(dq dq') ... matrices which are dumped into
(barely) C compatible expressions by save_system(..) into separate
files of the form "expr/pend_Pv_12". The lisp code currently just
takes the expression in these files and generates C++ code to compute
the various terms of the equations of motion, all the while using
that one can obtain the derivatives of these matrices.

There is a C library which then takes these functions and assembles
the vector field and its jacobian, so that all one needs to do in C is
something like:
-----------------------------------------------------------------
clmech_system *pend = create_double_pendulum_system();
clmech_workspace_alloc(pend);

clmech_field(qdot,
t, q, u, p,
pend);

clmech_jacobian(qdot,
dfdt, dfdx, dfdu, dfdp,
t, q, u, p,
pend);
-----------------------------------------------------------------

I have a vague idea of what I should write in Maxima (lisp module), I
think I have to write something like fortra.lisp which generates C
compatible expressions. I will then have to then format them into a
file-stream which should be easy to do because the existing parser is
in lisp anyway.

I thought string processing was hard (annoying) in maxima itself ?
That was my actual motivation to use lisp: so that it can be merged
back into maxima.

Akshay

On 03/17/2012 10:25 PM, Stavros Macrakis wrote:
>
> In looking at your existing code, it seems that most of it is
> concerned with the details of string manipulation and C syntax.
> Fortunately, none of this is necessary in Maxima.
>
> It sounds as though you're looking at a presentation of Maxima
> internals (URL?).  For your application, I'm almost certain that
> you'd be better off programming in the Maxima language rather than
> in Lisp.  You can then use normal mathematical infix notation, and
> simplification etc. is "magically" taken care of.
>
> Can you give us an example of the input-output behavior of your
> code?
>
> -s
>
> On Sat, Mar 17, 2012 at 12:43, Akshay Srinivasan
> <akshaysrinivasan at gmail.com <mailto:akshaysrinivasan at gmail.com>>
> wrote:
>
> Hello, I was thinking of moving my Lagrange equations-of-motion
> put here: https://github.com/enupten/Classical-Mechanic completely
> into Maxima, because then a class I'll be lecturing will be able to
> use it completely in Windows without requiring a Lisp
> implementation installed explicitly. I looked at the tutorial by
> Dieter Kaiser, but it still misses out a lot of things (and I
> couldn't find the other 4 chapters). For instance what exactly is a
> prop ? It looks like some kind of a special function-symbol
> means (verb .. ?). I have to say it is terribly hard for want-to-be
> developers to get -a vague- understanding of Maxima works. Help.
>
> Thanks! Akshay
>
> P.S: Is there a good reason why parts of Maxima still has
> (exploden ..) instead of using proper strings ?
> _______________________________________________ Maxima mailing
> list Maxima at math.utexas.edu <mailto:Maxima at math.utexas.edu>
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
>