# Binary matrices

DiagHam stores matrices in binary mode. Here we will discuss a few useful tools to manipulate matrices and the file format that it is used to store them.

## Tools

MatrixBinary2Ascii
converts a binary encoded matrix into its ASCII version.
GenericHamiltonianDiagonalization
performs the diagonalization of a binary encoded matrix (or a linear combination of binary encoded matrices).
MatrixExtractColumns
extract columns from a binary encoded matrx and store them as binary vectors.

## File format

In Diagham, binary matrix file format is fully supported by any class deriving from Matrix. But the format itself is simple enough to be used by any other code. All data is recorded accroding to the little endian ordering for bytes (the default ordering if you use a x86 or related cpu architecture). Using the C/C++ types, the format is

• int (4-bytes) : matrix type. The first bit is one for a real matrix, the second bit is one for a complex matrix
• int (4-bytes) : number of rows (m)
• int (4-bytes) : number of columns (n)

Then for real matrices, we have mn double (8-bytes) corresponding to the matrix elements stored as (0,0),(0,1),...(1,0),(1,1),... (where the first index is the row index, the second index is the column index). For complex matrices, we have 2mn double (8-bytes), where the real part and the imaginary part are stored consecutively i.e. Re(0,0),Im(0,0),Re(0,1),Im(0,1),...

Below we provide a simple C++ code that reads a binary matrix

```   ifstream File;
File.open("mymatrix.mat", ios::binary | ios::in);
int TmpType;
int TmpNbrRow;
int TmpNbrColumn;
if ((TmpType & 1) != 0)
{
double Tmp;
for (int i = 0; i < TmpNbrRow; ++i)
for (int j = 0; j < TmpNbrColumn; ++j)
{
cout << i << " " << j << " " << Tmp << endl;
}
}
else
{
double TmpRe;
double TmpIm;
for (int i = 0; i < TmpNbrRow; ++i)
for (int j = 0; j < TmpNbrColumn; ++j)
{