Skip to content

Commit

Permalink
Refactoring: changing namesspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
muehlhaus committed May 26, 2017
1 parent 0aabfb2 commit 1f9d221
Show file tree
Hide file tree
Showing 17 changed files with 1,043 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
// RV = row vector (dense)


namespace Microsoft.FSharp.Math
//namespace Microsoft.FSharp.Math // old namespace
namespace FSharp.Stats

#nowarn "60" // implementations in augmentations
#nowarn "69" // implementations in augmentations

open Microsoft.FSharp.Math
//open Microsoft.FSharp.Math
open System
open System.Globalization
open System.Collections
Expand Down Expand Up @@ -1222,8 +1223,10 @@ namespace Microsoft.FSharp.Math
/// Generic operations that, when used on floating point types, use the specialized versions in DoubleImpl
module SpecializedGenericImpl =

open Microsoft.FSharp.Math.Instances
open Microsoft.FSharp.Math.GlobalAssociations
// open Microsoft.FSharp.Math.Instances
// open Microsoft.FSharp.Math.GlobalAssociations
open FSharp.Stats.Instances
open FSharp.Stats.GlobalAssociations

module GU = GenericImpl
module DS = DoubleImpl
Expand Down
3 changes: 2 additions & 1 deletion src/FSharp.Stats/Algebra/ILinearAlgebra.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace FSharp.Stats.Algebra

open Microsoft.FSharp.Math
//namespace Microsoft.FSharp.Math // old namespace
open FSharp.Stats


///This is an internal interface and not for user usage.
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Stats/Algebra/LinearAlgebra.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.FSharp.Math.LinearAlgebra
namespace FSharp.Stats.Algebra

open Microsoft.FSharp.Math
open FSharp.Stats


module LinearAlgebra =
Expand Down
6 changes: 4 additions & 2 deletions src/FSharp.Stats/Algebra/LinearAlgebraServiceManaged.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// (c) Microsoft Corporation 2005-2009.

namespace Microsoft.FSharp.Math.LinearAlgebra
//namespace Microsoft.FSharp.Math.LinearAlgebra // old namespace
namespace FSharp.Stats.Algebra

open Microsoft.FSharp.Math
//open Microsoft.FSharp.Math
//open Microsoft.FSharp.Math. Bindings.Internals.NativeUtilities
open FSharp.Stats

/// This module is for internal use only.
module LinearAlgebraManaged =
Expand Down
3 changes: 2 additions & 1 deletion src/FSharp.Stats/Algebra/NativeArrayExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Microsoft.FSharp.NativeInterop
open System
open System.Runtime.InteropServices
open Microsoft.FSharp.NativeInterop
open Microsoft.FSharp.Math
//open Microsoft.FSharp.Math
open FSharp.Stats

[<AutoOpen>]
module NativArrayExtensionsForMatrix =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#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

namespace Microsoft.FSharp.Math
//namespace Microsoft.FSharp.Math // old namespace
namespace FSharp.Stats

open System
open System.Numerics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#nowarn "52" // defensive copy of structs warning

namespace Microsoft.FSharp.Math
//namespace Microsoft.FSharp.Math // old namespace
namespace FSharp.Stats

open Microsoft.FSharp.Math
open System
Expand Down
24 changes: 14 additions & 10 deletions src/FSharp.Stats/FSharp.Stats.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@
<Compile Include="Ops.fs" />
<Compile Include="Random.fs" />
<Compile Include="ServiceLocator.fs" />
<!-- Math -->
<Compile Include="Permutation.fs" />
<Compile Include="BigRational.fs" />
<Compile Include="INumeric.fs" />
<Compile Include="Complex.fs" />
<Compile Include="GlobalAssociations.fs" />
<Compile Include="AlgTypes.fs" />
<Compile Include="Matrix.fs" />
<Compile Include="Vector.fs" />
<Compile Include="RowVector.fs" />
<Compile Include="MatrixTopLevelOperators.fs" />
<!-- Col -->
<Compile Include="Intervals.fs" />
<Compile Include="SeqStats.fs" />
<!-- SpecialFunctions -->
<Compile Include="SpecialFunctions\Gamma.fs" />
<Compile Include="SpecialFunctions\Factorial.fs" />
Expand All @@ -73,17 +87,7 @@
<Compile Include="SpecialFunctions\Logistic.fs" />
<Compile Include="SpecialFunctions\Binomial.fs" />
<!-- Algebra -->
<Compile Include="Algebra\Permutation.fs" />
<Compile Include="Algebra\NativeArray.fs" />
<Compile Include="Algebra\BigRational.fs" />
<Compile Include="Algebra\INumeric.fs" />
<Compile Include="Algebra\Complex.fs" />
<Compile Include="Algebra\GlobalAssociations.fs" />
<Compile Include="Algebra\AlgTypes.fs" />
<Compile Include="Algebra\Matrix.fs" />
<Compile Include="Algebra\Vector.fs" />
<Compile Include="Algebra\RowVector.fs" />
<Compile Include="Algebra\MatrixTopLevelOperators.fs" />
<Compile Include="Algebra\NativeArrayExtensions.fs" />
<Compile Include="Algebra\ILinearAlgebra.fs" />
<Compile Include="Algebra\LinearAlgebraServiceManaged.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// (c) Microsoft Corporation 2005-2009.

