-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsl.c
62 lines (46 loc) · 1.14 KB
/
gsl.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
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_linalg.h>
#include "options.h"
int main (int argc, char** argv)
{
int n = 1000;
size_t ai, aj, bi;
double* a_data = 0;
double* b_data = 0;
size_t sizen = 0;
char* output_file = 0;
FILE* fout = 0;
options(argc, argv, &sizen, &output_file);
n = (int) sizen;
a_data = malloc(sizeof(double)*n*n);
b_data = malloc(sizeof(double)*n);
srand(42);
for (ai=0;ai<n;ai++) {
for (aj=0;aj<n;aj++) {
a_data[ai*n+aj] = rand();
}
}
for (bi=0;bi<n;bi++) {
b_data[bi]=rand();
}
gsl_matrix_view m
= gsl_matrix_view_array (a_data, n, n);
gsl_vector_view b
= gsl_vector_view_array (b_data, n);
gsl_vector *x = gsl_vector_alloc (n);
int s;
gsl_permutation * p = gsl_permutation_alloc (n);
gsl_linalg_LU_decomp (&m.matrix, p, &s);
gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
if (output_file) {
fout = fopen(output_file,"w");
gsl_vector_fprintf (fout, x, "%g");
free(output_file);
}
gsl_permutation_free (p);
gsl_vector_free (x);
free(a_data);
free(b_data);
return 0;
}