Complex Arithmetics

Let it all hang out

Complex Arithmetics

PostPosted by manandpc » Tue Nov 13, 2012 8:35 am

This can be usefull for an implementation of complex arithmetics using base formulas to evalute base functions.


algorithms :
X = a + bi
Y = c + di
X + Y = (a + c) + i x (b + d)
X - Y = (a - c) + i x (b - d)
X x Y = (ac - bd) + i x (ad + bc)
X / Y = [(ac + bd) / (cc + dd)] + i x [(bc - ad) / (cc + dd)] , Y != 0
Y ^ X = e^(X x ln(Y)) , Y != 0
log[Y]X = ln(X) / ln(Y) , X != 0, Y != 0 , ln(Y) != 0
Y ^ (1/X) = e^(ln(Y) / X) , X != 0 , Y != 0


algorithms :
X = a + bi
Polar Form of X :
r = sqrt( (a x a) + (b x b) )
@ in (-PI/2 <= @ <= 3xPI/2)
@ = atan2( b / a ) , a != 0
@ = PI / 2 , a == 0 , b > 0
@ = - PI / 2 , a == 0 , b < 0
X ^ 2 = r^2 x ( cos(2@) + i x sin(2@) )
X ^ 1/2 = r^1/2 x ( cos(@/2) + i x sin(@/2) )
1 / X = 1 / ( a + bi ) = a / r^2 - i x b / r^2 , X != 0
e ^ X = e^a x cos(b) + i x e^a x sin(b)
ln(X) = ln(r) + i@ , X != 0


algorithms :
X = a + bi
sin(X) = (e^iX - 1/e^iX) / 2i , e^iX != 0
cos(X) = (e^iX + 1/e^iX) / 2 , e^iX != 0
tan(X) = sin(X) / cos(X) , cos(X) != 0
asin(X) = ln( iX + sqrt(1 - X^2) ) / i
acos(X) = ln( X + i x sqrt(1 - X^2) ) / i
atan(X) = ln( (1 + iX) / (1 - iX) ) / 2i , (1+iX) != 0 , (1-iX) != 0

Reference: Texas Instruments TI58-59 , Master Library (TI58-59-MasterLibraryManual.pdf) , page 18-23

algorithms :
X = a + bi
sinh(X) = (e^X - 1/e^X) / 2 , e^X != 0
cosh(X) = (e^X + 1/e^X) / 2 , e^X != 0
tanh(X) = sinh(X) / cosh(X) , cosh(X) != 0
asinh(X) = ln( X + sqrt(1 + X^2) )
acosh(X) = ln( X + ( sqrt(X + 1) x sqrt(X - 1) ) )
atanh(X) = ( ln(1 + X) - ln(1 - X) ) / 2 , (1+X) != 0 , (1-X) != 0

Posts: 67
Joined: Tue Jun 19, 2012 9:39 am

Re: Complex Arithmetics

PostPosted by forummaster » Mon Oct 20, 2014 4:42 pm

Thanks for the references. Soon I'll be implementing Complex datatype and can use these.

Thanks again.
Posts: 567
Joined: Wed Jan 23, 2013 1:00 pm

Re: Complex Arithmetics

PostPosted by FDA OSInet » Sat Sep 19, 2015 8:06 pm

"Soon I'll be implementing Complex datatype"

I guess the work had to be postponed. To be honest, while complex numbers may be useful in a couple of cases, lack of associative indexing is now what sets APL apart from more commonplace languages, and unfortunately not for its good.
Posts: 29
Joined: Sun Aug 30, 2015 5:07 am

Re: Complex Arithmetics

PostPosted by Robert Wallick » Thu Sep 24, 2015 9:19 am

Thank you for joining NARS2000 and also posting to the BB. Very good hearing from you!

Re: >> "Soon I'll be implementing Complex datatype"

I have had indications from Bob Smith that he has been working on complex datatyping, ref. Bob's NARS2000 wiki newly created System Function Quad DC (which converts R between the coefficients of a Hypercomplex number and the corresponding Hypercomplex number), at link and which he created and posted 8/21/15, barely one month ago.

Re: >> "lack of associative indexing is now what sets APL apart from more commonplace languages, and unfortunately not for its good."

Thank you very much for posting your thoughts on the topic of associative indexing. Associative Indexing can be somewhat of an esoteric programming topic, although in principle it can mean something like, "as we, or as people, may naturally think" - how the human brain itself normally functions.

