cxtest2.cpp

来自「VC环境下的实用的复数库函数,编译后可作为库函数使用.」· C++ 代码 · 共 1,101 行 · 第 1/3 页

CPP
1,101
字号
         CF(i,j) = CX(_I_) / CA(i,j);
         PD(i,j) = _I_ / PA(i,j); PD(i,j).AssertIsValid();
      }
      else { PD(i,j) = 0; CF(i,j) = 0; }
   }
   PrintMaxDiff(PC,CE);  PrintMaxDiff(PD,CF);

   cout << "check complex op= complex" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) + CB(i,j);
   CD = CA; CD += CB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) - CB(i,j);
   CD = CA; CD -= CB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) * CB(i,j);
   CD = CA; CD *= CB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (CB(i,j) != 0)
         { CC(i,j) = CA(i,j) / CB(i,j); CD(i,j) = CA(i,j); CD(i,j) /= CB(i,j); }
      else { CC(i,j) = 0; CD(i,j) = 0; }
   }
   PrintMaxDiff(CC, CD);

   cout << "check complex op= real" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) + RB(i,j);
   CD = CA; CD += RB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) - RB(i,j);
   CD = CA; CD -= RB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) * RB(i,j);
   CD = CA; CD *= RB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (RB(i,j) != 0)
         { CC(i,j) = CA(i,j) / RB(i,j); CD(i,j) = CA(i,j); CD(i,j) /= RB(i,j); }
      else { CC(i,j) = 0; CD(i,j) = 0; }
   }
   PrintMaxDiff(CC, CD);

   cout << "check complex op= imag" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) + IB(i,j);
   CD = CA; CD += IB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) - IB(i,j);
   CD = CA; CD -= IB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) * IB(i,j);
   CD = CA; CD *= IB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (IB(i,j) != 0)
         { CC(i,j) = CA(i,j) / IB(i,j); CD(i,j) = CA(i,j); CD(i,j) /= IB(i,j); }
      else { CC(i,j) = 0; CD(i,j) = 0; }
   }
   PrintMaxDiff(CC, CD);

   cout << "check complex op= imag" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) + IB(i,j);
   CD = CA; CD += IB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) - IB(i,j);
   CD = CA; CD -= IB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) * IB(i,j);
   CD = CA; CD *= IB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (IB(i,j) != 0)
         { CC(i,j) = CA(i,j) / IB(i,j); CD(i,j) = CA(i,j); CD(i,j) /= IB(i,j); }
      else { CC(i,j) = 0; CD(i,j) = 0; }
   }
   PrintMaxDiff(CC, CD);

   cout << "check complex op= _I_" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) + _I_;
   CD = CA; CD += _I_; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) - _I_;
   CD = CA; CD -= _I_; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) * _I_;
   CD = CA; CD *= _I_; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) / _I_;
   CD = CA; CD /= _I_; PrintMaxDiff(CC, CD);

   cout << "check complex op= polar" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) + CB(i,j);
   CD = CA; CD += PB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) - CB(i,j);
   CD = CA; CD -= PB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) CC(i,j) = CA(i,j) * CB(i,j);
   CD = CA; CD *= PB; PrintMaxDiff(CC, CD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (CB(i,j) != 0)
         { CC(i,j) = CA(i,j) / CB(i,j); CD(i,j) = CA(i,j); CD(i,j) /= PB(i,j); }
      else { CC(i,j) = 0; CD(i,j) = 0; }
   }
   PrintMaxDiff(CC, CD);

   cout << "check imag op= imag" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) IC(i,j) = IA(i,j) + IB(i,j);
   ID = IA; ID += IB; PrintMaxDiff(IC, ID);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) IC(i,j) = IA(i,j) - IB(i,j);
   ID = IA; ID -= IB; PrintMaxDiff(IC, ID);

   cout << "check imag op= real" << endl;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++) IC(i,j) = IA(i,j) * RB(i,j);
   ID = IA; ID *= RB; PrintMaxDiff(IC, ID);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (RB(i,j) != 0)
         { IC(i,j) = IA(i,j) / RB(i,j); ID(i,j) = IA(i,j); ID(i,j) /= RB(i,j); }
      else { IC(i,j) = Imag(0); ID(i,j) = Imag(0); }
   }
   PrintMaxDiff(IC, ID);

   cout << "check polar op= polar" << endl;

   CD = CA; CD += CB; PD = PA; PD += PB; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   CD = CA; CD -= CB; PD = PA; PD -= PB; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   CD = CA; CD *= CB; PD = PA; PD *= PB; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (CB(i,j) != 0)
         { CD(i,j) = CA(i,j) / CB(i,j); PD(i,j) = PA(i,j); PD(i,j) /= PB(i,j); }
      else { CD(i,j) = 0; PD(i,j) = 0; }
   }
   PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   cout << "check polar op= complex" << endl;

   CD = CA; CD += CB; PD = PA; PD += CB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   CD = CA; CD -= CB; PD = PA; PD -= CB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   CD = CA; CD *= CB; PD = PA; PD *= CB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (CB(i,j) != 0)
         { CD(i,j) = CA(i,j) / CB(i,j); PD(i,j) = PA(i,j); PD(i,j) /= CB(i,j); }
      else { CD(i,j) = 0; PD(i,j) = 0; }
   }
   PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   cout << "check polar op= real" << endl;

   CD = CA; CD += RB; PD = PA; PD += RB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   CD = CA; CD -= RB; PD = PA; PD -= RB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   CD = CA; CD *= RB; PD = PA; PD *= RB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (RB(i,j) != 0)
         { CD(i,j) = CA(i,j) / RB(i,j); PD(i,j) = PA(i,j); PD(i,j) /= RB(i,j); }
      else { CD(i,j) = 0; PD(i,j) = 0; }
   }
   PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   cout << "check polar op= imag" << endl;

   CD = CA; CD += IB; PD = PA; PD += IB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   CD = CA; CD -= IB; PD = PA; PD -= IB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   CD = CA; CD *= IB; PD = PA; PD *= IB; PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (IB(i,j) != 0)
         { CD(i,j) = CA(i,j) / IB(i,j); PD(i,j) = PA(i,j); PD(i,j) /= IB(i,j); }
      else { CD(i,j) = 0; PD(i,j) = 0; }
   }
   PrintMaxDiff(CD, PD);  AssertIsValid(PD);

   cout << "check polar op= imag" << endl;

   CD = CA; CD += _I_; PD = PA; PD += _I_; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   CD = CA; CD -= _I_; PD = PA; PD -= _I_; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   CD = CA; CD *= _I_; PD = PA; PD *= _I_; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   CD = CA; CD /= _I_; PD = PA; PD /= _I_; PrintMaxDiff(CD, PD); AssertIsValid(PD);

   cout << "check complex ==, != complex" << endl;

   bool OnDiag = true; bool OffDiag = false;

   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (i == j)
      {
         if ( CA(i,j) == CB(i,j) ) {} else OnDiag = false;
         if ( CA(i,j) != CB(i,j) ) OnDiag = false;
      }
      else
      {
         if ( CA(i,j) != CB(i,j) ) {} else OffDiag = true;
         if ( CA(i,j) == CB(i,j) ) OffDiag = true;
      }
   }

   if (OnDiag && !OffDiag) cout << "test passes" << endl;
   else { cout << "test fails" << endl; OverallMax = 100; }

   cout << "check polar ==, != polar" << endl;
   TestCompare(PA, PB);

   cout << "check complex ==, != imag" << endl;
   TestCompare(CA, IB);

   cout << "check imag ==, != complex" << endl;
   TestCompare(IA, CB);

   cout << "check complex ==, != real" << endl;
   TestCompare(CA, RB);

   cout << "check real ==, != complex" << endl;
   TestCompare(RA, CB);

   cout << "check imag ==, != real" << endl;
   TestCompare(IA, RB);

   cout << "check real ==, != imag" << endl;
   TestCompare(RA, IB);

   cout << "check polar ==, != imag" << endl;
   TestCompare(PA, IB);

   cout << "check imag ==, != polar" << endl;
   TestCompare(IA, PB);

   cout << "check polar ==, != real" << endl;
   TestCompare(PA, RB);

   cout << "check real ==, != polar" << endl;
   TestCompare(RA, PB);

   cout << "check complex ==, != _I_ (both directions)" << endl;
   TestCompareWithI(CA);

   cout << "check polar ==, != _I_ (both directions)" << endl;
   TestCompareWithI(PA);

   cout << "check real ==, != _I_ (both directions)" << endl;
   TestCompareWithI(RA);

   cout << "check imag ==, != _I_ (both directions)" << endl;
   TestCompareWithI(IA);



   cout << "check pow(CX, CX), pow(Polar, CX)" << endl;
   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      CG(i,j) = exp(CA(i,j));
      CC(i,j) = pow(CG(i,j), CB(i,j));
      CF(i,j) = log(CG(i,j));  // shrink argument part
      CD(i,j) = exp(CF(i,j) * CB(i,j));
      PC(i,j) = pow(polar_exp(CA(i,j)), CB(i,j));
      CH(i,j) = exp(CF(i,j));
   }
   PrintMaxRelDiff(CC, CD);  PrintMaxRelDiff(PC, CC); AssertIsValid(PC);
   PrintMaxRelDiff(CG, CH);   // check shrunken bit

   cout << "check pow(Real, CX), pow(Imag, CX)" << endl;
   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (RA(i,j) != 0 || real(CB(i, j)) > 0)
      {
         CC(i,j) = pow(RA(i,j), CB(i,j));
         CD(i,j) = pow(CX(RA(i,j)), CB(i,j));
      }
      else { CC(i,j) = 0; CD(i,j) = 0; }
      if (IA(i,j) != 0 || real(CB(i, j)) > 0)
      {
         CE(i,j) = pow(IA(i,j), CB(i,j));
         CF(i,j) = pow(CX(IA(i,j)), CB(i,j));
      }
      else { CE(i,j) = 0; CF(i,j) = 0; }
   }
   PrintMaxRelDiff(CC, CD);  PrintMaxRelDiff(CE, CF);

   cout <<
      "check pow(CX, Real), pow(Polar, Real), pow(CX, Imag), pow(Polar, Imag)"
      << endl;
   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (CA(i,j) != 0 || RB(i,j) > 0)
      {
         CC(i,j) = pow(CA(i,j), RB(i,j));
         PC(i,j) = pow(PA(i,j), RB(i,j));
         CD(i,j) = pow(CA(i,j), CX(RB(i,j)));
      }
      else { CC(i,j) = 0; CD(i,j) = 0; PC(i,j) = 0; }
      if (CA(i,j) != 0)
      {
         CE(i,j) = pow(CA(i,j), IB(i,j));
         PD(i,j) = pow(PA(i,j), IB(i,j));
         CF(i,j) = pow(CA(i,j), CX(IB(i,j)));
      }
      else { CE(i,j) = 0; CF(i,j) = 0; PD(i,j) = 0; }
   }
   PrintMaxRelDiff(CC, CD);  PrintMaxRelDiff(CE, CF);
   PrintMaxRelDiff(PC, CC);  PrintMaxRelDiff(PD, CE);

   cout << "check pow(Imag, Complex)" << endl;
   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (IA(i,j) != 0 || real(CB(i,j)) > 0)
      {
         CC(i,j) = pow(IA(i,j), CB(i,j));
         CD(i,j) = pow(CX(IA(i,j)), CB(i,j));
      }
      else { CC(i,j) = 0; CD(i,j) = 0; }
   }
   PrintMaxRelDiff(CC, CD);

   cout << "check pow(Real, Imag), pow(Imag, Imag)" << endl;
   for (i = 0; i < n; i++) for (j = 0; j < n; j++)
   {
      if (RA(i,j) != 0)
      {
         CC(i,j) = pow(RA(i,j), IB(i,j));
         CD(i,j) = pow(CX(RA(i,j)), CX(IB(i,j)));
      }
      else { CC(i,j) = 0; CD(i,j) = 0; }
      if (IA(i,j) != 0)
      {
         CE(i,j) = pow(IA(i,j), IB(i,j));
         CF(i,j) = pow(CX(IA(i,j)), CX(IB(i,j)));
      }
      else { CE(i,j) = 0; CF(i,j) = 0; }
   }
   PrintMaxRelDiff(CC, CD);  PrintMaxRelDiff(CE, CF);

   cout << "end binary tests" << endl;
   return OverallMax;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?