Tuesday 10 August 2010

Matrix manipulation using Accord.NET


Matrix manipulation in Accord.NET
is very straightforward: Just add a new using directive on top of your class to
have (literally) about a hundred new extension methods that operate directly on
.NET multi-dimensional arrays.



target="_blank">
accord-matrix2 width="358" height="265" />



Introduction


target="_blank">
border="0" alt="accord-matrix" align="right" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5TMxjbp7bYPTPK7yI2QZyk-40mRVxet3XhWaSWb2Udc9n6ASMrz13p7rCDrQTeXpHdY_jZnehvXaPjjYbSdhlAI29tgvIYLqTc9iTHrH4sKAUoE-qUIamPv_itkZgo2Y1mx1UTXW6mkhZ/?imgmax=800"
width="246" height="251" />


Accord.NET uses a bit different approach for matrix manipulation in contrast to
other libraries. By using target="_blank">C# 3.0 extension methods, Accord adds several of the standard
methods you would expect from a Matrix library,
such as linear system solving, matrix algebra and numerical decompositions directly
to the standard double[,] (or more generally T[,]) matrices of the framework
[ href="http://accord-net.origo.ethz.ch/image/class_diagram_accord_math">^].



This approach offers some advantages since it avoids mismatches when one is using
multiple libraries, each with their own and often incompatible Matrix implementations.
Extending multi-dimensional arrays makes the use of matrices much more natural in
the .NET world, as no specialized Matrix classes have to be used and no code has
to be rewritten just to use a particular Matrix implementation.



 



Please note, however, that most methods implemented by Accord are not equivalent
to the heavily optimized versions of more specialized numerical packages, such as

BLAS
, from a performance view. Their real power comes when prototyping or realizing algorithms into code. Once an algorithm is written, several
unit tests can be created to test the correctness of the code. After that, because
we have an early working prototype, it will be much easier to perform optimizations
as needed. The key point is to have a working version early which can (if
actually necessary) be optimized later.



Using extension methods



The first step is to include a new using directive on the top of
your source file.




title="accord-matrix3" border="0" alt="accord-matrix3" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpo_b_J445BwyGME6LhJeH5ub3pRV4uJ5zYb0sSgXhXXdqC4m6HkzlvYv_8ZCEfJc_RssdRmbM_NbgE245V4a8yZgMu9Xohx8R6cEdKbO4D-7GRVciWE3fzE7_Kx4TBzg7llTX1PaZkzT0/?imgmax=800"
width="377" height="137" />



This is all that is necessary after you have referenced the Accord.Math library
in your project. Now you can use all of the methods and operations described below.
The following list is nowhere complete, but shows only some basics and highlights
from the current version of Accord.



Declaring matrices



Using standard .NET declarations



To declare matrices, no special code is required. Just create multi-dimensional
arrays as usual.











double[,] A =
{
{1, 2, 3},
{6, 2, 0},
{0, 0, 1}
};



double[,] B =
{
{2, 0, 0},
{0, 2, 0},
{0, 0, 2}
};



Using Accord.NET extensions



Additionally, one may wish to use one of the convenience methods of Accord.NET to
create specialized matrices such as the Identity matrix, multiples of the Identity
matrix or Diagonal matrices.



















