|
|
|
@ -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 |
|
|
|
|