Complex numbers in compact form.

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Complex numbers in compact form.

Post by dodicat »

Let C take the strain.

Code: Select all



'   cacos, Compute the complex arc cosine of a complex number
'   casin, Compute the complex arc sine of a complex number
'    ccos, Compute the complex cosine of a complex number
'    csin, Compute the complex sine of a complex number
'    ctan, Compute the complex tangent of a complex number
'   cacos, Compute the complex arc cosine of a complex number
'   casin, Compute the complex arc sine of a complex number
'   catan, Compute the complex arc tangent of a complex number
'    cexp, Compute the complex base-e exponential of a complex number
'    clog, Compute the complex natural (base-e) logarithm of a complex number
'  clog10, Compute the complex base-10 logarithm of a complex number
'    cabs, Compute the complex absolute value (also called the norm, modulus, or magnitude) of a complex number
'    cpow, Compute the complex power function xy
'   csqrt, Compute the complex square root of a complex number

'MANIPULATION FUNCTIONS

'   carg, Compute the argument (also called the phase angle) of a complex number
'  cimag, Compute the imaginary part of a complex number
'   conj, Compute the complex conjugate of a complex number
'  cproj, Compute a projection of a complex number onto the Riemann sphere
'  creal, Compute the real part of a complex number




'   cacos, Compute the complex arc cosine of a complex number
'   casin, Compute the complex arc sine of a complex number
'    ccos, Compute the complex cosine of a complex number
'    csin, Compute the complex sine of a complex number
'    ctan, Compute the complex tangent of a complex number
'   cacos, Compute the complex arc cosine of a complex number
'   casin, Compute the complex arc sine of a complex number
'   catan, Compute the complex arc tangent of a complex number
'    cexp, Compute the complex base-e exponential of a complex number
'    clog, Compute the complex natural (base-e) logarithm of a complex number
'  clog10, Compute the complex base-10 logarithm of a complex number
'    cabs, Compute the complex absolute value (also called the norm, modulus, or magnitude) of a complex number
'    cpow, Compute the complex power function xy
'   csqrt, Compute the complex square root of a complex number

'MANIPULATION FUNCTIONS

'   carg, Compute the argument (also called the phase angle) of a complex number
'  cimag, Compute the imaginary part of a complex number
'   conj, Compute the complex conjugate of a complex number
'  cproj, Compute a projection of a complex number onto the Riemann sphere
'  creal, Compute the real part of a complex number



Type complex 
    As Double re,im
End Type

Declare Function ccos Cdecl Alias "ccos"(Byval As complex) As complex
Declare Function csin Cdecl Alias "csin"(Byval As complex) As complex
Declare Function ctan Cdecl Alias "ctan"(Byval As complex) As complex
Declare Function clog Cdecl Alias "clog"(Byval As complex) As complex
Declare Function clog10 Cdecl Alias "clog10"(Byval As complex) As complex
Declare Function carccos Cdecl Alias "cacos"(Byval As complex) As complex
Declare Function carcsin Cdecl Alias "casin"(Byval As complex) As complex
Declare Function carctan Cdecl Alias "catan"(Byval As complex) As complex
Declare Function cexp Cdecl Alias "cexp"(Byval As complex) As complex
Declare Function cabs Cdecl Alias "cabs"(Byval As complex) As Double
Declare Function cpow Cdecl Alias "cpow"(Byval As complex,Byval As complex) As complex
Declare Function csqrt Cdecl Alias "csqrt"(Byval As complex) As complex
Declare Function conj Cdecl Alias "conj"(Byval As complex) As complex
Declare Function carg Cdecl Alias "carg"(Byval As complex) As Double
Declare Function cimag Cdecl Alias "cimag"(Byval As complex) As Double
Declare Function creal Cdecl Alias "creal"(Byval As complex) As Double
Declare Function cproj Cdecl Alias "cproj"(Byval As complex) As complex

Declare Function ccosh Cdecl Alias "ccosh"(Byval As complex) As complex
Declare Function csinh Cdecl Alias "csinh"(Byval As complex) As complex
Declare Function ctanh Cdecl Alias "ctanh"(Byval As complex) As complex
Declare Function carccosh Cdecl Alias "cacosh"(Byval As complex) As complex
Declare Function carcsinh Cdecl Alias "casinh"(Byval As complex) As complex
Declare Function carctanh Cdecl Alias "catanh"(Byval As complex) As complex
Declare Function _printf Cdecl Alias "printf"(Byval As zstring Ptr, ...) As Long

Sub show(z As complex)
    Dim As String sign="+"
    If cimag(z)<0 Then sign=""
    _printf(!"%f  "+sign+"%fj", creal(z),cimag(z))
    _printf(!"\n")
End Sub

Operator *(n1 As complex,n2 As complex) As complex
Return Type<complex>(n1.re*n2.re - n1.im*n2.im,n1.im*n2.re + n1.re*n2.im)
End Operator

Operator +(n1 As complex,n2 As complex) As complex
Return Type<complex>(n1.re+n2.re,n1.im+n2.im)'n
End Operator

Operator -(n1 As complex,n2 As complex) As complex
Return Type<complex>(n1.re-n2.re,n1.im-n2.im)
End Operator

Operator /(n1 As complex,n2 As complex) As complex
Dim As Double d = n2.re*n2.re+n2.im*n2.im
Return Type<complex>((n1.re*n2.re+n1.im*n2.im)/d,(n1.im*n2.re - n1.re*n2.im)/d)
End Operator

Operator ^(n1 As complex,n2 As complex) As complex
Return cpow(n1,n2)
End Operator


'Euler's formula -- e^(i*pi) + 1 =0
Dim As complex i,pi
i=Type(0,1)
pi=Type(Acos(-1),0)
Var z=cexp(i*pi)+Type<complex>(1,0)
show(z)


z=Type(1.3,4.9)
_printf(!"Phase Angle = %.1f radians\n", carg(z))


Var a=cpow(Type<complex>(3,-5),Type<complex>(.5,0))
show(a)
show(csqrt(Type<complex>(3,-5)))
_printf(!"press any key to end\n")

Sleep

 
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Complex numbers in compact form.

Post by D.J.Peters »

Do you know this from free book: http://cosinekitty.com/raytrace/raytrace_a4.pdf ?

Translated for FreeBASIC:
complex numbers: viewtopic.php?f=7&t=26607&p=246026

SolveLinearEquations, SolveQuadraticEquation, SolveCubicEquation, SolveQuadraticEquation viewtopic.php?f=7&t=26607&p=246026#p246031

Joshy
Post Reply