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

📄 test_ldl_cholesky.cxx

📁 DTMK软件开发包,此为开源软件,是一款很好的医学图像开发资源.
💻 CXX
字号:
// This is core/vnl/algo/tests/test_ldl_cholesky.cxx
#include <testlib/testlib_test.h>
#include <vcl_iostream.h>
#include <vnl/vnl_matrix.h>
#include <vnl/vnl_diag_matrix.h>
#include <vnl/algo/vnl_ldl_cholesky.h>
#include <vnl/algo/vnl_svd.h>
#include <vnl/vnl_random.h>

#include "test_util.h"

void test_ldl_cholesky()
{
  vnl_random rng(1000);
  vnl_matrix<double> A(3,3);
  test_util_fill_random(A.begin(), A.end(), rng);
  A = A * A.transpose();

  vnl_matrix<double> I(3,3);
  I.set_identity();

  {
    vnl_ldl_cholesky chol(A);
    vnl_matrix<double> A2 = chol.lower_triangle() * vnl_diag_matrix<double>(chol.diagonal()) * chol.upper_triangle();
    testlib_test_assert_near("LDL'=A",(A-A2).fro_norm());
  }
  {
    // Test the rank-1 update
    vnl_vector<double> v(3);
    vnl_matrix<double> Mv(3,1);
    test_util_fill_random(v.begin(), v.end(), rng);
    Mv.set_column(0,v);
    vnl_ldl_cholesky chol(A);
    chol.rank1_update(v);
    vnl_matrix<double> A2 = A + Mv*Mv.transpose();
    vnl_matrix<double> A3 = chol.lower_triangle() * vnl_diag_matrix<double>(chol.diagonal()) * chol.upper_triangle();
    testlib_test_assert_near("Rank 1 update",(A2-A3).fro_norm());
  }
  {
    // Test the rank 2 update
    vnl_matrix<double> W(3,2);
    test_util_fill_random(W.begin(), W.end(), rng);
    vnl_ldl_cholesky chol(A);
    chol.update(W);
    vnl_matrix<double> A2 = A + W*W.transpose();
    vnl_matrix<double> A3 = chol.lower_triangle() * vnl_diag_matrix<double>(chol.diagonal()) * chol.upper_triangle();
    testlib_test_assert_near("Rank 2 update",(A2-A3).fro_norm());
  }
  {
    // Test the rank 4 update
    vnl_matrix<double> W(3,4);
    test_util_fill_random(W.begin(), W.end(), rng);
    vnl_ldl_cholesky chol(A);
    chol.update(W);
    vcl_cout<<"Adding: "<<W*W.transpose()<<vcl_endl;
    vnl_matrix<double> A2 = A + W*W.transpose();
    vnl_matrix<double> A3 = chol.lower_triangle() * vnl_diag_matrix<double>(chol.diagonal()) * chol.upper_triangle();
    testlib_test_assert_near("Rank 2 update",(A2-A3).fro_norm());
  }

  {
    vnl_ldl_cholesky chol(A);
    vnl_svd<double> svd(A);
    vcl_cout << "cholesky inverse:\n" << chol.inverse() << '\n'
             << "svd inverse:\n" << svd.inverse() << '\n';
    testlib_test_assert_near("svd.inverse() ~= cholesky.inverse()",
                             (chol.inverse() - svd.inverse()).fro_norm());
  }
  {
    vnl_ldl_cholesky chol(A);
    testlib_test_assert_near("Ai * A - I", (chol.inverse() * A - I).fro_norm());
    testlib_test_assert_near("Ai * A - I", (A * chol.inverse() - I).fro_norm());
  }
  {
    vnl_ldl_cholesky chol(A, vnl_ldl_cholesky::estimate_condition);
    testlib_test_assert_near("Ai * A - I", (chol.inverse() * A - I).fro_norm());
    testlib_test_assert_near("Ai * A - I", (A * chol.inverse() - I).fro_norm());
  }

  {
    vnl_vector<double> b(3),x0(3),x;
    test_util_fill_random(x0.begin(), x0.end(), rng);
    b=A*x0;
    vnl_ldl_cholesky chol(A);
    x=chol.solve(b);
    testlib_test_assert_near("Solve Ax=b",(x-x0).one_norm(),0,1e-6);
  }
  {
    vnl_vector<double> b(3),x0(3),x;
    test_util_fill_random(x0.begin(), x0.end(), rng);
    vnl_ldl_cholesky chol(A);
    b=chol.lower_triangle()*x0;
    x=b;
    chol.solve_lx(x);
    testlib_test_assert_near("Solve Lx=b",(x-x0).one_norm(),0,1e-6);
  }
  {
    vnl_ldl_cholesky chol(A);
    vnl_vector<double> v(3);
    test_util_fill_random(v.begin(), v.end(), rng);

    double res1 = chol.xt_m_inv_x(v);
    double res2 = dot_product(v,chol.inverse()*v);

    testlib_test_assert_near("x' * inv(M) * x",res1,res2);
  }
  {
    vnl_ldl_cholesky chol(A);
    vnl_vector<double> v(3);
    test_util_fill_random(v.begin(), v.end(), rng);

    double res1 = chol.xt_m_x(v);
    double res2 = dot_product(v,A*v);

    testlib_test_assert_near("x' * M * x",res1,res2);
  }

}

TESTMAIN(test_ldl_cholesky);

⌨️ 快捷键说明

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