From: Ramasamy Date: Mon, 16 Jun 2014 09:54:07 +0000 (+0530) Subject: Adding matrix class implementation X-Git-Tag: submit/tizen/20150113.012540~164 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F33%2F23033%2F5;p=platform%2Fcore%2Fsystem%2Fsensord.git Adding matrix class implementation - Adding matrix template class implementation - Adding matrix class testing code Code Contributed by : Gurleen Kaur signed-off-by: Ramasamy Change-Id: Iadee665910db4e7d53ca55eabf2744cc81ec747e --- diff --git a/src/sensor_fusion/standalone/util/matrix.cpp b/src/sensor_fusion/standalone/util/matrix.cpp index 3544252..b6841ac 100755 --- a/src/sensor_fusion/standalone/util/matrix.cpp +++ b/src/sensor_fusion/standalone/util/matrix.cpp @@ -17,101 +17,237 @@ * */ -#include +#ifdef _MATRIX_H template matrix::matrix(void) { - + m_mat = NULL; } template matrix::matrix(const int rows, const int cols) { + m_rows = rows; + m_cols = cols; + m_mat = NULL; + m_mat = new TYPE *[m_rows]; + for (int i = 0; i < m_rows; i++) + m_mat[i] = new TYPE [m_cols]; } template -matrix::matrix(const int rows, const int cols, TYPE **mat_data) +matrix::matrix(const matrix& m) { + m_rows = m.m_rows; + m_cols = m.m_cols; + m_mat = NULL; + m_mat = new TYPE *[m_rows]; + + for (int i = 0; i < m_rows; i++) + m_mat[i] = new TYPE [m_cols]; + for (int p = 0; p < m_rows; p++) + for (int q = 0; q < m_cols; q++) + m_mat[p][q] = m.m_mat[p][q]; } template -matrix::matrix(const matrix& m) +matrix::matrix(const int rows, const int cols, TYPE *mat_data) { + m_rows = rows; + m_cols = cols; + m_mat = NULL; + m_mat = new TYPE *[m_rows]; + + for (int i = 0; i < m_rows; i++) + m_mat[i] = new TYPE [m_cols]; + for (int i = 0; i < m_rows; i++) + for (int j = 0; j < m_cols; j++) + m_mat[i][j] = *mat_data++; } template matrix::~matrix() { - + for (int i = 0; i < m_rows; i++) + delete[] m_mat[i]; + delete[] m_mat; } template matrix matrix::operator =(const matrix& m) { + if (this == &m) + { + return *this; + } + + for (int i = 0; i < m_rows; i++) + delete[] m_mat[i]; + delete[] m_mat; + + m_rows = m.m_rows; + m_cols = m.m_cols; + m_mat = new TYPE *[m_rows]; + + for (int i = 0; i < m_rows; i++) + m_mat[i] = new TYPE [m_cols]; + for (int p = 0; p < m_rows; p++) + for (int q = 0; q < m_cols; q++) + m_mat[p][q] = m.m_mat[p][q]; + + return *this; } -template -matrix operator +(const matrix m1, const matrix m2) +template +ostream& operator <<(ostream& dout, matrix& m) { - + for (int i = 0; i < m.m_rows; i++) + { + for (int j = 0; j < m.m_cols; j++) + { + dout << m.m_mat[i][j] << "\t"; + } + dout << endl; + } + return dout; } -template -matrix operator +(const matrix m, const TYPE val) +template +matrix operator +(const matrix m1, const matrix m2) { + assert(m1.m_rows == m2.m_rows); + assert(m1.m_cols == m2.m_cols); + + matrix m3(m1.m_rows, m1.m_cols); + for (int i = 0; i < m1.m_rows; i++) + for (int j = 0; j < m1.m_cols; j++) + m3.m_mat[i][j] = m1.m_mat[i][j] + m2.m_mat[i][j]; + + return m3; } -template -matrix operator -(const matrix m1, const matrix m2) +template +matrix operator +(const matrix m, const T val) { + matrix m1(m.m_rows, m.m_cols); + for (int i = 0; i < m.m_rows; i++) + for (int j = 0; j < m.m_cols; j++) + m1.m_mat[i][j] = m.m_mat[i][j] + val; + + return m1; } -template -matrix operator -(const matrix m, const TYPE val) +template +matrix operator -(const matrix m1, const matrix m2) { + assert(m1.m_rows == m2.m_rows); + assert(m1.m_cols == m2.m_cols); + matrix m3(m1.m_rows, m1.m_cols); + + for (int i = 0; i < m1.m_rows; i++) + for (int j = 0; j < m1.m_cols; j++) + m3.m_mat[i][j] = m1.m_mat[i][j] - m2.m_mat[i][j]; + + return m3; } -template -matrix operator *(const matrix m1, const matrix m2) +template +matrix operator -(const matrix m, const T val) { + matrix m1(m.m_rows, m.m_cols); + for (int i = 0; i < m.m_rows; i++) + for (int j = 0; j < m.m_cols; j++) + m1.m_mat[i][j] = m.m_mat[i][j] - val; + + return m1; } -template -matrix operator *(const matrix m, const TYPE val) +template +matrix operator *(const matrix m1, const matrix m2) { + assert(m1.m_rows == m2.m_cols); + assert(m1.m_cols == m2.m_rows); + matrix m3(m1.m_rows, m2.m_cols); + + for (int i = 0; i < m1.m_rows; i++) + { + for (int j = 0; j < m2.m_cols; j++) + { + m3.m_mat[i][j] = 0; + for (int k = 0; k < m2.m_rows; k++) + m3.m_mat[i][j] += m1.m_mat[i][k] * m2.m_mat[k][j]; + } + } + + return m3; } -template -matrix operator /(const matrix m1, const matrix m2) +template +matrix operator *(const matrix m, const T val) { + matrix m1(m.m_rows, m.m_cols); + + for (int i = 0; i < m.m_rows; i++) + for (int j = 0; j < m.m_cols; j++) + m1.m_mat[i][j] = m.m_mat[i][j] * val; + return m1; } -template -bool operator ==(const matrix m1, const matrix m2) +template +matrix operator /(const matrix m1, const T val) { + matrix m3(m1.m_rows, m1.m_cols); + + for (int i = 0; i < m1.m_rows; i++) + for (int j = 0; j < m1.m_cols; j++) + m3.m_mat[i][j] = m1.m_mat[i][j] / val; + return m3; } -template -bool operator !=(const matrix m1, const matrix m2) +template +bool operator ==(const matrix m1, const matrix m2) { + if ((m1.m_rows == m2.m_rows) && (m1.m_cols == m2.m_cols)) + { + for (int i = 0; i < m1.m_rows; i++) + for (int j = 0; j < m2.m_cols; j++) + if (m1.m_mat[i][j] != m2.m_mat[i][j]) + return false; + } + else + return false; + return true; } +template +bool operator !=(const matrix m1, const matrix m2) +{ + return (!(m1 == m2)); +} -template -matrix matrix::transpose(const matrix m) +template +matrix transpose(const matrix m) { + matrix m1(m.m_cols, m.m_rows); + for (int i = 0; i < m.m_rows; i++) + for (int j = 0; j < m.m_cols; j++) + m1.m_mat[j][i] = m.m_mat[i][j]; + + return m1; } + +#endif //_MATRIX_H diff --git a/src/sensor_fusion/standalone/util/matrix.h b/src/sensor_fusion/standalone/util/matrix.h index 7b95d98..0f01f5e 100755 --- a/src/sensor_fusion/standalone/util/matrix.h +++ b/src/sensor_fusion/standalone/util/matrix.h @@ -20,6 +20,10 @@ #ifndef _MATRIX_H #define _MATRIX_H +#include +#include +using namespace std; + template class matrix { public: @@ -29,23 +33,35 @@ public: matrix(void); matrix(const int rows, const int cols); - matrix(const int rows, const int cols, TYPE **mat_data); + matrix(const int rows, const int cols, TYPE *mat_data); matrix(const matrix& m); ~matrix(); matrix operator =(const matrix& m); - friend matrix operator +(const matrix m1, const matrix m2); - friend matrix operator +(const matrix m, const TYPE val); - friend matrix operator -(const matrix m1, const matrix m2); - friend matrix operator -(const matrix m, const TYPE val); - friend matrix operator *(const matrix m1, const matrix m2); - friend matrix operator *(const matrix m, const TYPE val); - friend matrix operator /(const matrix m1, const matrix m2); - friend bool operator ==(const matrix m1, const matrix m2); - friend bool operator !=(const matrix m1, const matrix m2); - - friend matrix transpose(const matrix m); + template friend ostream& operator << (ostream& dout, + matrix& m); + template friend matrix operator +(const matrix m1, + const matrix m2); + template friend matrix operator +(const matrix m, + const T val); + template friend matrix operator -(const matrix m1, + const matrix m2); + template friend matrix operator -(const matrix m, + const T val); + template friend matrix operator *(const matrix m1, + const matrix m2); + template friend matrix operator *(const matrix m, + const T val); + template friend matrix operator /(const matrix m1, + const T val); + template friend bool operator ==(const matrix m1, + const matrix m2); + template friend bool operator !=(const matrix m1, + const matrix m2); + template friend matrix transpose(const matrix m); }; +#include "matrix.cpp" + #endif //_MATRIX_H diff --git a/src/sensor_fusion/standalone/util/test/matrix_test/.cproject b/src/sensor_fusion/standalone/util/test/matrix_test/.cproject new file mode 100644 index 0000000..1e6728f --- /dev/null +++ b/src/sensor_fusion/standalone/util/test/matrix_test/.cproject @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/sensor_fusion/standalone/util/test/matrix_test/.project b/src/sensor_fusion/standalone/util/test/matrix_test/.project new file mode 100644 index 0000000..3d8db3d --- /dev/null +++ b/src/sensor_fusion/standalone/util/test/matrix_test/.project @@ -0,0 +1,89 @@ + + + matrix_test + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.buildLocation + ${workspace_loc:/matrix_test/Debug} + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + org.tizen.nativecpp.apichecker.core.builder + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + org.tizen.nativecpp.apichecker.core.tizenCppNature + + diff --git a/src/sensor_fusion/standalone/util/test/matrix_test/matrix_main.cpp b/src/sensor_fusion/standalone/util/test/matrix_test/matrix_main.cpp new file mode 100644 index 0000000..f7cbf80 --- /dev/null +++ b/src/sensor_fusion/standalone/util/test/matrix_test/matrix_main.cpp @@ -0,0 +1,140 @@ +/* + * sensord + * + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "../../matrix.h" + +int main() +{ + float arr0[2][2] = {{-2.243, 2.57},{3.56, -3.02}}; + float arr1[2][2] = {{2.2, 2.5},{3.5, 3.0}}; + float arr5[3][2] = {{0.22, 4.56}, {3.652, 5.86}, {1.430, 0.45}}; + float arr11[1][3] = {{2.0, 3.0, 4.0}}; + float arr12[3][1] = {{2.0}, {1.0}, {3.0}}; + float arr15[2][3] = {{20.0, -40.0, 10.0}, {36.0, 52.0, -55.0}}; + float arr3[3][3] = {{20.2, 40.5, 10.0}, {3.6, 52.0, 5.5}, {1.0, 45.5, 66.6}}; + float arr4[3][3] = {{2.24, 0.5, 0.023}, {3.675, 5.32, 0.556}, {1.023, 45.75, 621.6}}; + float arr8[3][3] = {{4.75, 0.65, 0.123}, {0.075, 5.302, 0.56}, {1.113, 0.475, 2.362}}; + + matrix m1(2, 2, (float *) arr0); + matrix m2(2, 2, (float *) arr1); + matrix m3(2, 2); + matrix m10(3, 3, (float *) arr3); + matrix m11(3, 2, (float *) arr5); + matrix m6(3, 3); + matrix m13(3, 2); + matrix m12(3, 3, (float *) arr4); + matrix m15(3, 3, (float *) arr8); + matrix m20(1, 3, (float *) arr11); + matrix m21(3, 1, (float *) arr12); + matrix m22(2, 3, (float *) arr15); + + cout<<"Addition\n"; + m6 = m10 + m15; + m13 = m11 + m11; + cout<< "\n" << m10 <<"\n"<< m15; + cout<< "\nSum:\n" << m6 << endl; + cout<< "\n" << m11 << "\n"<< m11; + cout<< "\nSum:\n" << m13 << endl; + + cout<< "\n\n\nSubtraction\n"; + m6 = m10 - m12; + cout<< "\n" << m10 << "\n" << m12; + cout<< "\nDifference:\n" << m6 << endl; + + cout<< "\n\n\nMultiplication\n"; + m6 = m10 * m12; + m3 = m1 * m2; + matrix m7(m20.m_rows, m21.m_cols); + m7 = m20 * m21; + cout<< "\n" << m10 << "\n" << m12; + cout<< "\nProduct:\n" << m6 << endl; + cout<< "\n" << m1 << "\n" << m2; + cout<< "\nProduct:\n" << m3 << endl; + cout<< "\n" << m20 << "\n" << m21; + cout<< "\nProduct:\n" << m7 << endl; + + cout<< "\n\n\nDivision\n"; + m3 = m1 / (float)2.5; + cout<< "\n" << m1 << "\n" << "2.5"; + cout<< "\nResult:\n" << m3 << endl; + m6 = m12 / (float)0.125; + cout<< "\n" << m12 << "\n" << ".125"; + cout<< "\nResult:\n" << m6 << endl; + + float num = 5.5650; + float num1 = -2.32; + cout<< "\n\n\nScalar addition\n"; + m3 = m2 + num; + m6 = m10 + num1; + cout<< "\nNumber added:" << num; + cout<< "\n\n" << m2; + cout<< "\nResult:\n\n" << m3; + cout<< "\nNumber added:" << num1; + cout<< "\n\n" << m10; + cout<< "\nResult:\n\n" << m6; + + float x = 4.0; + float x1 = -2.5; + cout<< "\n\n\nScalar subtraction\n"; + m3 = m11 - x; + m6 = m10 - x1; + cout<< "\nNumber Subtracted:" << x; + cout<< "\n\n" << m11; + cout<< "\nResult:\n\n" << m3; + cout<< "\nNumber Subtracted:" << x1; + cout<< "\n\n" << m10; + cout<< "\nResult:\n\n" << m6; + + float z = 3.50; + float z1 = -5.567; + cout<< "\n\n\nScalar multiplication\n"; + m3 = m1 * z; + m6 = m12 * z1; + cout<< "\nNumber Multiplied:"<< z; + cout<< "\n\n" << m1; + cout<< "\nResult:\n\n" << m3; + cout<< "\nNumber Multiplied:" << z1; + cout<< "\n\n" << m12; + cout<< "\nResult:\n\n" << m6; + + m6 = transpose(m15); + cout<< "\n\n\nTranspose:"; + cout << "\n\n" << m15; + cout << "\nResult:\n\n" << m6; + + cout << "\n\nm1:\n\n" << m1; + cout << "\n\nm2:\n\n" << m2; + cout << "\n\n\nm1 == m2 :"; + cout << (m1 == m2); + + cout << "\n\nm2:\n\n" << m2; + cout << "\n\nm2:\n\n" << m2; + cout << "\n\n\nm2 == m2 :"; + cout << (m2 == m2); + + cout << "\n\nm6:\n\n" << m6; + cout << "\n\nm6:\n\n" << m6; + cout << "\n\n\nm6 != m6 :"; + cout << (m6 != m6); + + cout << "\n\nm6:\n\n" << m6; + cout << "\n\nm1:\n\n" << m1; + cout << "\n\n\nm6 != m1 :"; + cout << (m6 != m1); +}