0 people like it.

Complex 6 - Roots

  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: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
114: 
115: 
116: 
117: 
118: 
119: 
120: 
121: 
122: 
123: 
124: 
125: 
126: 
127: 
128: 
129: 
130: 
131: 
132: 
133: 
// F# has lists, which we'll use often

// a list of floats 
let list1 = [1.0; 2.0; 5.0];;

// Make a list of integers from 1 to 4
let list2 = [1 .. 4];;

// Making a list of floats from 1.0 to 3.0, with step size 0.2
let list3 = [1.0 .. 0.2 .. 3.0];;

// map allows you to aply a function to each element in a list
let list4 = 
    List.map (fun x -> 2 * x) list2;;

// Shortcut to pi
let pi = atan2 0.0 -1.0 
    
// Make the list of fractions for roots on p.14
// So we want 
//    theta + 2 * k * pi / n where k = 0... n-1
// Now we will take angles mod 2 * pi, so they're in the range [0...2 * pi)
// And sort them from small to large.
let rootAngles theta n =
    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;;

// Test a list of angles
rootAngles pi 4;;

/////////////////////////////////////////////////////////////////////////
// Back to Complex numbers - define type and operators

// 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;;

/////////////////////////////////////////////////////

// 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;;

// Example 2.6
let z = {Re = 1.0; Im = 0.0};;

let roots = nthRoots 8 z;;

// Print out the list (apply the action to every item using iter)
List.iter print roots;;

////////////////////

// Example 2.7
let w = {Re = -16.0; Im = 0.0};;

// ... in polar
let wp = toPolar w;;
let wRoots = nthRootsPolar 4 w;;
List.iter printp wRoots;;

List.iter print (nthRoots 4 w);;
val list1 : float list

Full name: Script.list1
val list2 : int list

Full name: Script.list2
val list3 : float list

Full name: Script.list3
val list4 : int list

Full name: Script.list4
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 x : int
val pi : float

Full name: Script.pi
val atan2 : y:'T1 -> x:'T1 -> 'T2 (requires member Atan2)

Full name: Microsoft.FSharp.Core.Operators.atan2
val rootAngles : theta:float -> n:int -> float list

Full name: Script.rootAngles
val theta : float
val n : int
val kList : int list
val angles : float list
val k : int
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<_>
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
type Complex =
  {Re: float;
   Im: float;}
  static member ( + ) : z1:Complex * z2:Complex -> Complex
  static member ( / ) : z1:Complex * z2:Complex -> Complex
  static member ( * ) : z1:Complex * z2:Complex -> Complex
  static member ( - ) : z1:Complex * z2:Complex -> Complex
  static member ( ~- ) : z:Complex -> Complex

Full name: Script.Complex
Complex.Re: 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
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 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 z : Complex

Full name: Script.z
val roots : Complex list

Full name: Script.roots
val iter : action:('T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter
val w : Complex

Full name: Script.w
val wp : ComplexPolar

Full name: Script.wp
val wRoots : ComplexPolar list

Full name: Script.wRoots
Raw view Test code New version

More information

Link:http://fssnip.net/6P
Posted:14 years ago
Author:
Tags: