diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index 859175de..fae1e872 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -66,7 +66,6 @@ - diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index be854200..3c12adb7 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -32,36 +32,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double open MathNet.Numerics.LinearAlgebra -/// A module which implements functional matrix operations. -[] -module Matrix = - - // TODO: generalize or reconsider - - /// Returns the sum of all elements of a matrix. - let inline sum (A: #Matrix) = A |> Matrix.foldnz (+) 0.0 - - /// Fold all columns into one row vector. - let inline foldByCol f acc (A: #Matrix) = - let v = new DenseVector(A.ColumnCount) - for k=0 to A.ColumnCount-1 do - let mutable macc = acc - for i=0 to A.RowCount-1 do - macc <- f macc (A.At(i,k)) - v.At(k, macc) - v :> _ Vector - - /// Fold all rows into one column vector. - let inline foldByRow f acc (A: #Matrix) = - let v = new DenseVector(A.RowCount) - for k=0 to A.RowCount-1 do - let mutable macc = acc - for i=0 to A.ColumnCount-1 do - macc <- f macc (A.At(k,i)) - v.At(k, macc) - v :> _ Vector - - /// A module which implements functional dense vector operations. [] module DenseMatrix = @@ -226,3 +196,11 @@ module SparseMatrix = /// Create a matrix with the array elements on the diagonal. let inline ofDiagArray2 (rows: int) (cols: int) (array: float array) = SparseMatrix.OfDiagonalArray(rows, cols, array) :> _ Matrix + + +/// A module which implements some F# utility functions. +[] +module MatrixUtility = + + /// Construct a dense matrix from a nested list of floating point numbers. + let inline matrix (lst: list>) = DenseMatrix.ofRowList lst diff --git a/src/FSharp/LinearAlgebra.Double.Vector.fs b/src/FSharp/LinearAlgebra.Double.Vector.fs index 4b3de758..37899226 100644 --- a/src/FSharp/LinearAlgebra.Double.Vector.fs +++ b/src/FSharp/LinearAlgebra.Double.Vector.fs @@ -32,22 +32,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double open MathNet.Numerics.LinearAlgebra -/// A module which implements functional vector operations. -[] -module Vector = - - // TODO: generalize or reconsider - - /// Creates a new vector and inserts the given value at the given index. - let inline insert index value (v: #Vector) = - let newV = new DenseVector(v.Count + 1) - for i = 0 to index - 1 do - newV.At(i, v.At i) - newV.At(index, value) - for i = index + 1 to v.Count do - newV.At(i, v.At (i - 1)) - newV - /// A module which implements functional dense vector operations. [] module DenseVector = @@ -116,3 +100,11 @@ module SparseVector = /// Create a sparse vector with a given dimension from an indexed sequence of index, value pairs. let inline ofSeqi (n: int) (fs: #seq) = SparseVector.OfIndexedEnumerable(n, fs) :> _ Vector + + +/// A module which implements some F# utility functions. +[] +module VectorUtility = + + /// Construct a dense vector from a list of floating point numbers. + let inline vector (lst: list) = DenseVector.ofList lst diff --git a/src/FSharp/LinearAlgebra.Double.fs b/src/FSharp/LinearAlgebra.Double.fs deleted file mode 100644 index 827d2821..00000000 --- a/src/FSharp/LinearAlgebra.Double.fs +++ /dev/null @@ -1,43 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2012 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -namespace MathNet.Numerics.LinearAlgebra.Double - -open MathNet.Numerics.LinearAlgebra - -/// A module which implements some F# utility functions. -[] -module Utility = - - /// Construct a dense matrix from a nested list of floating point numbers. - let inline matrix (lst: list>) = DenseMatrix.ofRowList lst - - /// Construct a dense vector from a list of floating point numbers. - let inline vector (lst: list) = DenseVector.ofList lst diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index f81408b2..10838b2f 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Matrix.fs @@ -359,10 +359,43 @@ module Matrix = let inline inplaceAssign f (A: #Matrix<_>) = A.MapIndexedInplace((fun i j x -> f i j), true) + /// Fold all columns into one row vector. + let inline foldByCol f acc (A: #Matrix<_>) = + let v = A.CreateVector(A.ColumnCount) + for k=0 to A.ColumnCount-1 do + let mutable macc = acc + for i=0 to A.RowCount-1 do + macc <- f macc (A.At(i,k)) + v.At(k, macc) + v :> _ Vector + + /// Fold all rows into one column vector. + let inline foldByRow f acc (A: #Matrix<_>) = + let v = A.CreateVector(A.RowCount) + for k=0 to A.RowCount-1 do + let mutable macc = acc + for i=0 to A.ColumnCount-1 do + macc <- f macc (A.At(k,i)) + v.At(k, macc) + v :> _ Vector + + + + /// In-place matrix addition. + let inline addInPlace (v: #Matrix<_>) (w: #Matrix<_>) = v.Add(w, v) + + /// In place matrix subtraction. + let inline subInPlace (v: #Matrix<_>) (w: #Matrix<_>) = v.Subtract(w, v) + + + + /// Returns the sum of all elements of a matrix. + let inline sum (A: #Matrix<'a>) = A |> foldnz (+) Matrix<'a>.Zero + /// Returns the sum of the results generated by applying a position dependent function to each column of the matrix. let inline sumColsBy f (A: #Matrix<_>) = A.EnumerateColumnsIndexed() |> Seq.map (fun (j,col) -> f j col) |> Seq.reduce (+) /// Returns the sum of the results generated by applying a position dependent function to each row of the matrix. let inline sumRowsBy f (A: #Matrix<_>) = - A.EnumerateRowsIndexed() |> Seq.map (fun (i,row) -> f i row) |> Seq.reduce (+) + A.EnumerateRowsIndexed() |> Seq.map (fun (i,row) -> f i row) |> Seq.reduce (+) \ No newline at end of file diff --git a/src/FSharp/LinearAlgebra.Vector.fs b/src/FSharp/LinearAlgebra.Vector.fs index 10b77f7f..716af7e5 100644 --- a/src/FSharp/LinearAlgebra.Vector.fs +++ b/src/FSharp/LinearAlgebra.Vector.fs @@ -202,12 +202,6 @@ module Vector = - /// In-place vector addition. - let inline addInPlace (v: #Vector<_>) (w: #Vector<_>) = v.Add(w, v) - - /// In place vector subtraction. - let inline subInPlace (v: #Vector<_>) (w: #Vector<_>) = v.Subtract(w, v) - /// Fold all entries of a vector in reverse order. let inline foldBack f state (v: #Vector<_>) = let mutable acc = state @@ -231,3 +225,19 @@ module Vector = rstate := f (v.At(i)) !rstate yield !rstate } + + /// Creates a new vector and inserts the given value at the given index. + let inline insert index value (v: #Vector<_>) = + let newV = v.CreateVector(v.Count + 1) + v.CopySubVectorTo(newV, 0, 0, index) + v.CopySubVectorTo(newV, index, index+1, v.Count - index) + newV.At(index, value) + newV + + + + /// In-place vector addition. + let inline addInPlace (v: #Vector<_>) (w: #Vector<_>) = v.Add(w, v) + + /// In place vector subtraction. + let inline subInPlace (v: #Vector<_>) (w: #Vector<_>) = v.Subtract(w, v) diff --git a/src/FSharpPortable/FSharpPortable.fsproj b/src/FSharpPortable/FSharpPortable.fsproj index 4d22b8e3..453e4d60 100644 --- a/src/FSharpPortable/FSharpPortable.fsproj +++ b/src/FSharpPortable/FSharpPortable.fsproj @@ -62,9 +62,6 @@ LinearAlgebra.Double.Vector.fs - - LinearAlgebra.Double.fs - Complex.fsi diff --git a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs index 50a366f9..14b53c96 100644 --- a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs @@ -42,12 +42,12 @@ namespace MathNet.Numerics.LinearAlgebra /// /// The value of 1.0. /// - static readonly T One = Common.OneOf(); + public static readonly T One = Common.OneOf(); /// /// The value of 0.0. /// - static readonly T Zero = Common.ZeroOf(); + public static readonly T Zero = Common.ZeroOf(); /// /// Negate each element of this matrix and place the results into the result matrix. diff --git a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs index 876259ff..33e38b79 100644 --- a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs @@ -35,6 +35,16 @@ namespace MathNet.Numerics.LinearAlgebra { public abstract partial class Vector { + /// + /// The zero value for type T. + /// + public static readonly T Zero = Common.ZeroOf(); + + /// + /// The value of 1.0 for type T. + /// + public static readonly T One = Common.OneOf(); + /// /// Negates vector and save result to /// diff --git a/src/Numerics/LinearAlgebra/Vector.cs b/src/Numerics/LinearAlgebra/Vector.cs index e5343ce6..50f0662a 100644 --- a/src/Numerics/LinearAlgebra/Vector.cs +++ b/src/Numerics/LinearAlgebra/Vector.cs @@ -49,16 +49,6 @@ namespace MathNet.Numerics.LinearAlgebra #endif where T : struct, IEquatable, IFormattable { - /// - /// The zero value for type T. - /// - static readonly T Zero = Common.ZeroOf(); - - /// - /// The value of 1.0 for type T. - /// - static readonly T One = Common.OneOf(); - /// /// Initializes a new instance of the Vector class. ///