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 multidimensional arrays.
Introduction
Accord.NET uses a bit different approach for matrix manipulation in contrast to
other libraries. By using 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[^].
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 multidimensional 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.
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 multidimensional
arrays as usual.

<span>double</span>[,] A = { {1, 2, 3}, {6, 2, 0}, {0, 0, 1} }; 


<span>double</span>[,] 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®


<span>double</span>[,] B = <span>Matrix</span>.Identity(3).Multiply(2); 


B = <span>eye</span>(3)*2; 


<span>double</span>[,] B = <span>Matrix</span>.Diagonal(3, 2.0); 


B = <span>eye</span>(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®


<span>var</span> I = <span>Matrix</span>.Identity(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

<span>var</span> I = <span>eye</span>(3) 
directly. The code will be based on current Octave syntax. Octave is a highlevel
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


<span>var</span> At = A.Transpose(); 


Inverse


<span>var</span> invA = A.Inverse(); 


invA = <span>inv</span>(A) 

PseudoInverse


<span>var</span> pinvA = A.PseudoInverse(); 


pinvA = <span>pinv</span>(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


<span>var</span> C = A.Add(B); 


Subtraction


<span>var</span> C = A.Subtract(B); 


Multiplication


<span>var</span> C = A.Multiply(B); 


Division


<span>var</span> C = A.Divide(B); 


The above also works with vectors and scalars.
Operations

Accord.NET (C#)

MATLAB®

Multiplying by a scalar


<span>var</span> H = A.Multiply(3.14); 


Multiplying by a column vector


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


Special elementwise operations
Elementwise 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


<span>var</span> C = A.ElementwiseMultiply(B); 


Division


<span>var</span> C = A.ElementwiseDivide(B); 


Power


<span>var</span> C = A.ElementwisePower(B); 


Vector operations
Accord.NET can also perform many vector operations. Among them are the many flavors
of products between vectors, such as the inner, the outer and the Cartesian.
Operation

Accord.NET (C#)

MATLAB®

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


<span>var</span> w = u.InnerProduct(v); 


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


<span>var</span> w = u.OuterProduct(v); 

w = u’*v

Cartesian product


<span>var</span> w = u.CartesianProduct(v); 


Euclidean Norm


<span>double</span> n = u.Norm(); 


Sum


<span>double</span> s = u.Sum(); 


Product


<span>double</span> p = u.Product(); 


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

Accord.NET (C#)

MATLAB®

Determinant



Trace



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

Accord.NET (C#)

MATLAB®

Sum vector


<span>double</span>[] sum = A.Sum(); 


Sum of elements


<span>double</span> sum = A.Sum().Sum() 


<span>sum</span>(<span>sum</span>(A)) 

Sum along columns


<span>double</span>[] sum = A.Sum(0) 


Sum along rows


<span>double</span>[] sum = A.Sum(1) 


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


<span>var</span> evd = <span>new</span> <span>EigenvalueDecomposition</span>(A); <span>var</span> V = evd.Eigenvectors; <span>var</span> D = evd.DiagonalMatrix; 


Generalized decomposition


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


Singular Value Decomposition
Operation

Accord.NET (C#)

MATLAB®

Economy decomposition


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


QR Decomposition
Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


<span>var</span> qr = <span>new</span> <span>QrDecomposition</span>(A); <span>var</span> Q = qr.OrthogonalFactor; <span>var</span> R = qr.UpperTriangularFactor; 


Cholesky decomposition
Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


<span>var</span> chol = <span>new</span> <span>CholeskyDecomposition</span>(A); <span>var</span> R = chol.LeftTriangularFactor; 


LU Decomposition
Operation

Accord.NET (C#)

MATLAB®

Standard decomposition


<span>var</span> lu = <span>new</span> <span>LuDecomposition</span>(A); <span>var</span> L = lu.LowerTriangularFactor; <span>var</span> U = lu.UpperTriangularFactor; 


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


<span>var</span> idx = <span>Matrix</span>.Indices(0,10); 

idx = 1:9

Selecting elements


<span>var</span> B = A.Submatrix(idx); 

B = A(idx)

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


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

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

Reshaping a vector to a matrix.


<span>double</span>[] m = { 1, 2, 3, 4 }; <span>double</span>[,] M = <span>Matrix</span>.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, logGamma, Digamma, Bessel functions, Incomplete beta integrals, and so
on.
For a complete listing of the framework features, please check the project page at Origo. In particular, don’t forget to check
out the the 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.
What would your thoughts be on optomizing these routines? This is great work
I am afraid it would be very difficult to achieve native BLAS performance using this architecture.
These routines, however, are not that unoptimized, and are OK for most situations. Optimizations will also come as the framework evolves.
Thanks for the feedback!
César
hi..is any any body can help me…. i’ve have a matrix 6×16 and i want to take matrix 3×8 into matrix 6×16 to be diagonal matrix, the first matrix 3×8 is from [0,0] to [3,8] and the second is from [4,9] to [6,16]. how can i solve it by using the accord .NET
Hi there,
I didn’t quite understand what you are trying to accomplish. Could you please elaborate a bit more?
Best regards,
Cesar
I have 3 matrices with diferent value
The first is :
23 45
65 12
32 34
the second is :
19 17
28 34
28 34
and the third is :
65 12
32 34
28 34
I want to combine it as below :
23 45 0 0 0 0
65 12 0 0 0 0
32 34 0 0 0 0
0 0 19 17 0 0
0 0 28 34 0 0
0 0 28 34 0 0
0 0 0 0 65 12
0 0 0 0 32 34
0 0 0 0 28 34
could you help me to do combine this matrices…..?
Is there a way that I can use Accord from within VisualBasic?
Absolutely! Accord.NET should work without problem from Visual Basic. Just import the framework .dlls into your project and it should work. You can also use NuGet for automatically adding the references you need.