0 people like it.
Like the snippet!
Complex 7 - Chapter 2 Problems
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:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
|
// Basic Complex type definitions
// 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;;
let sprint z = sprintf "%.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;;
// Get list of angles used for roots
let rootAngles theta n =
let pi = atan2 0.0 -1.0
let kList = [0 .. (n-1)]
let angles = List.map (fun k -> (theta + 2.0 * (float k) * pi) / (float n)) kList
let anglesModPi = List.map (fun angle -> angle % (2.0 * pi)) angles
let anglesSorted = List.sort anglesModPi
anglesSorted;;
// Find roots
let nthRootsPolar n z =
let zp = toPolar z
let angles = rootAngles zp.Arg n
let mag = System.Math.Pow(zp.Mag, (1.0 / (float n)))
List.map (fun angle -> {Mag = mag; Arg = angle}) angles;;
// ... one way to convert a list from polar
let fromPolarList polars =
List.map fromPolar polars;;
// ... another way...
// send (pipe) the output from the nthRootsPolar
// to a list conversion
let nthRoots n z =
nthRootsPolar n z
|> List.map fromPolar;;
///////////////////////////////////////////////////////////
// Problem 2.6
let test z =
let one = {Re = 1.0; Im = 0.0}
let zplus = z + one
let zmin = z - one
let absplus = abs zplus
let absmin = abs zmin
if absplus > absmin then
printfn "%s is %s" (sprint z) "OK (satisfies the ineqality)"
else
printfn "%s is %s" (sprint z) "BAD (does not satisfy the inequality)"
test {Re = 3.0; Im = -0.5};;
test {Re = -2.0; Im = 4.5};;
|
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 sprint : z:Complex -> string
Full name: Script.sprint
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
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 rootAngles : theta:float -> n:int -> float list
Full name: Script.rootAngles
val theta : float
val n : int
val pi : float
val kList : int list
val angles : float list
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list
Full name: Microsoft.FSharp.Collections.List.map
val k : int
val anglesModPi : float list
val angle : float
val anglesSorted : float list
val sort : list:'T list -> 'T list (requires comparison)
Full name: Microsoft.FSharp.Collections.List.sort
val nthRootsPolar : n:int -> z:Complex -> ComplexPolar list
Full name: Script.nthRootsPolar
val mag : float
namespace System
type Math =
static val PI : float
static val E : float
static member Abs : value:sbyte -> sbyte + 6 overloads
static member Acos : d:float -> float
static member Asin : d:float -> float
static member Atan : d:float -> float
static member Atan2 : y:float * x:float -> float
static member BigMul : a:int * b:int -> int64
static member Ceiling : d:decimal -> decimal + 1 overload
static member Cos : d:float -> float
...
Full name: System.Math
System.Math.Pow(x: float, y: float) : float
val fromPolarList : polars:ComplexPolar list -> Complex list
Full name: Script.fromPolarList
val polars : ComplexPolar list
val nthRoots : n:int -> z:Complex -> Complex list
Full name: Script.nthRoots
val test : z:Complex -> unit
Full name: Script.test
val one : Complex
val zplus : Complex
val zmin : Complex
val absplus : float
val absmin : float
More information