# All (Notes) About Matrix (in Ruby)

## Basis

New a matrix:

New a special matrix:

Extract a sub matrix:

Get rows/cols:

Get a row/col/elem:

## Basic Operations

Four arithmetic operations +, -, * and / also work for vectors and numbers.

Row rank is the dimension of row vectors and column rank is the dimension of column vectors It is proved that row rank equals to column rank for a rectangular matrix:

For complex matrices:

The conjugate of a complex matrix:

In the following sections, I denote the conjugate transpose of $M$ as $M^{*}$. In the Ruby sense:

## Attributes

A square matrix is a matrix that its row_size equals to column_size.

A hermitian is a square matrix that equals to it's conjugate transpose.

A positive-definite (real) matrix is an n-dimension matrix $M$ s.t.

$\forall$ real n-dimension vector $z$, $z^{T} M z > 0$.

A positive-definite hermitian is a n-dimension hermitian $M$ s.t.

$\forall$ complex n-dimension vector $z$, $z^{*} M z > 0$.

To test triangular matrices:

An orthogonal (real) matrix $M$ means $M M^T = I$:

Similar to orthogonal, a unitary (complex) matrix $M$ implies $M M^{*} = I$.

A square matrix is regular if invertible.

A square matrix that is not regular is called singular or degenerate.

A permutation matrix is a square binary matrix that has exactly one entry 1 in each row and each column, and $0$s elsewhere.

NOTE: Many of the above attributes raise error if matrix not square.

## Eigen System

Eigenvalue and eigenvector: $A v = \lambda v$ .

The eigen decomposition ($d$ is diagonal matrix of eigenvalues):

For each eigenvalue, the corresponding vector in $v$ is the eigenvector.

## Decompositions

• $LU$ ($L$ower triangle - $U$pper triangle decomposition):

$A = L U$, a variant of Gaussian Elimination

• $LDU$ decomposition:

$A = L D U$ , where $D$ is diagonal

• $LUP$ ($LU$ decomposition with partial pivoting):

$P A = L U$, where $P$ is a permutation matrix

* To solve a linear equation with $LUP$ decomposition:

• Cholesky decomposition:

$A = L L^{*}$, it is twice as efficient as $LU$ decomposition, but limited to positive-definite hermitians.

• $QR$ decomposition:

$A = Q R$, where $Q$ is orthogonal and $R$ upper triangular.

* there are block $LU$ and block Cholesky decompositions to reduce complexity.

## NArray

Vector and Matrix are implemented in pure ruby array, which is polymorphic thus slow.

NArray is the ruby interface for POD arrays, which can make a performance boost.

http://narray.rubyforge.org/SPEC.en

There are methods to mutate the NArray:

## Boost Matrix Operation Speed with AVX

AVX stands for Advanced Vector Extension of Intel's recent CPUs. To be more superior than SSE, you can operate on 256 bit data with 1 instruction, which can be long as 2 or 4 elements in a matrix vector.

Just a reference (from IDF 2011):

How To Optimize Your Software For Intel AVX (It's interesting to see that you build software for their instruction set instead of them build instruction set for your software).

An i5 CPU should have AVX 1.0. But one needs at least GCC4.6 to enable this optimization.

TODO

tags: