Browse Source

Wrap the existing Complex type instead of redefining it

v2
Gustavo Guerra 14 years ago
parent
commit
6729141084
  1. 60
      src/FSharp/complex.fs
  2. 121
      src/FSharp/complex.fsi

60
src/FSharp/complex.fs

@ -10,7 +10,9 @@ namespace MathNet.Numerics
open Microsoft.FSharp.Math open Microsoft.FSharp.Math
open System open System
open System.Globalization open System.Globalization
open System.Numerics
(*
[<Struct>] [<Struct>]
[<CustomEquality; CustomComparison>] [<CustomEquality; CustomComparison>]
type Complex(real: float, imaginary: float) = type Complex(real: float, imaginary: float) =
@ -34,10 +36,16 @@ namespace MathNet.Numerics
| _ -> false | _ -> false
override x.GetHashCode() = override x.GetHashCode() =
(hash x.r >>> 5) ^^^ (hash x.r <<< 3) ^^^ (((hash x.i >>> 4) ^^^ (hash x.i <<< 4)) + 0x9e3779b9) (hash x.r >>> 5) ^^^ (hash x.r <<< 3) ^^^ (((hash x.i >>> 4) ^^^ (hash x.i <<< 4)) + 0x9e3779b9)
*)
type complex = Complex type complex = Complex
[<AutoOpen>]
module private ComplexExtensionsBasic =
type Complex with
member x.r = x.Real
member x.i = x.Imaginary
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Complex = module Complex =
let mkRect(a,b) = new Complex(a,b) let mkRect(a,b) = new Complex(a,b)
@ -98,34 +106,30 @@ namespace MathNet.Numerics
let tan x = let exp2ix = exp(iscale 2.0 x) in let tan x = let exp2ix = exp(iscale 2.0 x) in
(div (sub exp2ix one) (add exp2ix one)) |> iscale -1.0 (div (sub exp2ix one) (add exp2ix one)) |> iscale -1.0
[<AutoOpen>]
type Complex with module ComplexExtensions =
static member Create(a,b) = Complex.mkRect (a,b) type Complex with
static member CreatePolar(a,b) = Complex.mkPolar (a,b) member x.r = x.Real
member x.Magnitude = Complex.magnitude x member x.i = x.Imaginary
member x.Phase = Complex.phase x static member Create(a,b) = Complex.mkRect (a,b)
member x.RealPart = x.r static member CreatePolar(a,b) = Complex.mkPolar (a,b)
member x.ImaginaryPart = x.i member x.Magnitude = Complex.magnitude x
member x.Conjugate = Complex.conjugate x member x.Phase = Complex.phase x
member x.RealPart = x.r
static member Sin(x) = Complex.sin(x) member x.ImaginaryPart = x.i
static member Cos(x) = Complex.cos(x) member x.Conjugate = Complex.conjugate x
static member Abs(x) = Complex.abs(x)
static member Tan(x) = Complex.tan(x) static member Sin(x) = Complex.sin(x)
static member Log(x) = Complex.log(x) static member Cos(x) = Complex.cos(x)
static member Exp(x) = Complex.exp(x) static member Abs(x) = Complex.abs(x)
static member Sqrt(x) = Complex.sqrt(x) static member Tan(x) = Complex.tan(x)
static member Log(x) = Complex.log(x)
static member Exp(x) = Complex.exp(x)
static member Sqrt(x) = Complex.sqrt(x)
static member Zero = Complex.zero static member Zero = Complex.zero
static member One = Complex.one static member One = Complex.one
static member OneI = Complex.onei static member OneI = Complex.onei
static member ( + ) (a,b) = Complex.add a b
static member ( - ) (a,b) = Complex.sub a b
static member ( * ) (a,b) = Complex.mul a b
static member ( / ) (a,b) = Complex.div a b
static member ( ~- ) a = Complex.neg a
static member ( * ) (a,b) = Complex.smul a b
static member ( * ) (a,b) = Complex.muls a b
module ComplexTopLevelOperators = module ComplexTopLevelOperators =

121
src/FSharp/complex.fsi

