📄 mcscl_02.cc
字号:
((TIntegral)(Integral::PI / (double)6), (TIntegral)0.5)); val1.sinh(val0); if (!val1.almostEqual(SysComplex<TIntegral> ((TIntegral)0.4808, (TIntegral)0.5467))) { val0.debug(L"val0.sinh"); val1.debug(L"val1.sinh"); return Error::handle(name(), L"sinh", Error::TEST, __FILE__, __LINE__); } // test sqrt // val0.assign ((TIntegral)3.3, (TIntegral)4.3); val2.sqrt(val0); if ((!val2.almostEqual(SysComplex<TIntegral> ((TIntegral)2.0881, (TIntegral)1.02964)))) { val2.debug(L"sqrt"); return Error::handle(name(), L"sqrt", Error::TEST, __FILE__, __LINE__); } } // end of if != typeid(long) // test square // val0.assign (3, 4); val2.square(val0); if ((!val2.almostEqual(SysComplex<TIntegral> ((TIntegral)-7, (TIntegral)24)))) { return Error::handle(name(), L"square", Error::TEST, __FILE__, __LINE__); } if ((typeid(TIntegral) != typeid(long))) { // test tan // val0.assign(SysComplex<TIntegral> ((TIntegral)Integral::QUARTER_PI, (TIntegral)0.5)); val1.tan(val0); if (!val1.almostEqual(SysComplex<TIntegral> ((TIntegral)0.648, (TIntegral)0.7616))) { val0.debug(L"val0.tan"); val1.debug(L"val1.tan"); return Error::handle(name(), L"tan", Error::TEST, __FILE__, __LINE__); } // test tanh // val0.assign(SysComplex<TIntegral> ((TIntegral)1.0, (TIntegral)0.5)); val1.tanh(val0); if (!val1.almostEqual(SysComplex<TIntegral> ((TIntegral)0.8430, (TIntegral)0.1956))) { val0.debug(L"val0.tanh"); val1.debug(L"val1.tanh"); return Error::handle(name(), L"tanh", Error::TEST, __FILE__, __LINE__); } } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // exit gracefully // return true;}// method: diagnose2//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: logical error status//// this method tests all non-required methods found in MScalarComplex.h// (functions not implemented via MScalar and not part of the required// methods section).//template<class TCScalar, class TIntegral>boolean MComplexScalarMethods::diagnose2(Integral::DEBUG level_a) { //-------------------------------------------------------------------- // // 3. testing the random number generation methods // //-------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing methods not implemented via MComplexScalar...\n"); Console::increaseIndention(); } // declare local variables // long N = 100000; SysComplex<TIntegral> min, max, mean; SysComplex<TIntegral> expected_min, expected_max, expected_mean; SysComplex<TIntegral> comp_mean; TCScalar rand; //-------------------------------------------------------------------- // // test rand() // //-------------------------------------------------------------------- // determine the expected values based on the specific type // if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { expected_min = 0; expected_max = SysComplex<TIntegral>(1, 1); expected_mean = SysComplex<TIntegral>((TIntegral)0.5, (TIntegral)0.5); comp_mean = SysComplex<TIntegral>((TIntegral)0.5, (TIntegral)0.5); } else if (typeid(TIntegral) == typeid(long)) { expected_min = SysComplex<TIntegral>((long)-100,(long)-100); expected_max = SysComplex<TIntegral>((long)100, (long)100); expected_mean = SysComplex<TIntegral>(0, 0); } else { return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // set the seed // Random::GLOBAL_UNIFORM.seed(27); // generate N random numbers: use rand() // mean = (TIntegral)0; min = expected_max; max = expected_min; for (long i = 0; i < N; i++) { // take a random sample // SysComplex<TIntegral> tmp = rand.rand(expected_min, expected_max); // update the mean // mean += tmp; // update the min/max // min = SysComplex<TIntegral>((TIntegral)Integral::min(tmp.real(), min.real()), (TIntegral)Integral::min(tmp.imag(), min.imag())); max = SysComplex<TIntegral>((TIntegral)Integral::max(tmp.real(), max.real()), (TIntegral)Integral::max(tmp.imag(), max.imag())); } mean /= N; // check if the computed values are in the proper range // if (!Integral::almostEqual(mean, comp_mean, 2.0)) { SysString out(L"mean = "); out.concat(mean); out.concat(L"; comp_mean = "); out.concat(comp_mean); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (min.real() < expected_min.real() || min.imag() < expected_min.imag()) { SysString out(L"min = "); out.concat(min); out.concat(L"expected_min"); out.concat(expected_min); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (max.real() > expected_max.real() || max.imag() > expected_max.imag()) { SysString out(L"max = "); out.concat(max); out.concat(L"expected_max"); out.concat(expected_max); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } //-------------------------------------------------------------------- // // test rand(expected_min, expected_max) // //-------------------------------------------------------------------- // determine the expected values based on the specific type // expected_min = SysComplex<TIntegral>((TIntegral)-10.0, (TIntegral)-5.0); expected_max = SysComplex<TIntegral>((TIntegral)30.0, (TIntegral)15.0); expected_mean = (expected_min + expected_max) / (TIntegral)2; if ((typeid(TIntegral) == typeid(float)) || (typeid(TIntegral) == typeid(double))) { comp_mean = SysComplex<TIntegral>((TIntegral)10.0, (TIntegral)5.0); } else if (typeid(TIntegral) == typeid(long)) { comp_mean = SysComplex<TIntegral>(9, 4); } else { return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // set the seed // Random::GLOBAL_UNIFORM.seed(27); // generate N random numbers: use rand() // mean = (TIntegral)0.0; min = expected_max; max = expected_min; for (long i = 0; i < N; i++) { // take a random sample // SysComplex<TIntegral> tmp = rand.rand((SysComplex<TIntegral>)expected_min, (SysComplex<TIntegral>)expected_max); // update the mean // mean += tmp; // update the min/max // min = SysComplex<TIntegral>((TIntegral)Integral::min(tmp.real(), min.real()), (TIntegral)Integral::min(tmp.imag(), min.imag())); max = SysComplex<TIntegral>((TIntegral)Integral::max(tmp.real(), max.real()), (TIntegral)Integral::max(tmp.imag(), max.imag())); } mean /= (TIntegral)N; // check if the computed values are in the proper range // if (!Integral::almostEqual(mean, comp_mean, 4.0)) { SysString out(L"mean = "); out.concat(mean); out.concat(L"; comp_mean = "); out.concat(comp_mean); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (min.real() < expected_min.real() || min.imag() < expected_min.imag()) { SysString out(L"min = "); out.concat(min); out.concat(L"expected_min"); out.concat(expected_min); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } if (max.real() > expected_max.real() || max.imag() > expected_max.imag()) { SysString out(L"max = "); out.concat(max); out.concat(L"expected_max"); out.concat(expected_max); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } //--------------------------------------------------------------------------- // // test grand(expected_mean, expected_stddev) // //--------------------------------------------------------------------------- // determine the expected values based on the specific type: // choose a mean and stdev that works for all datatypes // expected_mean = SysComplex<TIntegral>((TIntegral)128.0, (TIntegral)-14.0); SysComplex<TIntegral>expected_stddev((TIntegral)10.0, (TIntegral)30.0); SysComplex<TIntegral> comp_stddev; double stddev_r = 0.0; double stddev_i = 0.0; double mean_r = 0.0; double mean_i = 0.0; // set the seed // Random::GLOBAL_GAUSSIAN.seed(27); mean = (TIntegral)0.0; // generate N random numbers: use grand() // for (long i = 0; i < N; i++) { // generate a random number // SysComplex<TIntegral> tmp = rand.grand((SysComplex<TIntegral>)expected_mean, (SysComplex<TIntegral>)expected_stddev); // update the mean / stddev // mean += tmp; stddev_r += tmp.real() * tmp.real(); stddev_i += tmp.imag() * tmp.imag(); } // normalize the mean and stddev: // note stddev = sqrt(E(x^2) - (Ex)^2) // // use the same equation for real and complex, but for complex we // want to calculate each channel independently. // mean_r = (double)mean.real() / (double)N; stddev_r = (double)stddev_r / (double)N; stddev_r = Integral::sqrt(stddev_r - mean_r * mean_r); mean_i = (double)mean.imag() / (double)N; stddev_i = (double)stddev_i / (double)N; stddev_i = Integral::sqrt(stddev_i - mean_i * mean_i); // change to double to compare // SysComplex<double> mean_temp(mean_r, mean_i); SysComplex<double> comp_mean_temp(expected_mean); // check if the computed values are in the proper range // if (!Integral::almostEqual(mean_temp, comp_mean_temp, 5.0)) { SysString out(L"expected_mean = "); out.concat(comp_mean_temp); out.concat(L"; computed_mean = "); out.concat(mean_temp); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // change to double to compare // SysComplex<double> stddev_temp(stddev_r, stddev_i); SysComplex<double> comp_stddev_temp(expected_stddev); if (!Integral::almostEqual(stddev_temp, comp_stddev_temp, 5.0)) { SysString out(L"expected_stddev = "); out.concat(comp_stddev_temp); out.concat(L"; computed_stddev = "); out.concat(stddev_temp); Console::put(out); return Error::handle(name(), L"rand", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // exit gracefully // return true;}template booleanMComplexScalarMethods::diagnose<MComplexScalar<ISIP_TEMPLATE_TARGET>, ISIP_TEMPLATE_T0>(Integral::DEBUG level_a);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -