Page 1 of 1

complex arithmetics

PostPosted: Thu Sep 11, 2014 10:09 pm
by thefausap
Hello all,

attached there's a "library" for newlisp to perform mathematical operations with complex numbers.
Such numbers are created with a list with the format : '(re im).
I added and tested all the included functions.

I also created a library for complex matrices determinant. The matrices needs to be written in column-major order.
I borrowed the permutation code from the newlisp web site and I added the function to calculate the parity of permutation.

Please feel free to test it and comment it.
This is my first newlisp program, and I'm sure there're a lot to improve.

Regards

Re: complex arithmetics

PostPosted: Thu Sep 18, 2014 1:23 pm
by kosh
There is a point to problem.

Code: Select all
> (constant '*2pi* (mul 3.14159265358979323846264338327950288419716939937510 2))
3.159714599e+020
> (read-expr "(mul 3.14159265358979323846264338327950288419716939937510 2)")
(mul 3.141592653589793115997963468544185161590576171875 50288419716939937510L 2)


newlisp parser reads too long a numeric value (or symbol) without no warning.
Therefore, it is better to avoid such writing.

I recommend PI defined as:
Code: Select all
(constant 'pi (mul 4 (atan 1)))  ; or short written 3.141592654


P.S. I tried writing a mandelbrot set. like this http://www.newlisp.org/complex.cgi
Code: Select all
#!/usr/bin/env newlisp

(load "complex_lib.lsp")
 
(for (y -1 1.1 0.08)
  (for (x -2 1 0.04)
    (letn ((c 126)
           (z (cmplx x y))
           (a z))
      (while (and (begin
                    (setq z (cadd (cmul z z) a))
                    (< (abs (cmod z)) 2))
                  (< 32 (dec c))))
      (print (char c))))
  (println))
 
(exit)

Re: complex arithmetics

PostPosted: Thu Sep 18, 2014 8:26 pm
by Lutz
Currently decimal-point numbers are limited to 32 characters including the decimal point and sign. Which is about double the digits precision which internally can be distinguished in IEEE-754 floating point numbers.

This character limit will be increased in the next version.