Matrix and Vector

Binder

Summary: this tutorial demonstrates some of the functionality for matrices and vectors provided by FSharp.Stats.

Vector

open FSharp.Stats

let v = 
    vector [|2.0; 20.0; 1.|]
vector [|2.0; 20.0; 1.0|]
let rv = 
    rowvec [|2.0; 20.0; 1.|]
rowvec [|2.0; 20.0; 1.0|]

Addition to every element of vector.

v + 1.
vector [|3.0; 21.0; 2.0|]

Addition to every element of row vector.

rv + 1.
rowvec [|3.0; 21.0; 2.0|]

Matrix

Creating Matrices with FSharp.Stats.

Matrices will also be printed with the FSharp.Stats.FSIPrinters.matrix function to get a nice console output.

open FSharp.Stats

// http://fdatamining.blogspot.de/2010/03/matrix-and-linear-algebra-in-f-part-i-f.html
// http://fdatamining.blogspot.de/search/label/linear%20algebra

let A = 
    matrix [ [ 1.0; 7.0; 2.0 ]
             [ 1.0; 3.0; 1.0 ]
             [ 2.0; 9.0; 1.0 ] ]
"
               0         1         2 
_____________________________________
0     |    1.000     7.000     2.000
1     |    1.000     3.000     1.000
2     |    2.000     9.000     1.000
"
let B = 
    matrix [ [ 10.0; 70.0; 20.0 ]
             [ 10.0; 30.0; 10.0 ]
             [ 20.0; 90.0; 10.0 ] ]
"
               0         1         2 
_____________________________________
0     |   10.000    70.000    20.000
1     |   10.000    30.000    10.000
2     |   20.000    90.000    10.000
"

Calculation Examples for Matrices

Get sum of each row.

A
|> Matrix.Generic.enumerateRowWise (Seq.sum)
[10.0; 5.0; 12.0]

Create seq of vectors of matrix columns.

A
|> Matrix.mapiCols (fun i r -> r)
[vector [|1.0; 1.0; 2.0|]; vector [|7.0; 3.0; 9.0|]; vector [|2.0; 1.0; 1.0|]]

Sum of two matrices.

A + B
"
               0         1         2 
_____________________________________
0     |   11.000    77.000    22.000
1     |   11.000    33.000    11.000
2     |   22.000    99.000    11.000
"

Difference between two matrices.

A - B
"
               0         1         2 
_____________________________________
0     |    -9.00    -63.00    -18.00
1     |    -9.00    -27.00     -9.00
2     |   -18.00    -81.00     -9.00
"

Product of two matrices.

A * B 
"
               0         1         2 
_____________________________________
0     |  120.000   460.000   110.000
1     |   60.000   250.000    60.000
2     |  130.000   500.000   140.000
"

Element-wise product of two matrices

A .* B 
"
               0         1         2 
_____________________________________
0     |   10.000   490.000    40.000
1     |   10.000    90.000    10.000
2     |   40.000   810.000    10.000
"

Scalar product of a matrix.

A * 2.0 
2.0 * A // also ok, gives same result
"
               0         1         2 
_____________________________________
0     |    2.000    14.000     4.000
1     |    2.000     6.000     2.000
2     |    4.000    18.000     2.000
"

Negation of a matrix

-A 
"
               0         1         2 
_____________________________________
0     |    -1.00     -7.00     -2.00
1     |    -1.00     -3.00     -1.00
2     |    -2.00     -9.00     -1.00
"

Product of a matrix-vector.

A * v
vector [|144.0; 63.0; 185.0|]

Dot product of two matrices.

Matrix.dot A B
1510.0

Addition to every element of matrix.

A + 1.
"
               0         1         2 
_____________________________________
0     |    2.000     8.000     3.000
1     |    2.000     4.000     2.000
2     |    3.000    10.000     2.000
"
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Stats
val v : Vector<float>
Multiple items
val vector : l:seq<float> -> Vector<float>

--------------------
type vector = Vector<float>
val rv : rowvec
Multiple items
val rowvec : l:seq<float> -> rowvec

--------------------
type rowvec = RowVector<float>
val exmp13 : Vector<float>
val exmp14 : RowVector<float>
val A : Matrix<float>
Multiple items
val matrix : ll:seq<#seq<float>> -> Matrix<float>

--------------------
type matrix = Matrix<float>
val APrint : string
module FSIPrinters

from FSharp.Stats
val matrix : mat:Matrix<float> -> string
val B : Matrix<float>
val BPrint : string
val exmp1 : seq<float>
Multiple items
module Matrix

from FSharp.Stats

--------------------
type Matrix<'T> =
  | DenseRepr of DenseMatrix<'T>
  | SparseRepr of SparseMatrix<'T>
    interface IEnumerable
    interface IEnumerable<'T>
    interface IStructuralEquatable
    interface IStructuralComparable
    interface IComparable
    override Equals : yobj:obj -> bool
    override GetHashCode : unit -> int
    member GetSlice : start1:int option * finish1:int option * start2:int option * finish2:int option -> Matrix<'T>
    member PermuteColumns : p:permutation -> Matrix<'T>
    member PermuteRows : p:permutation -> Matrix<'T>
    ...
module Generic

from FSharp.Stats.MatrixModule
val enumerateRowWise : f:(seq<'a> -> 'a0) -> matrix:Matrix<'a> -> seq<'a0>
Multiple items
module Seq

from FSharp.Stats

--------------------
module Seq

from Microsoft.FSharp.Collections
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)
val exmp2 : seq<vector>
val mapiCols : f:(int -> vector -> 'b) -> m:matrix -> seq<'b>
val i : int
val r : vector
val exmp3 : string
val exmp4 : string
val exmp5 : string
val exmp6 : string
val exmp7 : string
val exmp9 : string
val exmp10 : Vector<float>
val exmp11 : float
val dot : a:matrix -> b:Matrix<float> -> float
val exmp12 : string