1 /* matrix/oper_complex_source.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 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, add) (TYPE (gsl_matrix) * a,
22 const TYPE (gsl_matrix) * b)
24 const size_t M = a->size1;
25 const size_t N = a->size2;
27 if (b->size1 != M || b->size2 != N)
29 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
33 const size_t tda_a = a->tda;
34 const size_t tda_b = b->tda;
38 for (i = 0; i < M; i++)
40 for (j = 0; j < N; j++)
42 const size_t aij = 2 * (i * tda_a + j);
43 const size_t bij = 2 * (i * tda_b + j);
45 a->data[aij] += b->data[bij];
46 a->data[aij + 1] += b->data[bij + 1];
55 FUNCTION (gsl_matrix, sub) (TYPE (gsl_matrix) * a,
56 const TYPE (gsl_matrix) * b)
58 const size_t M = a->size1;
59 const size_t N = a->size2;
61 if (b->size1 != M || b->size2 != N)
63 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
67 const size_t tda_a = a->tda;
68 const size_t tda_b = b->tda;
72 for (i = 0; i < M; i++)
74 for (j = 0; j < N; j++)
76 const size_t aij = 2 * (i * tda_a + j);
77 const size_t bij = 2 * (i * tda_b + j);
79 a->data[aij] -= b->data[bij];
80 a->data[aij + 1] -= b->data[bij + 1];
89 FUNCTION (gsl_matrix, mul_elements) (TYPE (gsl_matrix) * a,
90 const TYPE (gsl_matrix) * b)
92 const size_t M = a->size1;
93 const size_t N = a->size2;
95 if (b->size1 != M || b->size2 != N)
97 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
101 const size_t tda_a = a->tda;
102 const size_t tda_b = b->tda;
106 for (i = 0; i < M; i++)
108 for (j = 0; j < N; j++)
110 const size_t aij = 2 * (i * tda_a + j);
111 const size_t bij = 2 * (i * tda_b + j);
113 ATOMIC ar = a->data[aij];
114 ATOMIC ai = a->data[aij + 1];
116 ATOMIC br = b->data[bij];
117 ATOMIC bi = b->data[bij + 1];
119 a->data[aij] = ar * br - ai * bi;
120 a->data[aij + 1] = ar * bi + ai * br;
129 FUNCTION (gsl_matrix, div_elements) (TYPE (gsl_matrix) * a,
130 const TYPE (gsl_matrix) * b)
132 const size_t M = a->size1;
133 const size_t N = a->size2;
135 if (b->size1 != M || b->size2 != N)
137 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
141 const size_t tda_a = a->tda;
142 const size_t tda_b = b->tda;
146 for (i = 0; i < M; i++)
148 for (j = 0; j < N; j++)
150 const size_t aij = 2 * (i * tda_a + j);
151 const size_t bij = 2 * (i * tda_b + j);
153 ATOMIC ar = a->data[aij];
154 ATOMIC ai = a->data[aij + 1];
156 ATOMIC br = b->data[bij];
157 ATOMIC bi = b->data[bij + 1];
159 ATOMIC s = 1.0 / hypot(br, bi);
164 a->data[aij] = (ar * sbr + ai * sbi) * s;
165 a->data[aij + 1] = (ai * sbr - ar * sbi) * s;
173 int FUNCTION (gsl_matrix, scale) (TYPE (gsl_matrix) * a, const BASE x)
175 const size_t M = a->size1;
176 const size_t N = a->size2;
177 const size_t tda = a->tda;
181 ATOMIC xr = GSL_REAL(x);
182 ATOMIC xi = GSL_IMAG(x);
184 for (i = 0; i < M; i++)
186 for (j = 0; j < N; j++)
188 const size_t aij = 2 * (i * tda + j);
190 ATOMIC ar = a->data[aij];
191 ATOMIC ai = a->data[aij + 1];
193 a->data[aij] = ar * xr - ai * xi;
194 a->data[aij + 1] = ar * xi + ai * xr;
201 int FUNCTION (gsl_matrix, add_constant) (TYPE (gsl_matrix) * a, const BASE x)
203 const size_t M = a->size1;
204 const size_t N = a->size2;
205 const size_t tda = a->tda;
209 for (i = 0; i < M; i++)
211 for (j = 0; j < N; j++)
213 a->data[2 * (i * tda + j)] += GSL_REAL (x);
214 a->data[2 * (i * tda + j) + 1] += GSL_IMAG (x);
222 int FUNCTION (gsl_matrix, add_diagonal) (TYPE (gsl_matrix) * a, const BASE x)
224 const size_t M = a->size1;
225 const size_t N = a->size2;
226 const size_t tda = a->tda;
227 const size_t loop_lim = (M < N ? M : N);
229 for (i = 0; i < loop_lim; i++)
231 a->data[2 * (i * tda + i)] += GSL_REAL (x);
232 a->data[2 * (i * tda + i) + 1] += GSL_IMAG (x);