Are you perhaps [also] referring to Artificial Intelligence or AI? Could you perhaps please give an example(s) of how you might like APL/NARS to work and or respond - with respect to Associative Indexing? I have programmed with multiple languages and I cannot say I prefer any of them to APL. Yes I still prefer APL to other languages; however, every mind is also unique.

Have a nice week. Thanks again for joining, visiting, presumably trying out NARS and commenting,
Robert Wallick
Robert Wallick
Posts: 22
Joined: Thu Feb 05, 2015 5:35 pm

Re: Complex Arithmetics (and associative arrays)

PostPosted by FDA OSInet » Sun Sep 27, 2015 3:02 pm

Note : I do not know what happens for any of you, dear readers, but on this post I can see APL characters correctly only when using Firefox, not Edge nor Opera nor Chrome. I suggest you use Firefox if anything is wrong for you too.

Why associative-indexed arrays, like in so many other languages ?

In many applications we have to associate various contents to various strings for very commonplace purposes. This can be done for instance in Perl that way :
Code: Select all
$state{"AZ"} = "Arizona";
$capital{"AZ"} = "Phoenix";

In Perl, this has to be specified by {} and not by [] because Perl would not distinguish well otherwise whether we mean the string "0" or the number 0 (not a bug in Perl, though, but a feature ! ;-) ). However Javascript allows quite well to index with strings as well as with integers using regular brackets. Il shall steal this line from somewhere:
Code: Select all
var cars = ["Saab", "Volvo", "BMW"];

Could APL do it without any perturbation of either existing code or standard APL thinking ? Perhaps yes.
Code: Select all
      capital['USA' 'China' 'Russia']←'Washington' 'Beijing' 'Moscow'

does not seem THAT shocking, looks rather understandable, and can help in a lot of everyday problems, from grocery lists to remembering FM channels and the like.

Using such information seems to comply nicely with classic APL habits concerning arrays (I cannot see APL characters correctly here except in Firefox) :
Code: Select all
      capital[2 1⍴'USA' 'China']

Just that (OK, it's probably much easier said than done) would make APL eligible for a lot of applications presently developed in Perl, Javascript or others. Those languages are chosen only because associating data to strings within the language itself (and in very readable way) is needed. It is the case almost everywhere nowadays : nobody wants to code human-friendly information into numbers anymore. It is a pity that APL does not allow associative indexing as a basic (no pun intended) language feature because those language do not offer the useful persistence of data between program invocations offered by APL workspaces.

What about going a little further ? (not mandatory at all, but.)

Of course, things might get more complicated. Could an array have mixed indexing, integer AND associative ? What is its rho in such a case ? What should reduction and scan become ? Work on the numerically indexed part only ? Would then some expected equalities be jeopardized, like :

(rho +\x) <===> (rho x) ?

Just like a complex number has a "real" and sometimes an "imaginary" part, should a general array in APL be considered as having an integer indexed and SOMETIMES a string-indexed part, everything behaving as ever whenever the string-indexed part is non-existent (just as a complex number in regular operations when its "imaginary" part is 0 ?).

Longer-term dreams (?)

Hmm... could we dream a little, by imagining possible APL operations as well ? (though not at all priorities, just experiences of thought)

Transposition : given an associative array specifying Y as a function of X (for instance capital given the country name), yields an associative array of the same size giving X as a function of Y (here, country according to the capital). The regular APL transposition symbol ⍉ could be used. However I do not see clearly what should happen whenever there is a collision (say Paris, France and Paris, Texas, if Dallas was called Paris). Domain error?

Also I do not see clearly either what should happen with rank 2 or more associative arrays, if authorized.

Well, I guess that is what the "R" in "NARS" stands for ;-)

Note that if all these issues could be properly solved in the long run, then APL would probably become eligible as a possible SQL replacement. Only much better.
Posts: 29
Joined: Sun Aug 30, 2015 5:07 am

Re: Complex Arithmetics

PostPosted by forummaster » Fri Oct 30, 2015 9:00 pm

Back on the original topic of Complex arithmetic, there is an alpha version of this (along with Quaternion and Octonion datatypes) in You will also need to read the paper I wrote for the last Minnowbrook APL Implementor's Workshop in on "Hypercomplex Numbers In APL".
Posts: 567
Joined: Wed Jan 23, 2013 1:00 pm

Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 0 guests