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">
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.
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.
|
|
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); |
|
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 |
| 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 |
| [V, D] = eig(A) |
Generalized decomposition |
| [V, D] = eig(A,B) |
Singular Value Decomposition
Operation | Accord.NET (C#) | MATLAB® |
Economy decomposition |
| [U,S,V] = svd(A,0) |
QR Decomposition
Operation | Accord.NET (C#) | MATLAB® |
Standard decomposition |
| [Q,R] = QR(A) |
Cholesky decomposition
Operation | Accord.NET (C#) | MATLAB® |
Standard decomposition |
| R = CHOL(A) |
LU Decomposition
Operation | Accord.NET (C#) | MATLAB® |
Standard decomposition |
| [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). |
| v = [ 5 2 2 7 1 0]; idx = find(v > 2) |
Reshaping a vector to a matrix. |
| 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