Browse Source

FSharp: use more efficient constructs where possible

v2
Christoph Ruegg 14 years ago
parent
commit
12c918202a
  1. 62
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  2. 70
      src/FSharp/LinearAlgebra.Double.Vector.fs

62
src/FSharp/LinearAlgebra.Double.Matrix.fs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2012 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -43,7 +43,7 @@ module Matrix =
let mutable acc = acc0
for i=0 to n-1 do
for j=0 to m-1 do
acc <- f acc (A.Item(i,j))
acc <- f acc (A.At(i,j))
acc
/// Fold a function over all matrix elements in reverse order.
@ -53,7 +53,7 @@ module Matrix =
let mutable acc = acc0
for i in n-1 .. -1 .. 0 do
for j in m-1 .. -1 .. 0 do
acc <- f (A.Item(i,j)) acc
acc <- f (A.At(i,j)) acc
acc
/// Fold a matrix by applying a given function to all matrix elements.
@ -63,7 +63,7 @@ module Matrix =
let mutable acc = acc0
for i=0 to n-1 do
for j=0 to m-1 do
acc <- f i j acc (A.Item(i,j))
acc <- f i j acc (A.At(i,j))
acc
/// Create a 2D array from a matrix.
@ -78,7 +78,7 @@ module Matrix =
let mutable i = 0
let mutable j = 0
while b && i < A.RowCount do
b <- b && (p (A.Item(i,j)))
b <- b && (p (A.At(i,j)))
j <- j+1
if j = A.ColumnCount then i <- i+1; j <- 0
b
@ -89,7 +89,7 @@ module Matrix =
let mutable i = 0
let mutable j = 0
while not(b) && i < A.RowCount do
b <- b || (p (A.Item(i,j)))
b <- b || (p (A.At(i,j)))
j <- j+1
if j = A.ColumnCount then i <- i+1; j <- 0
b
@ -100,7 +100,7 @@ module Matrix =
let mutable i = 0
let mutable j = 0
while b && i < A.RowCount do
b <- b && (p i j (A.Item(i,j)))
b <- b && (p i j (A.At(i,j)))
j <- j+1
if j = A.ColumnCount then i <- i+1; j <- 0
b
@ -111,7 +111,7 @@ module Matrix =
let mutable i = 0
let mutable j = 0
while not(b) && i < A.RowCount do
b <- b || (p i j (A.Item(i,j)))
b <- b || (p i j (A.At(i,j)))
j <- j+1
if j = A.ColumnCount then i <- i+1; j <- 0
b
@ -123,7 +123,7 @@ module Matrix =
let C = A.Clone()
for i=0 to N-1 do
for j=0 to M-1 do
C.[i,j] <- f (C.Item(i,j))
C.At(i, j, f (C.At(i,j)))
C
/// Map every matrix element using the given position dependent function.
@ -133,7 +133,7 @@ module Matrix =
let C = A.Clone()
for i=0 to N-1 do
for j=0 to M-1 do
C.[i,j] <- f i j (C.Item(i,j))
C.At(i, j, f i j (C.At(i,j)))
C
/// In-place map every matrix column using the given position dependent function.
@ -164,26 +164,26 @@ module Matrix =
let inline inplaceAssign (f: int -> int -> float) (A: #Matrix<float>) =
for i=0 to A.RowCount-1 do
for j=0 to A.ColumnCount-1 do
A.Item(i,j) <- f i j
A.At(i, j, f i j)
/// In-place map of every matrix element using a position dependent function.
let inline inplaceMapi (f: int -> int -> float -> float) (A: #Matrix<float>) =
for i=0 to A.RowCount-1 do
for j=0 to A.ColumnCount-1 do
A.Item(i,j) <- f i j (A.Item(i,j))
A.At(i, j, f i j (A.At(i,j)))
/// Creates a sequence that iterates the non-zero entries in the matrix.
let inline nonZeroEntries (A: #Matrix<float>) =
seq { for i in 0 .. A.RowCount-1 do
for j in 0 .. A.ColumnCount-1 do
if A.Item(i,j) <> 0.0 then yield (i,j, A.Item(i,j)) }
if A.At(i,j) <> 0.0 then yield (i, j, A.At(i,j)) }
/// Returns the sum of all elements of a matrix.
let inline sum (A: #Matrix<float>) =
let mutable f = 0.0
for i=0 to A.RowCount-1 do
for j=0 to A.ColumnCount-1 do
f <- f + A.Item(i,j)
f <- f + A.At(i,j)
f
/// Returns the sum of the results generated by applying a position dependent function to each column of the matrix.
@ -198,14 +198,14 @@ module Matrix =
let inline iter (f: float -> unit) (A: #Matrix<float>) =
for i=0 to A.RowCount-1 do
for j=0 to A.ColumnCount-1 do
f (A.Item(i,j))
f (A.At(i,j))
()
/// Iterates over all elements of a matrix using the element indices.
let inline iteri (f: int -> int -> float -> unit) (A: #Matrix<float>) =
for i=0 to A.RowCount-1 do
for j=0 to A.ColumnCount-1 do
f i j (A.Item(i,j))
f i j (A.At(i,j))
()
/// Fold one column.
@ -228,8 +228,8 @@ module Matrix =
for k=0 to A.ColumnCount-1 do
let mutable macc = acc
for i=0 to A.RowCount-1 do
macc <- f macc (A.Item(i,k))
v.[k] <- macc
macc <- f macc (A.At(i,k))
v.At(k, macc)
v :> Vector<float>
/// Fold all rows into one column vector.
@ -238,8 +238,8 @@ module Matrix =
for k=0 to A.RowCount-1 do
let mutable macc = acc
for i=0 to A.ColumnCount-1 do
macc <- f macc (A.Item(k,i))
v.[k] <- macc
macc <- f macc (A.At(k,i))
v.At(k, macc)
v :> Vector<float>
/// A module which implements functional dense vector operations.
@ -251,7 +251,7 @@ module DenseMatrix =
let A = new DenseMatrix(n,m)
for i=0 to n-1 do
for j=0 to m-1 do
A.[i,j] <- f i j
A.At(i, j, f i j)
A
/// Create a matrix from a list of float lists. Every list in the master list specifies a row.
@ -261,7 +261,7 @@ module DenseMatrix =
let A = DenseMatrix(n,m)
fll |> List.iteri (fun i fl ->
if (List.length fl) <> m then failwith "Each subrow must be of the same length." else
List.iteri (fun j f -> A.[i,j] <- f) fl)
List.iteri (fun j f -> A.At(i,j,f)) fl)
A
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
@ -271,7 +271,7 @@ module DenseMatrix =
let A = DenseMatrix(n,m)
fss |> Seq.iteri (fun i fs ->
if (Seq.length fs) <> m then failwith "Each subrow must be of the same length." else
Seq.iteri (fun j f -> A.[i,j] <- f) fs)
Seq.iteri (fun j f -> A.At(i,j,f)) fs)
A
/// Create a matrix from a 2D array of floating point numbers.
@ -280,14 +280,14 @@ module DenseMatrix =
/// Create a matrix with the given entries.
let inline initDense (n: int) (m: int) (es: #seq<int * int * float>) =
let A = new DenseMatrix(n,m)
Seq.iter (fun (i,j,f) -> A.[i,j] <- f) es
Seq.iter (fun (i,j,f) -> A.At(i,j,f)) es
A
/// Create a square matrix with constant diagonal entries.
let inline constDiag (n: int) (f: float) =
let A = new DenseMatrix(n,n)
for i=0 to n-1 do
A.[i,i] <- f
A.At(i,i,f)
A
/// Create a square matrix with the vector elements on the diagonal.
@ -295,7 +295,7 @@ module DenseMatrix =
let n = v.Count
let A = new DenseMatrix(n,n)
for i=0 to n-1 do
A.[i,i] <- v.Item(i)
A.At(i,i,v.At(i))
A
/// Initialize a matrix by calling a construction function for every row.
@ -317,20 +317,20 @@ module SparseMatrix =
/// Create a matrix from a list of float lists. Every list in the master list specifies a row.
let inline ofList (rows: int) (cols: int) (fll: list<int * int * float>) =
let A = new SparseMatrix(rows, cols)
fll |> List.iter (fun (i, j, x) -> A.[i,j] <- x)
fll |> List.iter (fun (i, j, x) -> A.At(i,j,x))
A
/// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row.
let inline ofSeq (rows: int) (cols: int) (fss: #seq<int * int * float>) =
let A = new SparseMatrix(rows, cols)
fss |> Seq.iter (fun (i, j, x) -> A.[i,j] <- x)
fss |> Seq.iter (fun (i, j, x) -> A.At(i,j,x))
A
/// Create a square matrix with constant diagonal entries.
let inline constDiag (n: int) (f: float) =
let A = new SparseMatrix(n,n)
for i=0 to n-1 do
A.[i,i] <- f
A.At(i,i,f)
A
/// Create a square matrix with the vector elements on the diagonal.
@ -338,7 +338,7 @@ module SparseMatrix =
let n = v.Count
let A = new SparseMatrix(n,n)
for i=0 to n-1 do
A.[i,i] <- v.Item(i)
A.At(i,i, v.At i)
A
/// Initialize a matrix by calling a construction function for every row.
@ -351,4 +351,4 @@ module SparseMatrix =
let inline initCol (n: int) (m: int) (f: int -> #Vector<float>) =
let A = new SparseMatrix(n,m)
for i=0 to m-1 do A.SetColumn(i, f i)
A
A

70
src/FSharp/LinearAlgebra.Double.Vector.fs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2012 Math.NET
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -37,25 +37,21 @@ open MathNet.Numerics.LinearAlgebra.Generic
module Vector =
/// Transform a vector into an array.
let inline toArray (v: #Vector<float>) =
let n = v.Count
Array.init n (fun i -> v.Item(i))
let inline toArray (v: #Vector<float>) = v.ToArray()
/// Transform a vector into an array.
let inline toList (v: #Vector<float>) =
let n = v.Count
List.init n (fun i -> v.Item(i))
/// Transform a vector into a list.
let inline toList (v: #Vector<float>) = List.init v.Count v.At
/// In-place mutation by applying a function to every element of the vector.
let inline mapInPlace (f: float -> float) (v: #Vector<float>) =
for i=0 to v.Count-1 do
v.Item(i) <- f (v.Item(i))
v.At(i, f (v.At i))
()
/// In-place mutation by applying a function to every element of the vector.
let inline mapiInPlace (f: int -> float -> float) (v: #Vector<float>) =
for i=0 to v.Count-1 do
v.Item(i) <- f i (v.Item(i))
v.At(i, f i (v.At i))
()
/// In-place vector addition.
@ -74,12 +70,12 @@ module Vector =
/// Applies a function to all elements of the vector.
let inline iter (f: float -> unit) (v: #Vector<float>) =
for i=0 to v.Count-1 do
f (v.Item i)
f (v.At i)
/// Applies a function to all elements of the vector.
let inline iteri (f: int -> float -> unit) (v: #Vector<float>) =
for i=0 to v.Count-1 do
f i (v.Item i)
f i (v.At i)
/// Maps a vector to a new vector by applying a function to every element.
let inline mapi (f: int -> float -> float) (v: #Vector<float>) =
@ -91,21 +87,21 @@ module Vector =
let inline fold (f: 'a -> float -> 'a) (acc0: 'a) (v: #Vector<float>) =
let mutable acc = acc0
for i=0 to v.Count-1 do
acc <- f acc (v.Item(i))
acc <- f acc (v.At i)
acc
/// Fold all entries of a vector in reverse order.
let inline foldBack (f: float -> 'a -> 'a) (acc0: 'a) (v: #Vector<float>) =
let mutable acc = acc0
for i=2 to v.Count do
acc <- f (v.Item(v.Count - i)) acc
acc <- f (v.At (v.Count - i)) acc
acc
/// Fold all entries of a vector using a position dependent folding function.
let inline foldi (f: int -> 'a -> float -> 'a) (acc0: 'a) (v: #Vector<float>) =
let mutable acc = acc0
for i=0 to v.Count-1 do
acc <- f i acc (v.Item(i))
acc <- f i acc (v.At i)
acc
/// Checks whether a predicate is satisfied for every element in the vector.
@ -113,7 +109,7 @@ module Vector =
let mutable b = true
let mutable i = 0
while b && i < v.Count do
b <- b && (p (v.Item(i)))
b <- b && (p (v.At i))
i <- i+1
b
@ -122,7 +118,7 @@ module Vector =
let mutable b = false
let mutable i = 0
while not(b) && i < v.Count do
b <- b || (p (v.Item(i)))
b <- b || (p (v.At i))
i <- i+1
b
@ -131,7 +127,7 @@ module Vector =
let mutable b = true
let mutable i = 0
while b && i < v.Count do
b <- b && (p i (v.Item(i)))
b <- b && (p i (v.At i))
i <- i+1
b
@ -140,7 +136,7 @@ module Vector =
let mutable b = false
let mutable i = 0
while not(b) && i < v.Count do
b <- b || (p i (v.Item(i)))
b <- b || (p i (v.At i))
i <- i+1
b
@ -149,41 +145,41 @@ module Vector =
let w = v.Clone()
let mutable p = v.Item(0)
for i=1 to v.Count-1 do
p <- f p (v.Item(i))
w.[i] <- p
p <- f p (v.At i)
w.At(i, p)
w
/// Scans a vector in reverse order; like foldBack but returns the intermediate result.
let inline scanBack (f: float -> float -> float) (v: #Vector<float>) =
let w = v.Clone()
let mutable p = v.Item(v.Count-1)
let mutable p = v.At (v.Count-1)
for i=2 to v.Count do
p <- f (v.Item(v.Count - i)) p
w.[v.Count - i] <- p
p <- f (v.At (v.Count - i)) p
w.At(v.Count - i, p)
w
/// Reduces a vector: the result of this function will be f(...f(f(v[0],v[1]), v[2]),..., v[n]).
let inline reduce (f: float -> float -> float) (v: #Vector<float>) =
let mutable p = v.Item(0)
for i=1 to v.Count-1 do
p <- f p (v.Item(i))
p <- f p (v.At i)
p
/// Reduces a vector in reverse order: the result of this function will be f(v[1], ..., f(v[n-2], f(v[n-1],v[n]))...).
let inline reduceBack (f: float -> float -> float) (v: #Vector<float>) =
let mutable p = v.Item(v.Count-1)
for i=2 to v.Count do
p <- f (v.Item(v.Count - i)) p
p <- f (v.At (v.Count - i)) p
p
/// 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.Item(i) <- v.Item(i)
newV.Item(index) <- value
newV.At(i, v.At i)
newV.At(index, value)
for i = index + 1 to v.Count do
newV.Item(i) <- v.Item(i - 1)
newV.At(i, v.At (i - 1))
newV
/// A module which implements functional dense vector operations.
@ -194,29 +190,21 @@ module DenseVector =
let inline init (n: int) f =
let v = new DenseVector(n)
for i=0 to n-1 do
v.[i] <- f i
v.At(i, f i)
v
/// Create a vector from a float list.
let inline ofList (fl: float list) =
let n = List.length fl
let v = DenseVector(n)
fl |> List.iteri (fun i f -> v.[i] <- f)
v
let inline ofList (fl: float list) = DenseVector(Array.ofList fl)
/// Create a vector from a sequences.
let inline ofSeq (fs: #seq<float>) =
let n = Seq.length fs
let v = DenseVector(n)
fs |> Seq.iteri (fun i f -> v.[i] <- f)
v
let inline ofSeq (fs: #seq<float>) = DenseVector(Array.ofSeq fs)
/// Create a vector with evenly spaced entries: e.g. rangef -1.0 0.5 1.0 = [-1.0 -0.5 0.0 0.5 1.0]
let inline rangef (start: float) (step: float) (stop: float) =
let n = (int ((stop - start) / step)) + 1
let v = new DenseVector(n)
for i=0 to n-1 do
v.[i] <- (float i) * step + start
v.At(i, (float i) * step + start)
v
/// Create a vector with integer entries in the given range.

Loading…
Cancel
Save