namespace Microsoft.FSharp.Math
//namespace Microsoft.FSharp.Math // old namespace
namespace FSharp.Stats

module GlobalAssociations =

open Microsoft.FSharp.Math
open Microsoft.FSharp.Math.Instances
// open Microsoft.FSharp.Math
// open Microsoft.FSharp.Math.Instances
open FSharp.Stats.Instances
open System
open System.Numerics

let ComplexNumerics =
{ new IFractional<_> with
member __.Zero = Microsoft.FSharp.Math.Complex.Zero
member __.One = Microsoft.FSharp.Math.Complex.One
member __.Zero = FSharp.Stats.Complex.Zero
member __.One = FSharp.Stats.Complex.One
member __.Add(a,b) = a + b
member __.Subtract(a,b) = a - b
member __.Multiply(a,b) = a * b
Expand All @@ -22,9 +24,9 @@ module GlobalAssociations =
member __.Negate(a) = -a
member __.Abs(a) = a // not signed
member __.Sign(a) = 1 // not signed
member __.Reciprocal(a) = Microsoft.FSharp.Math.Complex.One / a
member __.ToString((x:Microsoft.FSharp.Math.Complex),fmt,fmtprovider) = x.ToString(fmt,fmtprovider)
member __.Parse(s,numstyle,fmtprovider) = Microsoft.FSharp.Math.Complex.mkRect (System.Double.Parse(s,numstyle,fmtprovider),0.0) }
member __.Reciprocal(a) = FSharp.Stats.Complex.One / a
member __.ToString((x:FSharp.Stats.Complex),fmt,fmtprovider) = x.ToString(fmt,fmtprovider)
member __.Parse(s,numstyle,fmtprovider) = FSharp.Stats.Complex.mkRect (System.Double.Parse(s,numstyle,fmtprovider),0.0) }

let ht =
let ht = new System.Collections.Generic.Dictionary<Type,obj>()
Expand All @@ -34,7 +36,7 @@ module GlobalAssociations =
typeof<int64>, (Some(Int64Numerics :> INumeric<int64>) :> obj);
typeof<BigInteger>, (Some(BigIntNumerics :> INumeric<BigInteger>) :> obj);
typeof<float32>, (Some(Float32Numerics :> INumeric<float32>) :> obj);
typeof<Microsoft.FSharp.Math.Complex>, (Some(ComplexNumerics :> INumeric<Microsoft.FSharp.Math.Complex>) :> obj);
typeof<FSharp.Stats.Complex>, (Some(ComplexNumerics :> INumeric<FSharp.Stats.Complex>) :> obj);
typeof<bignum>, (Some(BigRationalNumerics :> INumeric<bignum>) :> obj); ]

List.iter (fun (ty,ops) -> ht.Add(ty,ops)) optab;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// (c) Microsoft Corporation 2005-2009.

namespace Microsoft.FSharp.Math
//namespace Microsoft.FSharp.Math // old namespace
namespace FSharp.Stats

open Microsoft.FSharp.Math
//open Microsoft.FSharp.Math
open System
open System.Numerics
open System.Globalization
Expand Down
204 changes: 204 additions & 0 deletions src/FSharp.Stats/Intervals.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
namespace FSharp.Stats

module Intervals =

/// Closed interval [Start,End]
type Interval<'a> =
| ClosedInterval of 'a * 'a
| Empty

/// Creates closed interval [min,max] by given min and max
let create min max =
ClosedInterval (min, max)

/// Creates closed interval [min,max] by given start and size
let ofSize min size =
ClosedInterval (min, min + size)


/// Returns the size of an closed interval
let inline trySize interval =
match interval with
| ClosedInterval (min,max) -> Some (max - min)
| Empty -> None


/// Returns the interval as a string
let toString interval =
match interval with
| ClosedInterval (min,max) -> sprintf "[%A,%A]" min max
| Empty -> "[empty]"


/// Add two given intervals.
let add a b =
match a,b with
| ClosedInterval (minA,maxA), ClosedInterval (minB,maxB)
-> ClosedInterval (minA + minB, maxA + maxB)
| ClosedInterval (min,max), Empty -> a
| Empty, ClosedInterval (min,max) -> b
| Empty,Empty -> Empty



/// Subtract a given interval from the other interval.
let subtract a b =
match a,b with
| ClosedInterval (minA,maxA), ClosedInterval (minB,maxB)
-> ClosedInterval (minA - maxB, maxA - minB)
| ClosedInterval (min,max), Empty -> a
| Empty, ClosedInterval (min,max) -> b
| Empty,Empty -> Empty


