📄 math_02.cc
字号:
math7.compute(output, input); if (!output.getMatrixFloat().almostEqual(b)) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test scalar division // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting scalar division (1):\n"); } values_op(1).assign((long)DIVIDE); math7.setOperation(values_op); input(0).makeVectorFloat().assign(L"9.0, 6.0, 3.0"); input(1).makeVectorFloat().assign(L"3.0"); expected.assign(L"3.0, 2.0, 1.0"); math7.compute(output, input); if (!output.getVectorFloat().almostEqual(expected)) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting scalar division (2):\n"); } a.clear(Integral::RETAIN); a.setDiagonal(6.0); input(0).makeMatrixFloat().assign(a); input(1).makeVectorFloat().assign(L"3.0"); b.clear(Integral::RETAIN); b.setDiagonal(2.0); math7.compute(output, input); if (!output.getMatrixFloat().almostEqual(b)) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 4. class-specific public methods: // computational methods for Double // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: computational methods for double...\n"); Console::increaseIndention(); } /* // test Y = 2.0 * X1 + 3.0 * X2 + 2.0 // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [2.0 * X1 + 3.0 * X2 + 2.0]:\n"); } Math math6; math6.setNumOperands(2); math6.setConstant(2.0); math6.setWeight(L"2.0, 3.0"); // set up the variables for test // Vector<AlgorithmData> input(2); AlgorithmData output; VectorDouble result; input(0).makeVectorDouble().assign(L"1.1, 4.0, 1.5, 2.3"); input(1).makeVectorDouble().assign(L"1.0, 1.0, 0.0, 0.0"); math6.compute(output, input); result.assign(L"7.2, 13.0, 5.0, 6.6"); if (!result.almostEqual(output.getVectorDouble())) { output.getVectorDouble().debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = log(X1); // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [log(X1)]:\n"); } VectorLong values_01(1); values_01(0).assign((long)LOG); VectorLong values_op_01(1); values_op_01(0).assign((long)ASSIGN); math6.clear(); math6.setNumOperands(1); math6.setWeight(L"1.0"); math6.setOperation(values_op_01); math6.setFunction(values_01); input.setLength(1); input(0).makeVectorDouble().assign(L"10.0, 40.0"); output.clear(); math6.compute(output, input); result.assign(L"2.30259, 3.6888"); if (!result.almostEqual(output.getVectorDouble())) { output.debug(L"output"); result.debug(L"result"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = log(X1) + constant; // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [log(X1) + 10.0]:\n"); } VectorLong values_02(1); values_02(0).assign((long)LOG); VectorLong values_op_02(1); values_op_02(0).assign((long)ASSIGN); math6.setNumOperands(1); math6.setWeight(L"1.0"); math6.setOperation(values_op_02); math6.setFunction(values_02); math6.setConstant(10.0); input.setLength(1); input(0).makeVectorDouble().assign(L"10.0, 40.0"); math6.compute(output, input); result.assign(L"12.30259, 13.6888"); if (!result.almostEqual(output.getVectorDouble())) { output.debug(L"output"); result.debug(L"result"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } */ // test Y = X1 + 2 * X2 - log(X3) - exp(X4) // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [X1 + 2.0 * X2 - log(X3) - exp(X4)]:\n"); } values.setLength(4); values(0).assign((long)IDENTITY); values(1).assign((long)IDENTITY); values(2).assign((long)LOG); values(3).assign((long)EXP); values_op.setLength(4); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)ADD); values_op(2).assign((long)SUBTRACT); values_op(3).assign((long)SUBTRACT); math6.setNumOperands(4); math6.setWeight(L"1.0, 2.0, 1.0, 1.0"); math6.setOperation(values_op); math6.setFunction(values); math6.setConstant(0.0); input.setLength(4); input(0).makeVectorDouble().assign(L"1.1, 4.0"); input(1).makeVectorDouble().assign(L"1.5, 2.3"); input(2).makeVectorDouble().assign(L"1.0, 1.0"); input(3).makeVectorDouble().assign(L"0.0, 0.0"); math6.compute(output, input); VectorDouble result_db; result_db.assign(L"3.1, 7.6"); if (!result_db.almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } /* // test Y = X1 + 2 * X2 - log(X3) - exp(X4) // test wrong input "if ASSIGN appears middle of computation // everything before it will clear. // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [X1 + 2.0 * X2 - log(X3) - exp(X4)]:\n"); } values(0).assign((long)IDENTITY); values(1).assign((long)IDENTITY); values(2).assign((long)LOG); values(3).assign((long)EXP); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)ADD); values_op(2).assign((long)ASSIGN); values_op(3).assign((long)SUBTRACT); math6.setNumOperands(4); math6.setWeight(L"1.0, 2.0, 1.0, 1.0"); math6.setOperation(values_op); math6.setFunction(values); math6.setConstant(0.0); input.setLength(4); input(0).makeVectorDouble().assign(L"1.1, 4.0"); input(1).makeVectorDouble().assign(L"1.5, 2.3"); input(2).makeVectorDouble().assign(L"1.0, 1.0"); input(3).makeVectorDouble().assign(L"0.0, 0.0"); math6.compute(output, input); result_db.assign(L"-1, -1"); if (!result_db.almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = X1 + 2 * X2 - log(X3) - exp(X4) (all zero and constant) // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [X1 + 2.0 * X2 - log(X3) - exp(X4)] for all zero values:\n"); } input.setLength(4); input(0).makeVectorDouble().assign(L"0.0, 0.0"); input(1).makeVectorDouble().assign(L"0.0, 0.0"); input(2).makeVectorDouble().assign(L"1.0, 1.0"); input(3).makeVectorDouble().assign(L"0.0, 0.0"); math6.compute(output, input); result_db.assign(L"-1.0, -1.0"); if (!result_db.almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = [ {X1* X2 + log(X3)} / exp(X4)] // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [ {X1* X2 + log(X3)} / exp(X4)]:\n"); } math6.setWeight(L"1.0, 1.0, 1.0, 1.0"); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)MULTIPLY); values_op(2).assign((long)ADD); values_op(3).assign((long)DIVIDE); math6.setOperation(values_op); input(0).makeVectorDouble().assign(L"1.1, 4.0"); input(1).makeVectorDouble().assign(L"1.5, 2.3"); input(2).makeVectorDouble().assign(L"1.0, 1.0"); input(3).makeVectorDouble().assign(L"0.0, 0.0"); math6.compute(output, input); result_db.assign(L"1.65, 9.2"); if (!result_db.almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = square(X1) + 2.0 * sqrt(X2) + 3.0 // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [square(X1) + 2.0 * sqrt(X2) + 3.0]:\n"); } values.setLength(2); values(0).assign((long)SQUARE); values(1).assign((long)SQRT); math6.setNumOperands(2); math6.setWeight(L"1.0, 2.0"); str.assign(L"ASSIGN, ADD"); math6.setOperation(str); math6.setFunction(values); math6.setConstant(3.0); input.setLength(2); input(0).makeVectorDouble().assign(L"1.1, 4.0, 1.5, 2.3"); input(1).makeVectorDouble().assign(L"4.0, 1.21, 4.0, 9.0"); math6.compute(output, input); result_db.assign(L"8.21, 21.2, 9.25, 14.29"); if (!result_db.almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = 2.0 * X1^2 + 3.0 * X2 + 3.0 // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [2.0 * X1^2 + 3.0 * X2 + 3.0]:\n"); } values.setLength(2); values(0).assign((long)SQUARE); values(1).assign((long)IDENTITY); math6.setNumOperands(2); math6.setWeight(L"2.0, 3.0"); str.assign(L"ASSIGN, ADD"); math6.setOperation(str); math6.setFunction(values); input(0).makeVectorDouble().assign(L"1.1, 4.0, 1.5, 1.1"); input(1).makeVectorDouble().assign(L"4.0, 0.5, 4.0, 9.0"); math6.compute(output, input); result_db.assign(L"17.42, 36.5, 19.5, 32.42"); if (!result_db.almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__); } // test Y = X1 * X2 (vector * matrix) // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [X1 * X2 (vector * matrix)]:\n"); } input(0).makeVectorDouble().assign(L"1.1, 2.0, 3.0"); input(1).makeMatrixDouble().assign(3, 3, L"1.0, 0.0, 5.0, 1.2, 2.4, 3.6, 1.8, 2.1, 9.8"); values(0).assign((long)IDENTITY); values(1).assign((long)IDENTITY); values_op.setLength(2); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)MULTIPLY); math6.setOperation(values_op); math6.setFunction(values); math6.setWeight(L"1.0, 1.0"); math6.setConstant(0.0); math6.compute(output, input); result_db.assign(L"8.9, 11.1, 42.1"); if (!(result_db).almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__); } // test Y = X1 * X2 (vector * inv(matrix)) // values(0).assign((long)IDENTITY); values(1).assign((long)INVERSE); values_op.setLength(2); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)MULTIPLY); math6.setOperation(values_op); math6.setFunction(values); math6.setWeight(L"1.0, 1.0"); math6.setConstant(0.0); math6.compute(output, input); result_db.assign(L"0.2293, 0.9842, -0.1724"); if (!(result_db).almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__); } // test Y = X1 * X2 (vector * matrix) with vector of zeros and constants // if (level_a > Integral::BRIEF) { Console::put(L"\n\ttesting for Y = [X1 * X2 (vector * matrix)] with vector of zeros and constants:\n"); } //test vector of zeros // input(0).makeVectorDouble().assign(L"0.0, 0.0, 0.0"); input(1).makeMatrixDouble().assign(3, 3, L"1.0, 0.0, 5.0, 1.2, 2.4, 3.6, 1.8, 2.1, 9.8"); values(0).assign((long)IDENTITY); values(1).assign((long)IDENTITY); values_op.setLength(2); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)MULTIPLY); math6.setOperation(values_op); math6.setFunction(values); math6.setWeight(L"1.0, 1.0"); math6.setConstant(0.0); math6.compute(output, input); result_db.assign(L"0.0, 0.0, 0.0"); if (!(result_db).almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__); } //test matrix of zeros // input(0).makeVectorDouble().assign(L"1.0, 1.0, 1.0"); input(1).makeMatrixDouble().assign(3, 3, L"0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0"); values(0).assign((long)IDENTITY); values(1).assign((long)IDENTITY); values_op.setLength(2); values_op(0).assign((long)ASSIGN); values_op(1).assign((long)MULTIPLY); math6.setOperation(values_op); math6.setFunction(values); math6.setWeight(L"1.0, 1.0"); math6.setConstant(0.0); math6.compute(output, input); result_db.assign(L"0.0, 0.0, 0.0"); if (!(result_db).almostEqual(output.getVectorDouble())) { output.debug(L"output"); return Error::handle(name(), L"apply", Error::TEST, __FILE__, __LINE__); } */ // test Y = X1 + X2 * X3' + inv(x4) // (matrix + matrix * trans(matrix) + inv(matrix)) //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -