-
Notifications
You must be signed in to change notification settings - Fork 40
/
example.c
177 lines (139 loc) · 4.41 KB
/
example.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "qdldl.h"
#include <stdlib.h>
#include <stdio.h>
void print_arrayi(const QDLDL_int* data, QDLDL_int n,char* varName);
void print_arrayf(const QDLDL_float* data, QDLDL_int n, char* varName);
void print_line(void);
const QDLDL_int An = 10;
const QDLDL_int Ap[] = {0, 1, 2, 4, 5, 6, 8, 10, 12, 14, 17};
const QDLDL_int Ai[] = {0, 1, 1, 2, 3, 4, 1, 5, 0, 6, 3, 7, 6, 8, 1, 2, 9};
const QDLDL_float Ax[] = {1.0, 0.460641, -0.121189, 0.417928, 0.177828, 0.1,
-0.0290058, -1.0, 0.350321, -0.441092, -0.0845395,
-0.316228, 0.178663, -0.299077, 0.182452, -1.56506, -0.1};
const QDLDL_float b[] = {1,2,3,4,5,6,7,8,9,10};
int main()
{
QDLDL_int i; // Counter
//data for L and D factors
QDLDL_int Ln = An;
QDLDL_int *Lp;
QDLDL_int *Li;
QDLDL_float *Lx;
QDLDL_float *D;
QDLDL_float *Dinv;
//data for elim tree calculation
QDLDL_int *etree;
QDLDL_int *Lnz;
QDLDL_int sumLnz;
//working data for factorisation
QDLDL_int *iwork;
QDLDL_bool *bwork;
QDLDL_float *fwork;
//Data for results of A\b
QDLDL_float *x;
/*--------------------------------
* pre-factorisation memory allocations
*---------------------------------*/
//These can happen *before* the etree is calculated
//since the sizes are not sparsity pattern specific
//For the elimination tree
etree = (QDLDL_int*)malloc(sizeof(QDLDL_int)*An);
Lnz = (QDLDL_int*)malloc(sizeof(QDLDL_int)*An);
//For the L factors. Li and Lx are sparsity dependent
//so must be done after the etree is constructed
Lp = (QDLDL_int*)malloc(sizeof(QDLDL_int)*(An+1));
D = (QDLDL_float*)malloc(sizeof(QDLDL_float)*An);
Dinv = (QDLDL_float*)malloc(sizeof(QDLDL_float)*An);
//Working memory. Note that both the etree and factor
//calls requires a working vector of QDLDL_int, with
//the factor function requiring 3*An elements and the
//etree only An elements. Just allocate the larger
//amount here and use it in both places
iwork = (QDLDL_int*)malloc(sizeof(QDLDL_int)*(3*An));
bwork = (QDLDL_bool*)malloc(sizeof(QDLDL_bool)*An);
fwork = (QDLDL_float*)malloc(sizeof(QDLDL_float)*An);
/*--------------------------------
* elimination tree calculation
*---------------------------------*/
sumLnz = QDLDL_etree(An,Ap,Ai,iwork,Lnz,etree);
/*--------------------------------
* LDL factorisation
*---------------------------------*/
//First allocate memory for Li and Lx
Li = (QDLDL_int*)malloc(sizeof(QDLDL_int)*sumLnz);
Lx = (QDLDL_float*)malloc(sizeof(QDLDL_float)*sumLnz);
//now factor
QDLDL_factor(An,Ap,Ai,Ax,Lp,Li,Lx,D,Dinv,Lnz,etree,bwork,iwork,fwork);
/*--------------------------------
* solve
*---------------------------------*/
x = (QDLDL_float*)malloc(sizeof(QDLDL_float)*An);
//when solving A\b, start with x = b
for(i=0;i < Ln; i++) x[i] = b[i];
QDLDL_solve(Ln,Lp,Li,Lx,Dinv,x);
/*--------------------------------
* print factors and solution
*---------------------------------*/
printf("\n");
printf("A (CSC format):\n");
print_line();
print_arrayi(Ap, An + 1, "A.p");
print_arrayi(Ai, Ap[An], "A.i");
print_arrayf(Ax, Ap[An], "A.x");
printf("\n\n");
printf("elimination tree:\n");
print_line();
print_arrayi(etree, Ln, "etree");
print_arrayi(Lnz, Ln, "Lnz");
printf("\n\n");
printf("L (CSC format):\n");
print_line();
print_arrayi(Lp, Ln + 1, "L.p");
print_arrayi(Li, Lp[Ln], "L.i");
print_arrayf(Lx, Lp[Ln], "L.x");
printf("\n\n");
printf("D:\n");
print_line();
print_arrayf(D, An, "diag(D) ");
print_arrayf(Dinv, An, "diag(D^{-1})");
printf("\n\n");
printf("solve results:\n");
print_line();
print_arrayf(b, An, "b");
print_arrayf(x, An, "A\\b");
printf("\n\n");
/*--------------------------------
* clean up
*---------------------------------*/
free(Lp);
free(Li);
free(Lx);
free(D);
free(Dinv);
free(etree);
free(Lnz);
free(iwork);
free(bwork);
free(fwork);
free(x);
return 0 ;
}
void print_line(void){
printf("--------------------------\n");
}
void print_arrayi(const QDLDL_int* data, QDLDL_int n,char* varName){
QDLDL_int i;
printf("%s = [",varName);
for(i=0; i< n; i++){
printf("%i,",(int)data[i]);
}
printf("]\n");
}
void print_arrayf(const QDLDL_float* data, QDLDL_int n, char* varName){
QDLDL_int i;
printf("%s = [",varName);
for(i=0; i< n; i++){
printf("%.3g,",data[i]);
}
printf("]\n");
}