TechBlox » matrix http://techblox.fusun.ch/wordpress Solutions To A Computer Scientist's Daily Problems Tue, 14 Sep 2010 11:11:54 +0000 en-US hourly 1 http://wordpress.org/?v=3.4.1 Transfer Sparse Matrix From C++ To Matlab http://techblox.fusun.ch/wordpress/2010/06/sparse-matrix-trick/ http://techblox.fusun.ch/wordpress/2010/06/sparse-matrix-trick/#comments Mon, 14 Jun 2010 04:00:24 +0000 TechBloxWriter http://techblox.fusun.ch/wordpress/?p=121 It happens that your C++ code solves large sparse linear systems that involve large sparse matrices. Debugging such code is a be-yotch. Only super-brained engineers can spot hidden errors in matrices that are displayed as an unformatted array of numbers. And even for them it’s an inconvenient affair. But not to worry. Matlab gladly helps us displaying any matrix in many wonderful ways.

First, we need some mechanism to render the matrix into a file. Let A be a sparse matrix. We open a file file, iterate over all values in A, and write every item sItem with preceding row and column number into a new line:

1
2
3
4
5
6
7
8
9
10
11
12
13
QFile *file = OpenFileWriteOnly("A.txt");
if (file != NULL)
{
    SparseRowMatrix::const_iterator it;
    for (it = A.begin(); it != A.end(); it++)
    {
        QString sItem = QString::number(it.data());
        QString sLine;
        sLine.sprintf("%d %d %s \n", it.row(), it.col(), sItem.toAscii().data());
        file.write(sLine.toAscii());
    }
    CloseFile(file);
}


Second, we load the matrix into matlab using the following code:

1
2
>> tmp = load('A.txt');
>> A = sparse(tmp(:, 1) + 1, tmp(:, 2) + 1, tmp(:, 3));

And third, voilĂ , you can use your all time favorite matlab methods to analyse the sh*t out of A. If you have no favorite one, try

1
>> spy(A)

Wow, this matrix surely looks odd…

]]>
http://techblox.fusun.ch/wordpress/2010/06/sparse-matrix-trick/feed/ 0
Advanced Formulae in Latex http://techblox.fusun.ch/wordpress/2010/05/advanced-formulae-in-latex/ http://techblox.fusun.ch/wordpress/2010/05/advanced-formulae-in-latex/#comments Thu, 27 May 2010 17:06:39 +0000 TechBloxWriter http://techblox.fusun.ch/wordpress/?p=15 Remember when you couln’t think of the correct commands to write a certain formula in latex because it’s been a year since you did it the last time? This might help you find the light.

Vectors

Using

1
\newcommand{\ve}[1]{\mbox{\bf #1}}

in your preamble, you can easily write vectors as follows:

1
2
3
\begin{equation}
    \mathscr{L}(\ve v_i) = \ve v_i - \frac{1}{\left| N_i \right|} \sum_{j\in N_i} \ve v_j
\end{equation}

Matrices

You always need matrices. Try this code:

1
2
3
4
5
6
7
8
9
10
\begin{equation}
    \begin{pmatrix}
        A & A_k
    \end{pmatrix}
    \begin{pmatrix}
        D \\
        D_k
    \end{pmatrix}
    = 0
\end{equation}

Multi-case definition

This code comes in handy to define certain funtions or matrices:

1
2
3
4
5
6
7
8
\begin{equation}
    A_{ij} = \left\{
        \begin{array}{l l}
            1 & \quad (i,j) \in K \\
            0 & \quad \text{otherwise} \\
        \end{array}
        \right
\end{equation}
]]>
http://techblox.fusun.ch/wordpress/2010/05/advanced-formulae-in-latex/feed/ 2