📄 mmat_02.cc
字号:
}#endif // test multv // in_vec.assign((long)MMAT_NUM_ROWS, (double*)MMAT_MULTV_ARG_REAL); vec_res.assign((long)MMAT_MULTV_RES_REAL[mat1][0], (double*)&MMAT_MULTV_RES_REAL[mat1][1]);#ifdef ISIP_TEMPLATE_complex tmp.assign((long)MMAT_MULTV_RES_REAL[mat1][0], (double*)&MMAT_MULTV_RES_REAL[mat1][1 + MMAT_NUM_ROWS]); tmp.mult(TIntegral(0, 1)); vec_res.add(tmp);#endif // test only if the dimensions match - as we test 8 matrices // of different dimensions, we don't want the diagnose method to // return test error for obvious cases // if (arg_mat1.getNumColumns() == in_vec.length()) { arg_mat1.multv(out_vec, in_vec); if (!out_vec.almostEqual(vec_res)) { out_vec.debug(L"out value"); vec_res.debug(L"res value"); Console::put(arg1_str); Error::handle(name(), L"multv", Error::TEST, __FILE__, __LINE__); } } // additional test for input vector with non-zero imaginary part //#ifdef ISIP_TEMPLATE_complex in_vec.assign((long)MMAT_NUM_ROWS, (double*)MMAT_MULTV_ARG_COMPLEX); tmp.assign((long)MMAT_NUM_ROWS, (double*)&MMAT_QUADRATIC_VEC_COMPLEX[MMAT_NUM_ROWS]); tmp.mult(TScalar(0, 1)); in_vec.add(tmp); vec_res.assign((long)MMAT_MULTV_RES_COMPLEX[mat1][0], (double*)&MMAT_MULTV_RES_COMPLEX[mat1][1]); tmp.assign((long)MMAT_MULTV_RES_COMPLEX[mat1][0], (double*)&MMAT_MULTV_RES_COMPLEX[mat1][1 + MMAT_NUM_ROWS]); tmp.mult(TIntegral(0, 1)); vec_res.add(tmp); // test only if the dimensions match - as we test 8 matrices // of different dimensions, we don't want the diagnose method to // return test error for obvious cases // if (arg_mat1.getNumColumns() == in_vec.length()) { arg_mat1.multv(out_vec, in_vec); if (!out_vec.almostEqual(vec_res)) { out_vec.debug(L"out value"); vec_res.debug(L"res value"); Console::put(arg1_str); Error::handle(name(), L"multv complex", Error::TEST, __FILE__, __LINE__); } }#endif // test get/set row methods // if ((mat1 == Integral::FULL) && (mat1 < 5)) { in_vec.assign((long)MMAT_NUM_COLS, (double*)MMAT_MULTV_ARG_REAL); vec_res.assign(in_vec); tmp_mat.assign(arg_mat1); tmp_mat.setRow(2, in_vec); tmp_mat.getRow(out_vec, 2); if (!out_vec.almostEqual(in_vec)) { Console::put(arg1_str); Error::handle(name(), L"getRow", Error::TEST, __FILE__, __LINE__); } } // test get/set column methods // if (((mat1 == Integral::FULL) || (mat1 == Integral::SPARSE)) && (mat1 < 6)) { in_vec.assign((long)MMAT_NUM_COLS, (double*)MMAT_MULTV_ARG_REAL); tmp_mat.assign(arg_mat1); tmp_mat.setColumn(2, in_vec); tmp_mat.getColumn(out_vec, 2); if (!out_vec.almostEqual(in_vec)) { Console::put(arg1_str); Error::handle(name(), L"getColumn", Error::TEST, __FILE__, __LINE__); } } } // test quadratic // TScalar val_res; TScalar dval; TIntegral dval_ret; TVector in_vec; // test only if the matrix is square as quadratic method is for // square matrix only // if (arg_mat1.isSquare()) { in_vec.assign((long)MMAT_NUM_ROWS, (double*)MMAT_QUADRATIC_VEC_REAL);#ifndef ISIP_TEMPLATE_complex val_res.assign((TIntegral)MMAT_QUADRATIC_RES_REAL[mat1]);#else complexdouble tmp_complex8(MMAT_QUADRATIC_RES_REAL[mat1], MMAT_QUADRATIC_RES_REAL [mat1 + MMAT_NUM_MATS_TOTAL]); val_res.assign((TIntegral)tmp_complex8);#endif arg_mat1.quadratic(dval_ret, in_vec); dval = dval_ret; if (!val_res.almostEqual(dval)) { dval.debug(L"out value"); val_res.debug(L"res value"); Error::handle(name(), L"quadratic real", Error::TEST, __FILE__, __LINE__); } // additional test for input vector with non-zero imaginary part //#ifdef ISIP_TEMPLATE_complex in_vec.assign((long)MMAT_NUM_ROWS, (double*)MMAT_QUADRATIC_VEC_COMPLEX); TVector tmp; tmp.assign((long)MMAT_NUM_ROWS, (double*)&MMAT_QUADRATIC_VEC_COMPLEX[MMAT_NUM_ROWS]); tmp.mult(TScalar(0, 1)); in_vec.add(tmp); complexdouble tmp_complex7(MMAT_QUADRATIC_RES_COMPLEX[mat1], MMAT_QUADRATIC_RES_COMPLEX [mat1 + MMAT_NUM_MATS_TOTAL]); val_res.assign((TIntegral)tmp_complex7); arg_mat1.quadratic(dval_ret, in_vec); dval = dval_ret; if (!val_res.almostEqual(dval)) { dval.debug(L"out value"); val_res.debug(L"res value"); Error::handle(name(), L"quadratic complex", Error::TEST, __FILE__, __LINE__); }#endif } } // test the vector outerProduct methods. This doesn't go in the loop because // it is a conversion from vector to matrix // TVector op_vec; op_vec.assign(L"1,2,0,4,5"); MMatrix<TScalar, TIntegral> op_vec_mat; op_vec_mat.assign(1, op_vec.length(), L"1,2,0,4,5", Integral::FULL); MMatrix<TScalar, TIntegral> op_vec_mat_trans; op_vec_mat_trans.assign(op_vec.length(), 1, L"1,2,0,4,5", Integral::FULL); MMatrix<TScalar, TIntegral> res_mat(op_vec.length(), op_vec.length(), Integral::FULL); res_mat.mult(op_vec_mat_trans, op_vec_mat); for (long type1 = Integral::FULL; type1 <= Integral::SPARSE; type1++) { MMatrix<TScalar, TIntegral> tmp_mat(0, 0, (Integral::MTYPE)type1); Integral::MTYPE exp_type = (type1 == Integral::SPARSE) ? Integral::SPARSE : Integral::FULL; // compute the outer product // tmp_mat.outerProduct(op_vec); // compare the matrix with the expected result // if ((tmp_mat.getType() != exp_type) || !tmp_mat.almostEqual(res_mat)) { Error::handle(name(), L"outerProduct (vector)", Error::TEST, __FILE__, __LINE__); } } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // exit gracefully // return true;}// method: diagnose3//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolean value indicating status//// this method tests matrix to matrix methods for matrix - the diagnose// method is designed in such a way that it tests the methods for 8// different matrices and for every possible type//template<class TScalar, class TIntegral>boolean MMatrixMethods::diagnose3(Integral::DEBUG level_a) { // define TVector // typedef MVector<TScalar, TIntegral> TVector; // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: single matrix --> matrix...\n"); Console::increaseIndention(); } // declare an array of matrix to hold all the 8 matrices used for testing // MMatrix<TScalar, TIntegral> arg_mats[MMAT_NUM_MATS_TEST]; // load up the argument matrices // for (long i = 0; i < MMAT_NUM_MATS_TEST; i++) { // assign the arguments //#ifndef ISIP_TEMPLATE_complex arg_mats[i].assign((long)MMAT_ARG_MATRICES[i][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[i][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[i][MMAT_DATA_LOC]);#else arg_mats[i].assignComplexDiagnose((long)MMAT_ARG_MATRICES[i][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[i][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[i][MMAT_DATA_LOC]);#endif } // declare temporary matrix to be used in the whole diagnose method // for testing // MMatrix<TScalar, TIntegral> res_mat; MMatrix<TScalar, TIntegral> tmp_mat; MMatrix<TScalar, TIntegral> res_mat0; MMatrix<TScalar, TIntegral> arg_mat1; // loop over different matrices // for (long mat1 = 0; mat1 < MMAT_NUM_MATS_TEST; mat1++) { // declare local variables // Char mat1c; mat1c.assign((byte)((int)'A' + mat1)); // loop over the different types of matrices // for (long type1 = Integral::FULL; type1 <= Integral::SPARSE; type1++) { // test if the type conversion is possible // if (arg_mats[mat1].isTypePossible((Integral::MTYPE)type1)) { // create a diagnostic string to indicate the matrix and its type // String type1_str(MMatrix<TScalar, TIntegral>::TYPE_MAP(type1)); String arg1_str(L"testing mat1: "); arg1_str.concat(mat1c); arg1_str.concat(L", "); arg1_str.concat(type1_str); // assign the input matrix and change the type - type is // changed here so that this particular matrix can be tested for // every type it can have // arg_mat1.assign(arg_mats[mat1]); arg_mat1.changeType((Integral::MTYPE)type1); // test neg - this method is not applicable for byte and // unsigned types // if ((typeid(TIntegral) != typeid(byte)) && (typeid(TIntegral) != typeid(ushort)) && (typeid(TIntegral) != typeid(ulong)) && (typeid(TIntegral) != typeid(ullong))) { tmp_mat.neg(arg_mat1);#ifndef ISIP_TEMPLATE_complex res_mat.assign((long)MMAT_NEG_RES[mat1][MMAT_NROW_LOC], (long)MMAT_NEG_RES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_NEG_RES[mat1][MMAT_DATA_LOC]);#else res_mat.assignComplexDiagnose((long)MMAT_NEG_RES[mat1][MMAT_NROW_LOC], (long)MMAT_NEG_RES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_NEG_RES[mat1][MMAT_DATA_LOC]);#endif if (!res_mat.eq(tmp_mat)) { Console::put(arg1_str); Error::handle(name(), L"neg", Error::TEST, __FILE__, __LINE__); } } // test matrix assign methods - here we test for all matrix // types as matrix assign is type specific // MMatrix<Byte, byte> mat_byt; MMatrix<Ushort, ushort> mat_usht; MMatrix<Ulong, ulong> mat_ulng; MMatrix<Ullong, ullong> mat_ullg; MMatrix<Short, short> mat_sht; MMatrix<Long, long> mat_lng; MMatrix<Llong, llong> mat_llng; MMatrix<Float, float> mat_flt; MMatrix<Double, double> mat_dbl; // assign result // res_mat.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); // assign values to byte type // mat_byt.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_byt); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to ushort type // mat_usht.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_usht); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to ulong type // mat_ulng.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_ulng); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to ullong type // mat_ullg.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_ullg); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to short type // mat_sht.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_sht); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to long type // mat_lng.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_lng); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to llong type // mat_llng.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_llng); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to float type // mat_flt.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MMAT_ARG_MATRICES[mat1][MMAT_NCOL_LOC], (double*)&MMAT_ARG_MATRICES[mat1][MMAT_DATA_LOC]); tmp_mat.assign(mat_flt); if (!tmp_mat.almostEqual(res_mat)) { Console::put(arg1_str); Error::handle(name(), L"assign", Error::TEST, __FILE__, __LINE__); } // assign values to double type // mat_dbl.assign((long)MMAT_ARG_MATRICES[mat1][MMAT_NROW_LOC], (long)MM
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -