Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_t_cholmod_dense.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /* === Core/t_cholmod_dense ================================================= */
3 /* ========================================================================== */
4 
5 /* -----------------------------------------------------------------------------
6  * CHOLMOD/Core Module. Copyright (C) 2005-2006,
7  * Univ. of Florida. Author: Timothy A. Davis
8  * The CHOLMOD/Core Module is licensed under Version 2.1 of the GNU
9  * Lesser General Public License. See lesser.txt for a text of the license.
10  * CHOLMOD is also available under other licenses; contact authors for details.
11  * http://www.cise.ufl.edu/research/sparse
12  * -------------------------------------------------------------------------- */
13 
14 /* Template routine for cholmod_dense. All xtypes supported, except that there
15  * are no dense matrices with an xtype of pattern. */
16 
18 
19 /* ========================================================================== */
20 /* === t_cholmod_sparse_to_dense ============================================ */
21 /* ========================================================================== */
22 
23 static cholmod_dense *TEMPLATE (cholmod_sparse_to_dense)
24 (
25  /* ---- input ---- */
26  cholmod_sparse *A, /* matrix to copy */
27  /* --------------- */
28  cholmod_common *Common
29 )
30 {
31  double *Ax, *Xx, *Az, *Xz ;
32  Int *Ap, *Ai, *Anz ;
33  cholmod_dense *X ;
34  Int i, j, p, pend, nrow, ncol, packed ;
35 
36  /* ---------------------------------------------------------------------- */
37  /* get inputs */
38  /* ---------------------------------------------------------------------- */
39 
40  nrow = A->nrow ;
41  ncol = A->ncol ;
42  packed = A->packed ;
43  Ap = A->p ;
44  Ai = A->i ;
45  Ax = A->x ;
46  Az = A->z ;
47  Anz = A->nz ;
48 
49  /* ---------------------------------------------------------------------- */
50  /* allocate result */
51  /* ---------------------------------------------------------------------- */
52 
53  X = CHOLMOD(zeros) (nrow, ncol, XTYPE2, Common) ;
54  if (Common->status < CHOLMOD_OK)
55  {
56  return (NULL) ; /* out of memory */
57  }
58  Xx = X->x ;
59  Xz = X->z ;
60 
61  /* ---------------------------------------------------------------------- */
62  /* copy into dense matrix */
63  /* ---------------------------------------------------------------------- */
64 
65  if (A->stype < 0)
66  {
67  /* A is symmetric with lower stored, but both parts of X are present */
68  for (j = 0 ; j < ncol ; j++)
69  {
70  p = Ap [j] ;
71  pend = (packed) ? (Ap [j+1]) : (p + Anz [j]) ;
72  for ( ; p < pend ; p++)
73  {
74  i = Ai [p] ;
75  if (i >= j)
76  {
77  ASSIGN2 (Xx, Xz, i+j*nrow, Ax, Az, p) ;
78  ASSIGN2_CONJ (Xx, Xz, j+i*nrow, Ax, Az, p) ;
79  }
80  }
81  }
82  }
83  else if (A->stype > 0)
84  {
85  /* A is symmetric with upper stored, but both parts of X are present */
86  for (j = 0 ; j < ncol ; j++)
87  {
88  p = Ap [j] ;
89  pend = (packed) ? (Ap [j+1]) : (p + Anz [j]) ;
90  for ( ; p < pend ; p++)
91  {
92  i = Ai [p] ;
93  if (i <= j)
94  {
95  ASSIGN2 (Xx, Xz, i+j*nrow, Ax, Az, p) ;
96  ASSIGN2_CONJ (Xx, Xz, j+i*nrow, Ax, Az, p) ;
97  }
98  }
99  }
100  }
101  else
102  {
103  /* both parts of A and X are present */
104  for (j = 0 ; j < ncol ; j++)
105  {
106  p = Ap [j] ;
107  pend = (packed) ? (Ap [j+1]) : (p + Anz [j]) ;
108  for ( ; p < pend ; p++)
109  {
110  i = Ai [p] ;
111  ASSIGN2 (Xx, Xz, i+j*nrow, Ax, Az, p) ;
112  }
113  }
114  }
115 
116  return (X) ;
117 }
118 
119 
120 #ifndef PATTERN
121 
122 /* There are no dense matrices of xtype CHOLMOD_PATTERN */
123 
124 /* ========================================================================== */
125 /* === t_cholmod_dense_to_sparse ============================================ */
126 /* ========================================================================== */
127 
128 static cholmod_sparse *TEMPLATE (cholmod_dense_to_sparse)
129 (
130  /* ---- input ---- */
131  cholmod_dense *X, /* matrix to copy */
132  int values, /* TRUE if values to be copied, FALSE otherwise */
133  /* --------------- */
134  cholmod_common *Common
135 )
136 {
137  double *Xx, *Cx, *Xz, *Cz ;
138  Int *Ci, *Cp ;
139  cholmod_sparse *C ;
140  Int i, j, p, d, nrow, ncol, nz ;
141 
142  /* ---------------------------------------------------------------------- */
143  /* get inputs */
144  /* ---------------------------------------------------------------------- */
145 
146  nrow = X->nrow ;
147  ncol = X->ncol ;
148  d = X->d ;
149  Xx = X->x ;
150  Xz = X->z ;
151 
152  /* ---------------------------------------------------------------------- */
153  /* count the number of nonzeros in the result */
154  /* ---------------------------------------------------------------------- */
155 
156  nz = 0 ;
157  for (j = 0 ; j < ncol ; j++)
158  {
159  for (i = 0 ; i < nrow ; i++)
160  {
161  if (ENTRY_IS_NONZERO (Xx, Xz, i+j*d))
162  {
163  nz++ ;
164  }
165  }
166  }
167 
168  /* ---------------------------------------------------------------------- */
169  /* allocate the result C */
170  /* ---------------------------------------------------------------------- */
171 
172  C = CHOLMOD(allocate_sparse) (nrow, ncol, nz, TRUE, TRUE, 0,
173  values ? XTYPE : CHOLMOD_PATTERN, Common) ;
174  if (Common->status < CHOLMOD_OK)
175  {
176  return (NULL) ; /* out of memory */
177  }
178  Cp = C->p ;
179  Ci = C->i ;
180  Cx = C->x ;
181  Cz = C->z ;
182 
183  /* ---------------------------------------------------------------------- */
184  /* copy the dense matrix X into the sparse matrix C */
185  /* ---------------------------------------------------------------------- */
186 
187  p = 0 ;
188  for (j = 0 ; j < ncol ; j++)
189  {
190  Cp [j] = p ;
191  for (i = 0 ; i < nrow ; i++)
192  {
193  if (ENTRY_IS_NONZERO (Xx, Xz, i+j*d))
194  {
195  Ci [p] = i ;
196  if (values)
197  {
198  ASSIGN (Cx, Cz, p, Xx, Xz, i+j*d) ;
199  }
200  p++ ;
201  }
202  }
203  }
204  ASSERT (p == nz) ;
205  Cp [ncol] = nz ;
206 
207  /* ---------------------------------------------------------------------- */
208  /* return result */
209  /* ---------------------------------------------------------------------- */
210 
211  ASSERT (CHOLMOD(dump_sparse) (C, "C", Common) >= 0) ;
212  return (C) ;
213 }
214 
215 
216 /* ========================================================================== */
217 /* === t_cholmod_copy_dense2 ================================================ */
218 /* ========================================================================== */
219 
220 /* Y = X, where X and Y are both already allocated. */
221 
222 static int TEMPLATE (cholmod_copy_dense2)
223 (
224  /* ---- input ---- */
225  cholmod_dense *X, /* matrix to copy */
226  /* ---- output --- */
227  cholmod_dense *Y /* copy of matrix X */
228 )
229 {
230  double *Xx, *Xz, *Yx, *Yz ;
231  Int i, j, nrow, ncol, dy, dx ;
232 
233  /* ---------------------------------------------------------------------- */
234  /* get inputs */
235  /* ---------------------------------------------------------------------- */
236 
237  Xx = X->x ;
238  Xz = X->z ;
239  Yx = Y->x ;
240  Yz = Y->z ;
241  dx = X->d ;
242  dy = Y->d ;
243  nrow = X->nrow ;
244  ncol = X->ncol ;
245 
246  /* ---------------------------------------------------------------------- */
247  /* copy */
248  /* ---------------------------------------------------------------------- */
249 
250  CLEAR (Yx, Yz, 0) ;
251  for (j = 0 ; j < ncol ; j++)
252  {
253  for (i = 0 ; i < nrow ; i++)
254  {
255  ASSIGN (Yx, Yz, i+j*dy, Xx, Xz, i+j*dx) ;
256  }
257  }
258  return (TRUE) ;
259 }
260 
261 #endif
262 
263 #undef PATTERN
264 #undef REAL
265 #undef COMPLEX
266 #undef ZOMPLEX
static int TEMPLATE() cholmod_copy_dense2(cholmod_dense *X, cholmod_dense *Y)
#define Int
static cholmod_dense *TEMPLATE() cholmod_sparse_to_dense(cholmod_sparse *A, cholmod_common *Common)
#define CHOLMOD_PATTERN
#define CHOLMOD(name)
#define NULL
cholmod_dense *CHOLMOD() zeros(size_t nrow, size_t ncol, int xtype, cholmod_common *Common)
#define CLEAR(c)
#define ASSERT(expression)
#define CHOLMOD_OK
cholmod_sparse *CHOLMOD() allocate_sparse(size_t nrow, size_t ncol, size_t nzmax, int sorted, int packed, int stype, int xtype, cholmod_common *Common)
static cholmod_sparse *TEMPLATE() cholmod_dense_to_sparse(cholmod_dense *X, int values, cholmod_common *Common)
#define TRUE
#define ASSIGN(c, s1, s2, p, split)