@ -6,66 +6,71 @@
namespace MathNet.Numerics namespace MathNet.Numerics
open System open System
open System.Numerics
/// The type of complex numbers stored as pairs of 64-bit floating point numbers in rectangular coordinates
[<Struct>] [<AutoOpen>]
[<CustomEquality; CustomComparison>] module ComplexExtensions =
type Complex = /// The type of complex numbers stored as pairs of 64-bit floating point numbers in rectangular coordinates
/// The real part of a complex number type Complex with
member r: float /// The real part of a complex number
/// The imaginary part of a complex number member r: float
member i: float /// The imaginary part of a complex number
/// The polar-coordinate magnitude of a complex number member i: float
member Magnitude: float /// The polar-coordinate magnitude of a complex number
/// The polar-coordinate phase of a complex number member Magnitude: float
member Phase: float /// The polar-coordinate phase of a complex number
/// The real part of a complex number member Phase: float
member RealPart: float /// The real part of a complex number
/// The imaginary part of a complex number member RealPart: float
member ImaginaryPart: float /// The imaginary part of a complex number
/// The conjugate of a complex number, i.e. x-yi member ImaginaryPart: float
member Conjugate: Complex /// The conjugate of a complex number, i.e. x-yi
/// Create a complex number x+ij using rectangular coordinates member Conjugate: Complex
static member Create : float * float -> Complex /// Create a complex number x+ij using rectangular coordinates
/// Create a complex number using magnitude/phase polar coordinates static member Create : float * float -> Complex
static member CreatePolar : float * float -> Complex /// Create a complex number using magnitude/phase polar coordinates
/// The complex number 0+0i static member CreatePolar : float * float -> Complex
static member Zero : Complex /// The complex number 0+0i
/// The complex number 1+0i static member Zero : Complex
static member One : Complex /// The complex number 1+0i
/// The complex number 0+1i static member One : Complex
static member OneI : Complex /// The complex number 0+1i
/// Add two complex numbers static member OneI : Complex
static member ( + ) : Complex * Complex -> Complex (*
/// Subtract one complex number from another /// Add two complex numbers
static member ( - ) : Complex * Complex -> Complex static member ( + ) : Complex * Complex -> Complex
/// Multiply two complex numbers /// Subtract one complex number from another
static member ( * ) : Complex * Complex -> Complex static member ( - ) : Complex * Complex -> Complex
/// Complex division of two complex numbers /// Multiply two complex numbers
static member ( / ) : Complex * Complex -> Complex static member ( * ) : Complex * Complex -> Complex
/// Unary negation of a complex number /// Complex division of two complex numbers
static member ( ~- ) : Complex -> Complex static member ( / ) : Complex * Complex -> Complex
/// Multiply a scalar by a complex number /// Unary negation of a complex number
static member ( * ) : float * Complex -> Complex static member ( ~- ) : Complex -> Complex
/// Multiply a complex number by a scalar /// Multiply a scalar by a complex number
static member ( * ) : Complex * float -> Complex static member ( * ) : float * Complex -> Complex
/// Multiply a complex number by a scalar
static member Sin : Complex -> Complex static member ( * ) : Complex * float -> Complex
static member Cos : Complex -> Complex *)
static member Sin : Complex -> Complex
static member Cos : Complex -> Complex
/// Computes the absolute value of a complex number: e.g. Abs x+iy = sqrt(x**2.0 + y**2.0.) /// Computes the absolute value of a complex number: e.g. Abs x+iy = sqrt(x**2.0 + y**2.0.)
/// Note: Complex.Abs(z) is the same as z.Magnitude /// Note: Complex.Abs(z) is the same as z.Magnitude
static member Abs : Complex -> float static member Abs : Complex -> float
static member Tan : Complex -> Complex static member Tan : Complex -> Complex
static member Log : Complex -> Complex static member Log : Complex -> Complex
static member Exp : Complex -> Complex static member Exp : Complex -> Complex
static member Sqrt : Complex -> Complex static member Sqrt : Complex -> Complex
override ToString : unit -> string (*
override Equals : obj -> bool override ToString : unit -> string
interface System.IComparable override Equals : obj -> bool
member ToString : format:string -> string interface System.IComparable
member ToString : format:string * provider:System.IFormatProvider -> string member ToString : format:string -> string
member ToString : format:string * provider:System.IFormatProvider -> string
*)
/// The type of complex numbers /// The type of complex numbers
type complex = Complex type complex = Complex

Loading…
Cancel
Save