1 /* ========================================================================== */
2 /* === RBio/RBio/RBread.c: MATLAB mexFunction for reading R/B file ========== */
3 /* ========================================================================== */
4
5 /* Copyright 2009, Timothy A. Davis, All Rights Reserved.
6 Refer to RBio/Doc/license.txt for the RBio license. */
7
8 /*
9 [A Z title key mtype] = RBread (filename)
10
11 A: a sparse matrix (no explicit zero entries)
12 Z: binary pattern of explicit zero entries in Rutherford/Boeing file.
13 This always has the same size as A, and is always sparse.
14
15 title: the 72-character title string in the file
16 key: the 8-character matrix name in the file
17 mtype: see RBwrite.m for a description.
18 */
19
20 #include "RBio.h"
21 #define LEN 1024
22 #define TRUE (1)
23 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
24 #define Long SuiteSparse_long
25
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])26 void mexFunction
27 (
28 int nargout,
29 mxArray *pargout [ ],
30 int nargin,
31 const mxArray *pargin [ ]
32 )
33 {
34 Long *Ap, *Ai, *Zp, *Zi ;
35 double *Ax, *Az, *Zx ;
36 Long p, j, build_upper, zero_handling, nrow, ncol, mkind, skind, asize, znz,
37 status ;
38 char filename [LEN+1], title [73], key [9], mtype [4] ;
39
40 /* ---------------------------------------------------------------------- */
41 /* check inputs */
42 /* ---------------------------------------------------------------------- */
43
44 if (nargin != 1 || nargout > 5 || !mxIsChar (pargin [0]))
45 {
46 mexErrMsgTxt ("Usage: [A Z title key mtype] = RBread (filename)") ;
47 }
48
49 /* ---------------------------------------------------------------------- */
50 /* get filename */
51 /* ---------------------------------------------------------------------- */
52
53 if (mxGetString (pargin [0], filename, LEN) != 0)
54 {
55 mexErrMsgTxt ("filename too long") ;
56 }
57
58 /* ---------------------------------------------------------------------- */
59 /* read the matrix */
60 /* ---------------------------------------------------------------------- */
61
62 build_upper = TRUE ; /* always build upper tri. part */
63 zero_handling = (nargout > 1) ? 2 : 1 ; /* prune or extract zeros */
64
65 status = RBread (filename, build_upper, zero_handling, title, key, mtype,
66 &nrow, &ncol, &mkind, &skind, &asize, &znz,
67 &Ap, &Ai, &Ax, &Az, &Zp, &Zi) ;
68
69 if (status != RBIO_OK)
70 {
71 RBerror (status) ;
72 mexErrMsgTxt ("error reading file") ;
73 }
74
75 /* ---------------------------------------------------------------------- */
76 /* return A to MATLAB */
77 /* ---------------------------------------------------------------------- */
78
79 pargout [0] = mxCreateSparse (0, 0, 0, (mkind == 2) ? mxCOMPLEX : mxREAL) ;
80 mxFree (mxGetJc (pargout [0])) ;
81 mxFree (mxGetIr (pargout [0])) ;
82 mxFree (mxGetPr (pargout [0])) ;
83 if (mkind == 2) mxFree (mxGetPi (pargout [0])) ;
84 mxSetM (pargout [0], nrow) ;
85 mxSetN (pargout [0], ncol) ;
86 mxSetNzmax (pargout [0], asize) ;
87 mxSetJc (pargout [0], (mwIndex *) Ap) ;
88 mxSetIr (pargout [0], (mwIndex *) Ai) ;
89 mxSetPr (pargout [0], Ax) ;
90 if (mkind == 2) mxSetPi (pargout [0], Az) ;
91
92 /* ---------------------------------------------------------------------- */
93 /* return Z to MATLAB */
94 /* ---------------------------------------------------------------------- */
95
96 if (nargout > 1)
97 {
98 Zx = (double *) SuiteSparse_malloc (znz, sizeof (double)) ;
99 for (p = 0 ; p < znz ; p++)
100 {
101 Zx [p] = 1 ;
102 }
103 pargout [1] = mxCreateSparse (0, 0, 0, mxREAL) ;
104 mxFree (mxGetJc (pargout [1])) ;
105 mxFree (mxGetIr (pargout [1])) ;
106 mxFree (mxGetPr (pargout [1])) ;
107 mxSetM (pargout [1], nrow) ;
108 mxSetN (pargout [1], ncol) ;
109 mxSetNzmax (pargout [1], MAX (znz,1)) ;
110 mxSetJc (pargout [1], (mwIndex *) Zp) ;
111 mxSetIr (pargout [1], (mwIndex *) Zi) ;
112 mxSetPr (pargout [1], Zx) ;
113 }
114
115 /* ---------------------------------------------------------------------- */
116 /* return title */
117 /* ---------------------------------------------------------------------- */
118
119 if (nargout > 2)
120 {
121 pargout [2] = mxCreateString (title) ;
122 }
123
124 /* ---------------------------------------------------------------------- */
125 /* return key */
126 /* ---------------------------------------------------------------------- */
127
128 if (nargout > 3)
129 {
130 pargout [3] = mxCreateString (key) ;
131 }
132
133 /* ---------------------------------------------------------------------- */
134 /* return mtype */
135 /* ---------------------------------------------------------------------- */
136
137 if (nargout > 4)
138 {
139 pargout [4] = mxCreateString (mtype) ;
140 }
141 }
142