Browse Source

Twek naming of new F# matrix creating functions

v2
Christoph Ruegg 14 years ago
parent
commit
53200083fa
  1. 16
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  2. 6
      src/FSharpExamples/LinearRegression.fsx
  3. 17
      src/FSharpExamples/Matrices.fsx
  4. 1
      src/FSharpExamples/Vectors.fsx
  5. 16
      src/FSharpUnitTests/DenseMatrixTests.fs
  6. 14
      src/FSharpUnitTests/SparseMatrixTests.fs

16
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<float>>) = DenseMatrix.OfRowsCovariant(rows, cols, fss)
let inline ofRows (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<float>>) = DenseMatrix.OfColumnsCovariant(rows, cols, fss)
let inline ofColumns (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<int * int * float>) = DenseMatrix.OfIndexed(rows, cols, fs)
@ -364,16 +364,16 @@ module SparseMatrix =
let inline ofList (rows: int) (cols: int) (fl: list<int * int * float>) = 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<float>>) = SparseMatrix.OfRowsCovariant(rows, cols, fss)
let inline ofRows (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<float>>) = SparseMatrix.OfColumnsCovariant(rows, cols, fss)
let inline ofColumns (rows: int) (cols: int) (fss: #seq<#seq<float>>) = 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<int * int * float>) = SparseMatrix.OfIndexed(rows, cols, fs)

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

17
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]

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

16
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
[<Test>]
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
[<Test>]
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
[<Test>]
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
[<Test>]
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
[<Test>]
let ``DenseMatrix.ofSeqi`` () =

14
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
[<Test>]
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
[<Test>]
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
[<Test>]
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
[<Test>]
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
[<Test>]
let ``SparseMatrix.ofSeqi`` () =

Loading…
Cancel
Save