Arch Linux

2011-02-02

How to work with Complex Numbers in KAlgebra

KAlgebra is a calculator with symbolic and analysis features that lets you plot 2D and 3D functions as well as to easily calculate mathematical expressions.

I've already talked about KAlgebra before

However, it still can't work with complex numbers, or at least out of the box.
You can define some functions this way:
c:=(a, b)->vector { a, b }

Now we can enter this:

c(1,2)

And we are going to assume it's 1 + 2 i

At this point we just have to define some basic operations. Since we are working with complex numbers in vectors, sums and substractions work without further worries:

c(1, 2)+c(3, 1)
= vector { 4, 3 }
We can define the norm this way:

normtest:=c->root(selector(1, c)^2+selector(2, c)^2, 2)

And the conjugate:

conj:=v->c(selector(1, v), -selector(2, v))

Or with a more generic approax, to work with multi-dimensional vectors:

norm:=v->root(sum(i^2:i@v), 2)

We can even define the argument of a complex number:

arg:=c->arctan(selector(2, c)/selector(1, c))

And of course, convert it to it's polar form:

polar:=v->c(norm(v), arg(v))

And we are going to need the reverse operation:

rect:=v->c(selector(1, v)*cos(selector(2, v)), selector(1, v)*sin(selector(2, v)))

Here it comes the hardest part, multiplications and divisions. We can define them in two different ways, directly:

(a + b·i) · (c + d·i) = a·c - b·d + (a·d + b·c)·i
(a + b·i) / (c + d·i) = (a + b·i) · (c - d·i) / ||(c + d·i)||²

Then, we would code the product and the division as:

cprod:=(u, v)->c(selector(1, u)*selector(1, v)-selector(2, u)*selector(2, v), selector(1, u)*selector(2, v)+selector(2, u)*selector(1, v))
cdiv:=(u, v)->cprod(u, conj(v))/norm(v)^2

The other way would be converting both complex numbers to their polar form, and simply multiply or divide the modulus and sum or substract the arguments, depending on what we are doing, and then convert them to the binomical form again. The syntax looks a little stranger, though:

cprodtest:=(u, v)->rect(c(selector(1, polar(u))*selector(1, polar(v)), selector(2, polar(u))+selector(2, polar(v))))
cdivtest:=(u, v)->rect(c(selector(1, polar(u))/selector(1, polar(v)), selector(2, polar(u))-selector(2, polar(v))))

I think we are done now! We are able to work with complex numbers in KAlgebra. I suggest to put al this functions on a file, and then load it with KAlgebra whenever we want support for complex numbers.

I hope you find this helpful :)

3 comments:

Anonymous said...

Hi i am kavin, its my first time to commenting anywhere,
when i read this paragraph i thought i could also make comment due to this sensible post.


my page ... vues youtube

Anonymous said...

I'm excited to uncover this page. I wanted to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and I have you book-marked to check out new information on your site.

my web blog; Achat Retweet pas cher
My page > plus de tweet

Anonymous said...

AArgghh, I am in Nirvana! This post is the best post in all the world!!!

P.S.
Lobachevsky rulezz!



P.S.2
Apparently this is a joke comment in responce to previous two comments.