1 /* matrix/oper_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, const TYPE(gsl_matrix) * b)
23 const size_t M = a->size1;
24 const size_t N = a->size2;
26 if (b->size1 != M || b->size2 != N)
28 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
32 const size_t tda_a = a->tda;
33 const size_t tda_b = b->tda;
37 for (i = 0; i < M; i++)
39 for (j = 0; j < N; j++)
41 a->data[i * tda_a + j] += b->data[i * tda_b + j];
50 FUNCTION(gsl_matrix, sub) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b)
52 const size_t M = a->size1;
53 const size_t N = a->size2;
55 if (b->size1 != M || b->size2 != N)
57 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
61 const size_t tda_a = a->tda;
62 const size_t tda_b = b->tda;
66 for (i = 0; i < M; i++)
68 for (j = 0; j < N; j++)
70 a->data[i * tda_a + j] -= b->data[i * tda_b + j];
79 FUNCTION(gsl_matrix, mul_elements) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b)
81 const size_t M = a->size1;
82 const size_t N = a->size2;
84 if (b->size1 != M || b->size2 != N)
86 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
90 const size_t tda_a = a->tda;
91 const size_t tda_b = b->tda;
95 for (i = 0; i < M; i++)
97 for (j = 0; j < N; j++)
99 a->data[i * tda_a + j] *= b->data[i * tda_b + j];
108 FUNCTION(gsl_matrix, div_elements) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b)
110 const size_t M = a->size1;
111 const size_t N = a->size2;
113 if (b->size1 != M || b->size2 != N)
115 GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN);
119 const size_t tda_a = a->tda;
120 const size_t tda_b = b->tda;
124 for (i = 0; i < M; i++)
126 for (j = 0; j < N; j++)
128 a->data[i * tda_a + j] /= b->data[i * tda_b + j];
137 FUNCTION(gsl_matrix, scale) (TYPE(gsl_matrix) * a, const double x)
139 const size_t M = a->size1;
140 const size_t N = a->size2;
141 const size_t tda = a->tda;
145 for (i = 0; i < M; i++)
147 for (j = 0; j < N; j++)
149 a->data[i * tda + j] *= x;
157 FUNCTION(gsl_matrix, add_constant) (TYPE(gsl_matrix) * a, const double x)
159 const size_t M = a->size1;
160 const size_t N = a->size2;
161 const size_t tda = a->tda;
165 for (i = 0; i < M; i++)
167 for (j = 0; j < N; j++)
169 a->data[i * tda + j] += x;
178 FUNCTION(gsl_matrix, add_diagonal) (TYPE(gsl_matrix) * a, const double x)
180 const size_t M = a->size1;
181 const size_t N = a->size2;
182 const size_t tda = a->tda;
183 const size_t loop_lim = ( M < N ? M : N );
185 for (i = 0; i < loop_lim; i++)
187 a->data[i * tda + i] += x;