# [Maxima] Bad formed Maxima expressions

Dieter Kaiser drdieterkaiser at web.de
Tue Nov 17 15:28:08 CST 2009

```I have written a test function to detect bad simplified Maxima
expressions. The idea is that a well simplified or well formed Maxima
expression does not have any nested unsimplified subexpression.

The arguments are a FORM and a FLAG which indicates, if we expect a
simplified or an unsimplified expression. The return value is T for a
well formed and NIL for a bad formed expression.

(defun well-formed-p (form flag)
(let ((expr (specrepcheck form)))
(cond ((atom expr))
((and (listp (car expr))
(member (caar expr) '(mdefine lambda))))
((and flag
(listp (car expr))
(member 'simp (car expr)))
(do ((l (cdr expr) (cdr l)))
((null l) t)
(when (not (well-formed-p (car l) t))
(return nil))))
((and (not flag)
(listp (car expr)))
(cond ((member 'simp (car expr))
(do ((l (cdr expr) (cdr l)))
((null l) t)
(when (not (well-formed-p (car l) t))
(return nil))))
(t t))))))

Examples for well formed expressions:

'((mplus) a b ((mtimes) x y z))           --> T
'((mplus) a b ((mtimes simp) x y z))      --> T
'((mplus simp) a b ((mtimes simp) x y z)) --> T

Example for a bad formed expression:

'((mplus simp) a b ((mtimes) x y))        --> NIL

Expressions with lambda and mdefine are excluded from the test.

I have done a first check for the whole testsuite and I have counted
is not too much, because I have counted every call to the simplifier and
some expressions go very often through the simplifier. Furthermore, the
simplifier is called about 10^7 times from the testsuite.

A lot of bad expressions are harmless, e.g. contain unsimplified
rational constants. Other expressions contain unsimplified trig
expressions. I am not sure if nested subexpressions which contain a
RATSIMP flag, but not a SIMP flag are well formed, e.g.

((MEXPT SIMP) \$%E ((MTIMES RATSIMP) -1 \$%I \$T))

Some more examples for bad formed expressions from the testsuite:

Error 226:
((MTIMES SIMP) ((MEXPT SIMP) 2 ((RAT SIMP) 1 2))
((MEXPT SIMP) \$%PI ((RAT SIMP) -1 2)) ((MEXPT SIMP) \$X ((RAT SIMP) -1
2))
((%SINH) \$X))

Error 503:
((%INTEGRATE SIMP)
((MTIMES SIMP) ((MEXPT SIMP) \$X -4)
((MEXPT SIMP) ((%SIN) ((MTIMES SIMP) \$A \$X)) 7))
\$X 0 \$INF)

Error 922:
((MTIMES SIMP) ((MEXPT SIMP) 4 ((MTIMES SIMP) ((RAT SIMP) 1 2) \$V))
((%BESSEL_J) \$V \$Z) ((%GAMMA SIMP) ((MPLUS SIMP) 1 \$V))
((MEXPT SIMP) \$Z ((MTIMES SIMP) -1 \$V))
((MEXPT SIMP) \$%E ((MTIMES SIMP) \$%I \$Z)))

Error 1748:
((\$CONJUGATE SIMP) ((%LOG) \$X))

Error 2732:
((MNCEXPT SIMP) ((MNCTIMES) \$A \$B) 2)

Error 2990:
((MPLUS SIMP)
((MTIMES SIMP) ((RAT) 4 3) ((MEXPT SIMP) \$%PI ((RAT SIMP) 1 2))
((%ERFC SIMP) ((MEXPT SIMP) \$Z ((RAT SIMP) 1 2))))
((MTIMES SIMP) -1 ((MEXPT SIMP) \$Z ((RAT SIMP) -3 2))
((MPLUS SIMP) ((RAT SIMP) -2 3) ((MTIMES SIMP) ((RAT SIMP) 4 3) \$Z))
((MEXPT SIMP) \$%E ((MTIMES SIMP) -1 \$Z))))

I will have a look at the different reasons for the simplification
errors to reduce the number of bad formed expressions.

Dieter Kaiser

```