Amesos Package Browser (Single Doxygen Collection) Development
amesos_t_cholmod_dense.c
Go to the documentation of this file.
00001 /* ========================================================================== */
00002 /* === Core/t_cholmod_dense ================================================= */
00003 /* ========================================================================== */
00004 
00005 /* -----------------------------------------------------------------------------
00006  * CHOLMOD/Core Module.  Copyright (C) 2005-2006,
00007  * Univ. of Florida.  Author: Timothy A. Davis
00008  * The CHOLMOD/Core Module is licensed under Version 2.1 of the GNU
00009  * Lesser General Public License.  See lesser.txt for a text of the license.
00010  * CHOLMOD is also available under other licenses; contact authors for details.
00011  * http://www.cise.ufl.edu/research/sparse
00012  * -------------------------------------------------------------------------- */
00013 
00014 /* Template routine for cholmod_dense.  All xtypes supported, except that there
00015  * are no dense matrices with an xtype of pattern. */
00016 
00017 #include "amesos_cholmod_template.h"
00018 
00019 /* ========================================================================== */
00020 /* === t_cholmod_sparse_to_dense ============================================ */
00021 /* ========================================================================== */
00022 
00023 static cholmod_dense *TEMPLATE (cholmod_sparse_to_dense)
00024 (
00025     /* ---- input ---- */
00026     cholmod_sparse *A,  /* matrix to copy */
00027     /* --------------- */
00028     cholmod_common *Common
00029 )
00030 {
00031     double *Ax, *Xx, *Az, *Xz ;
00032     Int *Ap, *Ai, *Anz ;
00033     cholmod_dense *X ;
00034     Int i, j, p, pend, nrow, ncol, packed ;
00035 
00036     /* ---------------------------------------------------------------------- */
00037     /* get inputs */
00038     /* ---------------------------------------------------------------------- */
00039 
00040     nrow = A->nrow ;
00041     ncol = A->ncol ;
00042     packed = A->packed ;
00043     Ap = A->p ;
00044     Ai = A->i ;
00045     Ax = A->x ;
00046     Az = A->z ;
00047     Anz = A->nz ;
00048 
00049     /* ---------------------------------------------------------------------- */
00050     /* allocate result */
00051     /* ---------------------------------------------------------------------- */
00052 
00053     X = CHOLMOD(zeros) (nrow, ncol, XTYPE2, Common) ;
00054     if (Common->status < CHOLMOD_OK)
00055     {
00056   return (NULL) ;     /* out of memory */
00057     }
00058     Xx = X->x ;
00059     Xz = X->z ;
00060 
00061     /* ---------------------------------------------------------------------- */
00062     /* copy into dense matrix */
00063     /* ---------------------------------------------------------------------- */
00064 
00065     if (A->stype < 0)
00066     {
00067   /* A is symmetric with lower stored, but both parts of X are present */
00068   for (j = 0 ; j < ncol ; j++)
00069   {
00070       p = Ap [j] ;
00071       pend = (packed) ? (Ap [j+1]) : (p + Anz [j]) ;
00072       for ( ; p < pend ; p++)
00073       {
00074     i = Ai [p] ;
00075     if (i >= j)
00076     {
00077         ASSIGN2 (Xx, Xz, i+j*nrow, Ax, Az, p) ;
00078         ASSIGN2_CONJ (Xx, Xz, j+i*nrow, Ax, Az, p) ;
00079     }
00080       }
00081   }
00082     }
00083     else if (A->stype > 0)
00084     {
00085   /* A is symmetric with upper stored, but both parts of X are present */
00086   for (j = 0 ; j < ncol ; j++)
00087   {
00088       p = Ap [j] ;
00089       pend = (packed) ? (Ap [j+1]) : (p + Anz [j]) ;
00090       for ( ; p < pend ; p++)
00091       {
00092     i = Ai [p] ;
00093     if (i <= j)
00094     {
00095         ASSIGN2 (Xx, Xz, i+j*nrow, Ax, Az, p) ;
00096         ASSIGN2_CONJ (Xx, Xz, j+i*nrow, Ax, Az, p) ;
00097     }
00098       }
00099   }
00100     }
00101     else
00102     {
00103   /* both parts of A and X are present */
00104   for (j = 0 ; j < ncol ; j++)
00105   {
00106       p = Ap [j] ;
00107       pend = (packed) ? (Ap [j+1]) : (p + Anz [j]) ;
00108       for ( ; p < pend ; p++)
00109       {
00110     i = Ai [p] ;
00111     ASSIGN2 (Xx, Xz, i+j*nrow, Ax, Az, p) ;
00112       }
00113   }
00114     }
00115 
00116     return (X) ;
00117 }
00118 
00119 
00120 #ifndef PATTERN
00121 
00122 /* There are no dense matrices of xtype CHOLMOD_PATTERN */
00123 
00124 /* ========================================================================== */
00125 /* === t_cholmod_dense_to_sparse ============================================ */
00126 /* ========================================================================== */
00127 
00128 static cholmod_sparse *TEMPLATE (cholmod_dense_to_sparse)
00129 (
00130     /* ---- input ---- */
00131     cholmod_dense *X, /* matrix to copy */
00132     int values,   /* TRUE if values to be copied, FALSE otherwise */
00133     /* --------------- */
00134     cholmod_common *Common
00135 )
00136 {
00137     double *Xx, *Cx, *Xz, *Cz ;
00138     Int *Ci, *Cp ;
00139     cholmod_sparse *C ;
00140     Int i, j, p, d, nrow, ncol, nz ;
00141 
00142     /* ---------------------------------------------------------------------- */
00143     /* get inputs */
00144     /* ---------------------------------------------------------------------- */
00145 
00146     nrow = X->nrow ;
00147     ncol = X->ncol ;
00148     d = X->d ;
00149     Xx = X->x ;
00150     Xz = X->z ;
00151 
00152     /* ---------------------------------------------------------------------- */
00153     /* count the number of nonzeros in the result */
00154     /* ---------------------------------------------------------------------- */
00155 
00156     nz = 0 ;
00157     for (j = 0 ; j < ncol ; j++)
00158     {
00159   for (i = 0 ; i < nrow ; i++)
00160   {
00161       if (ENTRY_IS_NONZERO (Xx, Xz, i+j*d))
00162       {
00163     nz++ ;
00164       }
00165   }
00166     }
00167 
00168     /* ---------------------------------------------------------------------- */
00169     /* allocate the result C */
00170     /* ---------------------------------------------------------------------- */
00171 
00172     C = CHOLMOD(allocate_sparse) (nrow, ncol, nz, TRUE, TRUE, 0,
00173       values ? XTYPE : CHOLMOD_PATTERN, Common) ;
00174     if (Common->status < CHOLMOD_OK)
00175     {
00176   return (NULL) ;     /* out of memory */
00177     }
00178     Cp = C->p ;
00179     Ci = C->i ;
00180     Cx = C->x ;
00181     Cz = C->z ;
00182 
00183     /* ---------------------------------------------------------------------- */
00184     /* copy the dense matrix X into the sparse matrix C */
00185     /* ---------------------------------------------------------------------- */
00186 
00187     p = 0 ;
00188     for (j = 0 ; j < ncol ; j++)
00189     {
00190   Cp [j] = p ;
00191   for (i = 0 ; i < nrow ; i++)
00192   {
00193       if (ENTRY_IS_NONZERO (Xx, Xz, i+j*d))
00194       {
00195     Ci [p] = i ;
00196     if (values)
00197     {
00198         ASSIGN (Cx, Cz, p, Xx, Xz, i+j*d) ;
00199     }
00200     p++ ;
00201       }
00202   }
00203     }
00204     ASSERT (p == nz) ;
00205     Cp [ncol] = nz ;
00206 
00207     /* ---------------------------------------------------------------------- */
00208     /* return result */
00209     /* ---------------------------------------------------------------------- */
00210 
00211     ASSERT (CHOLMOD(dump_sparse) (C, "C", Common) >= 0) ;
00212     return (C) ;
00213 }
00214 
00215 
00216 /* ========================================================================== */
00217 /* === t_cholmod_copy_dense2 ================================================ */
00218 /* ========================================================================== */
00219 
00220 /* Y = X, where X and Y are both already allocated.  */
00221 
00222 static int TEMPLATE (cholmod_copy_dense2)
00223 (
00224     /* ---- input ---- */
00225     cholmod_dense *X, /* matrix to copy */
00226     /* ---- output --- */
00227     cholmod_dense *Y  /* copy of matrix X */
00228 )
00229 {
00230     double *Xx, *Xz, *Yx, *Yz ;
00231     Int i, j, nrow, ncol, dy, dx ;
00232 
00233     /* ---------------------------------------------------------------------- */
00234     /* get inputs */
00235     /* ---------------------------------------------------------------------- */
00236 
00237     Xx = X->x ;
00238     Xz = X->z ;
00239     Yx = Y->x ;
00240     Yz = Y->z ;
00241     dx = X->d ;
00242     dy = Y->d ;
00243     nrow = X->nrow ;
00244     ncol = X->ncol ;
00245 
00246     /* ---------------------------------------------------------------------- */
00247     /* copy */
00248     /* ---------------------------------------------------------------------- */
00249 
00250     CLEAR (Yx, Yz, 0) ;
00251     for (j = 0 ; j < ncol ; j++)
00252     {
00253   for (i = 0 ; i < nrow ; i++)
00254   {
00255       ASSIGN (Yx, Yz, i+j*dy, Xx, Xz, i+j*dx) ;
00256   }
00257     }
00258     return (TRUE) ;
00259 }
00260 
00261 #endif
00262 
00263 #undef PATTERN
00264 #undef REAL
00265 #undef COMPLEX
00266 #undef ZOMPLEX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines