Recher für Komplexe Zahlen

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Joel

Betreff: Recher für Komplexe Zahlen

BeitragMi, Mai 16, 2012 22:30
Antworten mit Zitat
Benutzer-Profile anzeigen
Da ich hin und wieder mit Komplexen Zahlen arbeite, habe ich einige Funktionen zum Rechnen mit ihnen zusammengeschraubt. Es sind die Operationen Addition,Subtraktion,Multiplikation,Division und Potenzieren vertreten.
Wurzeln kann man nur begrenzt mit der Potenz-Funktion ziehen, da sie immer nur eine Wurzel zurückgibt.
Potenzieren geht nur mit reellen Zahlen im Exponenten.

Die Zahl wird in einem String angegeben :"Realteil:Imaginärteil"

Code: [AUSKLAPPEN]
SuperStrict

Global a:String = "3:7"
Global b:String = "4:2"

Print "Add:         " + Add(a, b)
Print "Subtract:    " + Subtract(a, b)
Print "Multiply:    " + Multiply(a, b)
Print "Divide:      " + Divide(a, b)
Print "Exponentiate:" + Exponentiate(a, 2)
Print "Root:        " + Exponentiate(a, 1 / 2.0)
Print ""
Print "Argument:    " + Argument(b)
Print "Absolute:    " + CAbs(a)
Print "Complex Conjugate: " + CConjugate(a)



Function Re:Double(z:String)
   Return Double(Left(z, Instr(z, ":")))
End Function

Function Im:Double(z:String)
   Return Double(Right(z, Instr(z, ":") - 1))
End Function

Function Argument:Double(z:String)
   Return ATan2(Im(z), Re(z))
End Function

Function CAbs:Double(z:String)
   Local a:Double = re(z)
   Local b:Double = im(z)
   Return Sqr(a * a + b * b)
End Function

Function CConjugate:String(z:String)
   Return Re(z) + ":" + (-Im(z))
End Function

Function Add:String(z1:String, z2:String)
   Return (Re(z1) + Re(z2)) + ":" + (Im(z1) + Im(z2))
End Function

Function Subtract:String(z1:String, z2:String)
   Return (Re(z1) - Re(z2)) + ":" + (Im(z1) - Im(z2))
End Function

Function Multiply:String(z1:String, z2:String)
   Local a:Double = Re(z1)
   Local b:Double = Im(z1)
   Local c:Double = Re(z2)
   Local d:Double = Im(z2)
   Return (a * c - b * d) + ":" + (a * d + b * c)
End Function

Function Divide:String(z1:String, z2:String)
   Local a:Double = Re(z1)
   Local b:Double = Im(z1)
   Local c:Double = Re(z2)
   Local d:Double = Im(z2)
   Return ((a * c + b * d) / (c * c + d * d)) + ":" + ((b * c - a * d) / (c * c + d * d))
End Function

Function Exponentiate:String(z1:String, Exponent:Double)
   Local a:Double = Re(z1)
   Local b:Double = Im(z1)
   Local w:Double = ATan2(b, a) * exponent
   Local l:Double = Sqr(a * a + b * b) ^ exponent
   Return (Cos(w) * l) + ":" + (Sin(w) * l)
End Function

blackgecko

BeitragDo, Mai 17, 2012 18:54
Antworten mit Zitat
Benutzer-Profile anzeigen
Ganz nett, aber die Verwaltung über Strings, das ständige Extrahieren und wieder zusammensetzen, muss doch ein ganz schöner Performance-Killer sein. Wir sind hier in BlitzMax, mach das Ganze doch objektorientiert Wink
So long and thanks for all the fish.
Fedora 17 | Windows 7 || BlitzPlus | BlitzMax
Rechtschreibflame GO!!! Deppenapostroph | SeidSeit | Deppenakzent | DassDas | Deppenleerzeichen | TodTot | enzigste.info - Ja, ich sammel die.

Joel

BeitragSa, Mai 19, 2012 13:53
Antworten mit Zitat
Benutzer-Profile anzeigen
Ahh klar hab ich gar nicht daran gedacht Idea ...

Hier:Code: [AUSKLAPPEN]
SuperStrict

Local a:CNum = New CNum
a.Is(3, 7)
Local b:CNum = New CNum
b.Is(4, 2)
Print a.re

Print "Add:         " + CNumToString(Add(a, b))
Print "Subtract:    " + CNumToString(Subtract(a, b))
Print "Multiply:    " + CNumToString(Multiply(a, b))
Print "Divide:      " + CNumToString(Divide(a, b))
Print "Exponentiate:" + CNumToString(Exponentiate(a, 2))
Print "Root:        " + CNumToString(Exponentiate(a, 1 / 2.0))
Print ""
Print "Argument:    " + Argument(b)
Print "Absolute:    " + CAbs(a)
Print "Complex Conjugate: " + CNumToString(CConjugate(a))



Type CNum
   Field re:Double
   Field im:Double
   
   Method Is(Re:Double, Im:Double)
      Self.Re = Re
      Self.Im = Im
   End Method
End Type


Function Argument:Double(z:CNum)
   Return ATan2(z.im, z.re)
End Function

Function CAbs:Double(z:CNum)
   Return Sqr(z.re * z.re + z.im * z.im)
End Function

Function CConjugate:CNum(z:CNum)
   z.im = -z.im
   Return z
End Function

Function Add:CNum(a:CNum, b:CNum)
   Local res:CNum
   res.re = a.re + b.re
   res.im = a.im + b.im
   Return res
End Function

Function Subtract:CNum(a:CNum, b:CNum)
   Local res:CNum
   res.re = a.re - b.re
   res.im = a.im - b.im
   Return res
End Function

Function Multiply:CNum(a:CNum, b:CNum)
   Local res:CNum
   res.re = a.re * b.re - a.im * b.im
   res.im = a.re * b.im + a.im * b.re
   Return res
End Function

Function Divide:CNum(a:CNum, b:CNum)
   Local res:CNum
   res.re = (a.re * b.re + a.im * b.im) / (b.re * b.re + b.im * b.im)
   res.im = (a.im * b.re - a.re * b.im) / (b.re * b.re + b.im * b.im)
   Return res
End Function

Function Exponentiate:CNum(z:CNum, Exponent:Double)
   Local res:CNum
   Local w:Double = ATan2(z.im, z.re) * Exponent
   Local l:Double = Sqr(z.re * z.re + z.im * z.im) ^ Exponent
   res.re = Cos(w) * l
   res.im = Sin(w) * l
   Return res
End Function

Function CNumToString:String(z:CNum)
   If z.im >= 0
      Return z.re + "+" + z.im + "i"
   Else
      Return z.re + "" + z.im + "i"
   End If
End Function

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group