Binary matrices

From diagham
Jump to navigation Jump to search

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.


converts a binary encoded matrix into its ASCII version.
performs the diagonalization of a binary encoded matrix (or a linear combination of binary encoded matrices).
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;"mymatrix.mat", ios::binary | ios::in);
   int TmpType;
   int TmpNbrRow;
   int TmpNbrColumn; ((char*) &(TmpType), sizeof(int)); ((char*) &(TmpNbrRow), sizeof(int)); ((char*) &(TmpNbrColumn), sizeof(int));
   if ((TmpType & 1) != 0)
       double Tmp;
       for (int i = 0; i < TmpNbrRow; ++i)
         for (int j = 0; j < TmpNbrColumn; ++j)
    ((char*) &(Tmp), sizeof(double));
             cout << i << " " << j << " " << Tmp << endl;
       double TmpRe;
       double TmpIm;
       for (int i = 0; i < TmpNbrRow; ++i)
         for (int j = 0; j < TmpNbrColumn; ++j)
    ((char*) &(TmpRe), sizeof(double));
    ((char*) &(TmpIm), sizeof(double));
             cout << i << " " << j << " " << TmpRe << " " << TmpIm <<endl;