-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspqr_solve.cpp
299 lines (257 loc) · 9.95 KB
/
spqr_solve.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// =============================================================================
// === spqr_solve mexFunction ==================================================
// =============================================================================
#include "spqr_mx.hpp"
/* x = A\b using a sparse QR factorization.
x = spqr_solve (A,b,opts) ;
A 2nd optional output gives statistics, in a self-explainable struct.
Let [m n] = size (A).
opts is an optional struct with the following fields. Fields not
present are set to their defaults. The defaults are:
opts.tol = 20 * (m+n) * eps * sqrt(max(diag(A'*A)))
opts.ordering = "colamd"
opts.tol: Default 20 * (m+n) * eps * sqrt(max(diag(A'*A))),
that is, 20 * (m+n) * eps * (max 2-norm of the columns of A).
If tol >= 0, then an approximate rank-revealing QR factorization
is used. A diagonal entry of R with magnitude < tol is treated
as zero; the matrix A is considered rank deficient.
If tol = -1, then no rank detection is attempted. This allows
the sparse QR to use less memory than when attempting to detect
rank, even for full-rank matrices. Thus, if you know your
matrix has full rank, tol = -1 will reduce memory usage. Zeros
may appear on the diagonal of R if A is rank deficient.
If tol <= -2, then the default tolerance is used.
opts.ordering: Default is "default"
See spqr.m for info.
opts.solution: default "basic"
Defines how an underdetermined system should be solved (m < n).
"basic": compute a basic solution, using
[C,R,E]=spqr(A,B) ; X=E*(R\C) ; where C=Q'*B
"min2norm": compute a minimum 2-norm solution, using
[C,R,E]=spqr(A') ; X = Q*(R'\(E'*B))
A min2norm solution is more costly to compute, in time
and memory, than a basic solution.
This option is ignored if m >= n.
*/
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
Long *Bp, *Bi ;
double *Ax, *Bx, dummy ;
Long m, n, k, bncols, p, i, rank, A_complex, B_complex, is_complex,
anz, bnz ;
spqr_mx_options opts ;
cholmod_sparse *A, Amatrix, *Xsparse ;
cholmod_dense *Xdense ;
cholmod_common Common, *cc ;
char msg [LEN+1] ;
double t0 = (nargout > 1) ? SuiteSparse_time ( ) : 0 ;
// -------------------------------------------------------------------------
// start CHOLMOD and set parameters
// -------------------------------------------------------------------------
cc = &Common ;
cholmod_l_start (cc) ;
spqr_mx_config (SPUMONI, cc) ;
// -------------------------------------------------------------------------
// check inputs
// -------------------------------------------------------------------------
if (nargout > 2)
{
mexErrMsgIdAndTxt ("MATLAB:maxlhs", "Too many output arguments") ;
}
if (nargin < 2)
{
mexErrMsgIdAndTxt ("MATLAB:minrhs", "Not enough input arguments") ;
}
if (nargin > 3)
{
mexErrMsgIdAndTxt ("MATLAB:maxrhs", "Too many input arguments") ;
}
// -------------------------------------------------------------------------
// get the input matrix A (must be sparse)
// -------------------------------------------------------------------------
if (!mxIsSparse (pargin [0]))
{
mexErrMsgIdAndTxt ("QR:invalidInput", "A must be sparse") ;
}
A = spqr_mx_get_sparse (pargin [0], &Amatrix, &dummy) ;
m = A->nrow ;
n = A->ncol ;
A_complex = mxIsComplex (pargin [0]) ;
B_complex = mxIsComplex (pargin [1]) ;
is_complex = (A_complex || B_complex) ;
Ax = spqr_mx_merge_if_complex (pargin [0], is_complex, &anz, cc) ;
if (is_complex)
{
// A has been converted from real or zomplex to complex
A->x = Ax ;
A->z = NULL ;
A->xtype = CHOLMOD_COMPLEX ;
}
// -------------------------------------------------------------------------
// determine usage and parameters
// -------------------------------------------------------------------------
spqr_mx_get_options ((nargin < 3) ? NULL : pargin [2], &opts, m, 3, cc) ;
opts.Qformat = SPQR_Q_DISCARD ;
opts.econ = 0 ;
opts.permvector = TRUE ;
opts.haveB = TRUE ;
// -------------------------------------------------------------------------
// get the input matrix B (sparse or dense)
// -------------------------------------------------------------------------
if (!mxIsNumeric (pargin [1]))
{
mexErrMsgIdAndTxt ("QR:invalidInput", "invalid non-numeric B") ;
}
if (mxGetM (pargin [1]) != m)
{
mexErrMsgIdAndTxt ("QR:invalidInput",
"A and B must have the same number of rows") ;
}
cholmod_sparse Bsmatrix, *Bsparse ;
cholmod_dense Bdmatrix, *Bdense ;
// convert from real or zomplex to complex
Bx = spqr_mx_merge_if_complex (pargin [1], is_complex, &bnz, cc) ;
int B_is_sparse = mxIsSparse (pargin [1]) ;
if (B_is_sparse)
{
Bsparse = spqr_mx_get_sparse (pargin [1], &Bsmatrix, &dummy) ;
Bdense = NULL ;
if (is_complex)
{
// Bsparse has been converted from real or zomplex to complex
Bsparse->x = Bx ;
Bsparse->z = NULL ;
Bsparse->xtype = CHOLMOD_COMPLEX ;
}
}
else
{
Bsparse = NULL ;
Bdense = spqr_mx_get_dense (pargin [1], &Bdmatrix, &dummy) ;
if (is_complex)
{
// Bdense has been converted from real or zomplex to complex
Bdense->x = Bx ;
Bdense->z = NULL ;
Bdense->xtype = CHOLMOD_COMPLEX ;
}
}
// -------------------------------------------------------------------------
// X = A\B
// -------------------------------------------------------------------------
if (opts.min2norm && m < n)
{
#ifndef NEXPERT
// This requires SuiteSparseQR_expert.cpp
if (is_complex)
{
if (B_is_sparse)
{
// X and B are both sparse and complex
Xsparse = SuiteSparseQR_min2norm <Complex> (opts.ordering,
opts.tol, A, Bsparse, cc) ;
pargout [0] = spqr_mx_put_sparse (&Xsparse, cc) ;
}
else
{
// X and B are both dense and complex
Xdense = SuiteSparseQR_min2norm <Complex> (opts.ordering,
opts.tol, A, Bdense, cc) ;
pargout [0] = spqr_mx_put_dense (&Xdense, cc) ;
}
}
else
{
if (B_is_sparse)
{
// X and B are both sparse and real
Xsparse = SuiteSparseQR_min2norm <double> (opts.ordering,
opts.tol, A, Bsparse, cc) ;
pargout [0] = spqr_mx_put_sparse (&Xsparse, cc) ;
}
else
{
// X and B are both dense and real
Xdense = SuiteSparseQR_min2norm <double> (opts.ordering,
opts.tol, A, Bdense, cc) ;
pargout [0] = spqr_mx_put_dense (&Xdense, cc) ;
}
}
#else
mexErrMsgIdAndTxt ("QR:notInstalled", "min2norm method not installed") ;
#endif
}
else
{
if (is_complex)
{
if (B_is_sparse)
{
// X and B are both sparse and complex
Xsparse = SuiteSparseQR <Complex> (opts.ordering, opts.tol,
A, Bsparse, cc) ;
pargout [0] = spqr_mx_put_sparse (&Xsparse, cc) ;
}
else
{
// X and B are both dense and complex
Xdense = SuiteSparseQR <Complex> (opts.ordering, opts.tol,
A, Bdense, cc) ;
pargout [0] = spqr_mx_put_dense (&Xdense, cc) ;
}
}
else
{
if (B_is_sparse)
{
// X and B are both sparse and real
Xsparse = SuiteSparseQR <double> (opts.ordering, opts.tol,
A, Bsparse, cc) ;
pargout [0] = spqr_mx_put_sparse (&Xsparse, cc) ;
}
else
{
// X and B are both dense and real
Xdense = SuiteSparseQR <double> (opts.ordering, opts.tol,
A, Bdense, cc) ;
pargout [0] = spqr_mx_put_dense (&Xdense, cc) ;
}
}
}
// -------------------------------------------------------------------------
// info output
// -------------------------------------------------------------------------
if (nargout > 1)
{
double flops = cc->SPQR_flopcount ;
double t = SuiteSparse_time ( ) - t0 ;
pargout [1] = spqr_mx_info (cc, t, flops) ;
}
// -------------------------------------------------------------------------
// warn if rank deficient
// -------------------------------------------------------------------------
rank = cc->SPQR_istat [4] ;
if (rank < MIN (m,n))
{
// snprintf would be safer, but Windows is oblivious to safety ...
// (Visual Studio C++ 2008 does not recognize snprintf!)
sprintf (msg, "rank deficient. rank = %ld tol = %g\n", rank,
cc->SPQR_tol_used) ;
mexWarnMsgIdAndTxt ("MATLAB:rankDeficientMatrix", msg) ;
}
if (is_complex)
{
// free the merged complex copies of A and B
cholmod_l_free (anz, sizeof (Complex), Ax, cc) ;
cholmod_l_free (bnz, sizeof (Complex), Bx, cc) ;
}
cholmod_l_finish (cc) ;
if (opts.spumoni > 0) spqr_mx_spumoni (&opts, is_complex, cc) ;
}