Full Storage Format for Matrices

Content

Goal: Getting familiar with the full storage format for matrices.

Exercise

In this first exercise you are asked to write a small C program that initializes a $$7 \times 8$$ col major matrix and then prints the matrix. The program should consist of three functions initMatrix, printMatrix and main:

• Function initMatrix initializes an $$m \times n$$ matrix. The initialization enumerates the matrix elements row-wise from $$1$$ to $$m\cdot n$$.

E.g. in the case where $$m=2$$ and $$n=3$$ the matrix afterwards looks like this:

$\begin{pmatrix}a_{0,0} & a_{0,1} & a_{0,2} \\a_{1,0} & a_{1,1} & a_{1,2} \\\end{pmatrix}\leftarrow\begin{pmatrix}1 & 2 & 3 \\4 & 5 & 6\end{pmatrix}$
• Function printMatrix is supposed to print the content of a matrix.

• In function main define and initialize local variables m, n, A, incRowA, incColA that describe the storage of a matrix:

• m and n should be of type size_t,

• A should be a pointer to double,

• incRowA and incColA should be of type ptrdiff.

After allocating memory for the matrix elements initialize and print the matrix. Before main returns release the memory.

Exercise

Modify your program such that it initializes a $$7 \times 8$$ row major matrix.

Exercise

Modify your program to achieve the following:

• In main initialize a $$7 \times 8$$ col major matrix $$A$$.

• Print matrix $$A$$.

• Print the transposed matrix, i.e. $$A^T$$

• Print the content of the allocated memory block as a row-vector with length $$m\cdot n$$.

• Print the second row of the matrix.

• Print the third column of the matrix.

• Print the $$2 \times 3$$ block

$\left(\begin{array}{ccc}a_{2,4} & a_{2,5} & a_{2,6} \\a_{3,4} & a_{3,5} & a_{3,6}\end{array}\right)$

Exercise

Modify your program to achieve the following:

• In main initialize a $$7 \times 8$$ row major matrix $$A$$.

• Print matrix $$A$$.

• Print the transposed matrix, i.e. $$A^T$$

• Print the content of the allocated memory block as a row-vector with length $$m\cdot n$$.

• Print the second row of the matrix.

• Print the third column of the matrix.

• Print the $$2 \times 3$$ block

$\left(\begin{array}{ccc}a_{2,4} & a_{2,5} & a_{2,6} \\a_{3,4} & a_{3,5} & a_{3,6}\end{array}\right)$