Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Complex Number Library

Author
Message
satcom4fun
14
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: North of Salt Lake City, Utah, USA
Posted: 24th Jul 2011 00:19
I am new to DBPro and I have read about half of the Hands on DBPro, Vol. 1. I decided to try to create a complex number library to take the language for a spin. In other languages, I would normally use a User-Defined data type (UDT) for the complex number have a real and imaginary field. Then I would write a class or set of functions that would allow complex math as well as complex matrix math. This would required return of complex numbers (UDT) from functions, return of arrays of complex numbers (UDT) from functions, and operator overloading. However, these techniques will not work in DBPro because you cannot overload operators, return a user-defined data type, or an array of user-defined data types from a function. Defining all of the UDT variables as global will work but there are times when it would be nice to keep a local variable. So, I used the following technique: Anytime I define a UDT, I define a global return variable used to return values from functions. I then set a local variable equal to the value of the global variable so that the global can be reused. My first attempt at the library is here.



Any suggestions for improvement are welcome.

Life is not a dream
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 24th Jul 2011 14:36
generally I've found that any kind of complicated math in DBPro is best multiplied out first, so instead of creating a library to achieve your goal, you code on a case by case basis. So if you, say, wanted to calculate 2*Z*(1+2i), you would just say:
2*Z*(1+2i)=
=2*(Zr+Zim*i)*(1+2*i)
=2*(Zr-2*Zim+i(Zim+2*Zr))

and so, in DBPro:
r=2*Zr-4*Zim
im=2*Zim+4*Zr

And so instead of saying:

you just say:


The easiest to use library I could think of would probably be something where a program parses a string like "RESULT=Z+R+Q*(a+bi)", and then accepts a string containing a list of all the needed values. So to use that, you would say something like:


That of course would require a lot more code for string manipulation and string parsing.


Why does blue text appear every time you are near?
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 24th Jul 2011 16:06
I found that 2D vectors work very well for complex representations as well (X-coordinate is real part and Y-coordinate is imaginary part). Adding and subtracting vectors are exactly the same as complex add and subtract. Complex multiplication doesn't have a default command for it, but can be easily added using the expression:


The same method can be applied for div and conj. Abs is the same as length vector2(), arg can be found using atanfull(), etc.
Of course, it all depends on what you fancy. You'll need to balance out what works best for your type of application.

Cheers!
Sven

satcom4fun
14
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: North of Salt Lake City, Utah, USA
Posted: 25th Jul 2011 05:08
Thanks for the two suggestions from Neuro Fuzzy. If there are not alot of complex number calcs then simply multiplying out the calculation would certainly be more efficient and another global variable may not be needed. It would also be nice to have a general equation evaluator library using a string argument for the equation and value arguments. It would come in handy for calculator applications.

Here is the code with all local variables and no functions.


Thanks also to Sven B for pointing out that vector functions are available that can be used to perform complex number math so a new library may not be necessary. One question: How do you make an array of vectors to create a complex number matrix?



Life is not a dream
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 27th Jul 2011 16:27
I you wish to make a matrix with complex numbers, then using UDT's might be more straight forward. In any case, the most simple workaround would be:



satcom4fun
14
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: North of Salt Lake City, Utah, USA
Posted: 27th Jul 2011 21:25
Good workaround. This discussion should be useful to anyone needing an array of 2D or 3D vectors. UDT's allow the code to be written using real(.Re) and imaginary(.Im) terminology rather than x and y fields of a vector. Not sure it is worth sacrificing the reuse of proven vector algorithms in DBPro to get different math terminology.

Life is not a dream
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 27th Jul 2011 23:29
The only real advantage with using vectors is getting the length of the vector/magnitude of the complex number (sqrt() is a bit slow) and the fact that you don't have to worry too much about using a global complex variable to return from a function. I discovered the vector way when coding a Julia Fractal and Mandelbrot generator which uses the magnitude quite a lot (eg. for checking convergence).

In any case, if readability is more important I would encourage your way, especially if vectors are also used for other purposes than representing complex numbers.

Cheers!
Sven B

Login to post a reply

Server time is: 2026-07-10 17:18:50
Your offset time is: 2026-07-10 17:18:50