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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/* spmatrix/compress_source.c
*
* Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018 Patrick Alken
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
gsl_spmatrix_csc()
Create a sparse matrix in compressed column format
Inputs: T - sparse matrix in triplet format
Return: pointer to new matrix (should be freed when finished with it)
*/
int
FUNCTION (gsl_spmatrix, csc) (TYPE (gsl_spmatrix) * dest, const TYPE (gsl_spmatrix) * src)
{
if (!GSL_SPMATRIX_ISCOO(src))
{
GSL_ERROR_NULL("input matrix must be in COO format", GSL_EINVAL);
}
else if (!GSL_SPMATRIX_ISCSC(dest))
{
GSL_ERROR_NULL("output matrix must be in CSC format", GSL_EINVAL);
}
else if (src->size1 != dest->size1 || src->size2 != dest->size2)
{
GSL_ERROR("matrices must have same dimensions", GSL_EBADLEN);
}
else
{
int status;
const int *Tj = src->p; /* column indices of triplet matrix */
int *Cp; /* column pointers of compressed column matrix */
int *w; /* copy of column pointers */
size_t n, r;
if (dest->nzmax < src->nz)
{
status = FUNCTION (gsl_spmatrix, realloc) (src->nz, dest);
if (status)
return status;
}
Cp = dest->p;
/* initialize column pointers to 0 */
for (n = 0; n < dest->size2 + 1; ++n)
Cp[n] = 0;
/*
* compute the number of elements in each column:
* Cp[j] = # non-zero elements in column j
*/
for (n = 0; n < src->nz; ++n)
Cp[Tj[n]]++;
/* compute column pointers: p[j] = p[j-1] + nnz[j-1] */
gsl_spmatrix_cumsum(dest->size2, Cp);
/* make a copy of the column pointers */
w = dest->work.work_int;
for (n = 0; n < dest->size2; ++n)
w[n] = Cp[n];
/* transfer data from triplet format to CSC */
for (n = 0; n < src->nz; ++n)
{
int k = w[Tj[n]]++;
dest->i[k] = src->i[n];
for (r = 0; r < MULTIPLICITY; ++r)
dest->data[MULTIPLICITY * k + r] = src->data[MULTIPLICITY * n + r];
}
dest->nz = src->nz;
return GSL_SUCCESS;
}
}
/* XXX deprecated function */
TYPE (gsl_spmatrix) *
FUNCTION (gsl_spmatrix, compcol) (const TYPE (gsl_spmatrix) * src)
{
return FUNCTION (gsl_spmatrix, ccs) (src);
}
/* XXX deprecated function */
TYPE (gsl_spmatrix) *
FUNCTION (gsl_spmatrix, ccs) (const TYPE (gsl_spmatrix) * src)
{
TYPE (gsl_spmatrix) * dest = FUNCTION (gsl_spmatrix, alloc_nzmax) (src->size1, src->size2, src->nz, GSL_SPMATRIX_CSC);
FUNCTION (gsl_spmatrix, csc) (dest, src);
return dest;
}
/*
gsl_spmatrix_csr()
Create a sparse matrix in compressed row format
Inputs: dest - (output) sparse matrix in CSR format
src - sparse matrix in triplet format
Return: success/error
*/
int
FUNCTION (gsl_spmatrix, csr) (TYPE (gsl_spmatrix) * dest, const TYPE (gsl_spmatrix) * src)
{
if (!GSL_SPMATRIX_ISCOO(src))
{
GSL_ERROR("input matrix must be in COO format", GSL_EINVAL);
}
else if (!GSL_SPMATRIX_ISCSR(dest))
{
GSL_ERROR("output matrix must be in CSR format", GSL_EINVAL);
}
else if (src->size1 != dest->size1 || src->size2 != dest->size2)
{
GSL_ERROR("matrices must have same dimensions", GSL_EBADLEN);
}
else
{
int status;
const int *Ti = src->i; /* row indices of triplet matrix */
int *Cp; /* row pointers of compressed row matrix */
int *w; /* copy of column pointers */
size_t n, r;
if (dest->nzmax < src->nz)
{
status = FUNCTION (gsl_spmatrix, realloc) (src->nz, dest);
if (status)
return status;
}
Cp = dest->p;
/* initialize row pointers to 0 */
for (n = 0; n < dest->size1 + 1; ++n)
Cp[n] = 0;
/*
* compute the number of elements in each row:
* Cp[i] = # non-zero elements in row i
*/
for (n = 0; n < src->nz; ++n)
Cp[Ti[n]]++;
/* compute row pointers: p[i] = p[i-1] + nnz[i-1] */
gsl_spmatrix_cumsum(dest->size1, Cp);
/* make a copy of the row pointers */
w = dest->work.work_int;
for (n = 0; n < dest->size1; ++n)
w[n] = Cp[n];
/* transfer data from triplet format to CSR */
for (n = 0; n < src->nz; ++n)
{
int k = w[Ti[n]]++;
dest->i[k] = src->p[n];
for (r = 0; r < MULTIPLICITY; ++r)
dest->data[MULTIPLICITY * k + r] = src->data[MULTIPLICITY * n + r];
}
dest->nz = src->nz;
return GSL_SUCCESS;
}
}
/* XXX deprecated function */
TYPE (gsl_spmatrix) *
FUNCTION (gsl_spmatrix, crs) (const TYPE (gsl_spmatrix) * src)
{
TYPE (gsl_spmatrix) * dest = FUNCTION (gsl_spmatrix, alloc_nzmax) (src->size1, src->size2, src->nz, GSL_SPMATRIX_CSR);
FUNCTION (gsl_spmatrix, csr) (dest, src);
return dest;
}
TYPE (gsl_spmatrix) *
FUNCTION (gsl_spmatrix, compress) (const TYPE (gsl_spmatrix) * src, const int sptype)
{
int status = GSL_SUCCESS;
TYPE (gsl_spmatrix) * dest = FUNCTION (gsl_spmatrix, alloc_nzmax) (src->size1, src->size2, src->nz, sptype);
if (dest == NULL)
return NULL;
if (sptype == GSL_SPMATRIX_CSC)
{
status = FUNCTION (gsl_spmatrix, csc) (dest, src);
}
else if (sptype == GSL_SPMATRIX_CSR)
{
status = FUNCTION (gsl_spmatrix, csr) (dest, src);
}
else if (sptype == GSL_SPMATRIX_COO)
{
/* make a copy of src */
status = FUNCTION (gsl_spmatrix, memcpy) (dest, src);
}
else
{
status = GSL_EINVAL;
GSL_ERROR_NULL ("unknown sparse matrix format", status);
}
if (status != GSL_SUCCESS)
{
FUNCTION (gsl_spmatrix, free) (dest);
return NULL;
}
return dest;
}
|