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
|
//------------------------------------------------------------------------------
// GB_mex_complex: convert a real matrix into a complex one
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// If A is real, C has an all-zero imaginary part.
// If A is complex, then C = A.
// This is a sparse version of the built-in 'complex' function, which does not
// work for sparse matrices. This is self-contained and does not use GraphBLAS
// at all.
#include "mex.h"
#include "matrix.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define USAGE "C = GB_mex_complex (A)"
void mexFunction
(
int nargout,
mxArray *pargout [ ],
int nargin,
const mxArray *pargin [ ]
)
{
// check inputs
if (nargout > 1 || nargin != 1)
{
mexErrMsgTxt ("Usage: " USAGE) ;
}
// get the input matrix
const mxArray *A = pargin [0] ;
bool A_is_sparse = mxIsSparse (A) ;
if (mxIsLogical (A))
{
mexErrMsgTxt ("A must be double or double complex") ;
}
int64_t *Ap, *Ai ;
if (A_is_sparse)
{
Ap = (int64_t *) mxGetJc (A) ;
Ai = (int64_t *) mxGetIr (A) ;
}
else
{
Ap = NULL ;
Ai = NULL ;
}
double *Ax = NULL ;
if (mxIsComplex (A))
{
Ax = (double *) mxGetComplexDoubles (pargin [0]) ;
}
else
{
Ax = (double *) mxGetDoubles (pargin [0]) ;
}
int64_t m = mxGetM (A) ;
int64_t n = mxGetN (A) ;
int64_t anz = (A_is_sparse) ? Ap [n] : (m*n) ;
// create the output matrix
if (A_is_sparse)
{
// A and C are sparse
pargout [0] = mxCreateSparse (m, n, anz+1, mxCOMPLEX) ;
int64_t *Cp = (int64_t *) mxGetJc (pargout [0]) ;
int64_t *Ci = (int64_t *) mxGetIr (pargout [0]) ;
// copy the pattern of A into C
memcpy (Cp, Ap, (n+1) * sizeof (int64_t)) ;
memcpy (Ci, Ai, anz * sizeof (int64_t)) ;
}
else
{
// A and C are full
pargout [0] = mxCreateDoubleMatrix (m, n, mxCOMPLEX) ;
}
// copy the values of A into C
double *Cx = (double *) mxGetComplexDoubles (pargout [0]) ;
if (mxIsComplex (A))
{
memcpy (Cx, Ax, anz * 2 * sizeof (double)) ;
}
else
{
for (int64_t k = 0 ; k < anz ; k++)
{
Cx [2*k ] = Ax [k] ;
Cx [2*k+1] = 0 ;
}
}
}
|