#include <stdio.h>		/* printf */
#include <stdlib.h>		/* malloc, abort */
#include <assert.h>

#include "matrix.h"

struct matrix {
  int num_rows;
  int num_columns;
  double *elements;
};

struct matrix *alloc_matrix(int num_rows, int num_columns)
{
  struct matrix *m = malloc(sizeof(struct matrix));
  if (m == NULL) {
    fprintf(stderr, "Allocation of memory failed\n");
    abort();
  }
  (*m).num_rows = num_rows;
  (*m).num_columns = num_columns;
  (*m).elements = malloc(sizeof(double) * num_rows * num_columns);
  if ((*m).elements == NULL) {
    fprintf(stderr, "Allocation of memory failed\n");
    abort();
  }
  return m;
}

/* Zugriffsfunktionen (Accessors) */

int matrix_num_rows(struct matrix *matrix)
{
  return matrix->num_rows;
}

int matrix_num_columns(struct matrix *matrix)
{
  return matrix->num_columns;
}

double matrix_element(struct matrix *matrix, int row, int column)
{
  return *((*matrix).elements + row * (*matrix).num_columns + column);
}

void set_matrix_element(struct matrix *matrix, int row, int column,
			double element)
{
  *((*matrix).elements + row * (*matrix).num_columns + column) = element;
}

/* Destruktor */

void free_matrix(struct matrix *matrix)
{
  free((*matrix).elements);
  free(matrix);
}

