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
|
//------------------------------------------------------------------------------
// COLAMD/MATLAB/symamdmex.c: MATLAB interface for SYMAMD
//------------------------------------------------------------------------------
// COLAMD, Copyright (c) 1998-2022, Timothy A. Davis and Stefan Larimore,
// All Rights Reserved.
// SPDX-License-Identifier: BSD-3-clause
//------------------------------------------------------------------------------
/* SYMAMD mexFunction
Usage:
P = symamd2 (A) ;
[ P, stats ] = symamd2 (A, knobs) ;
See symamd.m for a description.
Authors:
The authors of the code itself are Stefan I. Larimore and Timothy A.
Davis (DrTimothyAldenDavis@gmail.com). The algorithm was
developed in collaboration with John Gilbert, Xerox PARC, and Esmond
Ng, Oak Ridge National Laboratory.
Acknowledgements:
This work was supported by the National Science Foundation, under
grants DMS-9504974 and DMS-9803599.
Availability:
The colamd/symamd library is available at http://www.suitesparse.com
*/
/* ========================================================================== */
/* === Include files ======================================================== */
/* ========================================================================== */
#include "colamd.h"
#include "mex.h"
#include "matrix.h"
#include <stdlib.h>
/* ========================================================================== */
/* === symamd mexFunction =================================================== */
/* ========================================================================== */
void mexFunction
(
/* === Parameters ======================================================= */
int nlhs, /* number of left-hand sides */
mxArray *plhs [], /* left-hand side matrices */
int nrhs, /* number of right--hand sides */
const mxArray *prhs [] /* right-hand side matrices */
)
{
/* === Local variables ================================================== */
int64_t *perm ; /* column ordering of M and ordering of A */
int64_t *A ; /* row indices of input matrix A */
int64_t *p ; /* column pointers of input matrix A */
int64_t n_col ; /* number of columns of A */
int64_t n_row ; /* number of rows of A */
int full ; /* TRUE if input matrix full, FALSE if sparse */
double knobs [COLAMD_KNOBS] ; /* colamd user-controllable parameters */
double *out_perm ; /* output permutation vector */
double *out_stats ; /* output stats vector */
double *in_knobs ; /* input knobs vector */
int64_t i ; /* loop counter */
mxArray *Ainput ; /* input matrix handle */
int spumoni ; /* verbosity variable */
int64_t stats [COLAMD_STATS] ; /* stats for symamd */
/* === Check inputs ===================================================== */
if (nrhs < 1 || nrhs > 2 || nlhs < 0 || nlhs > 2)
{
mexErrMsgTxt (
"symamd: incorrect number of input and/or output arguments.") ;
}
/* === Get knobs ======================================================== */
colamd_l_set_defaults (knobs) ;
spumoni = 0 ;
/* check for user-passed knobs */
if (nrhs == 2)
{
in_knobs = mxGetPr (prhs [1]) ;
i = mxGetNumberOfElements (prhs [1]) ;
if (i > 0) knobs [COLAMD_DENSE_ROW] = in_knobs [0] ;
if (i > 1) spumoni = (int) (in_knobs [1] != 0) ;
}
/* print knob settings if spumoni is set */
if (spumoni)
{
mexPrintf ("\nsymamd version %d.%d, %s:\n",
COLAMD_MAIN_VERSION, COLAMD_SUB_VERSION, COLAMD_DATE) ;
if (knobs [COLAMD_DENSE_ROW] >= 0)
{
mexPrintf ("knobs(1): %g, rows/cols with > "
"max(16,%g*sqrt(size(A,2))) entries removed\n",
in_knobs [0], knobs [COLAMD_DENSE_ROW]) ;
}
else
{
mexPrintf ("knobs(1): %g, no dense rows removed\n", in_knobs [0]) ;
}
mexPrintf ("knobs(2): %g, statistics and knobs printed\n",
in_knobs [1]) ;
}
/* === If A is full, convert to a sparse matrix ========================= */
Ainput = (mxArray *) prhs [0] ;
if (mxGetNumberOfDimensions (Ainput) != 2)
{
mexErrMsgTxt ("symamd: input matrix must be 2-dimensional.") ;
}
full = !mxIsSparse (Ainput) ;
if (full)
{
mexCallMATLAB (1, &Ainput, 1, (mxArray **) prhs, "sparse") ;
}
/* === Allocate workspace for symamd ==================================== */
/* get size of matrix */
n_row = mxGetM (Ainput) ;
n_col = mxGetN (Ainput) ;
if (n_col != n_row)
{
mexErrMsgTxt ("symamd: matrix must be square.") ;
}
A = (int64_t *) mxGetIr (Ainput) ;
p = (int64_t *) mxGetJc (Ainput) ;
perm = (int64_t *) mxCalloc (n_col+1, sizeof (int64_t)) ;
/* === Order the rows and columns of A (does not destroy A) ============= */
if (!symamd_l (n_col, A, p, perm, knobs, stats, &mxCalloc, &mxFree))
{
symamd_l_report (stats) ;
mexErrMsgTxt ("symamd error!") ;
}
if (full)
{
mxDestroyArray (Ainput) ;
}
/* === Return the permutation vector ==================================== */
plhs [0] = mxCreateDoubleMatrix (1, n_col, mxREAL) ;
out_perm = mxGetPr (plhs [0]) ;
for (i = 0 ; i < n_col ; i++)
{
/* symamd is 0-based, but MATLAB expects this to be 1-based */
out_perm [i] = perm [i] + 1 ;
}
mxFree (perm) ;
/* === Return the stats vector ========================================== */
/* print stats if spumoni is set */
if (spumoni)
{
symamd_l_report (stats) ;
}
if (nlhs == 2)
{
plhs [1] = mxCreateDoubleMatrix (1, COLAMD_STATS, mxREAL) ;
out_stats = mxGetPr (plhs [1]) ;
for (i = 0 ; i < COLAMD_STATS ; i++)
{
out_stats [i] = stats [i] ;
}
/* fix stats (5) and (6), for 1-based information on jumbled matrix. */
/* note that this correction doesn't occur if symamd returns FALSE */
out_stats [COLAMD_INFO1] ++ ;
out_stats [COLAMD_INFO2] ++ ;
}
}
|