Matrices and Vectors

Content

In this examples a macro named COLMAJOR is used to specify whether matrices are stored column major (if COLMAJOR equals 1) or row major (if COLMAJOR equals ).

In the program we use the CPP directives

#ifndef COLMAJOR
#define COLMAJOR 1
#endif

for this purpose:

Notes

Exercise: Simple initialization and print functions for matrices

Complete the skeleton below:

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>

// function initGeMatrix
/*
   Your Code here.
*/

// function printGeMatrix
/*
   Your Code here.
*/

#ifndef COLMAJOR
#define COLMAJOR 1
#endif

int
main()
{
    printf("COLMAJOR = %d\n", COLMAJOR);

    size_t m = 5, n = 10;

    ptrdiff_t incRowA = COLMAJOR ? 0 : 0;       // <- FIXME
    ptrdiff_t incColA = COLMAJOR ? 0 : 0;       // <- FIXME

    // allocate memory for A
    double *A = malloc(m*n*sizeof(double));
    if (!A) {
        abort();
    }

    // initialize matrix A
    initGeMatrix(m, n, A, incRowA, incColA);

    // print matrix A
    printf("A =\n");
    printGeMatrix(m, n, A, incRowA, incColA);

    // print how elements are stored in memory
    printf("memory layout of A:\n");
    // use printGeMatrix!                       // <- Your code here

    // print a matrix view of A
    printf("A(0:2, 0:3) =\n");
    printGeMatrix(0, 0, A, incRowA, incColA);   // <- FIXME

    // print a matrix view of A
    printf("A(2:4, 3:8) =\n");
    printGeMatrix(0, 0, A, incRowA, incColA);   // <- FIXME

    // print a matrix view of A
    printf("A(2:4, 3:8)^T =\n");
    printGeMatrix(0, 0, A, incRowA, incColA);   // <- FIXME

    // release memory
    free(A);
}