complex arithmetics

Featuring the Dragonfly web framework
Post Reply
Posts: 2
Joined: Tue Sep 09, 2014 11:33 pm

complex arithmetics

Post 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.

test file for both of them
(377 Bytes) Downloaded 197 times
permutations library
(1.14 KiB) Downloaded 200 times
complex number library
(966 Bytes) Downloaded 204 times

Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan

Re: complex arithmetics

Post by kosh »

There is a point to problem.

Code: Select all

> (constant '*2pi* (mul 3.14159265358979323846264338327950288419716939937510 2))
> (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

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))))

Posts: 5285
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: complex arithmetics

Post 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.

Post Reply