2019独角兽企业重金招聘Python工程师标准>>>

头文件:

/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com** 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*//******************************************************************************                               ccholesky.h** Class template of complex matrix Cholesky factorization.** For a conjugate symmetric, positive definite matrix A, this class computes* the Cholesky factorization, i.e. it computes a lower triangular matrix L* such that A = L*L^H. If the matrix is not conjugate symmetric or positive* definite, the class computes only a partial decomposition. This can be* tested with the isSpd() flag.** Zhang Ming, 2010-12, Xi'an Jiaotong University.*****************************************************************************/#ifndef CCHOLESKY_H
#define CCHOLESKY_H#include <matrix.h>namespace splab
{template <typename Type>class CCholesky{public:CCholesky();~CCholesky();bool isSpd() const;void dec( const Matrix<Type> &A );Matrix<Type> getL() const;Vector<Type> solve( const Vector<Type> &b );Matrix<Type> solve( const Matrix<Type> &B );private:bool spd;Matrix<Type> L;};//    class CCholesky#include <ccholesky-impl.h>}
// namespace splab#endif
// CCHOLESKY_H

实现文件:

/** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com** 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*//******************************************************************************                             ccholesky-impl.h** Implementation for CCholesky (complex Cholesky) class.** Zhang Ming, 2010-12, Xi'an Jiaotong University.*****************************************************************************//*** constructor and destructor*/
template<typename Type>
CCholesky<Type>::CCholesky() : spd(true)
{
}template<typename Type>
CCholesky<Type>::~CCholesky()
{
}/*** return true, if original matrix is symmetric positive-definite.*/
template<typename Type>
inline bool CCholesky<Type>::isSpd() const
{return spd;
}/*** Constructs a lower triangular matrix L, such that L*L^H= 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 <typename Type>
void CCholesky<Type>::dec( const Matrix<Type> &A )
{int m = A.rows();int n = A.cols();spd = (m == n);if( !spd )return;L = Matrix<Type>(A.cols(),A.cols());// main loopfor( int j=0; j<A.rows(); ++j ){Type d = 0;spd = spd && (imag(A[j][j]) == 0);for( int k=0; k<j; ++k ){Type s = 0;for( int i=0; i<k; ++i )s += L[k][i] * conj(L[j][i]);L[j][k] = s = (A[j][k]-s) / L[k][k];d = d + s*conj(s);spd = spd && (A[k][j] == conj(A[j][k]));}d = A[j][j] - d;spd = spd && ( real(d) > 0 );L[j][j] = sqrt( real(d) > 0 ? d : 0 );for( int k=j+1; k<A.rows(); ++k )L[j][k] = 0;}
}/*** return the lower triangular factor, L, such that L*L'=A.*/
template<typename Type>
inline Matrix<Type> CCholesky<Type>::getL() const
{return L;
}/*** Solve a linear system A*x = b, using the previously computed* cholesky factorization of A: L*L'.*/
template <typename Type>
Vector<Type> CCholesky<Type>::solve( const Vector<Type> &b )
{int n = L.rows();if( b.dim() != n )return Vector<Type>();Vector<Type> x = b;// solve L*y = bfor( int k=0; k<n; ++k ){for( int i=0; i<k; ++i )x[k] -= x[i]*L[k][i];x[k] /= L[k][k];}// solve L^H*x = yfor( int k=n-1; k>=0; --k ){for( int i=k+1; i<n; ++i )x[k] -= x[i]*conj(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 <typename Type>
Matrix<Type> CCholesky<Type>::solve( const Matrix<Type> &B )
{int n = L.rows();if( B.rows() != n )return Matrix<Type>();Matrix<Type> X = B;int nx = B.cols();// solve L*Y = Bfor( int j=0; j<nx; ++j )for( int k=0; k<n; ++k ){for( int i=0; i<k; ++i )X[k][j] -= X[i][j]*L[k][i];X[k][j] /= L[k][k];}// solve L^H*x = yfor( int j=0; j<nx; ++j )for( int k=n-1; k>=0; --k ){for( int i=k+1; i<n; ++i )X[k][j] -= X[i][j]*conj(L[i][k]);X[k][j] /= L[k][k];}return X;
}

测试代码:

/******************************************************************************                             ccholesky_test.cpp** Comnplex Cholesky class testing.** Zhang Ming, 2010-12, Xi'an Jiaotong University.*****************************************************************************/#define BOUNDS_CHECK#include <iostream>
#include <iomanip>
#include <ccholesky.h>using namespace std;
using namespace splab;typedef double  Type;
const   int     N = 5;int main()
{cout << setiosflags(ios::fixed) << setprecision(3);Matrix<complex<Type> > A(N,N), L(N,N);Vector<complex<Type> > b(N);for( int i=1; i<N+1; ++i ){for( int j=1; j<N+1; ++j )if( i == j )A(i,i) = complex<Type>(N+i,0);elseif( i < j )A(i,j) = complex<Type>(Type(i),cos(Type(i)));elseA(i,j) = complex<Type>(Type(j),-cos(Type(j)));b(i) = i*(i+1)/2 + i*(N-i);}cout << "The original matrix A : " << A << endl;CCholesky<complex<Type> > 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^H is : " << A - L*trH(L) << endl;Vector<complex<Type> > x = cho.solve(b);cout << "The constant vector b : " << b << endl;cout << "The solution of Ax = b : " << x << endl;cout << "The solution of Ax - b : " << A*x-b << endl;Matrix<complex<Type> > IA = cho.solve(eye(N,complex<Type>(1,0)));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
(6.000,0.000)   (1.000,0.540)   (1.000,0.540)   (1.000,0.540)   (1.000,0.540)(1.000,-0.540)  (7.000,0.000)   (2.000,-0.416)  (2.000,-0.416)  (2.000,-0.416)(1.000,-0.540)  (2.000,0.416)   (8.000,0.000)   (3.000,-0.990)  (3.000,-0.990)(1.000,-0.540)  (2.000,0.416)   (3.000,0.990)   (9.000,0.000)   (4.000,-0.654)(1.000,-0.540)  (2.000,0.416)   (3.000,0.990)   (4.000,0.654)   (10.000,0.000)The lower triangular matrix L is : size: 5 by 5
(2.449,0.000)   (0.000,0.000)   (0.000,0.000)   (0.000,0.000)   (0.000,0.000)(0.408,-0.221)  (2.605,0.000)   (0.000,0.000)   (0.000,0.000)   (0.000,0.000)(0.408,-0.221)  (0.685,0.160)   (2.700,0.000)   (0.000,0.000)   (0.000,0.000)(0.408,-0.221)  (0.685,0.160)   (0.848,0.367)   (2.727,0.000)   (0.000,0.000)(0.408,-0.221)  (0.685,0.160)   (0.848,0.367)   (0.893,0.240)   (2.753,0.000)A - L*L^H 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,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,0.000)
(9.000,0.000)
(12.000,0.000)
(14.000,0.000)
(15.000,0.000)The solution of Ax = b : size: 5 by 1
(0.356,-0.310)
(0.562,0.207)
(0.746,0.284)
(0.843,-0.037)
(0.842,-0.214)The solution of Ax - b : size: 5 by 1
(0.000,-0.000)
(-0.000,0.000)
(-0.000,0.000)
(-0.000,0.000)
(0.000,0.000)The inverse matrix of A : size: 5 by 5
(0.177,0.000)   (-0.018,-0.007) (-0.014,-0.004) (-0.008,-0.006) (-0.005,-0.007)(-0.018,0.007)  (0.164,-0.000)  (-0.024,0.013)  (-0.020,0.003)  (-0.017,-0.002)(-0.014,0.004)  (-0.024,-0.013) (0.160,-0.000)  (-0.032,0.018)  (-0.029,0.008)(-0.008,0.006)  (-0.020,-0.003) (-0.032,-0.018) (0.150,0.000)   (-0.043,0.012)(-0.005,0.007)  (-0.017,0.002)  (-0.029,-0.008) (-0.043,-0.012) (0.132,0.000)The product of  A*inv(A) : size: 5 by 5
(1.000,0.000)   (-0.000,-0.000) (-0.000,-0.000) (-0.000,-0.000) (0.000,-0.000)(0.000,-0.000)  (1.000,0.000)   (-0.000,0.000)  (0.000,-0.000)  (0.000,-0.000)(0.000,-0.000)  (-0.000,-0.000) (1.000,0.000)   (-0.000,0.000)  (-0.000,-0.000)(0.000,-0.000)  (0.000,0.000)   (-0.000,-0.000) (1.000,0.000)   (0.000,0.000)(0.000,-0.000)  (0.000,0.000)   (-0.000,0.000)  (0.000,0.000)   (1.000,-0.000)Process returned 0 (0x0)   execution time : 0.125 s
Press any key to continue.

转载于:https://my.oschina.net/zmjerry/blog/3725

复数矩阵Cholesky分解算法的C++实现相关推荐

  1. 复数矩阵QR分解算法的C++实现

    2019独角兽企业重金招聘Python工程师标准>>> 头文件: /** Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry ...

  2. 三十分钟理解:矩阵Cholesky分解,及其在求解线性方程组、矩阵逆的应用

    写一篇关于Cholesky分解的文章,作为学习笔记,尽量一文看懂矩阵Cholesky分解,以及用Cholesky分解来求解对称正定线性方程组,以及求对称正定矩阵的逆的应用. 文章目录 直接Choles ...

  3. 复数矩阵分解的拆解思路(矩阵求逆/特征值分解)

    作者:桂. 时间:2017-10-26  07:11:02 链接:http://www.cnblogs.com/xingshansi/p/7735016.html 前言 主要记录特征值分解的硬件实现思 ...

  4. 将两个实数矩阵合并为一个复数矩阵

    问题描述:有时需要把两个实数矩阵,一个作为实部,一个作为虚部,合并为一个复数矩阵,该如何操作? 解决办法: 假如是在第二个维度上进行合并(real: Data[:, 0, :, :] imag: Da ...

  5. C语言实现实数和复数矩阵及其各种运算(二)

    一.前言 由于实数矩阵的运算较简单,因此在本章中,我只给出复数矩阵的相关运算,一般的实数矩阵,类似炮制即可: 复数矩阵的加/减/乘运算涉及到其复数元胞(cell)的相加减运算,由于complex.h头 ...

  6. C语言实现实数和复数矩阵及其各种运算(一)

    一.前言 本连载文章主要内容是实现复数矩阵的各种运算,并利用matlab进行联写.联调,验证C语言编写的各个矩阵运算函数的正确性.其难点笔者认为在于矩阵的各种运算,C++中有Eigen库可用,以前在学 ...

  7. 复数矩阵的转置、共轭、共轭转置

    复数矩阵的转置分[共轭转置]和[点转置]两种,如果不加说明默认指的是[共轭转置]. 在 MATLAB 中,点转置运算.共轭运算和共轭转置运算分别如下所示:

  8. MATLAB中复数矩阵的转置、共轭及共轭转置

    参考博客:https://blog.csdn.net/zhaozhichenghpu/article/details/79162287 MATLAB中生成一个复数矩阵 MATLAB中复数矩阵的共轭用c ...

  9. 【打通复数域】复数矩阵的实数等效表示

    在通信中,大部分的问题都是复数问题, 而大部分其他领域的问题都是实数问题, 也导致许多方法直观上看并不能简单地拓展到复数域来解决通信的问题. 然而事实上, 绝大部分的实数算法都可以拓展到复数域,其核心 ...

最新文章

  1. HP 服务器 iLO 远程控制软件 介绍
  2. coba mysql_在Android Studio中将数据从MySQL数据库显示到TextView中-问答-阿里云开发者社区-阿里云...
  3. Hadoop 之父趣事:用儿子的大象玩偶为大数据项目命名
  4. 微信小程序 全局变量异步函数_微信小程序制作简述
  5. 给函数传递不定关键字的参数 和
  6. 【计算机网络】Internet原理与技术2(因特网的路由协议RIP、OSPF、BGP,网络地址转换NAT,网络协议IPv6)
  7. 区块链会计案例_区块链在会计领域的应用分析与研究
  8. 数据中台对企业意义和作用有哪些
  9. 李炎恢老师的javascript的讲义以及 附带着javascript手册
  10. 软件工程项目迭代周报(一)
  11. windows驱动开发——使用sys文件
  12. JVM虚拟机基础知识(JVM位置、类加载生命周期、堆、元空间、jvm常用参数)
  13. 微服务设计指导-使用云原生微服务解决传统海量跑批时引起的系统间“级联雪崩”以及效率
  14. 什么是5G LAN 5G LAN商用爆发推动5G创新应用 提速数字转型新引擎
  15. android在framework层增加自己的service仿照GPS
  16. oracle语句查询时间范围
  17. android中怎么设置组件在LinearLayout中居中
  18. 第1关:knn算法概述
  19. 使用bootstrap时下拉菜单失效问题解决
  20. centos7搭建j2EE前后端分离集群常用命令

热门文章

  1. 图的存储结构之邻接表(详解)
  2. [Angular 2] Using events and refs
  3. android 4 高级编程 第一章摘
  4. 浅谈linux性能调优之六:IO调度算法的选择
  5. OSX下解决PIL的IOError: decoder jpeg not available 问题
  6. 把字符串复制到剪贴板
  7. 聚类性能度量指标及距离计算
  8. 一个好的大数据分析软件包含哪些功能
  9. 大数据平台有什么功能作用
  10. websocket php apache,PHP第一篇:PHP WebSocket实现前后端数据交互,亲测可用(windows+ apache2.4 +php5.6 )...