gsl_linalg__multiply.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* linalg/multiply.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, 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. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include <stdlib.h>
  22. #include "gsl_math.h"
  23. #include "gsl_linalg.h"
  24. #define SWAP_SIZE_T(a, b) do { size_t swap_tmp = a; a = b; b = swap_tmp; } while(0)
  25. int
  26. gsl_linalg_matmult (const gsl_matrix * A, const gsl_matrix * B, gsl_matrix * C)
  27. {
  28. if (A->size2 != B->size1 || A->size1 != C->size1 || B->size2 != C->size2)
  29. {
  30. GSL_ERROR ("matrix sizes are not conformant", GSL_EBADLEN);
  31. }
  32. else
  33. {
  34. double a, b;
  35. double temp;
  36. size_t i, j, k;
  37. for (i = 0; i < C->size1; i++)
  38. {
  39. for (j = 0; j < C->size2; j++)
  40. {
  41. a = gsl_matrix_get (A, i, 0);
  42. b = gsl_matrix_get (B, 0, j);
  43. temp = a * b;
  44. for (k = 1; k < A->size2; k++)
  45. {
  46. a = gsl_matrix_get (A, i, k);
  47. b = gsl_matrix_get (B, k, j);
  48. temp += a * b;
  49. }
  50. gsl_matrix_set (C, i, j, temp);
  51. }
  52. }
  53. return GSL_SUCCESS;
  54. }
  55. }
  56. int
  57. gsl_linalg_matmult_mod (const gsl_matrix * A, gsl_linalg_matrix_mod_t modA,
  58. const gsl_matrix * B, gsl_linalg_matrix_mod_t modB,
  59. gsl_matrix * C)
  60. {
  61. if (modA == GSL_LINALG_MOD_NONE && modB == GSL_LINALG_MOD_NONE)
  62. {
  63. return gsl_linalg_matmult (A, B, C);
  64. }
  65. else
  66. {
  67. size_t dim1_A = A->size1;
  68. size_t dim2_A = A->size2;
  69. size_t dim1_B = B->size1;
  70. size_t dim2_B = B->size2;
  71. size_t dim1_C = C->size1;
  72. size_t dim2_C = C->size2;
  73. if (modA & GSL_LINALG_MOD_TRANSPOSE)
  74. SWAP_SIZE_T (dim1_A, dim2_A);
  75. if (modB & GSL_LINALG_MOD_TRANSPOSE)
  76. SWAP_SIZE_T (dim1_B, dim2_B);
  77. if (dim2_A != dim1_B || dim1_A != dim1_C || dim2_B != dim2_C)
  78. {
  79. GSL_ERROR ("matrix sizes are not conformant", GSL_EBADLEN);
  80. }
  81. else
  82. {
  83. double a, b;
  84. double temp;
  85. size_t i, j, k;
  86. size_t a1, a2, b1, b2;
  87. for (i = 0; i < dim1_C; i++)
  88. {
  89. for (j = 0; j < dim2_C; j++)
  90. {
  91. a1 = i;
  92. a2 = 0;
  93. b1 = 0;
  94. b2 = j;
  95. if (modA & GSL_LINALG_MOD_TRANSPOSE)
  96. SWAP_SIZE_T (a1, a2);
  97. if (modB & GSL_LINALG_MOD_TRANSPOSE)
  98. SWAP_SIZE_T (b1, b2);
  99. a = gsl_matrix_get (A, a1, a2);
  100. b = gsl_matrix_get (B, b1, b2);
  101. temp = a * b;
  102. for (k = 1; k < dim2_A; k++)
  103. {
  104. a1 = i;
  105. a2 = k;
  106. b1 = k;
  107. b2 = j;
  108. if (modA & GSL_LINALG_MOD_TRANSPOSE)
  109. SWAP_SIZE_T (a1, a2);
  110. if (modB & GSL_LINALG_MOD_TRANSPOSE)
  111. SWAP_SIZE_T (b1, b2);
  112. a = gsl_matrix_get (A, a1, a2);
  113. b = gsl_matrix_get (B, b1, b2);
  114. temp += a * b;
  115. }
  116. gsl_matrix_set (C, i, j, temp);
  117. }
  118. }
  119. return GSL_SUCCESS;
  120. }
  121. }
  122. }