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
|
// =============================================================================
// === spqr_qmult mexFunction ==================================================
// =============================================================================
#include "spqr_mx.hpp"
// Multiply Q times X, where Q is stored as a struct, in Householder form.
//
// method = 0: Y = Q'*X default
// method = 1: Y = Q*X
// method = 2: Y = X*Q'
// method = 3: Y = X*Q
//
// Usage:
//
// Y = spqr_qmult (Q,X,method) ;
//
// where Q is the struct from [Q,R,E] = spqr (A,opts) with
// opts.Q = 'Householder'
//
// Q.H: Householder vectors (m-by-nh). In each column, the nonzero entry with
// the smallest row index must be equal to 1.0.
// Q.Tau: Householder coefficients (1-by-nh).
// Q.P: inverse row permutation. P [i] = k if row i of X is row k of H and Y.
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
mxArray *Hmatlab, *Tau, *P ;
Long *HPinv, *Yp, *Yi ;
double *Hx, *Xx, *Tx, *Px, dummy ;
Long m, n, k, nh, nb, p, i, method, mh, gotP, X_is_sparse, is_complex, hnz,
tnz, xnz, inuse, count ;
cholmod_sparse *Ysparse, *H, Hmatrix, *Xsparse, Xsmatrix ;
cholmod_dense *Ydense, *Xdense, Xdmatrix, *HTau, HTau_matrix ;
cholmod_common Common, *cc ;
// -------------------------------------------------------------------------
// start CHOLMOD and set parameters
// -------------------------------------------------------------------------
cc = &Common ;
cholmod_l_start (cc) ;
spqr_mx_config (SPUMONI, cc) ;
// -------------------------------------------------------------------------
// check inputs
// -------------------------------------------------------------------------
// nargin can be 2 or 3
// nargout can be 0 or 1
if (nargout > 1)
{
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") ;
}
if (!mxIsStruct (pargin [0]))
{
mexErrMsgIdAndTxt ("QR:invalidInput", "invalid Q (must be a struct)") ;
}
// -------------------------------------------------------------------------
// get H, Tau, and P from the Q struct
// -------------------------------------------------------------------------
i = mxGetFieldNumber (pargin [0], "H") ;
if (i < 0)
{
mexErrMsgIdAndTxt ("QR:invalidInput", "invalid Q struct") ;
}
Hmatlab = mxGetFieldByNumber (pargin [0], 0, i) ;
nh = mxGetN (Hmatlab) ;
if (!mxIsSparse (Hmatlab))
{
mexErrMsgIdAndTxt ("QR:invalidInput", "H must be sparse") ;
}
i = mxGetFieldNumber (pargin [0], "Tau") ;
if (i < 0)
{
mexErrMsgIdAndTxt ("QR:invalidInput", "invalid Q struct") ;
}
Tau = mxGetFieldByNumber (pargin [0], 0, i) ;
if (nh != mxGetNumberOfElements (Tau))
{
mexErrMsgIdAndTxt ("QR:invalidInput",
"H and Tau must have the same number of columns") ;
}
is_complex = mxIsComplex (Tau) || mxIsComplex (Hmatlab) ||
mxIsComplex (pargin [1]) ;
// -------------------------------------------------------------------------
// get the Householder vectors
// -------------------------------------------------------------------------
H = spqr_mx_get_sparse (Hmatlab, &Hmatrix, &dummy) ;
mh = H->nrow ;
Hx = spqr_mx_merge_if_complex (Hmatlab, is_complex, &hnz, cc) ;
if (is_complex)
{
// H has been converted from real or zomplex to complex
H->x = Hx ;
H->z = NULL ;
H->xtype = CHOLMOD_COMPLEX ;
}
// -------------------------------------------------------------------------
// get Tau
// -------------------------------------------------------------------------
HTau = spqr_mx_get_dense (Tau, &HTau_matrix, &dummy) ;
Tx = spqr_mx_merge_if_complex (Tau, is_complex, &tnz, cc) ;
if (is_complex)
{
// HTau has been converted from real or zomplex to complex
HTau->x = Tx ;
HTau->z = NULL ;
HTau->xtype = CHOLMOD_COMPLEX ;
}
// -------------------------------------------------------------------------
// get method
// -------------------------------------------------------------------------
if (nargin < 3)
{
method = 0 ;
}
else
{
method = (Long) mxGetScalar (pargin [2]) ;
if (method < 0 || method > 3)
{
mexErrMsgIdAndTxt ("QR:invalidInput", "invalid method") ;
}
}
// -------------------------------------------------------------------------
// get X
// -------------------------------------------------------------------------
m = mxGetM (pargin [1]) ;
n = mxGetN (pargin [1]) ;
X_is_sparse = mxIsSparse (pargin [1]) ;
Xsparse = NULL ;
if (X_is_sparse)
{
Xsparse = spqr_mx_get_sparse (pargin [1], &Xsmatrix, &dummy) ;
}
else
{
Xdense = spqr_mx_get_dense (pargin [1], &Xdmatrix, &dummy) ;
}
Xx = spqr_mx_merge_if_complex (pargin [1], is_complex, &xnz, cc) ;
if (is_complex)
{
// X has been converted from real or zomplex to complex
if (X_is_sparse)
{
Xsparse->x = Xx ;
Xsparse->xtype = CHOLMOD_COMPLEX ;
}
else
{
Xdense->x = Xx ;
Xdense->xtype = CHOLMOD_COMPLEX ;
}
}
if (method == 0 || method == 1)
{
if (mh != m)
{
mexErrMsgIdAndTxt ("QR:invalidInput",
"H and X must have same number of rows") ;
}
}
else
{
if (mh != n)
{
mexErrMsgIdAndTxt ("QR:invalidInput",
"# of cols of X must equal # of rows of H") ;
}
}
// -------------------------------------------------------------------------
// get P
// -------------------------------------------------------------------------
i = mxGetFieldNumber (pargin [0], "P") ;
gotP = (i >= 0) ;
HPinv = NULL ;
if (gotP)
{
// get P from the H struct
P = mxGetFieldByNumber (pargin [0], 0, i) ;
if (mxGetNumberOfElements (P) != mh)
{
mexErrMsgIdAndTxt ("QR:invalidInput",
"P must be a vector of length equal to # rows of H") ;
}
HPinv = (Long *) cholmod_l_malloc (mh, sizeof (Long), cc) ;
Px = mxGetPr (P) ;
for (i = 0 ; i < mh ; i++)
{
HPinv [i] = (Long) (Px [i] - 1) ;
if (HPinv [i] < 0 || HPinv [i] >= mh)
{
mexErrMsgIdAndTxt ("QR:invalidInput", "invalid permutation") ;
}
}
}
// -------------------------------------------------------------------------
// Y = Q'*X, Q*X, X*Q or X*Q'
// -------------------------------------------------------------------------
if (is_complex)
{
if (X_is_sparse)
{
Ysparse = SuiteSparseQR_qmult <Complex> (method, H,
HTau, HPinv, Xsparse, cc) ;
pargout [0] = spqr_mx_put_sparse (&Ysparse, cc) ;
}
else
{
Ydense = SuiteSparseQR_qmult <Complex> (method, H,
HTau, HPinv, Xdense, cc) ;
pargout [0] = spqr_mx_put_dense (&Ydense, cc) ;
}
}
else
{
if (X_is_sparse)
{
Ysparse = SuiteSparseQR_qmult <double> (method, H,
HTau, HPinv, Xsparse, cc) ;
pargout [0] = spqr_mx_put_sparse (&Ysparse, cc) ;
}
else
{
Ydense = SuiteSparseQR_qmult <double> (method, H,
HTau, HPinv, Xdense, cc) ;
pargout [0] = spqr_mx_put_dense (&Ydense, cc) ;
}
}
// -------------------------------------------------------------------------
// free workspace
// -------------------------------------------------------------------------
cholmod_l_free (mh, sizeof (Long), HPinv, cc) ;
if (is_complex)
{
// free the merged copies of the real parts of the H and Tau matrices
cholmod_l_free (hnz, sizeof (Complex), Hx, cc) ;
cholmod_l_free (tnz, sizeof (Complex), Tx, cc) ;
cholmod_l_free (xnz, sizeof (Complex), Xx, cc) ;
}
cholmod_l_finish (cc) ;
#if 0
// malloc count for testing only ...
spqr_mx_get_usage (pargout [0], 1, &inuse, &count, cc) ;
if (inuse != cc->memory_inuse || count != cc->malloc_count)
{
mexErrMsgIdAndTxt ("QR:internalError", "memory leak!") ;
}
#endif
}
|