⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ugentests.cpp

📁 C++ Math Lib. C++ Builder must use.
💻 CPP
字号:
//---------------------------------------------------------------------------
// N.V.Shokhirev
// created: 20040726
// modified: 20041106
//---------------------------------------------------------------------------

#pragma hdrstop

#include "uGenTests.h"
#include "MatUtils.h"
#include "dynarrays.h"
#include "dynarrutils.h"
#include "complex.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

String MachepsTest()
{
  String ss = ">Macheps = ";
  real eps1, meps, eps = 1.0;
//  while ((1.0+eps)> 1.0){ meps = eps; eps /= 2.0; };
  do {meps = eps; eps /= 2.0; eps1 = eps + 1.0;} while (eps1 > 1.0);
  String s = meps;
  ss += s;

  eps = 1.0;
  do {meps = eps; eps /= 2.0;} while (eps > 0.0);
  s = meps;
  ss += "\n>MinDoube = ";
  ss += s;

  eps = 1.0;
  meps = 2097152.0;
  do {meps /= 2.0; eps /= 2.0;} while (eps > 0.0);
  s = meps;
  ss += "\n>SafeMinDoube = ";  // 1048576*MinDoube
  ss += s;
  return ss;
};

String MiscTests()
{
  FArr2D a2, a3;
  String s = "", ss = "MiscTests";
  bool fail = false;
  int m = 2, n = 3;

  FArr2D a1(n,m);
  RandArr(a1,-1.0,1.0);
  a2 = MT(a1);
  a3 = MT(a2);

  if (! Equal(a3, a1) ) { fail = true;  s += " ( a^T )^T |"; };

  double y;
  y = sign( 2.2, 3.3);
  if (! Equal( 2.2, y) ) { fail = true;  s += " sign(2.2,3.3) |"; };
  y = sign(-2.2, 3.3);
  if (! Equal( 2.2, y) ) { fail = true;  s += " sign(-2.2,3.3) |"; };
  y = sign( 2.2,-3.3);
  if (! Equal(-2.2, y) ) { fail = true;  s += " sign(2.2,-3.3) |"; };
  y = sign(-2.2,-3.3);
  if (! Equal(-2.2, y) ) { fail = true;  s += " sign(-2.2,-3.3) |"; };

  y = max1(-2.2,3.3);
  if (! Equal(3.3, y) ) { fail = true;  s += " max1(-2.2,3.3) |"; };
  y = min1(-2.2,3.3);
  if (! Equal(-2.2, y) ) { fail = true;  s += " min1(-2.2,3.3) |"; };

  if (fail) ss += " FAIL:" + s;
  else ss += " - OK";
  return ss;
};

