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

📄 testnumerics.cpp

📁 《3D游戏引擎设计》的源码
💻 CPP
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License.html/free.pdf

#include <MgcCore.pkg>
#include <MgcNumerics.pkg>

//----------------------------------------------------------------------------
void TestEigensystem ()
{
    MgcEigen kES(3);

    kES.Matrix(0,0) = 2.0;  kES.Matrix(0,1) = 1.0;  kES.Matrix(0,2) = 1.0;
    kES.Matrix(1,0) = 1.0;  kES.Matrix(1,1) = 2.0;  kES.Matrix(1,2) = 1.0;
    kES.Matrix(2,0) = 1.0;  kES.Matrix(2,1) = 1.0;  kES.Matrix(2,2) = 2.0;

    kES.IncrSortEigenStuff3();

    cout.setf(ios::fixed);

    cout << "eigenvalues = " << endl;
    int iRow;
    for (iRow = 0; iRow < 3; iRow++)
        cout << kES.GetEigenvalue(iRow) << ' ';
    cout << endl;

    cout << "eigenvectors = " << endl;
    for (iRow = 0; iRow < 3; iRow++)
    {
        for (int iCol = 0; iCol < 3; iCol++)
            cout << kES.GetEigenvector(iRow,iCol) << ' ';
        cout << endl;
    }

    // eigenvalues =
    //    1.000000 1.000000 4.000000
    // eigenvectors =
    //    0.411953  0.704955 0.577350
    //    0.404533 -0.709239 0.577350
    //   -0.816485  0.004284 0.577350
}
//----------------------------------------------------------------------------
MgcReal FBisect1 (MgcReal fX)
{
    static const MgcReal gs_fRootHalf = MgcMath::Sqrt(0.5);
    return fX - gs_fRootHalf;
}

void TestBisect1 ()
{
    // Bisection should produce a root at (r2)
    // Output:
    // root (+0.707031)
    // func (-0.000076)

    MgcBisect1 kBisector(FBisect1,16,1e-04);
    MgcReal fRoot;
    bool bResult = kBisector.Bisect(0.0,1.0,fRoot);
}
//----------------------------------------------------------------------------
MgcReal FBisect2 (MgcReal fX, MgcReal fY)
{
    static const MgcReal gs_fRootHalf = MgcMath::Sqrt(0.5);
    return fX - gs_fRootHalf;
}

MgcReal GBisect2 (MgcReal fX, MgcReal fY)
{
    static const MgcReal gs_fRootThird = MgcMath::Sqrt(1.0/3.0);
    return fY - gs_fRootThird;
}

void TestBisect2 ()
{
    // Bisection should produce a root at (r2,r3).
    // Output:
    // root (+0.707031,+0.577393)
    // func (-0.000076,+0.000042)

    MgcBisect2 kBisector(FBisect2,GBisect2,16,1e-04);
    MgcReal fXRoot, fYRoot;
    bool bResult = kBisector.Bisect(0.0,0.0,1.0,1.0,fXRoot,fYRoot);
}
//----------------------------------------------------------------------------
MgcReal FBisect3 (MgcReal fX, MgcReal fY, MgcReal fZ)
{
    static const MgcReal gs_fRootHalf = MgcMath::Sqrt(0.5);
    return fX - gs_fRootHalf;
}

MgcReal GBisect3 (MgcReal fX, MgcReal fY, MgcReal fZ)
{
    static const MgcReal gs_fRootThird = MgcMath::Sqrt(1.0/3.0);
    return fY - gs_fRootThird;
}

MgcReal HBisect3 (MgcReal fX, MgcReal fY, MgcReal fZ)
{
    static const MgcReal gs_fRootFifth = MgcMath::Sqrt(1.0/5.0);
    return fZ - gs_fRootFifth;
}

void TestBisect3 ()
{
    // Bisection should produce a root at (r2,r3,r5).
    // Output:
    // root (+0.707031,+0.577393,+0.447266)
    // func (-0.000076,+0.000042,+0.000052)

    MgcBisect3 kBisector(FBisect3,GBisect3,HBisect3,16,1e-04);
    MgcReal fXRoot, fYRoot, fZRoot;
    bool bResult = kBisector.Bisect(0.0,0.0,0.0,1.0,1.0,1.0,fXRoot,fYRoot,
        fZRoot);
}
//----------------------------------------------------------------------------
MgcReal FMin1Test (MgcReal fT, void*)
{
    MgcReal fTSqr = fT*fT;
    MgcReal fTmp = 1.0 - fTSqr;
    return fTmp*fTmp;
}

void TestMinimize1D ()
{
    unsigned int uiMaxLevel = 8;
    unsigned int uiMaxBracket = 8;
    MgcMinimize1D kMinimize(FMin1Test,uiMaxLevel,uiMaxBracket);

    MgcReal fT0 = 0.0;
    MgcReal fT1 = 1.01;
    MgcReal fTInitial = 0.75;
    MgcReal fTMin, fFMin;

    kMinimize.GetMinimum(fT0,fT1,fTInitial,fTMin,fFMin);
}
//----------------------------------------------------------------------------
MgcReal FMinNTest (const MgcReal* afT, void*)
{
    return afT[0]*afT[0] + afT[1]*afT[1];
}

void TestMinimizeND ()
{
    unsigned int uiMaxLevel = 8;
    unsigned int uiMaxBracket = 8;
    unsigned int uiMaxIterations = 32;
    MgcMinimizeND kMinimize(2,FMinNTest,uiMaxLevel,uiMaxBracket,
        uiMaxIterations);

    MgcReal afT0[2] = { -0.25, -0.10 };
    MgcReal afT1[2] = { 0.75, 1.00 };
    MgcReal afTInitial[2] = { 0.5, 0.5 };
    MgcReal afTMin[2], fFMin;

    kMinimize.GetMinimum(afT0,afT1,afTInitial,afTMin,fFMin);
}
//----------------------------------------------------------------------------
int main ()
{
    //TestEigensystem();
    //TestBisect1();
    //TestBisect2();
    //TestBisect3();
    //TestMinimize1D();
    TestMinimizeND();
    return 0;
}
//----------------------------------------------------------------------------

⌨️ 快捷键说明

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