Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:31 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Complex numbers

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9122
Posted: 07:03pm 22 Nov 2023
Copy link to clipboard 
Print this post

For fun I've added a complete implementation of complex numbers to a test PicoMite build


PicoMite.zip






In this implementation complex numbers have a 32-bit real and 32-bit imaginary part and to make this work in MMBasic I am using integers (64-bit) to hold these

Supported functions are:

complex% = MATH(C_CPLX r!, i!) 'create a complex

real! = MATH(C_REAL complex%)
imag! = MATH(C_IMAG complex%)
arg! = MATH(C_ARG complex%)

complex1% = MATH(C_ADD complex2%,complex3%)
complex1% = MATH(C_SUB complex2%,complex3%)
complex1% = MATH(C_MUL complex2%,complex3%)
complex1% = MATH(C_DIV complex2%,complex3%)
complex1% = MATH(C_POW complex2%,complex3%)

complex1% = MATH(C_CONJ complex2%)
complex1% = MATH(C_SIN complex2%)
complex1% = MATH(C_COS complex2%)
complex1% = MATH(C_TAN complex2%)
complex1% = MATH(C_ASIN complex2%)
complex1% = MATH(C_ACOS complex2%)
complex1% = MATH(C_ATAN complex2%)
complex1% = MATH(C_SINH complex2%)
complex1% = MATH(C_COSH complex2%)
complex1% = MATH(C_TANH complex2%)
complex1% = MATH(C_ASINH complex2%)
complex1% = MATH(C_ACOSH complex2%)
complex1% = MATH(C_ATANH complex2%)
complex1% = MATH(C_PROJ complex2%)


I don't pretend to understand all of these so you will need to google details of complex.h for more information.

There is no protection if you use the wrong datatypes for the functions.

If there is proper interest I may include these in the main firmware but only if there is a demonstrable use-case
Edited 2023-11-23 05:15 by matherp
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6099
Posted: 07:39pm 22 Nov 2023
Copy link to clipboard 
Print this post

I don't use complex numbers much but that is probably because I would have to do it myself using arrays.

I will have a play.

What I would make use of is converting from rectangular to polar and the reverse.
Average wind etc is one real life use.

Take the instantaneous wind bearing and speed (Polar) and convert this to rectangular before averaging then convert back to polar for display.

Jim
VK7JH
MMedit   MMBasic Help
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1109
Posted: 09:05pm 22 Nov 2023
Copy link to clipboard 
Print this post

That's pretty cool.

There's one other base function, modulus! = MATH(C_MOD complex%) which returns the length of the complex number - ie root(x^2 + y^2)
Visit Vegipete's *Mite Library for cool programs.
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 396
Posted: 09:42pm 22 Nov 2023
Copy link to clipboard 
Print this post

I also think that is pretty cool, as an EE I always think of things like
in an alternating current circuit with the components resistor R, capacitor C and coil L, the current I is equal to one
certain phase shift φ to the applied voltage U.

Complex numbers prove to be particularly suitable for describing this situation. But of course other than real EE and physics things I am not aware of a specific usecase... Hm!

But I like the idea of the ultimate math implementation in MMBASIC...   So that you can do almost everything you want to.

Greetings
Daniel
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 09:51pm 22 Nov 2023
Copy link to clipboard 
Print this post

How big can the firmware get?
Perhaps a repository of Subs / CSubs for seldom used items that can be added as needed.
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 396
Posted: 10:39pm 22 Nov 2023
Copy link to clipboard 
Print this post

phil99, this is a great idea, too!
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9122
Posted: 08:07am 23 Nov 2023
Copy link to clipboard 
Print this post

  Quote  phil99, this is a great idea, too!


No its not. Not easy to do and then you end up like python having to assemble a program from lots of include libraries that you can't remember the name of - yuck
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9122
Posted: 08:11am 23 Nov 2023
Copy link to clipboard 
Print this post

I've added the following:

complex1% = MATH(C_POLAR radius!, angle!) 'NB: respects OPTION ANGLE
mod! = MATH(C_MOD complex%)
phase! = MATH(C_PHASE complex%) 'NB: respects OPTION ANGLE


PicoMite.zip
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4041
Posted: 09:52am 23 Nov 2023
Copy link to clipboard 
Print this post

  matherp said  
  Quote  phil99, this is a great idea, too!


No its not. Not easy to do and then you end up like python having to assemble a program from lots of include libraries that you can't remember the name of - yuck


Definitely a case of YMMV I feel.

Plus the option to "assemble a program from lots of ..." is true of most programming languages including MMBasic on the CMM2 and MMBasic for Windows. I do it on the PicoMite too using a pre-processor.

However Peter is driving the train so I guess I have to enjoy the ride or get off.

Best wishes folks,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
jwettroth

Regular Member

Joined: 02/08/2011
Location: United States
Posts: 71
Posted: 07:20pm 23 Nov 2023
Copy link to clipboard 
Print this post

I was a big fan of HP "Rocky Mountain Basic" that used to run on the their instrument controllers like the HP85 and embedded into instruments like the HP4396B IBASIC.  There were probably even Windows versions, I only used it on instruments.

The HP4396 is a Vector Network Analyzer that measures S parameters and complex impedances- all complex. The Nano VNA built around an ARM-4F does about 90% of what these big boxes can do in cigarette pack size package and for 1/100 the cost.  The code of these is near open source, written in C.  I've thought that having a Nano VNA with a built in basic would make for a very flexible instruments- basically a 4396B in your pocket.  Basic is a nice language for this and a prerequisite would be a native complex data type.  There are a lot of simple data massaging tasks that basic is ideal for.  I don't know if this is a real use case buts its interesting.

Thanks for all your hard work.
John Wettroth
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024