cholesky分解java代码,实数矩阵Cholesky分解算法的C++实现

cholesky分解java代码,实数矩阵Cholesky分解算法的C++实现头文件 Copyright c 2008 2011ZhangMin M Zhang programisfre youcanredist ormodifyit undertheterm

头文件:

/*

* Copyright (c) 2008-2011 Zhang Ming (M. Zhang),

*

* This program is free software; you can redistribute it and/or modify it

* under the terms of the GNU General Public License as published by the

* Free Software Foundation, either version 2 or any later version.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions are met:

*

* 1. Redistributions of source code must retain the above copyright notice,

* this list of conditions and the following disclaimer.

*

* 2. Redistributions in binary form must reproduce the above copyright

* notice, this list of conditions and the following disclaimer in the

* documentation and/or other materials provided with the distribution.

*

* This program is distributed in the hope that it will be useful, but WITHOUT

* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or

* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for

* more details. A copy of the GNU General Public License is available at:

* http://www.fsf.org/licensing/licenses

*/

/*

* cholesky.h

*

* Class template of Cholesky decomposition.

*

* For a symmetric, positive definite matrix A, this function computes the

* Cholesky factorization, i.e. it computes a lower triangular matrix L such

* that A = L*L’. If the matrix is not symmetric or positive definite, the

* function computes only a partial decomposition. This can be tested with

* the isSpd() flag.

*

* This class also supports factorization of complex matrix by specializing

* some member functions.

*

* Adapted form Template Numerical Toolkit.

*

* Zhang Ming, 2010-01 (revised 2010-12), Xi’an Jiaotong University.

*/

#ifndef CHOLESKY_H

#define CHOLESKY_H

#include

namespace splab

{

template

class Cholesky

{

public:

Cholesky();

~Cholesky();

bool isSpd() const;

void dec( const Matrix &A );

Matrix getL() const;

Vector solve( const Vector &b );

Matrix solve( const Matrix &B );

private:

bool spd;

Matrix L;

};

//class Cholesky

#include

}

// namespace splab

#endif

// CHOLESKY_H

实现文件:

/*

* Copyright (c) 2008-2011 Zhang Ming (M. Zhang),

*

* This program is free software; you can redistribute it and/or modify it

* under the terms of the GNU General Public License as published by the

* Free Software Foundation, either version 2 or any later version.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions are met:

*

* 1. Redistributions of source code must retain the above copyright notice,

* this list of conditions and the following disclaimer.

*

* 2. Redistributions in binary form must reproduce the above copyright

* notice, this list of conditions and the following disclaimer in the

* documentation and/or other materials provided with the distribution.

*

* This program is distributed in the hope that it will be useful, but WITHOUT

* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or

* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for

* more details. A copy of the GNU General Public License is available at:

* http://www.fsf.org/licensing/licenses

*/

/*

* cholesky-impl.h

*

* Implementation for Cholesky class.

*

* Zhang Ming, 2010-01 (revised 2010-12), Xi’an Jiaotong University.

*/

/

* constructor and destructor

*/

template

Cholesky::Cholesky() : spd(true)

{

}

template

Cholesky::~Cholesky()

{

}

/

* return true, if original matrix is symmetric positive-definite.

*/

template

inline bool Cholesky::isSpd() const

{

return spd;

}

/

* Constructs a lower triangular matrix L, such that L*L’= A.

* If A is not symmetric positive-definite (SPD), only a partial

* factorization is performed. If isspd() evalutate true then

* the factorizaiton was successful.

*/

template

void Cholesky::dec( const Matrix &A )

