Browse Source

LA: F# use 'SkipZeros' instead of cryptic 'nz' suffix

optimization-1
Christoph Ruegg 13 years ago
parent
commit
f28f4acc54
  1. 36
      src/FSharp/LinearAlgebra.Matrix.fs
  2. 34
      src/FSharp/LinearAlgebra.Vector.fs
  3. 20
      src/FSharpUnitTests/MatrixTests.fs
  4. 16
      src/FSharpUnitTests/VectorTests.fs

36
src/FSharp/LinearAlgebra.Matrix.fs

@ -49,10 +49,10 @@ module Matrix =
let inline toSeqi (m: #Matrix<_>) = m.EnumerateIndexed()
/// Transform a matrix into a sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
let inline toSeqnz (m: #Matrix<_>) = m.EnumerateNonZero()
let inline toSeqSkipZeros (m: #Matrix<_>) = m.EnumerateNonZero()
/// Transform a matrix into an indexed sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
let inline toSeqinz (m: #Matrix<_>) = m.EnumerateNonZeroIndexed()
let inline toSeqiSkipZeros (m: #Matrix<_>) = m.EnumerateNonZeroIndexed()
/// Transform a matrix into a column sequence.
let inline toColSeq (m: #Matrix<_>) = m.EnumerateColumns()
@ -74,10 +74,10 @@ module Matrix =
let inline iteri f (m: #Matrix<_>) = m.EnumerateIndexed() |> Seq.iter (fun (i, j, x) -> f i j x)
/// Applies a function to all non-zero elements of the matrix. Skipping zeros is efficient on sparse data.
let inline iternz f (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.iter f
let inline iterSkipZerosnz f (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.iter f
/// Applies a function to all non-zero indexed elements of the matrix. Skipping zeros is efficient on sparse data.
let inline iterinz f (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.iter (fun (i, j, x) -> f i j x)
let inline iteriSkipZeros f (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.iter (fun (i, j, x) -> f i j x)
/// Applies a function to all columns of the matrix.
let inline iterCols f (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.iter f
@ -99,10 +99,10 @@ module Matrix =
let inline foldi f state (m: #Matrix<_>) = m.EnumerateIndexed() |> Seq.fold (fun s (i,j,x) -> f i j s x) state
/// Fold all non-zero entries of a matrix. Skipping zeros is efficient on sparse data.
let inline foldnz f state (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.fold f state
let inline foldSkipZeros f state (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.fold f state
/// Fold all non-zero entries of a matrix with an indexed folding function. Skipping zeros is efficient on sparse data.
let inline foldinz f state (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.fold (fun s (i,j,x) -> f i j s x) state
let inline foldiSkipZeros f state (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.fold (fun s (i,j,x) -> f i j s x) state
/// Fold all columns of a matrix.
let inline foldCols f state (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.fold f state
@ -124,10 +124,10 @@ module Matrix =
let inline scani f state (m: #Matrix<_>) = m.EnumerateIndexed() |> Seq.scan (fun s (i,j,x) -> f i j s x) state
/// Scan all non-zero entries of a matrix. Skipping zeros is efficient on sparse data.
let inline scannz f state (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.scan f state
let inline scanSkipZeros f state (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.scan f state
/// Scan all non-zero entries of a matrix with an indexed folding function. Skipping zeros is efficient on sparse data.
let inline scaninz f state (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.scan (fun s (i,j,x) -> f i j s x) state
let inline scaniSkipZeros f state (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.scan (fun s (i,j,x) -> f i j s x) state
/// Scan all columns of a matrix.
let inline scanCols f state (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.scan f state
@ -146,7 +146,7 @@ module Matrix =
let inline reduce f (m: #Matrix<_>) = m.Enumerate() |> Seq.reduce f
/// Reduce all non-zero entries of a matrix. Skipping zeros is efficient on sparse data.
let inline reducenz f (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.reduce f
let inline reduceSkipZeros f (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.reduce f
/// Reduce all columns of a matrix.
let inline reduceCols f (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.reduce f
@ -162,10 +162,10 @@ module Matrix =
let inline existsi p (m: #Matrix<_>) = m.EnumerateIndexed() |> Seq.exists (fun (i,j,x) -> p i j x)
/// Checks whether there is a non-zero entry in the matrix that satisfies a predicate. Skipping zeros is efficient on sparse data.
let inline existsnz p (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.exists p
let inline existsSkipZeros p (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.exists p
/// Checks whether there is a non-zero entry in the matrix that satisfies a position dependent predicate. Skipping zeros is efficient on sparse data.
let inline existsinz p (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.exists (fun (i,j,x) -> p i j x)
let inline existsiSkipZeros p (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.exists (fun (i,j,x) -> p i j x)
/// Checks whether there is a column in the matrix that satisfies a predicate.
let inline existsCol p (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.exists p
@ -187,10 +187,10 @@ module Matrix =
let inline foralli p (m: #Matrix<_>) = m.EnumerateIndexed() |> Seq.forall (fun (i,j,x) -> p i j x)
/// Checks whether all non-zero entries in the matrix that satisfies a given predicate. Skipping zeros is efficient on sparse data.
let inline forallnz p (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.forall p
let inline forallSkipZeros p (m: #Matrix<_>) = m.EnumerateNonZero() |> Seq.forall p
/// Checks whether all non-zero entries in the matrix that satisfies a given position dependent predicate. Skipping zeros is efficient on sparse data.
let inline forallinz p (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.forall (fun (i,j,x) -> p i j x)
let inline foralliSkipZeros p (m: #Matrix<_>) = m.EnumerateNonZeroIndexed() |> Seq.forall (fun (i,j,x) -> p i j x)
/// Checks whether all columns in the matrix that satisfy a predicate.
let inline forallCols p (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.forall p
@ -216,12 +216,12 @@ module Matrix =
/// In-place map of every matrix element using a function.
/// Zero-values may be skipped (relevant mostly for sparse matrices).
let inline mapnzInPlace f (A: #Matrix<_>) =
let inline mapSkipZerosInPlace f (A: #Matrix<_>) =
A.MapInplace((fun x -> f x), false)
/// In-place map of every matrix element using a position dependent function.
/// Zero-values may be skipped (relevant mostly for sparse matrices).
let inline mapinzInPlace f (A: #Matrix<_>) =
let inline mapiSkipZerosInPlace f (A: #Matrix<_>) =
A.MapIndexedInplace((fun i j x -> f i j x), false)
/// In-place map every matrix column using the given position dependent function.
@ -243,7 +243,7 @@ module Matrix =
/// Map every matrix element using the given function.
/// Zero-values may be skipped (relevant mostly for sparse matrices).
let inline mapnz f (A: #Matrix<_>) =
let inline mapSkipZeros f (A: #Matrix<_>) =
let A = A.Clone()
A.MapInplace((fun x -> f x), false)
A
@ -256,7 +256,7 @@ module Matrix =
/// Map every matrix element using the given position dependent function.
/// Zero-values may be skipped (relevant mostly for sparse matrices).
let inline mapinz f (A: #Matrix<_>) =
let inline mapiSkipZeros f (A: #Matrix<_>) =
let A = A.Clone()
A.MapIndexedInplace((fun i j x -> f i j x), false)
A
@ -334,7 +334,7 @@ module Matrix =
/// Returns the sum of all elements of a matrix.
let inline sum (A: #Matrix<'a>) = A |> foldnz (+) Matrix<'a>.Zero
let inline sum (A: #Matrix<'a>) = A |> foldSkipZeros (+) 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<_>) =

34
src/FSharp/LinearAlgebra.Vector.fs

@ -49,10 +49,10 @@ module Vector =
let inline toSeqi (v: #Vector<_>) = v.EnumerateIndexed()
/// Transform a vector into a sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
let inline toSeqnz (v: #Vector<_>) = v.EnumerateNonZero()
let inline toSeqSkipZeros (v: #Vector<_>) = v.EnumerateNonZero()
/// Transform a vector into an indexed sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
let inline toSeqinz (v: #Vector<_>) = v.EnumerateNonZeroIndexed()
let inline toSeqiSkipZeros (v: #Vector<_>) = v.EnumerateNonZeroIndexed()
/// Applies a function to all elements of the vector.
@ -62,10 +62,10 @@ module Vector =
let inline iteri f (v: #Vector<_>) = v.Enumerate() |> Seq.iteri f
/// Applies a function to all non-zero elements of the vector. Skipping zeros is efficient on sparse data.
let inline iternz f (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.iter f
let inline iterSkipZeros f (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.iter f
/// Applies a function to all non-zero indexed elements of the vector. Skipping zeros is efficient on sparse data.
let inline iterinz f (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.iter (fun (i,x) -> f i x)
let inline iteriSkipZeros f (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.iter (fun (i,x) -> f i x)
/// Fold all entries of a vector.
@ -75,10 +75,10 @@ module Vector =
let inline foldi f state (v: #Vector<_>) = v.EnumerateIndexed() |> Seq.fold (fun s (i,x) -> f i s x) state
/// Fold all non-zero entries of a vector. Skipping zeros is efficient on sparse data.
let inline foldnz f state (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.fold f state
let inline foldSkipZeros f state (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.fold f state
/// Fold all non-zero entries of a vector using a position dependent folding function. Skipping zeros is efficient on sparse data.
let inline foldinz f state (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.fold (fun s (i,x) -> f i s x) state
let inline foldiSkipZeros f state (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.fold (fun s (i,x) -> f i s x) state
/// Scan all entries of a vector.
@ -88,17 +88,17 @@ module Vector =
let inline scani f state (v: #Vector<_>) = v.EnumerateIndexed() |> Seq.scan (fun s (i,x) -> f i s x) state
/// Scan all non-zero entries of a vector. Skipping zeros is efficient on sparse data.
let inline scannz f state (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.scan f state
let inline scanSkipZeros f state (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.scan f state
/// Scan all non-zero entries of a vector using a position dependent folding function. Skipping zeros is efficient on sparse data.
let inline scaninz f state (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.scan (fun s (i,x) -> f i s x) state
let inline scaniSkipZeros f state (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.scan (fun s (i,x) -> f i s x) state
/// Reduce all entries of a vector.
let inline reduce f (v: #Vector<_>) = v.Enumerate() |> Seq.reduce f
/// Reduce all non-zero entries of a vector. Skipping zeros is efficient on sparse data.
let inline reducenz f (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.reduce f
let inline reduceSkipZeros f (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.reduce f
/// Checks whether there is an entry in the vector that satisfies a predicate.
@ -108,10 +108,10 @@ module Vector =
let inline existsi p (v: #Vector<_>) = v.EnumerateIndexed() |> Seq.exists (fun (i,x) -> p i x)
/// Checks whether there is a non-zero entry in the vector that satisfies a predicate. Skipping zeros is efficient on sparse data.
let inline existsnz p (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.exists p
let inline existsSkipZeros p (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.exists p
/// Checks whether there is a non-zero entry in the vector that satisfies a position dependent predicate. Skipping zeros is efficient on sparse data.
let inline existsinz p (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.exists (fun (i,x) -> p i x)
let inline existsiSkipZeros p (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.exists (fun (i,x) -> p i x)
/// Checks whether all entries in the vector that satisfies a given predicate.
@ -121,10 +121,10 @@ module Vector =
let inline foralli p (v: #Vector<_>) = v.EnumerateIndexed() |> Seq.forall (fun (i,x) -> p i x)
/// Checks whether all non-zero entries in the vector that satisfies a given predicate. Skipping zeros is efficient on sparse data.
let inline forallnz p (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.forall p
let inline forallSkipZeros p (v: #Vector<_>) = v.EnumerateNonZero() |> Seq.forall p
/// Checks whether all non-zero entries in the vector that satisfies a given position dependent predicate. Skipping zeros is efficient on sparse data.
let inline forallinz p (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.forall (fun (i,x) -> p i x)
let inline foralliSkipZeros p (v: #Vector<_>) = v.EnumerateNonZeroIndexed() |> Seq.forall (fun (i,x) -> p i x)
@ -138,12 +138,12 @@ module Vector =
/// In-place mutation by applying a function to every element of the vector.
/// Zero-values may be skipped (relevant mostly for sparse vectors).
let inline mapnzInPlace f (v: #Vector<_>) =
let inline mapSkipZerosInPlace f (v: #Vector<_>) =
v.MapInplace((fun x -> f x), false)
/// In-place mutation by applying a function to every element of the vector.
/// Zero-values may be skipped (relevant mostly for sparse vectors).
let inline mapinzInPlace (f: int -> float -> float) (v: #Vector<float>) =
let inline mapiSkipZerosInPlace (f: int -> float -> float) (v: #Vector<float>) =
v.MapIndexedInplace((fun i x -> f i x), false)
@ -155,7 +155,7 @@ module Vector =
/// Maps a vector to a new vector by applying a function to every element.
/// Zero-values may be skipped (relevant mostly for sparse vectors).
let inline mapnz f (v: #Vector<_>) =
let inline mapSkipZeros f (v: #Vector<_>) =
let w = v.Clone()
w.MapInplace((fun x -> f x), false)
w
@ -168,7 +168,7 @@ module Vector =
/// Maps a vector to a new vector by applying a function to every element.
/// Zero-values may be skipped (relevant mostly for sparse vectors).
let inline mapinz f (v: #Vector<_>) =
let inline mapiSkipZeros f (v: #Vector<_>) =
let w = v.Clone()
w.MapIndexedInplace((fun i x -> f i x), false)
w

20
src/FSharpUnitTests/MatrixTests.fs

@ -62,9 +62,9 @@ module MatrixTests =
M |> should equal (3.0 * sparseM)
[<Test>]
let ``Matrix.mapnzInPlace.Sparse`` () =
let ``Matrix.mapSkipZerosInPlace.Sparse`` () =
let M = sparseM.Clone()
M |> Matrix.mapnzInPlace (fun x -> 3.0 * x)
M |> Matrix.mapSkipZerosInPlace (fun x -> 3.0 * x)
M |> should equal (3.0 * sparseM)
[<Test>]
@ -80,9 +80,9 @@ module MatrixTests =
M |> should equal (2.0 * sparseM + SparseMatrix.init 2 3 (fun i j -> if i=j then 1.0 else 0.0))
[<Test>]
let ``Matrix.mapinzInPlace.Sparse`` () =
let ``Matrix.mapiSkipZerosInPlace.Sparse`` () =
let M = sparseM.Clone()
M |> Matrix.mapinzInPlace (fun i j x -> 2.0*x)
M |> Matrix.mapiSkipZerosInPlace (fun i j x -> 2.0*x)
M |> should equal (2.0 * sparseM)
[<Test>]
@ -90,16 +90,16 @@ module MatrixTests =
Matrix.map (fun x -> 2.0 * x) smallM |> should equal (2.0 * smallM)
[<Test>]
let ``Matrix.mapnz`` () =
Matrix.mapnz (fun x -> 2.0 * x) smallM |> should equal (2.0 * smallM)
let ``Matrix.mapSkipZeros`` () =
Matrix.mapSkipZeros (fun x -> 2.0 * x) smallM |> should equal (2.0 * smallM)
[<Test>]
let ``Matrix.mapi`` () =
Matrix.mapi (fun i j x -> float i * 100.0 + float j + x) largeM |> should equal (2.0 * largeM)
[<Test>]
let ``Matrix.mapinz`` () =
Matrix.mapinz (fun i j x -> float i * 100.0 + float j + x) largeM |> should equal (2.0 * largeM)
let ``Matrix.mapiSkipZeros`` () =
Matrix.mapiSkipZeros (fun i j x -> float i * 100.0 + float j + x) largeM |> should equal (2.0 * largeM)
[<Test>]
let ``Matrix.mapCols`` () =
@ -148,8 +148,8 @@ module MatrixTests =
N |> should equal (0.0 * smallM)
[<Test>]
let ``Matrix.toSeqnz`` () =
Seq.length (Matrix.toSeqnz smallM) |> should equal 4
let ``Matrix.toSeqSkipZeros`` () =
Seq.length (Matrix.toSeqSkipZeros smallM) |> should equal 4
[<Test>]
let ``Matrix.sum`` () =

16
src/FSharpUnitTests/VectorTests.fs

@ -65,9 +65,9 @@ module VectorTests =
w |> should equal (2.0 * sparsev)
[<Test>]
let ``Vector.mapnzInPlace.Sparse`` () =
let ``Vector.mapSkipZerosInPlace.Sparse`` () =
let w = sparsev.Clone()
w |> Vector.mapnzInPlace (fun x -> 2.0 * x)
w |> Vector.mapSkipZerosInPlace (fun x -> 2.0 * x)
w |> should equal (2.0 * sparsev)
[<Test>]
@ -83,9 +83,9 @@ module VectorTests =
w |> should equal (2.0 * sparsev)
[<Test>]
let ``Vector.mapinzInPlace.Sparse`` () =
let ``Vector.mapiSkipZerosInPlace.Sparse`` () =
let w = sparsev.Clone()
w |> Vector.mapinzInPlace (fun i x -> 2.0 * float i * x)
w |> Vector.mapiSkipZerosInPlace (fun i x -> 2.0 * float i * x)
w |> should equal (2.0 * sparsev)
[<Test>]
@ -105,16 +105,16 @@ module VectorTests =
Vector.map (fun x -> 2.0 * x) largev |> should equal (2.0 * largev)
[<Test>]
let ``Vector.mapnz`` () =
Vector.mapnz (fun x -> 2.0 * x) largev |> should equal (2.0 * largev)
let ``Vector.mapSkipZeros`` () =
Vector.mapSkipZeros (fun x -> 2.0 * x) largev |> should equal (2.0 * largev)
[<Test>]
let ``Vector.mapi`` () =
Vector.mapi (fun i x -> float i / 100.0) largev |> should equal largev
[<Test>]
let ``Vector.mapinz`` () =
Vector.mapinz (fun i x -> float i / 100.0) largev |> should equal largev
let ``Vector.mapiSkipZeros`` () =
Vector.mapiSkipZeros (fun i x -> float i / 100.0) largev |> should equal largev
[<Test>]
let ``Vector.fold`` () =

Loading…
Cancel
Save