Using Lisp complex
- Subject: Using Lisp complex
- From: R Fateman
- Date: Sun, 31 Jul 2011 04:50:22 -0700
I haven't read your code in any detail, or tried it, but off the top of
my head, here are some cases to worry about.
(I am traveling..)
converting complex with zero imag part to a real.
matching a+b*%i to 3+5*%i
substituting x for 3 in 3+5*%i
substituting x for %i
bfloat(complex)
adding bigfloat to complex
multiplying bigfloat etc.
sin
cos
bessel_j
abs, cabs
sqrt
x^complex,
complex^complex
plot
realpart, imagpart
>, <
RJF
On 7/30/2011 4:18 PM, Robert Dodier wrote:
> OK, I've pushed the following patch which changes several
> instances of (AND (NUMBERP FOO) (MINUSP FOO)) to use
> REALP instead of NUMBERP. This is enough to make Maxima
> happy with some simple operations on Lisp complex numbers.
>
> My guess is that complex numbers should be converted to
> Maxima a + b*%i expressions, likewise rationals to ((RAT) ...).
> I hope this simple patch helps in the meantime.
>
> best,
>
> Robert Dodier
>
> PS. Here are some examples of Lisp complexes and rationals in Maxima.
>
> (%i3) display2d : true
> (%o3) true
> $Z(%i4) z
> (%o4) #C(5 3)
> (%i5) 10 z
> (%o5) #C(50 30)
> (%i6) z + 10
> (%o6) #C(15 3)
> (%i7) a z
> (%o7) #C(5 3) a
> 2
> (%i8) z
> (%o8) #C(16 30)
> n
> (%i9) z
> n
> (%o9) #C(5 3)
> $R(%i10) z r
> (%o10) #C(35/11 21/11)
> (%i11) b r
> (%o11) 7/11 b
> (%i12) b r + a z
> (%o12) 7/11 b + #C(5 3) a
> (%i13) L : [1, 2, x, %e, r, z]
> (%o13) [1, 2, x, %e, 7/11, #C(5 3)]
> (%i14) apply(+, L)
> (%o14) x + %e + #C(95/11 3)
> (%i15) apply(*, L)
> (%o15) #C(70/11 42/11) %e x
> (%i16) display2d : false
> (%o16) false
> $Z
> (%i17) z
> (%o17) #C(5 3)
> (%i18) 10*z
> (%o18) #C(50 30)
> (%i19) z+10
> (%o19) #C(15 3)
> (%i20) a*z
> (%o20) #C(5 3)*a
> (%i21) z^2
> (%o21) #C(16 30)
> (%i22) z^n
> (%o22) #C(5 3)^n
> $R
> (%i23) z*r
> (%o23) #C(35/11 21/11)
> (%i24) b*r
> (%o24) 7/11*b
> (%i25) b*r+a*z
> (%o25) 7/11*b+#C(5 3)*a
> (%i26) L:[1,2,x,%e,r,z]
> (%o26) [1,2,x,%e,7/11,#C(5 3)]
> (%i27) apply("+",L)
> (%o27) x+%e+#C(95/11 3)
> (%i28) apply("*",L)
> (%o28) #C(70/11 42/11)*%e*x
>
> PPS. Here's the input file.
>
> display2d : true;
> :lisp (defparameter $z #C(5 3))
> z;
> 10 * z;
> 10 + z;
> a * z;
> z^2;
> z^n;
> :lisp (defparameter $r (/ 7 11))
> z * r;
> b * r;
> a * z + b * r;
> L : [1, 2, x, %e, r, z];
> apply ("+", L);
> apply ("*", L);
>
> display2d : false;
> :lisp (defparameter $z #C(5 3))
> z;
> 10 * z;
> 10 + z;
> a * z;
> z^2;
> z^n;
> :lisp (defparameter $r (/ 7 11))
> z * r;
> b * r;
> a * z + b * r;
> L : [1, 2, x, %e, r, z];
> apply ("+", L);
> apply ("*", L);
>
>
> PPPS. Here's the patch.
> $ git diff 67e75d833e6dcc4e52296977b919698dfd156d4a
> 4cc0818b3229e41121d5a2680ad228b1b0b7350c
> diff --git a/src/compar.lisp b/src/compar.lisp
> index 26a597d..70df54a 100644
> --- a/src/compar.lisp
> +++ b/src/compar.lisp
> @@ -1339,7 +1339,7 @@ relational knowledge is contained in the default
> context GLOBAL.")
> (defun signdiff-special (xlhs xrhs)
> ;; xlhs may be a constant
> (let ((sgn nil))
> - (when (or (and (numberp xrhs) (minusp xrhs)
> + (when (or (and (realp xrhs) (minusp xrhs)
> (not (atom xlhs)) (eq (sign* xlhs) '$pos))
> ; e.g. sign(a^3+%pi-1) where a>0
> (and (mexptp xlhs)
> diff --git a/src/matrun.lisp b/src/matrun.lisp
> index 1c7a2bc..4788ec4 100644
> --- a/src/matrun.lisp
> +++ b/src/matrun.lisp
> @@ -140,7 +140,7 @@
> (setq e (reverse (cdr e))) (go b)))
> a (setq e (cdr e))
> b (cond ((null e)
> - (return (if (and (numberp expon) (minusp expon)) 1 0)))
> + (return (if (and (realp expon) (minusp expon)) 1 0)))
> ((and (mexptp (car e)) (alike1 expon (caddar e)))
> (return (cadar e))))
> (go a)))
> diff --git a/src/nforma.lisp b/src/nforma.lisp
> index 140c661..754299a 100644
> --- a/src/nforma.lisp
> +++ b/src/nforma.lisp
> @@ -25,7 +25,7 @@
>
> (defmfun nformat (form)
> (cond ((atom form)
> - (cond ((and (numberp form) (minusp form)) (list '(mminus) (- form)))
> + (cond ((and (realp form) (minusp form)) (list '(mminus) (- form)))
> ((eq t form) (if in-p t '$true))
> ((eq nil form) (if in-p nil '$false))
> ((and displayp (car (rassoc form aliaslist :test #'eq))))
> diff --git a/src/optim.lisp b/src/optim.lisp
> index 8fe3aab..47d6eff 100644
> --- a/src/optim.lisp
> +++ b/src/optim.lisp
> @@ -58,10 +58,10 @@
> (defun opmexpt (x)
> (let ((*base (opformat (cadr x))) (exp (opformat (caddr x))) xnew negexp)
> (setq negexp
> - (cond ((and (numberp exp) (minusp exp)) (- exp))
> + (cond ((and (realp exp) (minusp exp)) (- exp))
> ((and (ratnump exp) (minusp (cadr exp)))
> (list (car exp) (- (cadr exp)) (caddr exp)))
> - ((and (mtimesp exp) (numberp (cadr exp)) (minusp (cadr exp)))
> + ((and (mtimesp exp) (realp (cadr exp)) (minusp (cadr exp)))
> (if (equal (cadr exp) -1)
> (if (null (cdddr exp)) (caddr exp)
> (cons (car exp) (cddr exp)))
> diff --git a/src/rat3a.lisp b/src/rat3a.lisp
> index 38c4e56..229629b 100644
> --- a/src/rat3a.lisp
> +++ b/src/rat3a.lisp
> @@ -341,7 +341,7 @@
> (cdr l)))))
>
> (defmfun pminusp (p)
> - (if (numberp p) (minusp p)
> + (if (realp p) (minusp p)
> (pminusp (p-lc p))))
>
> (defmfun pminus (p)
> diff --git a/src/simp.lisp b/src/simp.lisp
> index 7335819..1b2e262 100644
> --- a/src/simp.lisp
> +++ b/src/simp.lisp
> @@ -235,7 +235,7 @@
> (defmfun mmminusp (x) (and (not (atom x)) (eq (caar x) 'mminus)))
>
> (defmfun mnegp (x)
> - (cond ((numberp x) (minusp x))
> + (cond ((realp x) (minusp x))
> ((or (ratnump x) ($bfloatp x)) (minusp (cadr x)))))
>
> (defmfun mqapplyp (e) (and (not (atom e)) (eq (caar e) 'mqapply)))
> @@ -2194,10 +2194,10 @@
> (return res))
> ((eq gr '$%i)
> (return (%itopot pot)))
> - ((and (numberp gr) (minusp gr) (mevenp pot))
> + ((and (realp gr) (minusp gr) (mevenp pot))
> (setq gr (- gr))
> (go cont))
> - ((and (numberp gr) (minusp gr) (moddp pot))
> + ((and (realp gr) (minusp gr) (moddp pot))
> (return (mul2 -1 (power (- gr) pot))))
> ((and (equal gr -1) (maxima-integerp pot) (mminusp pot))
> (setq pot (neg pot))
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima