0 people like it.
Like the snippet!
Complex 5 - Polar Form
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
|
// Define complex type with some operators
type Complex =
{ Re : float;
Im : float }
static member (+) (z1, z2) =
{ Re = z1.Re + z2.Re;
Im = z1.Im + z2.Im }
static member (-) (z1, z2) =
{ Re = z1.Re - z2.Re;
Im = z1.Im - z2.Im }
static member (*) (z1, z2) =
{ Re = ((z1.Re * z2.Re) - (z1.Im * z2.Im));
Im = ((z1.Re * z2.Im) + (z1.Im * z2.Re)) }
static member (/) (z1, z2) =
let z2_conj = {Re = z2.Re; Im = -z2.Im}
let den = (z2 * z2_conj).Re
let num = z1 * z2_conj
{ Re = num.Re / den;
Im = num.Im / den }
static member (~-) z =
{ Re = -z.Re;
Im = -z.Im };;
// .. and printing
let print z = printfn "%.3f%+.3fi" z.Re z.Im;;
// .. and the conjugate
let conj z =
{ Re = z.Re;
Im = -z.Im };;
// ... and the modulus (absolute value)
let abs z =
sqrt (z.Re * z.Re + z.Im * z.Im);;
// ... and the argument (actually this is the principal value of the argument (Arg)
let arg z =
atan2 z.Im z.Re;;
// Polar form of complex number
type ComplexPolar =
{ Mag : float;
Arg : float };;
// ... with conversion to and from the polar form
let toPolar z =
{ Mag = abs z;
Arg = arg z };;
let fromPolar zp =
{ Re = zp.Mag * (cos zp.Arg);
Im = zp.Mag * (sin zp.Arg) };;
// ... and define printing of the polar form
let printp zp =
printfn "%.1f(cos %.3f + i sin %.3f)" zp.Mag zp.Arg zp.Arg;;
// Try it
let i = {Re = 0.0; Im = 1.0};;
printp (toPolar i);;
let z1 = {Re = 3.0; Im = 4.0};;
let z2 = fromPolar (toPolar z1);;
print z1;;
print z2;; // should be equal to z1.
// Example 2.1
let z = {Re = -1.0; Im = -1.0};;
let zp = toPolar z;;
printp zp;;
// ...how to check those decimals?
let pi = atan2 0.0 -1.0;;
printfn "%f" (- 3.0 * pi / 4.0);;
|
Complex.Re: float
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
Complex.Im: float
val z1 : Complex
val z2 : Complex
val z2_conj : Complex
val den : float
val num : Complex
val z : Complex
val print : z:Complex -> unit
Full name: Script.print
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val conj : z:Complex -> Complex
Full name: Script.conj
val abs : z:Complex -> float
Full name: Script.abs
val sqrt : value:'T -> 'U (requires member Sqrt)
Full name: Microsoft.FSharp.Core.Operators.sqrt
val arg : z:Complex -> float
Full name: Script.arg
val atan2 : y:'T1 -> x:'T1 -> 'T2 (requires member Atan2)
Full name: Microsoft.FSharp.Core.Operators.atan2
type ComplexPolar =
{Mag: float;
Arg: float;}
Full name: Script.ComplexPolar
ComplexPolar.Mag: float
ComplexPolar.Arg: float
val toPolar : z:Complex -> ComplexPolar
Full name: Script.toPolar
val fromPolar : zp:ComplexPolar -> Complex
Full name: Script.fromPolar
val zp : ComplexPolar
val cos : value:'T -> 'T (requires member Cos)
Full name: Microsoft.FSharp.Core.Operators.cos
val sin : value:'T -> 'T (requires member Sin)
Full name: Microsoft.FSharp.Core.Operators.sin
val printp : zp:ComplexPolar -> unit
Full name: Script.printp
val i : Complex
Full name: Script.i
val z1 : Complex
Full name: Script.z1
val z2 : Complex
Full name: Script.z2
val z : Complex
Full name: Script.z
val zp : ComplexPolar
Full name: Script.zp
val pi : float
Full name: Script.pi
More information