4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 template <typename TYPE>
23 matrix<TYPE>::matrix(void)
28 template <typename TYPE>
29 matrix<TYPE>::matrix(const int rows, const int cols)
34 m_mat = new TYPE *[m_rows];
36 for (int i = 0; i < m_rows; i++)
37 m_mat[i] = new TYPE [m_cols]();
40 template <typename TYPE>
41 matrix<TYPE>::matrix(const matrix<TYPE>& m)
46 m_mat = new TYPE *[m_rows];
48 for (int i = 0; i < m_rows; i++)
49 m_mat[i] = new TYPE [m_cols];
51 for (int p = 0; p < m_rows; p++)
52 for (int q = 0; q < m_cols; q++)
53 m_mat[p][q] = m.m_mat[p][q];
56 template <typename TYPE>
57 matrix<TYPE>::matrix(const int rows, const int cols, TYPE *mat_data)
62 m_mat = new TYPE *[m_rows];
64 for (int i = 0; i < m_rows; i++)
65 m_mat[i] = new TYPE [m_cols];
67 for (int i = 0; i < m_rows; i++)
68 for (int j = 0; j < m_cols; j++)
69 m_mat[i][j] = *mat_data++;
72 template <typename TYPE>
73 matrix<TYPE>::~matrix()
77 for (int i = 0; i < m_rows; i++)
83 template <typename TYPE>
84 matrix<TYPE> matrix<TYPE>::operator =(const matrix<TYPE>& m)
95 m_mat = new TYPE *[m_rows];
97 for (int i = 0; i < m_rows; i++)
98 m_mat[i] = new TYPE [m_cols];
102 if ((m_rows != m.m_rows) || (m_cols != m.m_cols))
104 for (int i = 0; i < m_rows; i++)
110 m_mat = new TYPE *[m_rows];
112 for (int i = 0; i < m_rows; i++)
113 m_mat[i] = new TYPE [m_cols];
117 for (int p = 0; p < m_rows; p++)
118 for (int q = 0; q < m_cols; q++)
119 m_mat[p][q] = m.m_mat[p][q];
124 template <typename T>
125 ostream& operator <<(ostream& dout, matrix<T>& m)
127 for (int i = 0; i < m.m_rows; i++)
129 for (int j = 0; j < m.m_cols; j++)
131 dout << m.m_mat[i][j] << "\t";
138 template <typename T>
139 matrix<T> operator +(const matrix<T> m1, const matrix<T> m2)
141 assert(m1.m_rows == m2.m_rows);
142 assert(m1.m_cols == m2.m_cols);
144 matrix<T> m3(m1.m_rows, m1.m_cols);
146 for (int i = 0; i < m1.m_rows; i++)
147 for (int j = 0; j < m1.m_cols; j++)
148 m3.m_mat[i][j] = m1.m_mat[i][j] + m2.m_mat[i][j];
153 template <typename T>
154 matrix<T> operator +(const matrix<T> m, const T val)
156 matrix<T> m1(m.m_rows, m.m_cols);
158 for (int i = 0; i < m.m_rows; i++)
159 for (int j = 0; j < m.m_cols; j++)
160 m1.m_mat[i][j] = m.m_mat[i][j] + val;
165 template <typename T>
166 matrix<T> operator -(const matrix<T> m1, const matrix<T> m2)
168 assert(m1.m_rows == m2.m_rows);
169 assert(m1.m_cols == m2.m_cols);
171 matrix<T> m3(m1.m_rows, m1.m_cols);
173 for (int i = 0; i < m1.m_rows; i++)
174 for (int j = 0; j < m1.m_cols; j++)
175 m3.m_mat[i][j] = m1.m_mat[i][j] - m2.m_mat[i][j];
180 template <typename T>
181 matrix<T> operator -(const matrix<T> m, const T val)
183 matrix<T> m1(m.m_rows, m.m_cols);
185 for (int i = 0; i < m.m_rows; i++)
186 for (int j = 0; j < m.m_cols; j++)
187 m1.m_mat[i][j] = m.m_mat[i][j] - val;
192 template <typename T>
193 matrix<T> operator *(const matrix<T> m1, const matrix<T> m2)
195 assert(m1.m_rows == m2.m_cols);
196 assert(m1.m_cols == m2.m_rows);
198 matrix<T> m3(m1.m_rows, m2.m_cols);
200 for (int i = 0; i < m1.m_rows; i++)
202 for (int j = 0; j < m2.m_cols; j++)
205 for (int k = 0; k < m2.m_rows; k++)
206 m3.m_mat[i][j] += m1.m_mat[i][k] * m2.m_mat[k][j];
213 template <typename T>
214 matrix<T> operator *(const matrix<T> m, const T val)
216 matrix<T> m1(m.m_rows, m.m_cols);
218 for (int i = 0; i < m.m_rows; i++)
219 for (int j = 0; j < m.m_cols; j++)
220 m1.m_mat[i][j] = m.m_mat[i][j] * val;
225 template <typename T>
226 matrix<T> operator /(const matrix<T> m1, const T val)
228 matrix<T> m3(m1.m_rows, m1.m_cols);
230 for (int i = 0; i < m1.m_rows; i++)
231 for (int j = 0; j < m1.m_cols; j++)
232 m3.m_mat[i][j] = m1.m_mat[i][j] / val;
237 template <typename T>
238 bool operator ==(const matrix<T> m1, const matrix<T> m2)
240 if ((m1.m_rows == m2.m_rows) && (m1.m_cols == m2.m_cols))
242 for (int i = 0; i < m1.m_rows; i++)
243 for (int j = 0; j < m2.m_cols; j++)
244 if (m1.m_mat[i][j] != m2.m_mat[i][j])
253 template <typename T>
254 bool operator !=(const matrix<T> m1, const matrix<T> m2)
256 return (!(m1 == m2));
259 template <typename T>
260 matrix<T> tran(const matrix<T> m)
262 matrix<T> m1(m.m_cols, m.m_rows);
264 for (int i = 0; i < m.m_rows; i++)
265 for (int j = 0; j < m.m_cols; j++)
266 m1.m_mat[j][i] = m.m_mat[i][j];
272 template <typename T>
273 matrix<T> mul(const matrix<T> m1, const matrix<T> m2)
275 assert(m2.m_cols == 1);
276 assert(m1.m_cols == m2.m_rows);
278 matrix<T> m3(m1.m_rows, 1);
280 for (int i = 0; i < m1.m_rows; i++)
283 for (int k = 0; k < m2.m_rows; k++)
284 m3.m_mat[i][0] += m1.m_mat[i][k] * m2.m_mat[k][0];