| 1 | /* matrix/oper_source.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | int |
| 21 | FUNCTION(gsl_matrix, add) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b) |
| 22 | { |
| 23 | const size_t M = a->size1; |
| 24 | const size_t N = a->size2; |
| 25 | |
| 26 | if (b->size1 != M || b->size2 != N) |
| 27 | { |
| 28 | GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN); |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | const size_t tda_a = a->tda; |
| 33 | const size_t tda_b = b->tda; |
| 34 | |
| 35 | size_t i, j; |
| 36 | |
| 37 | for (i = 0; i < M; i++) |
| 38 | { |
| 39 | for (j = 0; j < N; j++) |
| 40 | { |
| 41 | a->data[i * tda_a + j] += b->data[i * tda_b + j]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return GSL_SUCCESS; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int |
| 50 | FUNCTION(gsl_matrix, sub) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b) |
| 51 | { |
| 52 | const size_t M = a->size1; |
| 53 | const size_t N = a->size2; |
| 54 | |
| 55 | if (b->size1 != M || b->size2 != N) |
| 56 | { |
| 57 | GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | const size_t tda_a = a->tda; |
| 62 | const size_t tda_b = b->tda; |
| 63 | |
| 64 | size_t i, j; |
| 65 | |
| 66 | for (i = 0; i < M; i++) |
| 67 | { |
| 68 | for (j = 0; j < N; j++) |
| 69 | { |
| 70 | a->data[i * tda_a + j] -= b->data[i * tda_b + j]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return GSL_SUCCESS; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int |
| 79 | FUNCTION(gsl_matrix, mul_elements) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b) |
| 80 | { |
| 81 | const size_t M = a->size1; |
| 82 | const size_t N = a->size2; |
| 83 | |
| 84 | if (b->size1 != M || b->size2 != N) |
| 85 | { |
| 86 | GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | const size_t tda_a = a->tda; |
| 91 | const size_t tda_b = b->tda; |
| 92 | |
| 93 | size_t i, j; |
| 94 | |
| 95 | for (i = 0; i < M; i++) |
| 96 | { |
| 97 | for (j = 0; j < N; j++) |
| 98 | { |
| 99 | a->data[i * tda_a + j] *= b->data[i * tda_b + j]; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return GSL_SUCCESS; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int |
| 108 | FUNCTION(gsl_matrix, div_elements) (TYPE(gsl_matrix) * a, const TYPE(gsl_matrix) * b) |
| 109 | { |
| 110 | const size_t M = a->size1; |
| 111 | const size_t N = a->size2; |
| 112 | |
| 113 | if (b->size1 != M || b->size2 != N) |
| 114 | { |
| 115 | GSL_ERROR ("matrices must have same dimensions", GSL_EBADLEN); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | const size_t tda_a = a->tda; |
| 120 | const size_t tda_b = b->tda; |
| 121 | |
| 122 | size_t i, j; |
| 123 | |
| 124 | for (i = 0; i < M; i++) |
| 125 | { |
| 126 | for (j = 0; j < N; j++) |
| 127 | { |
| 128 | a->data[i * tda_a + j] /= b->data[i * tda_b + j]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return GSL_SUCCESS; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | FUNCTION(gsl_matrix, scale) (TYPE(gsl_matrix) * a, const double x) |
| 138 | { |
| 139 | const size_t M = a->size1; |
| 140 | const size_t N = a->size2; |
| 141 | const size_t tda = a->tda; |
| 142 | |
| 143 | size_t i, j; |
| 144 | |
| 145 | for (i = 0; i < M; i++) |
| 146 | { |
| 147 | for (j = 0; j < N; j++) |
| 148 | { |
| 149 | a->data[i * tda + j] *= x; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return GSL_SUCCESS; |
| 154 | } |
| 155 | |
| 156 | int |
| 157 | FUNCTION(gsl_matrix, add_constant) (TYPE(gsl_matrix) * a, const double x) |
| 158 | { |
| 159 | const size_t M = a->size1; |
| 160 | const size_t N = a->size2; |
| 161 | const size_t tda = a->tda; |
| 162 | |
| 163 | size_t i, j; |
| 164 | |
| 165 | for (i = 0; i < M; i++) |
| 166 | { |
| 167 | for (j = 0; j < N; j++) |
| 168 | { |
| 169 | a->data[i * tda + j] += x; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return GSL_SUCCESS; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | int |
| 178 | FUNCTION(gsl_matrix, add_diagonal) (TYPE(gsl_matrix) * a, const double x) |
| 179 | { |
| 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 ); |
| 184 | size_t i; |
| 185 | for (i = 0; i < loop_lim; i++) |
| 186 | { |
| 187 | a->data[i * tda + i] += x; |
| 188 | } |
| 189 | |
| 190 | return GSL_SUCCESS; |
| 191 | } |