Accord.NET (C#)

MATLAB®

double[,] B =                         class="str">Matrix.Identity(3).Multiply(2);



B = eye(3)*2;


double[,] B =                         class="str">Matrix.Diagonal(3, 2.0);


B = eye(3)*2;




Using Accord.NET extensions with implicit typing



By using implicit type variable declaration, the code acquires a certain lightweight
feeling and gets closer of its MATLAB®/Octave counterpart (MATLAB is a registered
trademark of The MathWorks, Inc)
.















Accord.NET (C#)

MATLAB®

var I =                         class="str">Matrix.Identity(3);


I = eye(3)




A much more closer alternative will be possible by using Algorithm Environments,
a upcoming feature in Accord. When available, this feature will allow construction
of mathematical code using



var I =         class="kwrd">eye(3)



directly. The code will be based on current Octave syntax. Octave is a high-level
language, primarily intended for
numerical computations
, that is mostly compatible with
MATLAB
.



Matrix operations



All standard matrix operations such as transpose, inverse, column and row manipulations
are available in the extension methods. In the example below, A is the same standard
double[,] matrix declared in the first section of this
article. All methods return a new double[,] matrix
as a result, leaving the original matrix A untouched.



























Operation

Accord.NET (C#)

MATLAB®

Transpose

var At = A.Transpose();


At = A'


Inverse

var invA = A.Inverse();


invA = inv(A)


Pseudo-Inverse

var pinvA = A.PseudoInverse();


pinvA = pinv(A)




Matrix Algebra



All common algebraic operations for matrices are also implemented. Those are the
common operations such as addition, subtraction and multiplication. Here, division
is actually a shortcut for multiplying by the inverse.
































Operation

Accord.NET (C#)

MATLAB®

Addition

var C = A.Add(B);


C = A + B


Subtraction

var C = A.Subtract(B);


C = A - B


Multiplication

var C = A.Multiply(B);


C = A * B


Division

var C = A.Divide(B);


C = A / B




The above also works with vectors and scalars.





















Operations

Accord.NET (C#)

MATLAB®

Multiplying by a scalar

var H = A.Multiply(3.14);


H = A * 3.14;


Multiplying by a column vector


double[] u = { 1, 2, 3 };
double[] v = A.Multiply(u);


v = A * u';



Special element-wise operations



Element-wise operations are operations performed on each element of the matrix or
vector. In Matlab, they are some times known as the dot operations, since they are
usually denoted by prefixing a dot on the common operators.



























Operation

Accord.NET (C#)

MATLAB®

Multiplication

var C = A.ElementwiseMultiply(B);


C = A .* B


Division

var C = A.ElementwiseDivide(B);


C = A ./ B


Power

var C = A.ElementwisePower(B);


C = A .^ B




Vector operations



Accord.NET can also perform many vector operations. Among them are the many flavors
of products between vectors, such as the target="_blank">inner, the target="_blank">outer and the target="_blank">Cartesian.









































Operation

Accord.NET (C#)

MATLAB®

Inner product (a.k.a. the scalar product)

var w = u.InnerProduct(v); 


w = u*v'


Outer product (a.k.a. the matrix product)

var w = u.OuterProduct(v); 


w = u'*v

Cartesian product

var w = u.CartesianProduct(v);


 

Euclidean Norm

double n = u.Norm();


n = norm(u)


Sum

double s = u.Sum();


s = sum(u)


Product

double p = u.Product();


p = prod(u)



Matrix characteristics



Some common matrix characteristics, such as the determinant and trace, are readily
available.





















Operation

Accord.NET (C#)

MATLAB®

Determinant

A.Determinant();


det(A)


Trace

A.Trace();


tr(A)



Other characteristics



Other available characteristics are the Summation and Product of vector and matrix
elements.































Operation

Accord.NET (C#)

MATLAB®

Sum vector

double[] sum = A.Sum();


sum(A)


Sum of elements

double sum = A.Sum().Sum()


sum(                    class="kwrd">sum(A))


Sum along columns

double[] sum = A.Sum(0)


sum(A, 1)


Sum along rows

double[] sum = A.Sum(1)


sum(A, 2)



Linear Algebra



Linear algebra
is certainly one of the most important fields of mathematics, if not the most important
one. Accord includes many methods for numerical linear algebra such as matrix inversion
and matrix decompositions. Most of them were originally based on target="_blank">JAMA and MAPACK, but today Accord has some additions from
routines translated from EISPACK (mainly the
Generalized Eigenvalue Decomposition
[^],
which is currently absent from Jama).
















Operation

Accord.NET (C#)

MATLAB®

Solve a linear system

x = A.Solve(B)

x = A \ B


Eigenvalue Decomposition





















Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


var evd = new EigenvalueDecomposition(A);
var V = evd.Eigenvectors;
var D = evd.DiagonalMatrix;


[V, D] = eig(A)


Generalized decomposition


var gevd = new GeneralizedEigenvalueDecomposition(A,B);
var V = gevd.Eigenvectors;
var D = gevd.DiagonalMatrix;


[V, D] = eig(A,B)



Singular Value Decomposition
















Operation

Accord.NET (C#)

MATLAB®

Economy decomposition


var svd = new SingularValueDecomposition(A);
var U = evd.LeftSingularVectors;
var S = evd.Diagonal;
var V = evd.RightSingularVectors;


[U,S,V] = svd(A,0)



QR Decomposition
















Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


var qr = new QrDecomposition(A);
var Q = qr.OrthogonalFactor;
var R = qr.UpperTriangularFactor;


[Q,R] = QR(A)



Cholesky decomposition
















Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


var chol = new CholeskyDecomposition(A);
var R = chol.LeftTriangularFactor;


R = CHOL(A)



LU Decomposition
















Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


var lu = new LuDecomposition(A);
var L = lu.LowerTriangularFactor;
var U = lu.UpperTriangularFactor;


[L, U] = LU(A)



Special operators



There are some other useful operators available in Accord.NET. There are facilities
to create index vectors (very common in Matlab for accessing sub-portions of a matrix),
select elements, find elements based on a selection criteria, and so on.































Operation

Accord.NET (C#)

MATLAB®

Create a vector of indices

var idx =                     class="str">Matrix.Indices(0,10);


idx = 1:9

Selecting elements

var B = A.Submatrix(idx);


B = A(idx)

Finding elements matching a certain criteria (For example, finding x ∈ v / x > 2).


var v = { 5, 2, 2, 7, 1, 0 };
var idx = v.Find(x => x > 2);


v = [ 5 2 2 7 1 0];
idx = find(v > 2)

Reshaping a vector to a matrix.


double[] m = { 1, 2, 3, 4 };
double[,] M = Matrix.Reshape(m, 2, 2);


m = [1 2 3 4];
reshape(m,2,2)


 



More than just matrices



Accord.NET also offers many other standard mathematical functions such as the Gamma
function, log-Gamma, Digamma, Bessel functions, Incomplete beta integrals, and so
on.



target="_blank">
title="accord-matrix4" border="0" alt="accord-matrix4" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBD_5qI0IflDtm_E5eejzkOOLYsMwGu5OhTlsJ4YNqLJvQFmYNtIG0LocelOaRoNKe0iLAyny0EaKwiYQ_VEVMg-KGXXCWllFpM3XXpQDDHFuF739_mvxNbb1e2sF34hId6sEIraixKkOh/?imgmax=800"
width="412" height="357" />



For a complete listing of the framework features, please check the target="_blank">project page at Origo. In particular, don’t forget to check
out the the target="_blank">class diagram for the Accord.Math namespace.



Cheers!



 



Legal notice: MATLAB is a registered trademark of The MathWorks, Inc. All
other trademarks are the property of their respective owners.

No comments:

Post a Comment