String DiffTests()
{
  String s = "", ss = "Diff Tests";
  bool fail = false;
  int n = 5;

  FArr1D f(n);
  FArr1D f1(n);
  FArr1D f2(n);
  FArr1D f3(n);
  FArr1D f4(n);

  FArr1D g1(n);
  FArr1D g2(n);
  FArr1D g3(n);
  FArr1D g4(n);
  double x = -2.0;
  for (int i=1; i<=n;i++)
  {
    f(i)  = 1.0 + x + x*x + x*x*x + x*x*x*x;
    f1(i) =     1.0+2.0*x+3.0*x*x+4.0*x*x*x;
    f2(i) =           2.0 + 6.0*x +12.0*x*x;
    f3(i) =                   6.0  + 24.0*x;
    f4(i) =                            24.0;
    x+= 1.0;
  };
// -2
    g1(1) = (-25.0*f(1) +48.0*f(2) -36.0*f(3) +16.0*f(4)- 3.0*f(5))/12.0;
    g2(1) = (+35.0*f(1)-104.0*f(2)+114.0*f(3) -56.0*f(4)+11.0*f(5))/12.0;
    g3(1) = ( -5.0*f(1) +18.0*f(2) -24.0*f(3) +14.0*f(4) -3.0*f(5))/2.0;
    g4(1) = (  1.0*f(1) - 4.0*f(2) + 6.0*f(3) - 4.0*f(4)+ 1.0*f(5));
// -1
    g1(2) = ( -3.0*f(1) -10.0*f(2) +18.0*f(3)  -6.0*f(4) +1.0*f(5))/12.0;
    g2(2) = ( 11.0*f(1) -20.0*f(2)  +6.0*f(3) + 4.0*f(4) -1.0*f(5))/12.0;
    g3(2) = ( -3.0*f(1) +10.0*f(2) -12.0*f(3) + 6.0*f(4) -1.0*f(5))/2.0;
    g4(2) = (  1.0*f(1) - 4.0*f(2) + 6.0*f(3) - 4.0*f(4)+ 1.0*f(5));
// 0
    g1(3) = ( +1.0*f(1)  -8.0*f(2) + 0.0*f(3) + 8.0*f(4)- 1.0*f(5))/12.0;
    g2(3) = ( -1.0*f(1) +16.0*f(2) -30.0*f(3) +16.0*f(4)- 1.0*f(5))/12.0;
    g3(3) = ( -1.0*f(1) + 2.0*f(2) + 0.0*f(3) - 2.0*f(4)+ 1.0*f(5))/2.0;
    g4(3) = (  1.0*f(1) - 4.0*f(2) + 6.0*f(3) - 4.0*f(4)+ 1.0*f(5));
// 1
    g1(4) = ( -1.0*f(1)  +6.0*f(2) -18.0*f(3) +10.0*f(4) +3.0*f(5))/12.0;
    g2(4) = ( -1.0*f(1)  +4.0*f(2)  +6.0*f(3) -20.0*f(4)+11.0*f(5))/12.0;
    g3(4) = ( +1.0*f(1) - 6.0*f(2) +12.0*f(3) -10.0*f(4) +3.0*f(5))/2.0;
    g4(4) = (  1.0*f(1) - 4.0*f(2) + 6.0*f(3) - 4.0*f(4)+ 1.0*f(5));
// 2
    g1(5) = ( +3.0*f(1) -16.0*f(2) +36.0*f(3) -48.0*f(4)+25.0*f(5))/12.0;
    g2(5) = (+11.0*f(1) -56.0*f(2)+114.0*f(3)-104.0*f(4)+35.0*f(5))/12.0;
    g3(5) = ( +3.0*f(1) -14.0*f(2) +24.0*f(3) -18.0*f(4) +5.0*f(5))/2.0;
    g4(5) = (  1.0*f(1) - 4.0*f(2) + 6.0*f(3) - 4.0*f(4)+ 1.0*f(5));

  if ( ! Equal(g1, f1) ) { fail = true;   s += " d1 |"; };
  if ( ! Equal(g2, f2) ) { fail = true;   s += " d2 |"; };
  if ( ! Equal(g3, f3) ) { fail = true;   s += " d3 |"; };
  if ( ! Equal(g4, f4) ) { fail = true;   s += " d4 |"; };

  FArr1D p0(-3,3);
  FArr1D p1(-3,3);
  for (int i=-3; i<=3;i++) p0(i) = 1;
  Smooth5(p0,p1);
  if ( ! Equal(p1, p0) ) { fail = true;   s += " n=0 |"; };
  for (int i=-3; i<=3;i++) p0(i) = i;
  Smooth5(p0,p1);
  if ( ! Equal(p1, p0) ) { fail = true;   s += " n=1 |"; };
  for (int i=-3; i<=3;i++) p0(i) = i*i;
  Smooth5(p0,p1);
  if ( ! Equal(p1, p0) ) { fail = true;   s += " n=2 |"; };

  if (fail) ss += " FAIL:" + s;
  else ss += " - OK";
  return ss;
};

String ComplexTest()
{
  String s = "", ss = "Complex Test";
  bool fail = false;

  Complex c0;
  Complex c1(2.3);
  Complex c2(3.3,3.2);

  if (! Equal(c0, 0.0, 0.0)) { fail = true; s += " constructor 0 |"; };

  if (! Equal(c1, 2.3, 0.0)) { fail = true; s += " constructor 1 |"; };

  if (! Equal(c2, 3.3, 3.2)) { fail = true; s += " constructor 2 |"; };

  c0 = 2.2;
  if (! Equal(c0, 2.2, 0.0)) { fail = true; s += " real assignment |"; };

  c1.Im = 1.1; c0 = c1;
  if (! Equal(c0, 2.3, 1.1)) { fail = true; s += " complex assignment |"; };

  c0 = c1 = c2;
  if (! Equal(c0, c2) || !Equal(c1, c2))
                                   { fail = true; s += " chain assignment |"; };

  c0 = c2 + 1.1;  c1 = 1.1 + c2;
  if (! Equal(c0, 4.4, 3.2) || !Equal(c1.Re, 4.4, 3.2))
                                       { fail = true; s += "real addition |"; };

  c0 = c2 - 1.1;  c1 = 1.1 - c2;
  if (! Equal(c0, 2.2, 3.2) || !Equal(c1, -2.2, -3.2))
                                    { fail = true; s += "real subtraction |"; };

  c0 = c2*2.0;  c1 = 2.0*c2;
  if (! Equal(c0, 6.6, 6.4) || !Equal(c1, 6.6, 6.4))
                                 { fail = true; s += "real multiplication |"; };
  c0 = c0/2.0;  c1 = mod2(c1)/c1;
  if (! Equal(c0, c2) || !Equal(c1, 6.6, -6.4))
                                       { fail = true; s += "real division |"; };

  if (fail) ss += " FAIL:" + s;
  else ss += " - OK";
  return ss;
};

⌨️ 快捷键说明

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