-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
67 lines (52 loc) · 1.13 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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "matrix.h"
int alloc_matrix(struct matrix *m, size_t nr, size_t nc)
{
size_t i;
m->nr = nr;
m->nc = nc;
m->v = malloc(sizeof(*m->v) * nr * nc); if (m->v == NULL) return 1;
m->r = malloc(sizeof(*m->r) * nr); if (m->r == NULL) return 1;
for (i=0; i < nr; i++)
{
m->r[i] = m->v + i*nc;
}
return 0;
}
static int write_matrix0(FILE *fp, struct matrix *m)
{
size_t i,j;
for (i=0; i < m->nr; i++)
{
for (j=0; j < m->nc; j++)
{
fprintf(fp, "%24.15e", m->r[i][j]);
}
fprintf(fp, "\n");
}
return 0;
}
int write_matrix(struct matrix *m, const char *fmt, ...)
{
char *fname;
va_list ap;
va_start(ap, fmt);
vasprintf(&fname, fmt, ap);
va_end(ap);
if (fname == NULL) return 1;
va_start(ap, fmt);
vsprintf(fname, fmt, ap);
va_end(ap);
FILE *fp = fopen(fname, "wb");
if (fp != NULL)
{
Log("Writing %s...\n", fname);
write_matrix0(fp, m);
fclose(fp);
}
free(fname);
return 0;
}