Arc Forumnew | comments | leaders | submitlogin
Implementation of trigonometric functions and basic operations on complex numbers
4 points by wgac 5707 days ago | 4 comments

    (= e 2.718281828459045235360287471352662497757247093699)
    (= pi 3.14159265358979323846264338327950288419716939937)
    
    (def tocons (z)
         (coerce (coerce z 'string) 'cons))
    
    (def toint (z)
         (coerce (coerce z 'string) 'int))
    
    ;;Realis of complex number z
    (def rez (z)
      (= lz (tocons z))
      (= g (or (pos #\+ lz) (pos #\- lz)))
      (toint (firstn g lz)))
    
    ;;Imaginaris of complex number z
    (def imz (z)
      (= lz (tocons z))
      (= g (or (pos #\+ lz) (pos #\- lz)))
      (toint (if (pos #\i lz) (rem #\i (nthcdr g lz)) 0)))
    
    ;;Modulus of complex number z
    (def modz (z)
         (sqrt (+ (expt (rez z) 2)
         	(expt (imz z) 2))))
    
    ;;These functions return complex numbers.
    ;;They are less efficient when used with reals.
    (def sin (x)
         (/ (- (expt e (* x +i)) (expt e (* x -i))) +2.0i))
    
    (def cos (x)
         (/ (+ (expt e (* x +i)) (expt e (* x -i))) 2.0))
    
    (def tan (x)
         (/ (sin x) (cos x)))
    
    (def cot (x)
         (/ (cos x) (sin x)))
    
    ;;The following functions return real numbers.
    (def rsin (x)
      (imz (expt e (* 0+1.i x))))
    (def rcos (x)
      (rez (expt e (* 0+1.i x))))
    (def rtan (x) 
      (/ (rsin x) (rcos x)))
    (def rcot (x)
      (/ (rcos x) (rsin x)))
Enjoy! Any feedback is welcome. You may contact me at wgac@wp.pl


2 points by wgac 5699 days ago | link

Ok. So I've looked at the rez and imz and found the solution to the problem with scientific notation. Here are my results:

    ;;Realis of complex number z
    (def rez (z)
      (with (lz (tocons z) i 0 j 0)
      (until (and (isnt (lz j) #\e) (or (is (lz i) #\+) (is (lz i) #\-))) 
           	  (if (is i 0) (++ i) (do (++ i) (++ j))))
      (toint (firstn i lz))))

    ;;Imaginaris of complex number z
    (def imz (z)
      (with (lz (tocons z) i 0 j 0)
      (until (and (isnt (lz j) #\e) (or (is (lz i) #\+) (is (lz i) #\-))) 
           	  (if (is i 0) (++ i) (do (++ i) (++ j))))    
      (toint (if (pos #\i lz) (rem #\i (nthcdr i lz)) 0))))
I've also written zbar which returns complex conjugate of a number:

    ;;Complex conjugate of number z.
    (def zbar (z)
         (toint (join (tocons (rez z)) 
         	      (if (> (imz z) 0) '(#\-) '(#\+)) 
		      (tocons (abs (imz z))) 
		      '(#\i))))
And once more thank you, conanite.

-----

2 points by conanite 5707 days ago | link

Awesome work. Watch out for the numerous number formats scheme allows -

  1.3e-32-4.3e+56i
  3/8-4.3e+56i
I can attest that it's hell to parse these numbers - and imz returns 56 in each of these cases. You might need to use real-part and imag-part from scheme if you don't want to write a complex number parser in arc :)

-----

1 point by wgac 5698 days ago | link

I found some errors in the recent post. Here is the (I hope so) final version or rez and imz. Enjoy :)

    ;;Realis of complex number z
    (def rez (z)
         (with (lz (tocons z) i 0 j 0)
         (until (or (is i (len lz))
                    (and (isnt (lz j) #\e)
                         (or (is (lz i) #\+)
                             (is (lz i) #\-)))) 
            (if (is i 0) (++ i) (do (++ i) (++ j))))
            (toint (firstn i lz))))

    ;;Imaginaris of complex number z
    (def imz (z)
         (with (lz (tocons z) i 0 j 0)
         (until (or (is i (len lz))
         	    	(and (isnt (lz j) #\e)
	    	     (or (is (lz i) #\+)
	    	     	 (is (lz i) #\-)))) 
    	(if (is i 0) (++ i) (do (++ i) (++ j))))
    	(toint (if (pos #\i lz) (rem #\i (nthcdr i lz)) 0))))

-----

1 point by wgac 5701 days ago | link

Thanks for the feedback, conanite :) I think I know how to cope with that problem. I'll post my results as soon as I have some spare time. I'm also planning to successively add more elaborate mathematical functions. Maybe some numerical integration or solving differential equations.

-----