{

int m = A.rows();

int n = A.cols();

spd = (m == n);

if( !spd )

return;

L = Matrix(n,n);

// main loop.

for( int j=0; j

{

Type d = 0;

for( int k=0; k

{

Type s = 0;

for( int i=0; i

s += L[k][i]*L[j][i];

L[j][k] = s = (A[j][k]-s) / L[k][k];

d = d + s*s;

spd = spd && (A[k][j] == A[j][k]);

}

d = A[j][j] – d;

spd = spd && ( d > 0 );

L[j][j] = sqrt( d > 0 ? d : 0 );

for( int k=j+1; k

L[j][k] = 0;

}

}

/

* return the lower triangular factor, L, such that L*L’=A.

*/

template

inline Matrix Cholesky::getL() const

{

return L;

}

/

* Solve a linear system A*x = b, using the previously computed

* cholesky factorization of A: L*L’.

*/

template

Vector Cholesky::solve( const Vector &b )

{

int n = L.rows();

if( b.dim() != n )

return Vector();

Vector x = b;

// solve L*y = b

for( int k=0; k

{

for( int i=0; i

x[k] -= x[i]*L[k][i];

x[k] /= L[k][k];

}

// solve L’*x = y

for( int k=n-1; k>=0; –k )

{

for( int i=k+1; i

x[k] -= x[i]*L[i][k];

x[k] /= L[k][k];

}

return x;

}

/

* Solve a linear system A*X = B, using the previously computed

* cholesky factorization of A: L*L’.

*/

template

Matrix Cholesky::solve( const Matrix &B )

{

int n = L.rows();

if( B.rows() != n )

return Matrix();

Matrix X = B;

int nx = B.cols();

// solve L*Y = B

for( int j=0; j

for( int k=0; k

{

for( int i=0; i

X[k][j] -= X[i][j]*L[k][i];

X[k][j] /= L[k][k];

}

// solve L’*X = Y

for( int j=0; j

for( int k=n-1; k>=0; –k )

{

for( int i=k+1; i

X[k][j] -= X[i][j]*L[i][k];

X[k][j] /= L[k][k];

}

return X;

}

/

* Main loop of specialized member function. This macro definition is

* aimed at avoiding code duplication.

*/

#define MAINLOOP \

{ \

int m = A.rows(); \

int n = A.cols(); \

\

spd = (m == n); \

if( !spd ) \

return; \

\

for( int j=0; j

{ \

spd = spd && (imag(A[j][j]) == 0); \

d = 0; \

\

for( int k=0; k

{ \

s = 0; \

for( int i=0; i

s += L[k][i] * conj(L[j][i]); \

\

L[j][k] = s = (A[j][k]-s) / L[k][k]; \

d = d + norm(s); \

spd = spd && (A[k][j] == conj(A[j][k])); \

} \

\

d = real(A[j][j]) – d; \

spd = spd && ( d > 0 ); \

\

L[j][j] = sqrt( d > 0 ? d : 0 ); \

for( int k=j+1; k

L[j][k] = 0; \

} \

}

/

* Solving process of specialized member function. This macro definition is

* aimed at avoiding code duplication.

*/

#define SOLVE1 \

{ \

for( int k=0; k

{ \

for( int i=0; i

x[k] -= x[i]*L[k][i]; \

\

x[k] /= L[k][k]; \

} \

\

for( int k=n-1; k>=0; –k ) \

{ \

for( int i=k+1; i

x[k] -= x[i]*conj(L[i][k]); \

\

x[k] /= L[k][k]; \

} \

\

return x; \

}

/

* Solving process of specialized member function. This macro definition is

* aimed at avoiding code duplication.

*/

#define SOLVE2 \

{ \

int nx = B.cols(); \

for( int j=0; j

for( int k=0; k

{ \

for( int i=0; i

X[k][j] -= X[i][j]*L[k][i]; \

\

X[k][j] /= L[k][k]; \

} \

\

for( int j=0; j

for( int k=n-1; k>=0; –k ) \

{ \

for( int i=k+1; i

X[k][j] -= X[i][j]*conj(L[i][k]); \

\

X[k][j] /= L[k][k]; \

} \

\

return X; \

}

/

* Specializing for “dec” member function.

*/

template <>

void Cholesky >::dec( const Matrix > &A )

{

float d;

complex s;

L = Matrix >(A.cols(),A.cols());

MAINLOOP;

}

template <>

void Cholesky >::dec( const Matrix > &A )

{

double d;

complex s;

L = Matrix >(A.cols(),A.cols());

MAINLOOP;

}

template <>

void Cholesky >::dec( const Matrix > &A )

{

long double d;

complex s;

L = Matrix >(A.cols(),A.cols());

MAINLOOP;

}

/

* Specializing for “solve” member function.

*/

template <>

Vector > Cholesky >::solve( const Vector > &b )

{

int n = L.rows();

if( b.dim() != n )

return Vector >();

Vector > x = b;

SOLVE1

}

template <>

Vector > Cholesky >::solve( const Vector > &b )

{

int n = L.rows();

if( b.dim() != n )

return Vector >();

Vector > x = b;

SOLVE1

}

template <>

Vector > Cholesky >::solve( const Vector > &b )

{

int n = L.rows();

if( b.dim() != n )

return Vector >();

Vector > x = b;

SOLVE1

}

/

* Specializing for “solve” member function.

*/

template <>

Matrix > Cholesky >::solve( const Matrix > &B )

{

int n = L.rows();

if( B.rows() != n )

return Matrix >();

Matrix > X = B;

SOLVE2

}

template <>

Matrix > Cholesky >::solve( const Matrix > &B )

{

int n = L.rows();

if( B.rows() != n )

return Matrix >();

Matrix > X = B;

SOLVE2

}

template <>

Matrix > Cholesky >::solve( const Matrix > &B )

{

int n = L.rows();

if( B.rows() != n )

return Matrix >();

Matrix > X = B;

SOLVE2

}

测试代码:

/*

* cholesky_test.cpp

*

* Cholesky class testing.

*

* Zhang Ming, 2010-01 (revised 2010-12), Xi’an Jiaotong University.

*/

#define BOUNDS_CHECK

#include

#include

#include

using namespace std;

using namespace splab;

typedef double Type;

const int N = 5;

int main()

{

Matrix A(N,N), L(N,N);

Vector b(N);

for( int i=1; i

{

for( int j=1; j

if( i == j )

A(i,i) = i;

else

if( i < j )

A(i,j) = i;

else

A(i,j) = j;

b(i) = i*(i+1)/2.0 + i*(N-i);

}

cout << setiosflags(ios::fixed) << setprecision(3);

cout << “The original matrix A : ” << A << endl;

Cholesky cho;

cho.dec(A);

if( !cho.isSpd() )

cout << “Factorization was not complete.” << endl;

else

{

L = cho.getL();

cout << “The lower triangular matrix L is : ” << L << endl;

cout << “A – L*L^T is : ” << A – L*trT(L) << endl;

Vector x = cho.solve(b);

cout << “The constant vector b : ” << b << endl;

cout << “The solution of Ax = b : ” << x << endl;

cout << “The Ax – b : ” << A*x-b << endl;

Matrix IA = cho.solve(eye(N,Type(1)));

cout << “The inverse matrix of A : ” << IA << endl;

cout << “The product of A*inv(A) : ” << A*IA << endl;

}

return 0;

}

运行结果:

The original matrix A : size: 5 by 5

1.000 1.000 1.000 1.000 1.000

1.000 2.000 2.000 2.000 2.000

1.000 2.000 3.000 3.000 3.000

1.000 2.000 3.000 4.000 4.000

1.000 2.000 3.000 4.000 5.000

The lower triangular matrix L is : size: 5 by 5

1.000 0.000 0.000 0.000 0.000

1.000 1.000 0.000 0.000 0.000

1.000 1.000 1.000 0.000 0.000

1.000 1.000 1.000 1.000 0.000

1.000 1.000 1.000 1.000 1.000

A – L*L^T is : size: 5 by 5

0.000 0.000 0.000 0.000 0.000

0.000 0.000 0.000 0.000 0.000

0.000 0.000 0.000 0.000 0.000

0.000 0.000 0.000 0.000 0.000

0.000 0.000 0.000 0.000 0.000

The constant vector b : size: 5 by 1

5.000

9.000

12.000

14.000

15.000

The solution of Ax = b : size: 5 by 1

1.000

1.000

1.000

1.000

1.000

The Ax – b : size: 5 by 1

0.000

0.000

0.000

0.000

0.000

The inverse matrix of A : size: 5 by 5

2.000 -1.000 0.000 0.000 0.000

-1.000 2.000 -1.000 0.000 0.000

0.000 -1.000 2.000 -1.000 0.000

0.000 0.000 -1.000 2.000 -1.000

0.000 0.000 0.000 -1.000 1.000

The product of A*inv(A) : size: 5 by 5

1.000 0.000 0.000 0.000 0.000

0.000 1.000 0.000 0.000 0.000

0.000 0.000 1.000 0.000 0.000

0.000 0.000 0.000 1.000 0.000

0.000 0.000 0.000 0.000 1.000

Process returned 0 (0x0) execution time : 0.109 s

Press any key to continue.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233257.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C#下怎样处理并保存单色格式PNG图片?

    C#下怎样处理并保存单色格式PNG图片?  用微软自带的画图,打开一个已经存在的单色PNG图片文件,然后复制图像粘贴上去,做点反色或其他处理再保存,可以得到黑白单色PNG图片;但是,如果有很多黑底白字的图片要想改成白纸黑字的单色PNG格式保存这就很麻烦了,譬如2,3百张BMP或JPG图片用来保存只有白纸黑字的书页真是浪费。可是,有些索引格式图像如单色位图,或者单色PNG,如果用C#的Graphics类处理之后,保存文件只能得

    2022年7月21日
    11
  • 双非本科22届暑期实习,成功拿到B站、阿里实习offer[通俗易懂]

    双非本科22届暑期实习,成功拿到B站、阿里实习offer[通俗易懂]拼一把不一定成功,但是不试试看肯定没有结果!1.前言想写这篇文章很久了,也有粉丝留言、私信问我打卡系列怎么断更了这么多天(狗头保命),首先给大家解释一下最近为什么“失踪了”?由于近两周要入职,找租房,整理微信公众号,所以没多少时间写博客,今天难得闲下来,做一篇近期总结给大家。关于交流群:有粉丝私信,建议创建一个学习群,大家互相分享校招经验,学习心得(我因为怕管理群太麻烦,而一拖再拖,不过也好歹建群了),大家可以通过我的博客首页关注一波公众号:兴趣使然的草帽路飞去获取交流群和内推群群.

    2022年5月21日
    48
  • 从#65279字符看dede模板页面编码问题

    从#65279字符看dede模板页面编码问题

    2021年9月25日
    67
  • 医咖会SPSS免费教程学习笔记—2*C卡方检验

    医咖会SPSS免费教程学习笔记—2*C卡方检验1.2C卡方检验需要满足的假设:(1)观测变量是二分类变量(2)有多个分组(3)观测值相互独立(4)任意单元格的期望频数大于52.2C卡方检验的组间比较请依次点击:分析—描述统计—交叉表—将变量分别拖入行和列—点击右侧“统计”—选择“卡方”—继续点击右侧“单元格”—选择计数下的“实测”,百分比下的“列”,勾选z检验选择调整p值(邦弗仑尼法)3.结果解读两两比较有无差异,看输出的交叉表中计数下标是否一致。若一致,则无差异;否则,有差异总体有无差异,看输出的卡方检验表格中的显著性水平…

    2022年5月17日
    42
  • Redis集群搭建

    Redis集群搭建

    2021年6月15日
    110
  • linux 防火墙开放端口_防火墙放行端口

    linux 防火墙开放端口_防火墙放行端口Linux防火墙常用操作及端口开放1.查看防火墙状态firewall-cmd–state2.开启防火墙systemctlstartfirewalld.service3.开启指定端口firewall-cmd–zone=public–add-port=3306/tcp–permanentfirewall-cmd–zone=public–add-port=6379/tcp–permanent显示success表示成功–zone=public表示作用域为公共的

    2022年9月16日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号