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() 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. /// 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. /// 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. /// Transform a matrix into a column sequence.
let inline toColSeq (m: #Matrix<_>) = m.EnumerateColumns() 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) 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. /// 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. /// 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. /// Applies a function to all columns of the matrix.
let inline iterCols f (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.iter f 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 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. /// 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. /// 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. /// Fold all columns of a matrix.
let inline foldCols f state (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.fold f state 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 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. /// 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. /// 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. /// Scan all columns of a matrix.
let inline scanCols f state (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.scan f state 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 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. /// 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. /// Reduce all columns of a matrix.
let inline reduceCols f (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.reduce f 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) 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. /// 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. /// 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. /// Checks whether there is a column in the matrix that satisfies a predicate.
let inline existsCol p (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.exists p 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) 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. /// 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. /// 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. /// Checks whether all columns in the matrix that satisfy a predicate.
let inline forallCols p (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.forall p 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. /// In-place map of every matrix element using a function.
/// Zero-values may be skipped (relevant mostly for sparse matrices). /// 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) A.MapInplace((fun x -> f x), false)
/// In-place map of every matrix element using a position dependent function. /// In-place map of every matrix element using a position dependent function.
/// Zero-values may be skipped (relevant mostly for sparse matrices). /// 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) A.MapIndexedInplace((fun i j x -> f i j x), false)
/// In-place map every matrix column using the given position dependent function. /// 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. /// Map every matrix element using the given function.
/// Zero-values may be skipped (relevant mostly for sparse matrices). /// 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() let A = A.Clone()
A.MapInplace((fun x -> f x), false) A.MapInplace((fun x -> f x), false)
A A
@ -256,7 +256,7 @@ module Matrix =
/// Map every matrix element using the given position dependent function. /// Map every matrix element using the given position dependent function.
/// Zero-values may be skipped (relevant mostly for sparse matrices). /// 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() let A = A.Clone()
A.MapIndexedInplace((fun i j x -> f i j x), false) A.MapIndexedInplace((fun i j x -> f i j x), false)
A A
@ -334,7 +334,7 @@ module Matrix =
/// Returns the sum of all elements of a 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. /// 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<_>) = 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() 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. /// 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. /// 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. /// 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 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. /// 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. /// 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. /// 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 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. /// 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. /// 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. /// 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 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. /// 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. /// 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. /// Reduce all entries of a vector.
let inline reduce f (v: #Vector<_>) = v.Enumerate() |> Seq.reduce f 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. /// 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. /// 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) 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. /// 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. /// 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. /// 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) 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. /// 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. /// 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. /// In-place mutation by applying a function to every element of the vector.
/// Zero-values may be skipped (relevant mostly for sparse vectors). /// 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) v.MapInplace((fun x -> f x), false)
/// In-place mutation by applying a function to every element of the vector. /// In-place mutation by applying a function to every element of the vector.
/// Zero-values may be skipped (relevant mostly for sparse vectors). /// 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) 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. /// Maps a vector to a new vector by applying a function to every element.
/// Zero-values may be skipped (relevant mostly for sparse vectors). /// 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() let w = v.Clone()
w.MapInplace((fun x -> f x), false) w.MapInplace((fun x -> f x), false)
w w
@ -168,7 +168,7 @@ module Vector =
/// Maps a vector to a new vector by applying a function to every element. /// Maps a vector to a new vector by applying a function to every element.
/// Zero-values may be skipped (relevant mostly for sparse vectors). /// 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() let w = v.Clone()
w.MapIndexedInplace((fun i x -> f i x), false) w.MapIndexedInplace((fun i x -> f i x), false)
w w

20
src/FSharpUnitTests/MatrixTests.fs

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

16
src/FSharpUnitTests/VectorTests.fs

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

Loading…
Cancel
Save