diff --git a/src/FSharp/q.fs b/src/FSharp/BigRational.fs similarity index 73% rename from src/FSharp/q.fs rename to src/FSharp/BigRational.fs index 2cb24129..221970e2 100644 --- a/src/FSharp/q.fs +++ b/src/FSharp/BigRational.fs @@ -1,8 +1,8 @@ -// First version copied from the F# Power Pack +// First version copied from the F# Power Pack // https://raw.github.com/fsharp/powerpack/master/src/FSharp.PowerPack/math/q.fs -// (c) Microsoft Corporation. All rights reserved +// (c) Microsoft Corporation. All rights reserved #nowarn "44" // OK to use the "compiler only" function RangeGeneric #nowarn "52" // The value has been copied to ensure the original is not mutated by this operation @@ -13,7 +13,7 @@ namespace MathNet.Numerics open System.Numerics open System.Globalization - module BigRationalLargeImpl = + module BigRationalLargeImpl = let ZeroI = new BigInteger(0) let OneI = new BigInteger(1) let bigint (x:int) = new BigInteger(x) @@ -21,44 +21,44 @@ namespace MathNet.Numerics let ToInt32I (x:BigInteger) = int32 x open BigRationalLargeImpl - + [] - type BigRationalLarge = - | Q of BigInteger * BigInteger // invariants: (p,q) in lowest form, q >= 0 + type BigRationalLarge = + | Q of BigInteger * BigInteger // invariants: (p,q) in lowest form, q >= 0 override n.ToString() = - let (Q(p,q)) = n - if q.IsOne then p.ToString() + let (Q(p,q)) = n + if q.IsOne then p.ToString() else p.ToString() + "/" + q.ToString() - static member Hash (Q(ap,aq)) = + static member Hash (Q(ap,aq)) = // This hash code must be identical to the hash for BigInteger when the numbers coincide. if aq.IsOne then ap.GetHashCode() else (ap.GetHashCode() <<< 3) + aq.GetHashCode() - + override x.GetHashCode() = BigRationalLarge.Hash(x) - - static member Equals(Q(ap,aq), Q(bp,bq)) = - BigInteger.(=) (ap,bp) && BigInteger.(=) (aq,bq) // normal form, so structural equality - - static member LessThan(Q(ap,aq), Q(bp,bq)) = + + static member Equals(Q(ap,aq), Q(bp,bq)) = + BigInteger.(=) (ap,bp) && BigInteger.(=) (aq,bq) // normal form, so structural equality + + static member LessThan(Q(ap,aq), Q(bp,bq)) = BigInteger.(<) (ap * bq,bp * aq) - + // note: performance improvement possible here - static member Compare(p,q) = - if BigRationalLarge.LessThan(p,q) then -1 - elif BigRationalLarge.LessThan(q,p)then 1 - else 0 - - interface System.IComparable with - member this.CompareTo(obj:obj) = - match obj with + static member Compare(p,q) = + if BigRationalLarge.LessThan(p,q) then -1 + elif BigRationalLarge.LessThan(q,p)then 1 + else 0 + + interface System.IComparable with + member this.CompareTo(obj:obj) = + match obj with | :? BigRationalLarge as that -> BigRationalLarge.Compare(this,that) | _ -> invalidArg "obj" "the object does not have the correct type" - override this.Equals(that:obj) = - match that with + override this.Equals(that:obj) = + match that with | :? BigRationalLarge as that -> BigRationalLarge.Equals(this,that) | _ -> false @@ -69,7 +69,7 @@ namespace MathNet.Numerics member x.Denominator = let (Q(_,q)) = x in q member x.Sign = (let (Q(p,_)) = x in sign p) - static member ToDouble (Q(p,q)) = + static member ToDouble (Q(p,q)) = ToDoubleI p / ToDoubleI q static member Normalize (p:BigInteger,q:BigInteger) = @@ -79,59 +79,59 @@ namespace MathNet.Numerics Q(p,q) else let k = BigInteger.GreatestCommonDivisor(p,q) - let p = p / k - let q = q / k + let p = p / k + let q = q / k if sign q < 0 then Q(-p,-q) else Q(p,q) static member Rational (p:int,q:int) = BigRationalLarge.Normalize (bigint p,bigint q) static member RationalZ (p,q) = BigRationalLarge.Normalize (p,q) - + static member Parse (str:string) = - let len = str.Length + let len = str.Length if len=0 then invalidArg "str" "empty string"; - let j = str.IndexOf '/' - if j >= 0 then - let p = BigInteger.Parse (str.Substring(0,j)) - let q = BigInteger.Parse (str.Substring(j+1,len-j-1)) + let j = str.IndexOf '/' + if j >= 0 then + let p = BigInteger.Parse (str.Substring(0,j)) + let q = BigInteger.Parse (str.Substring(j+1,len-j-1)) BigRationalLarge.RationalZ (p,q) else - let p = BigInteger.Parse str + let p = BigInteger.Parse str BigRationalLarge.RationalZ (p,OneI) - - static member (~-) (Q(bp,bq)) = Q(-bp,bq) // still coprime, bq >= 0 + + static member (~-) (Q(bp,bq)) = Q(-bp,bq) // still coprime, bq >= 0 static member (+) (Q(ap,aq),Q(bp,bq)) = BigRationalLarge.Normalize ((ap * bq) + (bp * aq),aq * bq) static member (-) (Q(ap,aq),Q(bp,bq)) = BigRationalLarge.Normalize ((ap * bq) - (bp * aq),aq * bq) static member (*) (Q(ap,aq),Q(bp,bq)) = BigRationalLarge.Normalize (ap * bp,aq * bq) static member (/) (Q(ap,aq),Q(bp,bq)) = BigRationalLarge.Normalize (ap * bq,aq * bp) static member ( ~+ )(n1:BigRationalLarge) = n1 - - + + [] - module BigRationalLarge = + module BigRationalLarge = open System.Numerics - - let inv (Q(ap,aq)) = BigRationalLarge.Normalize(aq,ap) + + let inv (Q(ap,aq)) = BigRationalLarge.Normalize(aq,ap) let pown (Q(p,q)) (n:int) = Q(BigInteger.Pow(p,n),BigInteger.Pow (q,n)) // p,q powers still coprime - - let equal (Q(ap,aq)) (Q(bp,bq)) = ap=bp && aq=bq // normal form, so structural equality + + let equal (Q(ap,aq)) (Q(bp,bq)) = ap=bp && aq=bq // normal form, so structural equality let lt a b = BigRationalLarge.LessThan(a,b) let gt a b = BigRationalLarge.LessThan(b,a) let lte (Q(ap,aq)) (Q(bp,bq)) = BigInteger.(<=) (ap * bq,bp * aq) let gte (Q(ap,aq)) (Q(bp,bq)) = BigInteger.(>=) (ap * bq,bp * aq) - let of_bigint z = BigRationalLarge.RationalZ(z,OneI ) + let of_bigint z = BigRationalLarge.RationalZ(z,OneI) let of_int n = BigRationalLarge.Rational(n,1) - + // integer part let integer (Q(p,q)) = let mutable r = BigInteger(0) - let d = BigInteger.DivRem (p,q,&r) // have p = d.q + r, |r| < |q| + let d = BigInteger.DivRem (p,q,&r) // have p = d.q + r, |r| < |q| if r < ZeroI - then d - OneI // p = (d-1).q + (r+q) - else d // p = d.q + r - - + then d - OneI // p = (d-1).q + (r+q) + else d // p = d.q + r + + //---------------------------------------------------------------------------- // BigRational //-------------------------------------------------------------------------- @@ -142,35 +142,35 @@ namespace MathNet.Numerics | Z of BigInteger | Q of BigRationalLarge - static member ( + )(n1,n2) = + static member ( + )(n1,n2) = match n1,n2 with | Z z ,Z zz -> Z (z + zz) | Q q ,Q qq -> Q (q + qq) | Z z ,Q qq -> Q (BigRationalLarge.of_bigint z + qq) | Q q ,Z zz -> Q (q + BigRationalLarge.of_bigint zz) - static member ( * )(n1,n2) = + static member ( * )(n1,n2) = match n1,n2 with | Z z ,Z zz -> Z (z * zz) | Q q ,Q qq -> Q (q * qq) | Z z ,Q qq -> Q (BigRationalLarge.of_bigint z * qq) | Q q ,Z zz -> Q (q * BigRationalLarge.of_bigint zz) - static member ( - )(n1,n2) = + static member ( - )(n1,n2) = match n1,n2 with | Z z ,Z zz -> Z (z - zz) | Q q ,Q qq -> Q (q - qq) | Z z ,Q qq -> Q (BigRationalLarge.of_bigint z - qq) | Q q ,Z zz -> Q (q - BigRationalLarge.of_bigint zz) - static member ( / )(n1,n2) = + static member ( / )(n1,n2) = match n1,n2 with | Z z ,Z zz -> Q (BigRationalLarge.RationalZ(z,zz)) | Q q ,Q qq -> Q (q / qq) | Z z ,Q qq -> Q (BigRationalLarge.of_bigint z / qq) | Q q ,Z zz -> Q (q / BigRationalLarge.of_bigint zz) - static member ( ~- )(n1) = + static member ( ~- )(n1) = match n1 with | Z z -> Z (-z) | Q q -> Q (-q) @@ -178,28 +178,28 @@ namespace MathNet.Numerics static member ( ~+ )(n1:BigRational) = n1 // nb. Q and Z hash codes must match up - see notes above - override n.GetHashCode() = - match n with + override n.GetHashCode() = + match n with | Z z -> z.GetHashCode() - | Q q -> q.GetHashCode() + | Q q -> q.GetHashCode() - override this.Equals(obj:obj) = - match obj with + override this.Equals(obj:obj) = + match obj with | :? BigRational as that -> BigRational.(=)(this, that) | _ -> false - interface System.IComparable with - member n1.CompareTo(obj:obj) = - match obj with - | :? BigRational as n2 -> + interface System.IComparable with + member n1.CompareTo(obj:obj) = + match obj with + | :? BigRational as n2 -> if BigRational.(<)(n1, n2) then -1 elif BigRational.(=)(n1, n2) then 0 else 1 | _ -> invalidArg "obj" "the objects are not comparable" static member FromInt (x:int) = Z (bigint x) static member FromBigInt x = Z x - static member Zero = BigRational.FromInt(0) - static member One = BigRational.FromInt(1) + static member Zero = BigRational.FromInt(0) + static member One = BigRational.FromInt(1) static member PowN (n,i:int) = @@ -207,103 +207,103 @@ namespace MathNet.Numerics | Z z -> Z (BigInteger.Pow (z,i)) | Q q -> Q (BigRationalLarge.pown q i) - static member op_Equality (n,nn) = + static member op_Equality (n,nn) = match n,nn with | Z z ,Z zz -> BigInteger.(=) (z,zz) | Q q ,Q qq -> (BigRationalLarge.equal q qq) | Z z ,Q qq -> (BigRationalLarge.equal (BigRationalLarge.of_bigint z) qq) | Q q ,Z zz -> (BigRationalLarge.equal q (BigRationalLarge.of_bigint zz)) static member op_Inequality (n,nn) = not (BigRational.op_Equality(n,nn)) - - static member op_LessThan (n,nn) = + + static member op_LessThan (n,nn) = match n,nn with | Z z ,Z zz -> BigInteger.(<) (z,zz) | Q q ,Q qq -> (BigRationalLarge.lt q qq) | Z z ,Q qq -> (BigRationalLarge.lt (BigRationalLarge.of_bigint z) qq) | Q q ,Z zz -> (BigRationalLarge.lt q (BigRationalLarge.of_bigint zz)) - static member op_GreaterThan (n,nn) = + static member op_GreaterThan (n,nn) = match n,nn with | Z z ,Z zz -> BigInteger.(>) (z,zz) | Q q ,Q qq -> (BigRationalLarge.gt q qq) | Z z ,Q qq -> (BigRationalLarge.gt (BigRationalLarge.of_bigint z) qq) | Q q ,Z zz -> (BigRationalLarge.gt q (BigRationalLarge.of_bigint zz)) - static member op_LessThanOrEqual (n,nn) = + static member op_LessThanOrEqual (n,nn) = match n,nn with | Z z ,Z zz -> BigInteger.(<=) (z,zz) | Q q ,Q qq -> (BigRationalLarge.lte q qq) | Z z ,Q qq -> (BigRationalLarge.lte (BigRationalLarge.of_bigint z) qq) | Q q ,Z zz -> (BigRationalLarge.lte q (BigRationalLarge.of_bigint zz)) - static member op_GreaterThanOrEqual (n,nn) = + static member op_GreaterThanOrEqual (n,nn) = match n,nn with | Z z ,Z zz -> BigInteger.(>=) (z,zz) | Q q ,Q qq -> (BigRationalLarge.gte q qq) | Z z ,Q qq -> (BigRationalLarge.gte (BigRationalLarge.of_bigint z) qq) | Q q ,Z zz -> (BigRationalLarge.gte q (BigRationalLarge.of_bigint zz)) - - member n.IsNegative = - match n with - | Z z -> sign z < 0 + + member n.IsNegative = + match n with + | Z z -> sign z < 0 | Q q -> q.IsNegative - member n.IsPositive = - match n with + member n.IsPositive = + match n with | Z z -> sign z > 0 | Q q -> q.IsPositive - - member n.Numerator = - match n with + + member n.Numerator = + match n with | Z z -> z | Q q -> q.Numerator - member n.Denominator = - match n with + member n.Denominator = + match n with | Z _ -> OneI | Q q -> q.Denominator - member n.Sign = - if n.IsNegative then -1 - elif n.IsPositive then 1 + member n.Sign = + if n.IsNegative then -1 + elif n.IsPositive then 1 else 0 - static member Abs(n:BigRational) = + static member Abs(n:BigRational) = if n.IsNegative then -n else n - static member ToDouble(n:BigRational) = + static member ToDouble(n:BigRational) = match n with | Z z -> ToDoubleI z | Q q -> BigRationalLarge.ToDouble q - static member ToBigInt(n:BigRational) = - match n with + static member ToBigInt(n:BigRational) = + match n with | Z z -> z - | Q q -> BigRationalLarge.integer q + | Q q -> BigRationalLarge.integer q - static member ToInt32(n:BigRational) = - match n with + static member ToInt32(n:BigRational) = + match n with | Z z -> ToInt32I(z) - | Q q -> ToInt32I(BigRationalLarge.integer q ) + | Q q -> ToInt32I(BigRationalLarge.integer q) static member op_Explicit (n:BigRational) = BigRational.ToInt32 n static member op_Explicit (n:BigRational) = BigRational.ToDouble n static member op_Explicit (n:BigRational) = BigRational.ToBigInt n - override n.ToString() = - match n with + override n.ToString() = + match n with | Z z -> z.ToString() | Q q -> q.ToString() member x.StructuredDisplayString = x.ToString() - + static member Parse(s:string) = Q (BigRationalLarge.Parse s) type BigNum = BigRational type bignum = BigNum - module NumericLiteralN = - let FromZero () = BigRational.Zero - let FromOne () = BigRational.One + module NumericLiteralN = + let FromZero () = BigRational.Zero + let FromOne () = BigRational.One let FromInt32 i = BigRational.FromInt i let FromInt64 (i64:int64) = BigRational.FromBigInt (new BigInteger(i64)) let FromString s = BigRational.Parse s diff --git a/src/FSharp/q.fsi b/src/FSharp/BigRational.fsi similarity index 86% rename from src/FSharp/q.fsi rename to src/FSharp/BigRational.fsi index ffcf84e2..d6c7d422 100644 --- a/src/FSharp/q.fsi +++ b/src/FSharp/BigRational.fsi @@ -1,14 +1,14 @@ -// First version copied from the F# Power Pack +// First version copied from the F# Power Pack // https://raw.github.com/fsharp/powerpack/master/src/FSharp.PowerPack/math/q.fsi -// (c) Microsoft Corporation 2005-2009. +// (c) Microsoft Corporation 2005-2009. namespace MathNet.Numerics open System open System.Numerics - + /// The type of arbitrary-sized rational numbers [] type BigRational = @@ -30,26 +30,26 @@ namespace MathNet.Numerics interface System.IComparable /// Get zero as a rational number - static member Zero : BigRational + static member Zero : BigRational /// Get one as a rational number - static member One : BigRational + static member One : BigRational /// This operator is for use from other .NET languages static member op_Equality : BigRational * BigRational -> bool /// This operator is for use from other .NET languages static member op_Inequality : BigRational * BigRational -> bool /// This operator is for use from other .NET languages - static member op_LessThan: BigRational * BigRational -> bool + static member op_LessThan: BigRational * BigRational -> bool /// This operator is for use from other .NET languages - static member op_GreaterThan: BigRational * BigRational -> bool + static member op_GreaterThan: BigRational * BigRational -> bool /// This operator is for use from other .NET languages - static member op_LessThanOrEqual: BigRational * BigRational -> bool + static member op_LessThanOrEqual: BigRational * BigRational -> bool /// This operator is for use from other .NET languages static member op_GreaterThanOrEqual: BigRational * BigRational -> bool - + /// Return a boolean indicating if this rational number is strictly negative - member IsNegative: bool + member IsNegative: bool /// Return a boolean indicating if this rational number is strictly positive - member IsPositive: bool + member IsPositive: bool /// Return the numerator of the normalized rational number member Numerator: BigInteger @@ -58,29 +58,29 @@ namespace MathNet.Numerics member StructuredDisplayString : string - /// Return the absolute value of a rational number + /// Return the absolute value of a rational number static member Abs : BigRational -> BigRational /// Return the sign of a rational number; 0, +1 or -1 - member Sign : int + member Sign : int /// Return the result of raising the given rational number to the given power static member PowN : BigRational * int -> BigRational /// Return the result of converting the given integer to a rational number - static member FromInt : int -> BigRational + static member FromInt : int -> BigRational /// Return the result of converting the given big integer to a rational number - static member FromBigInt : BigInteger -> BigRational + static member FromBigInt : BigInteger -> BigRational /// Return the result of converting the given rational number to a floating point number - static member ToDouble: BigRational -> float + static member ToDouble: BigRational -> float /// Return the result of converting the given rational number to a big integer static member ToBigInt: BigRational -> BigInteger /// Return the result of converting the given rational number to an integer static member ToInt32 : BigRational -> int /// Return the result of converting the given rational number to a floating point number - static member op_Explicit : BigRational -> float + static member op_Explicit : BigRational -> float /// Return the result of converting the given rational number to a big integer static member op_Explicit : BigRational -> BigInteger /// Return the result of converting the given rational number to an integer static member op_Explicit : BigRational -> int - /// Return the result of converting the string to a rational number + /// Return the result of converting the string to a rational number static member Parse: string -> BigRational type BigNum = BigRational @@ -88,7 +88,7 @@ namespace MathNet.Numerics type bignum = BigRational [] - module NumericLiteralN = + module NumericLiteralN = val FromZero : unit -> BigRational val FromOne : unit -> BigRational val FromInt32 : int32 -> BigRational diff --git a/src/FSharp/complex.fs b/src/FSharp/Complex.fs similarity index 79% rename from src/FSharp/complex.fs rename to src/FSharp/Complex.fs index 949bf9d7..3a3c91af 100644 --- a/src/FSharp/complex.fs +++ b/src/FSharp/Complex.fs @@ -1,7 +1,7 @@ -// First version copied from the F# Power Pack +// First version copied from the F# Power Pack // https://raw.github.com/fsharp/powerpack/master/src/FSharp.PowerPack/math/complex.fs -// (c) Microsoft Corporation 2005-2009. +// (c) Microsoft Corporation 2005-2009. #nowarn "52" // defensive copy of structs warning @@ -21,62 +21,62 @@ namespace MathNet.Numerics member x.i = imaginary override x.ToString() = x.ToString("g") member x.ToString(fmt) = x.ToString(fmt,CultureInfo.InvariantCulture) - member x.ToString(fmt,fmtprovider:IFormatProvider) = + member x.ToString(fmt,fmtprovider:IFormatProvider) = x.r.ToString(fmt,fmtprovider)+"r"+(if x.i < 0.0 then "-" else "+")+(System.Math.Abs x.i).ToString(fmt,fmtprovider)+"i" - interface IComparable with - member x.CompareTo(obj) = - match obj with - | :? Complex as y -> + interface IComparable with + member x.CompareTo(obj) = + match obj with + | :? Complex as y -> let c = compare x.r y.r if c <> 0 then c else compare x.i y.i | _ -> invalidArg "obj" "not a Complex number" - override x.Equals(obj) = - match obj with + override x.Equals(obj) = + match obj with | :? Complex as y -> x.r = y.r && x.i = y.i | _ -> false - override x.GetHashCode() = + override x.GetHashCode() = (hash x.r >>> 5) ^^^ (hash x.r <<< 3) ^^^ (((hash x.i >>> 4) ^^^ (hash x.i <<< 4)) + 0x9e3779b9) *) - + type complex = Complex [] module private ComplexExtensionsBasic = - type Complex with + type Complex with member x.r = x.Real member x.i = x.Imaginary [] - module Complex = + module Complex = let mkRect(a,b) = new Complex(a,b) let conjugate (c:complex) = mkRect (c.r, -c.i) let mkPolar(a,b) = mkRect (a * Math.Cos(b), a * Math.Sin(b)) let cis b = mkPolar(1.0,b) let zero = mkRect(0.,0.) - let one = mkRect(1.,0.) - let onei = mkRect(0.,1.) + let one = mkRect(1.,0.) + let onei = mkRect(0.,1.) let magnitude (c:complex) = sqrt(c.r*c.r + c.i*c.i) let phase (c:complex) = Math.Atan2(c.i,c.r) let realPart (c:complex) = c.r - let imagPart (c:complex) = c.i + let imagPart (c:complex) = c.i let abs (a:complex) = sqrt (a.r**2.0 + a.i**2.0) let add (a:complex) (b:complex) = mkRect(a.r + b.r, a.i+b.i) let sub (a:complex) (b:complex) = mkRect(a.r - b.r, a.i-b.i) let mul (a:complex) (b:complex) = mkRect(a.r * b.r - a.i * b.i, a.i*b.r + b.i*a.r) - let div (x:complex) (y:complex) = - let a = x.r in let b = x.i in - let c = y.r in let d = y.i in - //(a+ib)/(c+id)=(ac+bd+i(bc-ad))/(c2+d2) - let q = c*c + d*d in + let div (x:complex) (y:complex) = + let a = x.r in let b = x.i in + let c = y.r in let d = y.i in + //(a+ib)/(c+id)=(ac+bd+i(bc-ad))/(c2+d2) + let q = c*c + d*d in mkRect((a*c+b*d)/q, (b*c - a*d)/q) let neg (a:complex) = mkRect(-a.r,-a.i) let smul (a:float)(b:complex) = mkRect(a * b.r, a*b.i) let muls (a:complex) (b:float) = mkRect(a.r *b, a.i*b) let fmt_of_string numstyle fmtprovider (s:string) = - mkRect (System.Double.Parse(s,numstyle,fmtprovider),0.0) + mkRect (System.Double.Parse(s,numstyle,fmtprovider),0.0) let of_string s = fmt_of_string NumberStyles.Any CultureInfo.InvariantCulture s - // ik.(r + i.th) = -k.th + i.k.r + // ik.(r + i.th) = -k.th + i.k.r let iscale k (x:complex) = mkRect (-k * x.i , k * x.r) // LogN : 'a * 'a -> 'a @@ -90,25 +90,25 @@ namespace MathNet.Numerics let pi = mkRect (Math.PI,0.0) - // exp(r+it) = exp(r).(cos(t)+i.sin(t)) - De Moivre Theorem + // exp(r+it) = exp(r).(cos(t)+i.sin(t)) - De Moivre Theorem let exp (x:complex) = smul (exp(x.r)) (mkRect(cos(x.i), sin(x.i))) - // x = mag.e^(i.th) = e^ln(mag).e^(i.th) = e^(ln(mag) + i.th) + // x = mag.e^(i.th) = e^ln(mag).e^(i.th) = e^(ln(mag) + i.th) let log x = mkRect (log(magnitude(x)),phase(x)) let sqrt x = mkPolar (sqrt(magnitude x),phase x / 2.0) - // cos(x) = (exp(i.x) + exp(-i.x))/2 + // cos(x) = (exp(i.x) + exp(-i.x))/2 let cos x = smul 0.5 (add (exp(iscale 1.0 x)) (exp(iscale -1.0 x))) - // sin(x) = (exp(i.x) - exp(-i.x))/2 . (-i) + // sin(x) = (exp(i.x) - exp(-i.x))/2 . (-i) let sin x = smul 0.5 (sub (exp(iscale 1.0 x)) (exp(iscale -1.0 x))) |> iscale (-1.0) - // tan(x) = (exp(i.x) - exp(-i.x)) . (-i) / (exp(i.x) + exp(-i.x)) - // = (exp(2i.x) - 1.0) . (-i) / (exp(2i.x) + 1.0) + // tan(x) = (exp(i.x) - exp(-i.x)) . (-i) / (exp(i.x) + exp(-i.x)) + // = (exp(2i.x) - 1.0) . (-i) / (exp(2i.x) + 1.0) let tan x = let exp2ix = exp(iscale 2.0 x) in (div (sub exp2ix one) (add exp2ix one)) |> iscale -1.0 [] module ComplexExtensions = - type Complex with + type Complex with member x.r = x.Real member x.i = x.Imaginary static member Create(a,b) = Complex.mkRect (a,b) @@ -126,12 +126,11 @@ namespace MathNet.Numerics 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 One = Complex.one - static member OneI = Complex.onei + static member One = Complex.one + static member OneI = Complex.onei - module ComplexTopLevelOperators = + module ComplexTopLevelOperators = let complex x y = Complex.mkRect (x,y) - diff --git a/src/FSharp/complex.fsi b/src/FSharp/Complex.fsi similarity index 93% rename from src/FSharp/complex.fsi rename to src/FSharp/Complex.fsi index fd916ef3..29308e16 100644 --- a/src/FSharp/complex.fsi +++ b/src/FSharp/Complex.fsi @@ -1,7 +1,7 @@ -// First version copied from the F# Power Pack +// First version copied from the F# Power Pack // https://raw.github.com/fsharp/powerpack/master/src/FSharp.PowerPack/math/complex.fsi -// (c) Microsoft Corporation 2005-2009. +// (c) Microsoft Corporation 2005-2009. namespace MathNet.Numerics @@ -9,7 +9,7 @@ namespace MathNet.Numerics open System.Numerics [] - module ComplexExtensions = + module ComplexExtensions = /// The type of complex numbers stored as pairs of 64-bit floating point numbers in rectangular coordinates type Complex with /// The real part of a complex number @@ -47,7 +47,7 @@ namespace MathNet.Numerics static member ( / ) : Complex * Complex -> Complex /// Unary negation of a complex number static member ( ~- ) : Complex -> Complex - /// Multiply a scalar by a complex number + /// Multiply a scalar by a complex number static member ( * ) : float * Complex -> Complex /// Multiply a complex number by a scalar static member ( * ) : Complex * float -> Complex @@ -55,7 +55,7 @@ namespace MathNet.Numerics 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.) /// Note: Complex.Abs(z) is the same as z.Magnitude static member Abs : Complex -> float @@ -63,7 +63,7 @@ namespace MathNet.Numerics static member Log : Complex -> Complex static member Exp : Complex -> Complex static member Sqrt : Complex -> Complex - + (* override ToString : unit -> string override Equals : obj -> bool @@ -72,7 +72,7 @@ namespace MathNet.Numerics member ToString : format:string * provider:System.IFormatProvider -> string *) - /// The type of complex numbers + /// The type of complex numbers type complex = Complex @@ -94,7 +94,7 @@ namespace MathNet.Numerics val mkPolar : float * float -> complex /// A complex of magnitude 1 and the given phase and , i.e. cis x = mkPolar 1.0 x val cis : float -> complex - + /// The conjugate of a complex number, i.e. x-yi val conjugate : complex -> complex @@ -114,7 +114,7 @@ namespace MathNet.Numerics val div : complex -> complex -> complex /// Unary negation of a complex number val neg : complex -> complex - /// Multiply a scalar by a complex number + /// Multiply a scalar by a complex number val smul : float -> complex -> complex /// Multiply a complex number by a scalar val muls : complex -> float -> complex @@ -128,17 +128,14 @@ namespace MathNet.Numerics /// sqrt(x) and 0 <= phase(x) < pi val sqrt : Complex -> Complex /// Sine - val sin : Complex -> Complex + val sin : Complex -> Complex /// Cosine val cos : Complex -> Complex /// Tagent val tan : Complex -> Complex - + [] - module ComplexTopLevelOperators = + module ComplexTopLevelOperators = /// Constructs a complex number from both the real and imaginary part. val complex : float -> float -> complex - - - diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index 7711a025..e0f6d1bb 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -57,10 +57,10 @@ - - - - + + + + diff --git a/src/FSharpPortable/FSharpPortable.fsproj b/src/FSharpPortable/FSharpPortable.fsproj index f6c88e25..890b4b2d 100644 --- a/src/FSharpPortable/FSharpPortable.fsproj +++ b/src/FSharpPortable/FSharpPortable.fsproj @@ -62,18 +62,18 @@ LinearAlgebra.Double.fs - - - complex.fsi + + Complex.fsi - - complex.fs + + Complex.fs - - q.fsi + + + BigRational.fsi - - q.fs + + BigRational.fs RandomVariable.fs diff --git a/src/FSharpUnitTests/PokerTests.fs b/src/FSharpUnitTests/PokerTests.fs index 48382db9..86703f26 100644 --- a/src/FSharpUnitTests/PokerTests.fs +++ b/src/FSharpUnitTests/PokerTests.fs @@ -14,32 +14,32 @@ let suit = snd let A,K,Q,J,T = 14,13,12,11,10 let allRanksInSuit suit = [2..A] |> List.map (fun rank -> rank,suit) -let completeDeck = - [Spades; Hearts ; Diamonds; Clubs] - |> List.map allRanksInSuit +let completeDeck = + [Spades; Hearts ; Diamonds; Clubs] + |> List.map allRanksInSuit |> List.concat let isPair c1 c2 = value c1 = value c2 let isSuited c1 c2 = suit c1 = suit c2 -let isConnected c1 c2 = +let isConnected c1 c2 = let v1,v2 = value c1,value c2 (v1 - v2 |> abs |> (=) 1) || (v1 = A && v2 = 2) || (v1 = 2 && v2 = A) - + [] let ``When drawing from a full deck, then the probability for an Ace should equal 4/52``() = completeDeck - |> RandomVariable.selectOne + |> RandomVariable.selectOne |> RandomVariable.map fst |> RandomVariable.filter (fun card -> value card = A) - |> RandomVariable.probability + |> RandomVariable.probability |> should equal (4N/52N) [] let ``When drawing from a full deck, then the probability should equal 1/52``() = completeDeck - |> RandomVariable.selectOne + |> RandomVariable.selectOne |> RandomVariable.map fst |> RandomVariable.filter ((=) (A,Spades)) |> RandomVariable.probability @@ -47,23 +47,23 @@ let ``When drawing from a full deck, then the probability should equal 1/52``() [] let ``When drawing from a full deck, then the probability for the Ace of Clubs and Ace of Spaces (in order) should equal 1/52 * 1/51``() = - completeDeck + completeDeck |> RandomVariable.select 2 |> RandomVariable.filter ((=) [A,Clubs; A,Spades]) - |> RandomVariable.probability + |> RandomVariable.probability |> should equal (1N/52N * 1N/51N) [] let ``When drawing from a full deck, then the probability for the Ace of Clubs and Ace of Spaces (in any order) should equal (1/52 * 1/51) * 2``() = - completeDeck + completeDeck |> RandomVariable.select 2 - |> RandomVariable.filterInAnyOrder [A,Clubs; A,Spades] + |> RandomVariable.filterInAnyOrder [A,Clubs; A,Spades] |> RandomVariable.probability |> should equal ((1N/52N * 1N/51N) * 2N) [] let ``When drawing the Ace of Spades and the Ace of Clubs, then the probability for drawing another Ace should equal 2/50``() = - completeDeck + completeDeck |> RandomVariable.remove [A,Clubs; A,Spades] |> RandomVariable.toUniformDistribution |> RandomVariable.filter (fun card -> value card = A) @@ -73,7 +73,7 @@ let ``When drawing the Ace of Spades and the Ace of Clubs, then the probability [] let ``When drawing from the full deck, then the probability for drawing a Pair preflop should equal 1/17``() = - completeDeck + completeDeck |> RandomVariable.select 2 |> RandomVariable.filter (fun (c1::c2::_) -> isPair c1 c2) |> RandomVariable.probability @@ -81,7 +81,7 @@ let ``When drawing from the full deck, then the probability for drawing a Pair p [] let ``When drawing from the full deck, then the probability for drawing Suited Connectors should equal 1/25``() = - completeDeck + completeDeck |> RandomVariable.select 2 |> RandomVariable.filter (fun (c1::c2::_) -> isSuited c1 c2 && isConnected c1 c2) |> RandomVariable.probability @@ -89,10 +89,10 @@ let ``When drawing from the full deck, then the probability for drawing Suited C [] let ``When holding 3 Spades after the flop, than the probability for drawing a flush should equal 10/47*9/46``() = - completeDeck + completeDeck |> RandomVariable.remove [A,Clubs; A,Spades] // preflop |> RandomVariable.remove [2,Clubs; 3,Spades; 7,Spades] // flop |> RandomVariable.select 2 |> RandomVariable.filter (fun (c1::c2::_) -> suit c1 = Spades && suit c2 = Spades) |> RandomVariable.probability - |> should equal (10N/47N*9N/46N) \ No newline at end of file + |> should equal (10N/47N*9N/46N) diff --git a/src/Numerics/Complex32.cs b/src/Numerics/Complex32.cs index 97b73042..43fa8465 100644 --- a/src/Numerics/Complex32.cs +++ b/src/Numerics/Complex32.cs @@ -47,9 +47,9 @@ namespace MathNet.Numerics /// The class Complex32 provides all elementary operations /// on complex numbers. All the operators +, -, /// *, /, ==, != are defined in the - /// canonical way. Additional complex trigonometric functions - /// are also provided. Note that the Complex32 structures - /// has two special constant values and + /// canonical way. Additional complex trigonometric functions + /// are also provided. Note that the Complex32 structures + /// has two special constant values and /// . /// /// @@ -936,8 +936,8 @@ namespace MathNet.Numerics var keywords = new[] { - textInfo.ListSeparator, numberFormatInfo.NaNSymbol, - numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, + textInfo.ListSeparator, numberFormatInfo.NaNSymbol, + numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, "+", "-", "i", "j" }; @@ -1066,17 +1066,17 @@ namespace MathNet.Numerics } /// - /// Converts the string representation of a complex number to a single-precision complex number equivalent. + /// Converts the string representation of a complex number to a single-precision complex number equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// - /// A string containing a complex number to convert. + /// A string containing a complex number to convert. /// /// /// The parsed value. /// /// - /// If the conversion succeeds, the result will contain a complex number equivalent to value. + /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized /// public static bool TryParse(string value, out Complex32 result) @@ -1536,4 +1536,4 @@ namespace MathNet.Numerics return (Complex32)Trig.HyperbolicTangent(value.ToComplex()); } } -} \ No newline at end of file +} diff --git a/src/Numerics/Complex64.cs b/src/Numerics/Complex64.cs index 14f4f2c4..72d55df6 100644 --- a/src/Numerics/Complex64.cs +++ b/src/Numerics/Complex64.cs @@ -50,9 +50,9 @@ namespace System.Numerics /// The class Complex provides all elementary operations /// on complex numbers. All the operators +, -, /// *, /, ==, != are defined in the - /// canonical way. Additional complex trigonometric functions - /// are also provided. Note that the Complex structures - /// has two special constant values and + /// canonical way. Additional complex trigonometric functions + /// are also provided. Note that the Complex structures + /// has two special constant values and /// . /// /// @@ -616,8 +616,8 @@ namespace System.Numerics var keywords = new[] { - textInfo.ListSeparator, numberFormatInfo.NaNSymbol, - numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, + textInfo.ListSeparator, numberFormatInfo.NaNSymbol, + numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, "+", "-", "i", "j" }; @@ -746,17 +746,17 @@ namespace System.Numerics } /// - /// Converts the string representation of a complex number to a single-precision complex number equivalent. + /// Converts the string representation of a complex number to a single-precision complex number equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// - /// A string containing a complex number to convert. + /// A string containing a complex number to convert. /// /// /// The parsed value. /// /// - /// If the conversion succeeds, the result will contain a complex number equivalent to value. + /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized /// public static bool TryParse(string value, out Complex result) @@ -1293,4 +1293,4 @@ namespace System.Numerics } } } -#endif \ No newline at end of file +#endif diff --git a/src/Numerics/ComplexExtensions.cs b/src/Numerics/ComplexExtensions.cs index 8050e43d..19c8e03f 100644 --- a/src/Numerics/ComplexExtensions.cs +++ b/src/Numerics/ComplexExtensions.cs @@ -453,8 +453,8 @@ namespace MathNet.Numerics var keywords = new[] { - textInfo.ListSeparator, numberFormatInfo.NaNSymbol, - numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, + textInfo.ListSeparator, numberFormatInfo.NaNSymbol, + numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, "+", "-", "i", "j" }; @@ -583,17 +583,17 @@ namespace MathNet.Numerics } /// - /// Converts the string representation of a complex number to a double-precision complex number equivalent. + /// Converts the string representation of a complex number to a double-precision complex number equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// - /// A string containing a complex number to convert. + /// A string containing a complex number to convert. /// /// /// The parsed value. /// /// - /// If the conversion succeeds, the result will contain a complex number equivalent to value. + /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will contain Complex.Zero. This parameter is passed uninitialized. /// public static bool TryToComplex(this string value, out Complex result) @@ -677,17 +677,17 @@ namespace MathNet.Numerics } /// - /// Converts the string representation of a complex number to a single-precision complex number equivalent. + /// Converts the string representation of a complex number to a single-precision complex number equivalent. /// A return value indicates whether the conversion succeeded or failed. /// /// - /// A string containing a complex number to convert. + /// A string containing a complex number to convert. /// /// /// The parsed value. /// /// - /// If the conversion succeeds, the result will contain a complex number equivalent to value. + /// If the conversion succeeds, the result will contain a complex number equivalent to value. /// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized. /// public static bool TryToComplex32(this string value, out Complex32 result) diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index 8adaf587..37cafee1 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -37,7 +37,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// [Serializable] public abstract class Matrix : Matrix - { + { /// /// Initializes a new instance of the Matrix class. /// @@ -67,7 +67,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// Returns the conjugate transpose of this matrix. - /// + /// /// The conjugate transpose of this matrix. public override Matrix ConjugateTranspose() { @@ -102,7 +102,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// Calculates the infinity norm of this matrix. - /// The infinity norm of this matrix. + /// The infinity norm of this matrix. public override Complex32 InfinityNorm() { var norm = 0.0f; diff --git a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs index 662dcac9..ab01ad4a 100644 --- a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs @@ -127,7 +127,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage /// Returns a hash code for this instance. /// /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { diff --git a/src/Numerics/SpecialFunctions/Stability.cs b/src/Numerics/SpecialFunctions/Stability.cs index b8c27be7..15a858ba 100644 --- a/src/Numerics/SpecialFunctions/Stability.cs +++ b/src/Numerics/SpecialFunctions/Stability.cs @@ -192,4 +192,4 @@ namespace MathNet.Numerics return sum; } } -} \ No newline at end of file +} diff --git a/src/Numerics/Trigonometry.cs b/src/Numerics/Trigonometry.cs index d856dfc7..dff560ab 100644 --- a/src/Numerics/Trigonometry.cs +++ b/src/Numerics/Trigonometry.cs @@ -111,7 +111,7 @@ namespace MathNet.Numerics } return new Complex( - Cosine(value.Real) * HyperbolicCosine(value.Imaginary), + Cosine(value.Real) * HyperbolicCosine(value.Imaginary), -Sine(value.Real) * HyperbolicSine(value.Imaginary)); } @@ -209,7 +209,7 @@ namespace MathNet.Numerics } /// - /// Trigonometric Hyperbolic Cosecant + /// Trigonometric Hyperbolic Cosecant /// /// /// The angle in radian. @@ -279,12 +279,12 @@ namespace MathNet.Numerics } return new Complex( - HyperbolicCosine(value.Real) * Cosine(value.Imaginary), + HyperbolicCosine(value.Real) * Cosine(value.Imaginary), HyperbolicSine(value.Real) * Sine(value.Imaginary)); } /// - /// Trigonometric Hyperbolic Cotangent + /// Trigonometric Hyperbolic Cotangent /// /// /// The angle in radian angle. @@ -345,7 +345,7 @@ namespace MathNet.Numerics /// The angle in radian angle. /// /// - /// The hyperbolic secant of the radian angle. + /// The hyperbolic secant of the radian angle. /// public static double HyperbolicSecant(double radian) { @@ -409,7 +409,7 @@ namespace MathNet.Numerics } return new Complex( - HyperbolicSine(value.Real) * Cosine(value.Imaginary), + HyperbolicSine(value.Real) * Cosine(value.Imaginary), HyperbolicCosine(value.Real) * Sine(value.Imaginary)); } @@ -566,7 +566,7 @@ namespace MathNet.Numerics } /// - /// Trigonometric Hyperbolic Arc Cosecant + /// Trigonometric Hyperbolic Arc Cosecant /// /// /// The angle in radian angle. @@ -595,7 +595,7 @@ namespace MathNet.Numerics } /// - /// Trigonometric Hyperbolic Area Cosine + /// Trigonometric Hyperbolic Area Cosine /// /// /// The angle in radian angle. @@ -652,7 +652,7 @@ namespace MathNet.Numerics } /// - /// Trigonometric Hyperbolic Area Secant + /// Trigonometric Hyperbolic Area Secant /// /// /// The angle in radian angle. @@ -681,7 +681,7 @@ namespace MathNet.Numerics } /// - /// Trigonometric Hyperbolic Area Sine + /// Trigonometric Hyperbolic Area Sine /// /// /// The angle in radian angle. @@ -918,7 +918,7 @@ namespace MathNet.Numerics } return new Complex( - Sine(value.Real) * HyperbolicCosine(value.Imaginary), + Sine(value.Real) * HyperbolicCosine(value.Imaginary), Cosine(value.Real) * HyperbolicSine(value.Imaginary)); } diff --git a/src/UnitTests/TrigonometryTest.cs b/src/UnitTests/TrigonometryTest.cs index ee4c7b08..46720cef 100644 --- a/src/UnitTests/TrigonometryTest.cs +++ b/src/UnitTests/TrigonometryTest.cs @@ -116,7 +116,7 @@ namespace MathNet.Numerics.UnitTests } /// - /// Can compute cosine. + /// Can compute cosine. /// /// Input value. /// Expected value. @@ -164,7 +164,7 @@ namespace MathNet.Numerics.UnitTests } /// - /// Can compute hyperbolic cosine. + /// Can compute hyperbolic cosine. /// /// Input value. /// Expected value. @@ -379,7 +379,7 @@ namespace MathNet.Numerics.UnitTests } /// - /// Can compute inverse secant. + /// Can compute inverse secant. /// /// Input value. /// Expected value. @@ -501,7 +501,7 @@ namespace MathNet.Numerics.UnitTests } /// - /// Can convert grad to radian. + /// Can convert grad to radian. /// [Test] public void CanConvertGradToRadian()