2 // Copyright (c) 2015 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "matrix-ops.h"
21 void transpose (int rows, int cols, double m[rows][cols], double m_trans[cols][rows])
25 for (i = 0; i < rows; i++)
26 for (j = 0; j < cols; j++)
27 m_trans[j][i] = m[i][j];
31 void multiply (int m, int n, int p, double m1[m][n], double m2[n][p], double result[m][p])
35 for (i = 0; i < m; i++)
36 for (k = 0; k < p; k++) {
38 for (j = 0; j < n; j++)
39 result [i][k] += m1[i][j] * m2 [j][k];
44 void invert (int s, double m[s][s], double m_inv[s][s])
50 for (i = 0; i < s; i++)
51 for (j = 0; j < s; j++)
54 for (i = 0; i < s; i++)
59 for (i = 0; i < s; i++) {
61 for (j = i+1; j < s; j++) {
62 if (fabs(tmp[i][j]) > fabs(tmp[i][i]))
68 for (k = 0; k < s; k++) {
70 tmp[k][i] = tmp[k][swap];
74 m_inv[k][i] = m_inv[k][swap];
81 for (k = 0 ; k < s ; k++) {
86 for (j = 0 ; j < s ; j++)
89 for (k = 0 ; k < s; k++) {
90 tmp[k][j] -= tmp[k][i] * t;
91 m_inv[k][j] -= m_inv[k][i] * t;
98 void multiply_scalar_inplace(int rows, int cols, double m[rows][cols], double scalar)
102 for (i = 0; i < rows; i++)
103 for (j = 0; j < cols; j++)
104 m[i][j] = m[i][j] * scalar;
108 void assign (int rows, int cols, double m[rows][cols], double m1[rows][cols])
112 for (i = 0; i < rows; i++)
113 for (j = 0; j < cols; j++)
117 void substract (int rows, int cols, double m1[rows][cols], double m2[rows][cols], double res[rows][cols])
121 for (i = 0; i < rows; i++)
122 for (j = 0; j < cols; j++)
123 res[i][j] = m1[i][j] - m2[i][j];