From 53200083fa30f6fa83b67049963e71134e8e3ca8 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 14 Apr 2013 15:54:20 +0200 Subject: [PATCH] Twek naming of new F# matrix creating functions --- src/FSharp/LinearAlgebra.Double.Matrix.fs | 16 ++++++++-------- src/FSharpExamples/LinearRegression.fsx | 6 +++--- src/FSharpExamples/Matrices.fsx | 17 +++++++++-------- src/FSharpExamples/Vectors.fsx | 1 + src/FSharpUnitTests/DenseMatrixTests.fs | 16 ++++++++-------- src/FSharpUnitTests/SparseMatrixTests.fs | 14 +++++++------- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index acc236ba..ea5d3f88 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -295,16 +295,16 @@ module DenseMatrix = DenseMatrix.OfRowsCovariant(n, m, fll) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. - let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfRowsCovariant(rows, cols, fss) + let inline ofRows (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfRowsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a row. - let inline ofRowList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRowsCovariant(rows, cols, fll) + let inline ofRowsList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfRowsCovariant(rows, cols, fll) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. - let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss) + let inline ofColumns (rows: int) (cols: int) (fss: #seq<#seq>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a column. - let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumnsCovariant(rows, cols, fll) + let inline ofColumnsList (rows: int) (cols: int) (fll: float list list) = DenseMatrix.OfColumnsCovariant(rows, cols, fll) /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. let inline ofSeqi (rows: int) (cols: int) (fs: #seq) = DenseMatrix.OfIndexed(rows, cols, fs) @@ -364,16 +364,16 @@ module SparseMatrix = let inline ofList (rows: int) (cols: int) (fl: list) = SparseMatrix.OfIndexed(rows, cols, Seq.ofList fl) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. - let inline ofRowSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) + let inline ofRows (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfRowsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a row. - let inline ofRowList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRowsCovariant(rows, cols, fll) + let inline ofRowsList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfRowsCovariant(rows, cols, fll) /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. - let inline ofColumnSeq (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss) + let inline ofColumns (rows: int) (cols: int) (fss: #seq<#seq>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss) /// Create a matrix from a list of float lists. Every list in the master list specifies a column. - let inline ofColumnList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumnsCovariant(rows, cols, fll) + let inline ofColumnsList (rows: int) (cols: int) (fll: float list list) = SparseMatrix.OfColumnsCovariant(rows, cols, fll) /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. let inline ofSeqi (rows: int) (cols: int) (fs: #seq) = SparseMatrix.OfIndexed(rows, cols, fs) diff --git a/src/FSharpExamples/LinearRegression.fsx b/src/FSharpExamples/LinearRegression.fsx index e32b1b02..3ef2950e 100644 --- a/src/FSharpExamples/LinearRegression.fsx +++ b/src/FSharpExamples/LinearRegression.fsx @@ -43,7 +43,7 @@ open MathNet.Numerics.Distributions let ``Fitting to a line`` = printfn "Fitting to a line" - let X = DenseMatrix.ofColumnList 3 2 [ List.init 3 (fun i -> 1.0); [ 10.0; 20.0; 30.0 ] ] + let X = DenseMatrix.ofColumnsList 3 2 [ List.init 3 (fun i -> 1.0); [ 10.0; 20.0; 30.0 ] ] let y = DenseVector [| 15.0; 20.0; 25.0 |] let p = X.QR().Solve(y) @@ -73,7 +73,7 @@ let ``Fitting to an arbitrary linear function from noisy data`` = [ xdata |> List.map f1 xdata |> List.map f2 - ] |> DenseMatrix.ofColumnList 10 2 + ] |> DenseMatrix.ofColumnsList 10 2 let y = DenseVector.ofList ydata // solve @@ -101,7 +101,7 @@ let ``Fitting to an sine from noisy data`` = Array.create xdata.Length 1.0 xdata |> Array.map (fun x -> Math.Sin(omega*x)) xdata |> Array.map (fun x -> Math.Cos(omega*x)) - ] |> DenseMatrix.ofColumnSeq xdata.Length 3 + ] |> DenseMatrix.ofColumns xdata.Length 3 let y = DenseVector ydata let p = X.QR().Solve(y) diff --git a/src/FSharpExamples/Matrices.fsx b/src/FSharpExamples/Matrices.fsx index 24ae3e36..a465d04f 100644 --- a/src/FSharpExamples/Matrices.fsx +++ b/src/FSharpExamples/Matrices.fsx @@ -53,20 +53,21 @@ let d1 = DenseMatrix.init 3 4 (fun i j -> float i / 100.0 + float j) let d2 = SparseMatrix.init 3 4 (fun i j -> if i=j then float i / 100.0 + float j else 0.0) // Matrices can also be constructed from sequences of rows or of columns -let e1 = DenseMatrix.ofRowSeq 20 10 (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> float j + 100.0 * float i) }) -let e2 = SparseMatrix.ofRowSeq 20 10 (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) }) -let e3 = DenseMatrix.ofColumnSeq 20 10 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> float j + 100.0 * float i) }) -let e4 = SparseMatrix.ofColumnSeq 20 10 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) }) +let e1 = DenseMatrix.ofRows 20 10 (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> float j + 100.0 * float i) }) +let e2 = SparseMatrix.ofRows 20 10 (seq { for i in 1 .. 20 do yield Array.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) }) +let e3 = DenseMatrix.ofColumns 20 10 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> float j + 100.0 * float i) }) +let e4 = SparseMatrix.ofColumns 20 10 (seq { for j in 1 .. 10 do yield Array.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) }) // Or from F# lists -let f1 = DenseMatrix.ofRowList 20 10 [ for i in 1 .. 20 -> List.init 10 (fun j -> float j + 100.0 * float i) ] -let f2 = SparseMatrix.ofRowList 20 10 [ for i in 1 .. 20 -> List.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ] -let f3 = DenseMatrix.ofColumnList 20 10 [ for j in 1 .. 10 -> List.init 20 (fun i -> float j + 100.0 * float i) ] -let f4 = SparseMatrix.ofColumnList 20 10 [ for j in 1 .. 10 -> List.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ] +let f1 = DenseMatrix.ofRowsList 20 10 [ for i in 1 .. 20 -> List.init 10 (fun j -> float j + 100.0 * float i) ] +let f2 = SparseMatrix.ofRowsList 20 10 [ for i in 1 .. 20 -> List.init 10 (fun j -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ] +let f3 = DenseMatrix.ofColumnsList 20 10 [ for j in 1 .. 10 -> List.init 20 (fun i -> float j + 100.0 * float i) ] +let f4 = SparseMatrix.ofColumnsList 20 10 [ for j in 1 .. 10 -> List.init 20 (fun i -> if i%5 = 0 then float j + 100.0 * float i else 0.0) ] // Or from indexed lists or sequences where all other values are zero (useful mostly for sparse data) let g1 = DenseMatrix.ofListi 20 10 [(4,3,20.0); (18,9,3.0); (2,1,100.0)] let g2 = SparseMatrix.ofListi 20 10 [(4,3,20.0); (18,9,3.0); (2,1,100.0)] +let g3 = SparseMatrix.ofSeqi 20 10 (Seq.ofList [(4,3,20.0); (18,9,3.0); (2,1,100.0)]) // Another way to create dense matrix is using the matrix function. let h = matrix [[1.0; 2.0; 3.0] diff --git a/src/FSharpExamples/Vectors.fsx b/src/FSharpExamples/Vectors.fsx index 3610722b..2fd2111d 100644 --- a/src/FSharpExamples/Vectors.fsx +++ b/src/FSharpExamples/Vectors.fsx @@ -60,6 +60,7 @@ let f = DenseVector.ofList [ for i in 1 .. 100 -> float i ] // Or from indexed lists or sequences where all other values are zero (useful mostly for sparse data) let g1 = DenseVector.ofListi 100 [(4,20.0); (18,3.0); (2,100.0)] let g2 = SparseVector.ofListi 100 [(4,20.0); (18,3.0); (2,100.0)] +let g3 = SparseVector.ofSeqi 100 (Seq.ofList [(4,20.0); (18,3.0); (2,100.0)]) // Another way to create a 100 dimensional dense vector is using the vector function. let h = vector (List.init 100 (fun i -> float i ** 2.0)) diff --git a/src/FSharpUnitTests/DenseMatrixTests.fs b/src/FSharpUnitTests/DenseMatrixTests.fs index 16405ea5..055210fa 100644 --- a/src/FSharpUnitTests/DenseMatrixTests.fs +++ b/src/FSharpUnitTests/DenseMatrixTests.fs @@ -53,20 +53,20 @@ module DenseMatrixTests = DenseMatrix.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM [] - let ``DenseMatrix.ofRowSeq`` () = - DenseMatrix.ofRowSeq 3 2 (Seq.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]]) |> should equal smallM + let ``DenseMatrix.ofRows`` () = + DenseMatrix.ofRows 3 2 (Seq.ofList [[0.3;0.3];[0.3;0.3];[0.3;0.3]]) |> should equal smallM [] - let ``DenseMatrix.ofRowList`` () = - DenseMatrix.ofRowList 3 2 [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM + let ``DenseMatrix.ofRowsList`` () = + DenseMatrix.ofRowsList 3 2 [[0.3;0.3];[0.3;0.3];[0.3;0.3]] |> should equal smallM [] - let ``DenseMatrix.ofColumnSeq`` () = - DenseMatrix.ofColumnSeq 3 2 (Seq.ofList [[0.3;0.3;0.3];[0.3;0.3;0.3]]) |> should equal smallM + let ``DenseMatrix.ofColumn`` () = + DenseMatrix.ofColumns 3 2 (Seq.ofList [[0.3;0.3;0.3];[0.3;0.3;0.3]]) |> should equal smallM [] - let ``DenseMatrix.ofColumnList`` () = - DenseMatrix.ofColumnList 3 2 [[0.3;0.3;0.3];[0.3;0.3;0.3]] |> should equal smallM + let ``DenseMatrix.ofColumnsList`` () = + DenseMatrix.ofColumnsList 3 2 [[0.3;0.3;0.3];[0.3;0.3;0.3]] |> should equal smallM [] let ``DenseMatrix.ofSeqi`` () = diff --git a/src/FSharpUnitTests/SparseMatrixTests.fs b/src/FSharpUnitTests/SparseMatrixTests.fs index b6955004..87427163 100644 --- a/src/FSharpUnitTests/SparseMatrixTests.fs +++ b/src/FSharpUnitTests/SparseMatrixTests.fs @@ -25,20 +25,20 @@ module SparseMatrixTests = SparseMatrix.ofArray2 (Array2D.init 4 6 (fun i j -> if i = 1 && j = 2 then 1.0 else 0.0)) |> should equal smallM [] - let ``SparseMatrix.ofRowSeq`` () = - SparseMatrix.ofRowSeq 4 6 (Seq.ofList [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]]) |> should equal smallM + let ``SparseMatrix.ofRows`` () = + SparseMatrix.ofRows 4 6 (Seq.ofList [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]]) |> should equal smallM [] - let ``SparseMatrix.ofRowList`` () = - SparseMatrix.ofRowList 4 6 [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]] |> should equal smallM + let ``SparseMatrix.ofRowsList`` () = + SparseMatrix.ofRowsList 4 6 [[0.;0.;0.;0.;0.;0.];[0.;0.;1.;0.;0.;0.];[0.;0.;0.;0.;0.;0.];[0.;0.;0.;0.;0.;0.]] |> should equal smallM [] - let ``SparseMatrix.ofColumnSeq`` () = - SparseMatrix.ofColumnSeq 4 6 (Seq.ofList [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]]) |> should equal smallM + let ``SparseMatrix.ofColumns`` () = + SparseMatrix.ofColumns 4 6 (Seq.ofList [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]]) |> should equal smallM [] let ``SparseMatrix.ofColumnList`` () = - SparseMatrix.ofColumnList 4 6 [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]] |> should equal smallM + SparseMatrix.ofColumnsList 4 6 [[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;1.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.];[0.;0.;0.;0.]] |> should equal smallM [] let ``SparseMatrix.ofSeqi`` () =