-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrices.cpp
More file actions
144 lines (123 loc) · 2.97 KB
/
Copy pathmatrices.cpp
File metadata and controls
144 lines (123 loc) · 2.97 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "matrices.h"
//prints the elements of a matrix in a file
void printmatrix(char* filename,gsl_matrix* m)
{
int i,j;
double s;
FILE* out = fopen(filename,"w");
if(NULL==out)
{
printf("Cannot open output file [%s]\n",filename);
exit(1);
}
for(i=0;i<m->size1;i++)
{
fprintf(out,"%.3lf",gsl_matrix_get(m,i,0));
for(j=1;j<m->size2;j++)
{
fprintf(out,"\t%.3lf",
gsl_matrix_get(m,i,j));
}
fprintf(out,"\n");
}
fclose(out);
return;
}
//creates the transpose of the matrix m
gsl_matrix* transposematrix(gsl_matrix* m)
{
int i,j;
gsl_matrix* tm = gsl_matrix_alloc(m->size2,m->size1);
for(i=0;i<tm->size1;i++)
{
for(j=0;j<tm->size2;j++)
{
gsl_matrix_set(tm,i,j,gsl_matrix_get(m,j,i));
}
}
return(tm);
}
//calculates the product of a nxp matrix m1 with a pxl matrix m2
//returns a nxl matrix m
void matrixproduct(gsl_matrix* m1,gsl_matrix* m2,gsl_matrix* m)
{
int i,j,k;
double s;
for(i=0;i<m->size1;i++)
{
for(k=0;k<m->size2;k++)
{
s = 0;
for(j=0;j<m1->size2;j++)
{
s += gsl_matrix_get(m1,i,j)*gsl_matrix_get(m2,j,k);
}
gsl_matrix_set(m,i,k,s);
}
}
return;
}
//computes the inverse of a positive definite matrix
//the function returns a new matrix which contains the inverse
//the matrix that gets inverted is not modified
gsl_matrix* inverse(gsl_matrix* K)
{
int j;
gsl_matrix* copyK = gsl_matrix_alloc(K->size1,K->size1);
if(GSL_SUCCESS!=gsl_matrix_memcpy(copyK,K))
{
printf("GSL failed to copy a matrix.\n");
exit(1);
}
gsl_matrix* inverse = gsl_matrix_alloc(K->size1,K->size1);
gsl_permutation *myperm = gsl_permutation_alloc(K->size1);
if(GSL_SUCCESS!=gsl_linalg_LU_decomp(copyK,myperm,&j))
{
printf("GSL failed LU decomposition.\n");
exit(1);
}
if(GSL_SUCCESS!=gsl_linalg_LU_invert(copyK,myperm,inverse))
{
printf("GSL failed matrix inversion.\n");
exit(1);
}
gsl_permutation_free(myperm);
gsl_matrix_free(copyK);
return(inverse);
}
//creates a submatrix of matrix M
//the indices of the rows and columns to be selected are
//specified in the last four arguments of this function
gsl_matrix* MakeSubmatrix(gsl_matrix* M,
int* IndRow,int lenIndRow,
int* IndColumn,int lenIndColumn)
{
int i,j;
gsl_matrix* subM = gsl_matrix_alloc(lenIndRow,lenIndColumn);
for(i=0;i<lenIndRow;i++)
{
for(j=0;j<lenIndColumn;j++)
{
gsl_matrix_set(subM,i,j,
gsl_matrix_get(M,IndRow[i],IndColumn[j]));
}
}
return(subM);
}
//computes the log of the determinant of a symmetric positive definite matrix
double logdet(gsl_matrix* K)
{
int i;
gsl_matrix* CopyOfK = gsl_matrix_alloc(K->size1,K->size2);
gsl_matrix_memcpy(CopyOfK,K);
gsl_permutation *myperm = gsl_permutation_alloc(K->size1);
if(GSL_SUCCESS!=gsl_linalg_LU_decomp(CopyOfK,myperm,&i))
{
printf("GSL failed LU decomposition.\n");
exit(1);
}
double logdet = gsl_linalg_LU_lndet(CopyOfK);
gsl_permutation_free(myperm);
gsl_matrix_free(CopyOfK);
return(logdet);
}