1 /* matrix/swap_source.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 FUNCTION (gsl_matrix, swap_rows) (TYPE (gsl_matrix) * m,
22 const size_t i, const size_t j)
24 const size_t size1 = m->size1;
25 const size_t size2 = m->size2;
29 GSL_ERROR ("first row index is out of range", GSL_EINVAL);
34 GSL_ERROR ("second row index is out of range", GSL_EINVAL);
39 ATOMIC *row1 = m->data + MULTIPLICITY * i * m->tda;
40 ATOMIC *row2 = m->data + MULTIPLICITY * j * m->tda;
44 for (k = 0; k < MULTIPLICITY * size2; k++)
46 ATOMIC tmp = row1[k] ;
56 FUNCTION (gsl_matrix, swap_columns) (TYPE (gsl_matrix) * m,
57 const size_t i, const size_t j)
59 const size_t size1 = m->size1;
60 const size_t size2 = m->size2;
64 GSL_ERROR ("first column index is out of range", GSL_EINVAL);
69 GSL_ERROR ("second column index is out of range", GSL_EINVAL);
74 ATOMIC *col1 = m->data + MULTIPLICITY * i;
75 ATOMIC *col2 = m->data + MULTIPLICITY * j;
79 for (p = 0; p < size1; p++)
82 size_t n = p * MULTIPLICITY * m->tda;
84 for (k = 0; k < MULTIPLICITY; k++)
86 ATOMIC tmp = col1[n+k] ;
87 col1[n+k] = col2[n+k] ;
98 FUNCTION (gsl_matrix, swap_rowcol) (TYPE (gsl_matrix) * m,
99 const size_t i, const size_t j)
101 const size_t size1 = m->size1;
102 const size_t size2 = m->size2;
106 GSL_ERROR ("matrix must be square to swap row and column", GSL_ENOTSQR);
111 GSL_ERROR ("row index is out of range", GSL_EINVAL);
116 GSL_ERROR ("column index is out of range", GSL_EINVAL);
120 ATOMIC *row = m->data + MULTIPLICITY * i * m->tda;
121 ATOMIC *col = m->data + MULTIPLICITY * j;
125 for (p = 0; p < size1; p++)
129 size_t r = p * MULTIPLICITY;
130 size_t c = p * MULTIPLICITY * m->tda;
132 for (k = 0; k < MULTIPLICITY; k++)
134 ATOMIC tmp = col[c+k] ;
135 col[c+k] = row[r+k] ;
146 FUNCTION (gsl_matrix, transpose) (TYPE (gsl_matrix) * m)
148 const size_t size1 = m->size1;
149 const size_t size2 = m->size2;
154 GSL_ERROR ("matrix must be square to take transpose", GSL_ENOTSQR);
157 for (i = 0; i < size1; i++)
159 for (j = i + 1 ; j < size2 ; j++)
161 for (k = 0; k < MULTIPLICITY; k++)
163 size_t e1 = (i * m->tda + j) * MULTIPLICITY + k ;
164 size_t e2 = (j * m->tda + i) * MULTIPLICITY + k ;
166 ATOMIC tmp = m->data[e1] ;
167 m->data[e1] = m->data[e2] ;
178 FUNCTION (gsl_matrix, transpose_memcpy) (TYPE (gsl_matrix) * dest,
179 const TYPE (gsl_matrix) * src)
181 const size_t src_size1 = src->size1;
182 const size_t src_size2 = src->size2;
184 const size_t dest_size1 = dest->size1;
185 const size_t dest_size2 = dest->size2;
189 if (dest_size2 != src_size1 || dest_size1 != src_size2)
191 GSL_ERROR ("dimensions of dest matrix must be transpose of src matrix",
195 for (i = 0; i < dest_size1; i++)
197 for (j = 0 ; j < dest_size2; j++)
199 for (k = 0; k < MULTIPLICITY; k++)
201 size_t e1 = (i * dest->tda + j) * MULTIPLICITY + k ;
202 size_t e2 = (j * src->tda + i) * MULTIPLICITY + k ;
204 dest->data[e1] = src->data[e2] ;