|
Post by user5923 on Apr 11, 2023 10:28:44 GMT -5
I need to use complex number in number in BASIC, but
i = sqr(-1) returns the following error:
I don't know if there is a library or a separate command to use for this.
I looked for documentation on this, but couldn't find any.
Thanks for any help!
|
|
|
Post by tenochtitlanuk on Apr 11, 2023 13:30:54 GMT -5
Anatoly & I, among others, have written sets of complex operators. Complex numbers don't exist in LB so we pack the real and imaginary parts as a string with two part s, and write routines to display and manipulate them with all the usual operations. You could look at my pages at www.diga.me.uk/complexity.htmlwww.diga.me.uk/NRcomplexRoots.htmlI think tsh73 ( Anatoly) pit his routines up- was it on the #Programmer's Encyclopedia.
|
|
|
Post by user5923 on Apr 11, 2023 16:14:13 GMT -5
Anatoly & I, among others, have written sets of complex operators. Complex numbers don't exist in LB so we pack the real and imaginary parts as a string with two part sand write routines to display and manipulate them with all the usual operations. You could look at my pages at www.diga.me.uk/complexity.htmlwww.diga.me.uk/NRcomplexRoots.htmlI think tsh73 ( Anatoly) pit his routines up- was it on the #Programmer's Encyclopedia. Thanks for the response! I'm a beginner BASIC user, is there a specific way for me to implement this in my project? I need to be able to use and manipulate complex numbers.
|
|
|
Post by tsh73 on Apr 12, 2023 7:30:31 GMT -5
Hello My version of complex library got dusted off recently (Jan 2023) in this thread justbasiccom.proboards.com/thread/952/random-pictureprobably six-th post, with three pictures and long code. BUT
only things it has is function compl$(Re, Im) 'make a complex of two reals Re Im 'breaking it back apart function Re(compl$) function Im(compl$) 'some formatting function formatCompl$(compl$) 'tried to zero things like 1e-15, but this is just for display anyway '+ _ * / function addCompl$(c1$, c2$) function subCompl$(c1$, c2$) function mulCompl$(c1$, c2$) function divCompl$(c1$, c2$) 'Conjugation function barCompl$(c1$) 'abs (polar radius) function absCompl$(c1$) 'And you can get polar angle as atan2(Im(r$), Re(r$) The point is, there are no SQRT unction. BUT... since we can translate number to (r, phi) polar from we could use en.wikipedia.org/wiki/De_Moivre%27s_formulaJust tried to add this below: function RPhi2compl$(r, phi) '(r, phi) -> Re+i*Im function powerCompl$(c1$,n) 'n supposed to be integer function rootCompl$(c1$,n) 'n supposed to be integer 'and i-th root 'not really sure about phi, but it works for real numbers function rootICompl$(c1$,n,i) 'n supposed to be integer, is supposed to be 1..n
So part below '------------------------------------- 'complex library.
is a library (set of functions) Just put it into your program and go ahead. Complex number is stored as a space-delimited string of "re_part im_part" (so "0 0" is a valid 0) and since we cannot overload operators in LB - we have to use functions for all operators And nest them properly/ And close all the brackets (!). it tends to be rather weird looking, like this f$ = subCompl$("0 0" , addCompl$("1 0", addCompl$(z$, mulCompl$(z$, mulCompl$(z$, z$)))))
instead of f= 0-(1+z+z*z*z) ) (There was a library for interface existing in C++ and Pascal. (actually Borland's TurboVision) And in C++, they just overloaded + operator and used it to add stuff. While in Pascal they had to use just the same chain of functions like I here. ) But still better then multiply (or god forbid divide) complex numbers by hand!
And zz$=rootCompl$(compl$(1,0),2) print formatCompl$(zz$)
is actually sqr(-1)
'complex library test a$=compl$(1,0) print formatCompl$(a$) b$=compl$(0,1) print formatCompl$(b$)
print z$ = mulCompl$(b$, b$) print formatCompl$(z$)
print zz$=rootCompl$(compl$(1,0),2) print formatCompl$(zz$) print formatCompl$( powerCompl$(zz$,2))
print zz$=rootCompl$(compl$(0,1),2) print formatCompl$(zz$) print formatCompl$( powerCompl$(zz$,2))
print print "5 roots of 1" for i = 1 to 5 zz$=rootICompl$(compl$(1,0),5,i) print formatCompl$(zz$) ,"-> "; formatCompl$(powerCompl$(zz$,5)) next
end
'--------------------------- function atan2(y,x) pi = acs(-1) 'could be made global to save some ticks if x <> 0 then arctan = atn(y/x)
select case case x > 0 atan2 = arctan
case y>=0 and x<0 atan2 = pi + arctan
case y<0 and x<0 atan2 = arctan - pi
case y>0 and x=0 atan2 = pi / 2
case y<0 and x=0 atan2 = pi / -2 end select end function
'------------------------------------- '+ powers and roots
'first, r and phi as numbers function angleCompl$(c1$) angleCompl = atan2(Im(c1$), Re(c1$) ) end function
function absCompl(c1$) Re=Re(c1$) Im=Im(c1$) absCompl = sqr(Re*Re+Im*Im) end function
'and back function RPhi2compl$(r, phi) 'you better inline this, actually RPhi2compl$= str$(r*cos(phi))+" "+ str$(r*sin(phi)) end function
'then power function powerCompl$(c1$,n) 'n supposed to be integer Re=Re(c1$) Im=Im(c1$) r=sqr(Re*Re+Im*Im) phi=atan2(Im, Re) powerCompl$ = RPhi2compl$(r^n, phi*n) end function
function rootCompl$(c1$,n) 'n supposed to be integer Re=Re(c1$) Im=Im(c1$) r=sqr(Re*Re+Im*Im) phi=atan2(Im, Re) if phi = 0 then phi = 2*acs(-1) '2*pi rootCompl$=RPhi2compl$(r^(1/n), phi/n) end function
'and i-th root 'not really sure about phi function rootICompl$(c1$,n,i) 'n supposed to be integer, is supposed to be 1..n Re=Re(c1$) Im=Im(c1$) r=sqr(Re*Re+Im*Im) phi=atan2(Im, Re) if phi = 0 then rhi = 2*acs(-1) '2*pi rootICompl$=RPhi2compl$(r^(1/n), phi/n+(i-1)*2*acs(-1)/n) end function
'------------------------------------- 'complex library. I jsut was too lazy to multiply complex numbers by hand. function absCompl$(c1$) Re=Re(c1$) Im=Im(c1$) abs=sqr(Re*Re+Im*Im) absCompl$ = str$(abs) end function
'Conjugation function barCompl$(c1$) Re=Re(c1$) Im=0-Im(c1$) barCompl$ = str$(Re)+" "+ str$(Im) end function
function divCompl$(c1$, c2$) '(a + bi)/(c + di): = (ac + bd)/(c^2+d^2) + (bc - ad)/(c^2+d^2)i. a=Re(c1$) b=Im(c1$) c=Re(c2$) d=Im(c2$) dd=c*c+d*d divCompl$ = str$((a*c + b*d)/dd) +" " + str$((b*c - a*d)/dd) end function
function mulCompl$(c1$, c2$) '(a + bi)(c + di): = (ac - bd) + (bc + ad)i. a=Re(c1$) b=Im(c1$) c=Re(c2$) d=Im(c2$) mulCompl$ = str$(a*c - b*d) +" " + str$(b*c + a*d) end function
function subCompl$(c1$, c2$) Re=Re(c1$)-Re(c2$) Im=Im(c1$)-Im(c2$) subCompl$ = str$(Re)+" "+ str$(Im) end function
function addCompl$(c1$, c2$) Re=Re(c1$)+Re(c2$) Im=Im(c1$)+Im(c2$) addCompl$ = str$(Re)+" "+ str$(Im) end function
function formatCompl$(compl$) Re = Re(compl$) Im = Im(compl$) 'approx: clear "near zero" parts. 'This is just visualizing! if abs(Re)<1e-10 then Re=0 if abs(Im)<1e-10 then Im=0 if Re=0 and Im=0 then formatCompl$ = "0": exit function Re$ = word$(compl$,1) if Re =0 then Re$="" 'and no next "+" Im$=str$(Im)+"i" if Im =0 then Im$="" formatCompl$ = Re$+ iif$(Im>0 and Re<>0, "+", "")+ Im$ end function
function Re(compl$) Re = val(word$(compl$,1)) end function
function Im(compl$) Im = val(word$(compl$,2)) end function
function compl$(Re, Im) 'you better inline this, actually compl$= str$(Re)+" "+ str$(Im) end function
'----------------------------------- function iif(test, valYes, valNo) iif = valNo if test then iif = valYes end function
function iif$(test, valYes$, valNo$) iif$ = valNo$ if test then iif$ = valYes$ end function
|
|