Browse Source

LA: generalize remaining F# extensions, expose Zero/One

optimization-1
Christoph Ruegg 13 years ago
parent
commit
e0745b5e16
  1. 1
      src/FSharp/FSharp.fsproj
  2. 38
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  3. 24
      src/FSharp/LinearAlgebra.Double.Vector.fs
  4. 43
      src/FSharp/LinearAlgebra.Double.fs
  5. 35
      src/FSharp/LinearAlgebra.Matrix.fs
  6. 22
      src/FSharp/LinearAlgebra.Vector.fs
  7. 3
      src/FSharpPortable/FSharpPortable.fsproj
  8. 4
      src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs
  9. 10
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  10. 10
      src/Numerics/LinearAlgebra/Vector.cs

1
src/FSharp/FSharp.fsproj

@ -66,7 +66,6 @@
<Compile Include="LinearAlgebra.Matrix.fs" />
<Compile Include="LinearAlgebra.Double.Vector.fs" />
<Compile Include="LinearAlgebra.Double.Matrix.fs" />
<Compile Include="LinearAlgebra.Double.fs" />
<Compile Include="Complex.fsi" />
<Compile Include="Complex.fs" />
<Compile Include="BigIntegerExtensions.fs" />

38
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.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Matrix =
// TODO: generalize or reconsider
/// Returns the sum of all elements of a matrix.
let inline sum (A: #Matrix<float>) = A |> Matrix.foldnz (+) 0.0
/// Fold all columns into one row vector.
let inline foldByCol f acc (A: #Matrix<float>) =
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<float>) =
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.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
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.
[<AutoOpen>]
module MatrixUtility =
/// Construct a dense matrix from a nested list of floating point numbers.
let inline matrix (lst: list<list<float>>) = DenseMatrix.ofRowList lst

24
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.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
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<float>) =
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.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
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<int * float>) = SparseVector.OfIndexedEnumerable(n, fs) :> _ Vector
/// A module which implements some F# utility functions.
[<AutoOpen>]
module VectorUtility =
/// Construct a dense vector from a list of floating point numbers.
let inline vector (lst: list<float>) = DenseVector.ofList lst

43
src/FSharp/LinearAlgebra.Double.fs

@ -1,43 +0,0 @@
// <copyright file="Main.fs" company="Math.NET">
// 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.
// </copyright>
namespace MathNet.Numerics.LinearAlgebra.Double
open MathNet.Numerics.LinearAlgebra
/// A module which implements some F# utility functions.
[<AutoOpen>]
module Utility =
/// Construct a dense matrix from a nested list of floating point numbers.
let inline matrix (lst: list<list<float>>) = DenseMatrix.ofRowList lst
/// Construct a dense vector from a list of floating point numbers.
let inline vector (lst: list<float>) = DenseVector.ofList lst

35
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 (+)

22
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)

3
src/FSharpPortable/FSharpPortable.fsproj

@ -62,9 +62,6 @@
<Compile Include="..\FSharp\LinearAlgebra.Double.Vector.fs">
<Link>LinearAlgebra.Double.Vector.fs</Link>
</Compile>
<Compile Include="..\FSharp\LinearAlgebra.Double.fs">
<Link>LinearAlgebra.Double.fs</Link>
</Compile>
<Compile Include="..\FSharp\Complex.fsi">
<Link>Complex.fsi</Link>
</Compile>

4
src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs

@ -42,12 +42,12 @@ namespace MathNet.Numerics.LinearAlgebra
/// <summary>
/// The value of 1.0.
/// </summary>
static readonly T One = Common.OneOf<T>();
public static readonly T One = Common.OneOf<T>();
/// <summary>
/// The value of 0.0.
/// </summary>
static readonly T Zero = Common.ZeroOf<T>();
public static readonly T Zero = Common.ZeroOf<T>();
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.

10
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -35,6 +35,16 @@ namespace MathNet.Numerics.LinearAlgebra
{
public abstract partial class Vector<T>
{
/// <summary>
/// The zero value for type T.
/// </summary>
public static readonly T Zero = Common.ZeroOf<T>();
/// <summary>
/// The value of 1.0 for type T.
/// </summary>
public static readonly T One = Common.OneOf<T>();
/// <summary>
/// Negates vector and save result to <paramref name="result"/>
/// </summary>

10
src/Numerics/LinearAlgebra/Vector.cs

@ -49,16 +49,6 @@ namespace MathNet.Numerics.LinearAlgebra
#endif
where T : struct, IEquatable<T>, IFormattable
{
/// <summary>
/// The zero value for type T.
/// </summary>
static readonly T Zero = Common.ZeroOf<T>();
/// <summary>
/// The value of 1.0 for type T.
/// </summary>
static readonly T One = Common.OneOf<T>();
/// <summary>
/// Initializes a new instance of the Vector class.
/// </summary>

Loading…
Cancel
Save