// a0----a1
// b0-----b1
/// Checking for intersection of both intervals
let isIntersection a b =
match a,b with
| ClosedInterval (minA,maxA), ClosedInterval (minB,maxB)
-> minA <= maxB && minB <= maxA
| ClosedInterval (min,max), Empty -> false
| Empty, ClosedInterval (min,max) -> false
| Empty,Empty -> true


/// Returns the intersection of this interval with another.
let intersect a b =
if not (isIntersection a b) then
None
else
match a,b with
| ClosedInterval (minA,maxA), ClosedInterval (minB,maxB)
-> if not (minA <= maxB && minB <= maxA) then
None
else
let min' = max minA minB
let max' = min maxA maxB
ClosedInterval (min',max') |> Some
| ClosedInterval (min,max), Empty -> None
| Empty, ClosedInterval (min,max) -> None
| Empty,Empty -> Some (Empty)



/// Get the value at a given percentage within (0.0 - 1.0) or outside (< 0.0, > 1.0) of the interval. Rounding to nearest neighbour occurs when needed.
let inline getValueAt percentage interval =
match trySize interval with
| Some size -> percentage * (float size)
| None -> nan


/// Does the given value lie in the interval or not.
let liesInInterval value interval =
match interval with
| ClosedInterval (min,max) -> value >= min && value <= max
| Empty -> false



// ####################################################

// interval tree
//http://www.geeksforgeeks.org/interval-tree/
// https://fgiesen.wordpress.com/2011/10/16/checking-for-interval-overlap/
// https://github.com/Whathecode/Framework-Class-Library-Extension/blob/master/Whathecode.System/Arithmetic/Range/Interval.cs


///// <summary>
///// Get a percentage how far inside (0.0 - 1.0) or outside (< 0.0, > 1.0) the interval a certain value lies.
///// For single intervals, '1.0' is returned when inside the interval, '-1.0' otherwise.
///// </summary>
///// <param name = "position">The position value to get the percentage for.</param>
///// <returns>The percentage indicating how far inside (or outside) the interval the given value lies.</returns>
//let getPercentageFor position r =
// let inside = liesInInterval position r
// let sizeR = size r
// if (sizeR = 0.0) then
// if inside then 1.0 else -1.0
// else
// let rangeP = create r.Start position
// size rangeP / sizeR
//
///// Map a value from the source range, to a value in another range (target) linearly.
//let map source target value =
// let tmp = getPercentageFor value source
// getValueAt tmp target
//
///// Limit a given value to the range of the intertval. When the value is smaller/bigger than the range, snap it to the range border.
//let clampSingelton value r =
// if value < r.Start then r.Start
// elif value > r.End then r.End
// else value
//
//
//
///// Limit the target range to the source range.
///// When part of the given range lies outside of this range, it isn't included in the resulting range.
//let clamp source target =
// failwith "not implemented"
//
//
///// Split the interval into two intervals at the given point, or nearest valid point.
//let split atPoint interval =
// failwith "not implemented"
//
//
//
//
//
///// Get values for each step within the interval.
//let getValues stepSize interval =
// let rec gen c =
// seq {
// if c <= interval.End then
// let uc = c + stepSize
// yield uc
// yield! gen uc
// }
// gen interval.Start
//
//
///// Returns a reversed version of the current interval, swapping the start position with the end position.
//let reverse interval =
// create interval.End interval.Start
//
///// Checks if interval is reversed
//let isReversed interval =
// interval.End < interval.Start
//
///// Returns an interval offsetted from the current interval by a specified amount.
//let move amount interval =
// create (interval.Start + amount) (interval.End + amount)
//
///// <summary>
///// Returns a scaled version of the current interval, but prevents the interval from exceeding the values specified in a passed limit.
///// This is useful to prevent <see cref="ArgumentOutOfRangeException" /> during calculations for certain types.
///// </summary>
///// <param name="scale">
///// Percentage to scale the interval up or down.
///// Smaller than 1.0 to scale down, larger to scale up.
///// </param>
///// <param name="limit">The limit which the interval snaps to when scaling exceeds it.</param>
///// <param name="aroundPercentage">The percentage inside the interval around which to scale.</param>
//let scale a =
// failwith "not implemented"
//
/////<summary>
///// Returns an expanded interval of the current interval up to the given value.
///// When the value lies within the interval the returned interval is the same.
///// </summary>
///// <param name = "value">The value up to which to expand the interval.</param>
///// <param name = "include">Include the value to which is expanded in the interval.</param>
//let expandTo a =
// failwith "not implemented"
//
//let r1 = create 1.5 2.5
//let r2 = create 2.0 4.5
//
//let r3 = create 5.0 6.0
//
//
//getPercentageFor 2.6 r1


Loading

0 comments on commit 1f9d221

Please sign in to comment.