-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.c
executable file
·62 lines (50 loc) · 1.35 KB
/
util.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 "util.h"
void print_matrix(const gsl_matrix *X, const char *title, FILE *fp)
{
size_t i,j;
if(title != NULL) fprintf(fp,"Matrix %s:\n", title);
for(i=0;i<X->size1;i++)
{
for(j=0;j<X->size2;j++)
{
fprintf(fp, "%f%c", gsl_matrix_get(X,i,j), (j<X->size2-1?'\t':'\n'));
}
}
}
gsl_matrix *PyArrayObject_to_gsl_matrix(PyArrayObject *x)
{
gsl_block *b;
gsl_matrix *r;
if(x->nd != 2 || x->descr->type_num != NPY_DOUBLE)
{
PyErr_SetString(PyExc_ValueError, "Cannot convert non 2D matrix to gsl_matrix");
return NULL;
}
b = (gsl_block*)malloc(sizeof(gsl_block));
r = (gsl_matrix*)malloc(sizeof(gsl_matrix));
r->size1 = x->dimensions[0];
r->tda = r->size2 = x->dimensions[1];
r->owner = 1;
b->data = r->data = (double*)x->data;
r->block = b;
b->size = r->size1 * r->size2;
return r;
}
PyArrayObject *gsl_matrix_to_PyArrayObject(gsl_matrix *x, const int new_memory)
{
npy_intp dimensions[2] = {x->size1, x->size2};
PyArrayObject *ptr;
if(new_memory){
ptr = (PyArrayObject*)PyArray_SimpleNew(2, dimensions, NPY_DOUBLE);
memcpy(ptr->data, x->data, x->block->size * sizeof(double));
}else{
ptr = (PyArrayObject*)PyArray_SimpleNewFromData(2, dimensions, NPY_DOUBLE, (void*)x->data);
}
return (PyArrayObject*)PyArray_Return(ptr);
}
void gsl_matrix_partial_free(gsl_matrix *x)
{
// data fields is not my own
free(x->block);
free(x);
}