summaryrefslogtreecommitdiff
path: root/gsl-1.9/linalg/multiply.c
blob: 0522cdd7a707a7525164ad1e02dcd1af8059f424 (plain)
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
/* linalg/multiply.c
 * 
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman, Brian Gough
 * 
 * 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 2 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.
 */

/* Author:  G. Jungman */

#include <config.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_linalg.h>

#define SWAP_SIZE_T(a, b)  do { size_t swap_tmp = a; a = b; b = swap_tmp; } while(0)

int
gsl_linalg_matmult (const gsl_matrix * A, const gsl_matrix * B, gsl_matrix * C)
{
  if (A->size2 != B->size1 || A->size1 != C->size1 || B->size2 != C->size2)
    {
      GSL_ERROR ("matrix sizes are not conformant", GSL_EBADLEN);
    }
  else
    {
      double a, b;
      double temp;
      size_t i, j, k;

      for (i = 0; i < C->size1; i++)
        {
          for (j = 0; j < C->size2; j++)
            {
              a = gsl_matrix_get (A, i, 0);
              b = gsl_matrix_get (B, 0, j);
              temp = a * b;
              for (k = 1; k < A->size2; k++)
                {
                  a = gsl_matrix_get (A, i, k);
                  b = gsl_matrix_get (B, k, j);
                  temp += a * b;
                }
              gsl_matrix_set (C, i, j, temp);
            }
        }

      return GSL_SUCCESS;
    }
}


int
gsl_linalg_matmult_mod (const gsl_matrix * A, gsl_linalg_matrix_mod_t modA,
                    const gsl_matrix * B, gsl_linalg_matrix_mod_t modB,
                    gsl_matrix * C)
{
  if (modA == GSL_LINALG_MOD_NONE && modB == GSL_LINALG_MOD_NONE)
    {
      return gsl_linalg_matmult (A, B, C);
    }
  else
    {
      size_t dim1_A = A->size1;
      size_t dim2_A = A->size2;
      size_t dim1_B = B->size1;
      size_t dim2_B = B->size2;
      size_t dim1_C = C->size1;
      size_t dim2_C = C->size2;

      if (modA & GSL_LINALG_MOD_TRANSPOSE)
        SWAP_SIZE_T (dim1_A, dim2_A);
      if (modB & GSL_LINALG_MOD_TRANSPOSE)
        SWAP_SIZE_T (dim1_B, dim2_B);

      if (dim2_A != dim1_B || dim1_A != dim1_C || dim2_B != dim2_C)
        {
          GSL_ERROR ("matrix sizes are not conformant", GSL_EBADLEN);
        }
      else
        {
          double a, b;
          double temp;
          size_t i, j, k;
          size_t a1, a2, b1, b2;

          for (i = 0; i < dim1_C; i++)
            {
              for (j = 0; j < dim2_C; j++)
                {
                  a1 = i;
                  a2 = 0;
                  b1 = 0;
                  b2 = j;
                  if (modA & GSL_LINALG_MOD_TRANSPOSE)
                    SWAP_SIZE_T (a1, a2);
                  if (modB & GSL_LINALG_MOD_TRANSPOSE)
                    SWAP_SIZE_T (b1, b2);

                  a = gsl_matrix_get (A, a1, a2);
                  b = gsl_matrix_get (B, b1, b2);
                  temp = a * b;

                  for (k = 1; k < dim2_A; k++)
                    {
                      a1 = i;
                      a2 = k;
                      b1 = k;
                      b2 = j;
                      if (modA & GSL_LINALG_MOD_TRANSPOSE)
                        SWAP_SIZE_T (a1, a2);
                      if (modB & GSL_LINALG_MOD_TRANSPOSE)
                        SWAP_SIZE_T (b1, b2);
                      a = gsl_matrix_get (A, a1, a2);
                      b = gsl_matrix_get (B, b1, b2);
                      temp += a * b;
                    }

                  gsl_matrix_set (C, i, j, temp);
                }
            }

          return GSL_SUCCESS;
        }
    }
}