-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
78 lines (60 loc) · 2.11 KB
/
matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Author: Chris Wailes <chris.wailes@gmail.com>
* Project: Parallel Linear Program Solver
* Date: 2011/11/10
* Description: Functions for dealing with matrices.
*/
// Standard Includes
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
// Project Includes
#include "matrix.h"
#include "util.h"
inline double matrix_accum_value(matrix_t* m, uint row_index, uint col_index, double val) {
return m->values[row_index*m->num_cols + col_index] += val;
}
void matrix_free(matrix_t* m) {
free(m->values);
}
inline double* matrix_get_address(const matrix_t* m, uint row_index, uint col_index) {
return &m->values[row_index*m->num_cols + col_index];
}
inline double* matrix_get_row(const matrix_t* m, uint row_index) {
return &m->values[row_index * m->num_cols];
}
inline double matrix_get_value(const matrix_t* m, uint row_index, uint col_index) {
return m->values[row_index*m->num_cols + col_index];
}
void matrix_init(matrix_t* m, uint num_rows, uint num_cols) {
// Set the number of rows and columns.
m->num_rows = num_rows;
m->num_cols = num_cols;
// Allocate space for the actual values.
m->values = (double*)malloc(num_rows * num_cols * sizeof(double));
// Zero-initialize the matrix;
memset(m->values, 0, num_rows * num_cols * sizeof(double));
}
void matrix_resize(matrix_t* m, uint new_rows, uint new_cols) {
uint row_index, col_index;
double* new_v;
if (m->num_rows == new_rows && m->num_cols == new_cols) {
return;
}
new_v = (double*)malloc(new_rows * new_cols * sizeof(double));
// Zero-initialize the matrix.
memset(new_v, 0, new_rows * new_cols * sizeof(double));
// Copy the old values over.
for (row_index = 0; row_index < MIN(new_rows, m->num_rows); ++row_index) {
for (col_index = 0; col_index < MIN(new_cols, m->num_cols); ++ col_index) {
new_v[row_index*new_cols + col_index] = matrix_get_value(m, row_index, col_index);
}
}
free(m->values);
m->values = new_v;
m->num_rows = new_rows;
m->num_cols = new_cols;
}
inline double matrix_set_value(matrix_t* m, uint row_index, uint col_index, double val) {
return m->values[row_index*m->num_cols + col_index] = val;
}