📄 math_02.cc
字号:
// file: $isip/class/algo/Math/math_02.cc// version: $Id: math_02.cc,v 1.12 2002/07/15 18:02:16 parihar Exp $//// isip include files//#include "Math.h"#include <Console.h>// method: diagnose//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolen value indicating status//boolean Math::diagnose(Integral::DEBUG level_a) { //--------------------------------------------------------------------- // // 0. preliminaries // //--------------------------------------------------------------------- // output the class name // if (level_a > Integral::NONE) { String output(L"diagnosing class "); output.concat(CLASS_NAME); output.concat(L": "); Console::put(output); Console::increaseIndention(); } //-------------------------------------------------------------------- // // 1. required public methods // //-------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing required public methods...\n"); Console::increaseIndention(); } // test destructor/constructor(s) and memory management // Math math0; Math math1(math0); if (!math1.eq(math0)) { return Error::handle(name(), L"copy constructor", Error::TEST, __FILE__, __LINE__); } // test large allocation construction and deletion // if (level_a == Integral::ALL) { Console::put(L"\ntesting large chunk memory allocation and deletion:\n"); // set the memory to a strange block size so we can hopefully catch any // frame overrun errors // Math::setGrowSize((long)500); Math* pft = new Math(); for (long j = 1; j <= 100; j++) { Math** pfts = new Math*[j * 100]; // create the objects // for (long i = 0; i < j * 100; i++) { pfts[i] = new Math(); } // delete objects // for (long i = (j * 100) - 1; i >= 0; i--) { delete pfts[i]; } delete [] pfts; } delete pft; } // test the i/o methods // Math math2; Math math3; Math math4; math3.setNumOperands(3); math2.setAlgorithm(FUNCTION_CALCULATOR); // we need binary and text sof files // String tmp_filename0; Integral::makeTemp(tmp_filename0); String tmp_filename1; Integral::makeTemp(tmp_filename1); // open files in write algorithm // Sof tmp_file0; tmp_file0.open(tmp_filename0, File::WRITE_ONLY, File::TEXT); Sof tmp_file1; tmp_file1.open(tmp_filename1, File::WRITE_ONLY, File::BINARY); math2.write(tmp_file0, (long)0); math2.write(tmp_file1, (long)0); // close the files // tmp_file0.close(); tmp_file1.close(); // open the files in read algorithm // tmp_file0.open(tmp_filename0); tmp_file1.open(tmp_filename1); // read the value back // math3.read(tmp_file0, (long)0); if (!math3.eq(math2)) { math3.debug(L"math3"); math2.debug(L"math2"); return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__); } math4.read(tmp_file0, (long)0); if (!math4.eq(math2)) { math4.debug(L"math4"); return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__); } // close and delete the temporary files // tmp_file0.close(); tmp_file1.close(); File::remove(tmp_filename0); File::remove(tmp_filename1); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 2. class-specific public methods: // set and get methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: set and get methods...\n"); Console::increaseIndention(); } Math math5; long num0 = 4; math5.setNumOperands(num0); if (math5.getNumOperands() != num0) { math5.debug(L"math0"); return Error::handle(name(), L"setNumOperands", Error::TEST, __FILE__, __LINE__); } String str(L"ADD, SUBTRACT, ADD, SUBTRACT"); math5.setOperation(str); if (math5.operation_d(1) != (long)SUBTRACT) { math5.debug(L"math5"); return Error::handle(name(), L"setOperation", Error::TEST, __FILE__, __LINE__); } math5.setOperation(ASSIGN, 1); if (math5.operation_d(1) != (long)ASSIGN) { math5.debug(L"math5"); return Error::handle(name(), L"setOperation", Error::TEST, __FILE__, __LINE__); } String func(L"EXP, EXP2, EXP10, FACTORIAL"); math5.setFunction(func); if ((math5.function_d(0) != (long)EXP) || (math5.function_d(1) != (long)EXP2) || (math5.function_d(2) != (long)EXP10) || (math5.function_d(3) != (long)FACTORIAL)) { math5.debug(L"math5"); return Error::handle(name(), L"setFunction", Error::TEST, __FILE__, __LINE__); } math5.setFunction(SQUARE, 1); if (math5.function_d(1) != (long)SQUARE) { math5.debug(L"math5"); return Error::handle(name(), L"setFunction", Error::TEST, __FILE__, __LINE__); } VectorFloat weight(L"2.0, 1.0, 1.0, 0.5"); math5.setWeight(weight); if (!(math5.getWeight()).eq(weight)) { math5.debug(L"math5"); return Error::handle(name(), L"setWeight", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //-------------------------------------------------------------------------- // // 3. class-specific public methods: // computational methods // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: computational methods for float...\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"); } VectorLong values_00(2); values_00(0).assign((long)IDENTITY); values_00(1).assign((long)IDENTITY); VectorLong values_op_00(2); values_op_00(0).assign((long)ASSIGN); values_op_00(0).assign((long)ADD); Math math6; math6.setNumOperands(2); math6.setConstant(2.0); math6.setWeight(L"2.0, 3.0"); math6.setFunction(values_00); math6.setOperation(values_op_00); // set up the variables for test // Vector<AlgorithmData> input(2); AlgorithmData output; VectorFloat result; input(0).makeVectorFloat().assign(L"1.1, 4.0, 1.5, 2.3"); input(1).makeVectorFloat().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.getVectorFloat())) { output.getVectorFloat().debug(L"output"); result.debug(L"result"); 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).makeVectorFloat().assign(L"10.0, 40.0"); output.clear(); math6.compute(output, input); result.assign(L"2.30259, 3.6888"); if (!result.almostEqual(output.getVectorFloat())) { 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).makeVectorFloat().assign(L"10.0, 40.0"); math6.compute(output, input); result.assign(L"12.30259, 13.6888"); if (!result.almostEqual(output.getVectorFloat())) { 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"); } VectorLong values(4); values(0).assign((long)IDENTITY); values(1).assign((long)IDENTITY); values(2).assign((long)LOG); values(3).assign((long)EXP); VectorLong values_op(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); Math math6; 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); Vector<AlgorithmData> input(2); AlgorithmData output; VectorFloat result; input.setLength(4); input(0).makeVectorFloat().assign(L"1.1, 4.0"); input(1).makeVectorFloat().assign(L"1.5, 2.3"); input(2).makeVectorFloat().assign(L"1.0, 1.0"); input(3).makeVectorFloat().assign(L"0.0, 0.0"); math6.compute(output, input); result.assign(L"3.1, 7.6"); if (!result.almostEqual(output.getVectorFloat())) { 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).makeVectorFloat().assign(L"1.1, 4.0"); input(1).makeVectorFloat().assign(L"1.5, 2.3"); input(2).makeVectorFloat().assign(L"1.0, 1.0"); input(3).makeVectorFloat().assign(L"0.0, 0.0"); math6.compute(output, input); result.assign(L"-1, -1"); if (!result.almostEqual(output.getVectorFloat())) { 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).makeVectorFloat().assign(L"0.0, 0.0"); input(1).makeVectorFloat().assign(L"0.0, 0.0"); input(2).makeVectorFloat().assign(L"1.0, 1.0"); input(3).makeVectorFloat().assign(L"0.0, 0.0"); math6.compute(output, input); result.assign(L"-1.0, -1.0"); if (!result.almostEqual(output.getVectorFloat())) { output.debug(L"output"); return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -