C#,码海拾贝(15)——“对称正定矩阵”的求逆和“托伯利兹矩阵”求逆的“埃兰特”方法之C#源代码,《C#数值计算算法编程》源代码升级改进版
对称正定矩阵
在线性代数中,正定矩阵的性质类似复数中的正实数。与正定矩阵相对应的线性算子是对称正定双线性形式。
正定矩阵的行列式恒为正;实对称矩阵A正定当且仅当A与单位矩阵合同;若A是正定矩阵,则A的逆矩阵也是正定矩阵;两个正定矩阵的和是正定矩阵;正实数与正定矩阵的乘积是正定矩阵。
对于n阶实对称矩阵A,下列条件是等价的:A是正定矩阵;A的一切顺序主子式均为正;A的一切主子式均为正;A的特征值均为正。
对于具体的实对称矩阵,常用矩阵的各阶顺序主子式是否大于零来判断其正定性;对于抽象的矩阵,由给定矩阵的正定性,利用标准型,特征值及充分必要条件来证相关矩阵的正定性。
In linear algebra, the properties of positive definite matrices are similar to positive real numbers in complex numbers. The linear operator corresponding to a positive definite matrix is a symmetric positive definite bilinear form.
The determinant of a positive definite matrix is always positive; Real symmetric matrix A is positive definite if and only if A is congruent with identity matrix; If A is a positive definite matrix, then the inverse matrix of A is also a positive definite matrix; The sum of two positive definite matrices is a positive definite matrix; The product of positive real numbers and positive definite matrices is a positive definite matrix.
For n-order real symmetric matrix A, the following conditions are equivalent: A is a positive definite matrix; All sequential principal sub equations of A are positive; All principal and sub formulas of A are positive; The eigenvalues of A are all positive.
For a specific real symmetric matrix, it is commonly used to determine its positivity by determining whether the order of the main and sub expressions of each order of the matrix is greater than zero; For abstract matrices, the positive definiteness of the given matrix is demonstrated using the canonical form, eigenvalues, and necessary and sufficient conditions.
对称正定矩阵的求逆
using System;
namespace Zhou.CSharp.Algorithm
{
/// <summary>
/// 矩阵类
/// 作者:周长发
/// 改进:深度混淆
/// https://blog.csdn.net/beijinghorn
/// </summary>
public partial class Matrix
{
/// <summary>
/// 对称正定矩阵的求逆
/// </summary>
/// <param name="src">源矩阵</param>
/// <returns>求逆是否成功</returns>
public static bool InvertSsgj(Matrix src)
{
int i, j, k, m;
double w, g;
// 临时内存
double[] pTmp = new double[src.Columns];
// 逐列处理
for (k = 0; k <= src.Columns - 1; k++)
{
w = src[0];
if (Math.Abs(w) < float.Epsilon)
{
return false;
}
m = src.Columns - k - 1;
for (i = 1; i <= src.Columns - 1; i++)
{
g = src[i * src.Columns];
pTmp[i] = g / w;
if (i <= m)
{
pTmp[i] = -pTmp[i];
}
for (j = 1; j <= i; j++)
{
src[(i - 1) * src.Columns + j - 1] = src[i * src.Columns + j] + g * pTmp[j];
}
}
src[src.Columns * src.Columns - 1] = 1.0 / w;
for (i = 1; i <= src.Columns - 1; i++)
{
src[(src.Columns - 1) * src.Columns + i - 1] = pTmp[i];
}
}
// 行列调整
for (i = 0; i <= src.Columns - 2; i++)
{
for (j = i + 1; j <= src.Columns - 1; j++)
{
src[i * src.Columns + j] = src[j * src.Columns + i];
}
}
return true;
}
}
}
托伯利兹矩阵
托伯利兹矩阵求逆的埃兰特方法
Toeplitz矩阵(diagonal-constant matrix),指矩阵中每条自左上至右下的斜线上的元素相同。
对于方阵,Toeplitz方阵可以描述为:任一条平行于主对角线的直线上的元素相同。
matlab中生成托普利兹矩阵的函数是toeplitz(x,y),它生成一个以x为第一列,y为第一行的托普利兹矩阵。这里x, y均为向量,两者不必等长。
toeplitz(x)用向量x生成一个对称的托普利兹矩阵。
例如:T=toeplitz(1:6)
Toeplitz matrix refers to a diagonal constant matrix in which the elements on each diagonal line from top left to bottom right are the same.
For a square matrix, a Toeplitz square matrix can be described as: the elements on any straight line parallel to the main diagonal are the same.
The function used in MATLAB to generate a Toplitz matrix is toeplitz (x, y), which generates a Toplitz matrix with x as the first column and y as the first row. Here, x and y are both vectors, and they do not need to be of equal length.
Toeplitz (x) generates a symmetric Toeplitz matrix using the vector x.
For example: T=toeplitz (1:6)
using System;
namespace Zhou.CSharp.Algorithm
{
/// <summary>
/// 矩阵类
/// 作者:周长发
/// 改进:深度混淆
/// https://blog.csdn.net/beijinghorn
/// </summary>
public partial class Matrix
{
/// <summary>
/// 托伯利兹矩阵求逆的埃兰特方法
/// </summary>
/// <param name="src">源矩阵</param>
/// <returns>求逆是否成功</returns>
public static bool InvertTrench(Matrix src)
{
int i, j, k;
double a, s;
// 上三角元素
double[] t = new double[src.Columns];
// 下三角元素
double[] tt = new double[src.Columns];
// 上、下三角元素赋值
for (i = 0; i < src.Columns; ++i)
{
t[i] = src.GetElement(0, i);
tt[i] = src.GetElement(i, 0);
}
// 临时缓冲区
double[] c = new double[src.Columns];
double[] r = new double[src.Columns];
double[] p = new double[src.Columns];
// 非Toeplitz矩阵,返回
if (Math.Abs(t[0]) < float.Epsilon)
{
return false;
}
a = t[0];
c[0] = tt[1] / t[0];
r[0] = t[1] / t[0];
for (k = 0; k <= src.Columns - 3; k++)
{
s = 0.0;
for (j = 1; j <= k + 1; j++)
{
s = s + c[k + 1 - j] * tt[j];
}
s = (s - tt[k + 2]) / a;
for (i = 0; i <= k; i++)
{
p[i] = c[i] + s * r[k - i];
}
c[k + 1] = -s;
s = 0.0;
for (j = 1; j <= k + 1; j++)
{
s = s + r[k + 1 - j] * t[j];
}
s = (s - t[k + 2]) / a;
for (i = 0; i <= k; i++)
{
r[i] = r[i] + s * c[k - i];
c[k - i] = p[k - i];
}
r[k + 1] = -s;
a = 0.0;
for (j = 1; j <= k + 2; j++)
{
a = a + t[j] * c[j - 1];
}
a = t[0] - a;
// 求解失败
if (Math.Abs(a) < float.Epsilon)
{
return false;
}
}
src[0] = 1.0 / a;
for (i = 0; i <= src.Columns - 2; i++)
{
k = i + 1;
j = (i + 1) * src.Columns;
src[k] = -r[i] / a;
src[j] = -c[i] / a;
}
for (i = 0; i <= src.Columns - 2; i++)
{
for (j = 0; j <= src.Columns - 2; j++)
{
k = (i + 1) * src.Columns + j + 1;
src[k] = src[i * src.Columns + j] - c[i] * src[j + 1];
src[k] = src[k] + c[src.Columns - j - 2] * src[src.Columns - i - 1];
}
}
return true;
}
}
}
相关文章
- C# String 前面不足位数补零的方法
- C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- C#【高级篇】StructLayout特性是什么?怎么用?
- C#【中级篇】 C# 方法
- C#【特性篇】C#4.0的Dynamic特性-using System.Dynamic
- C#,码海拾贝(20)——一般实矩阵的奇异值分解(Singular Value Decomposition)方法之C#源代码,《C#数值计算算法编程》源代码升级改进版
- C#,精巧实用的代码,图片像素(Bitmap Pixel)的读取与绘制的快速方法与源程序
- c#中将WM_CLOSE消息发送到没有窗口的进程的方法
- C#实现软键盘的几个关键技术实现方法
- C# File类、Path类复习
- C# 实现对网站数据的采集和抓取
- c# 日期和时间System.DateTime
- 解决关闭窗口,C#报错"在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke"
- 《C#零基础入门之百识百例》(三十三)方法参数 -- 位置&命名参数 -- 求长方体体积
- C#-xml-读取节点数据方法
- C#基础之并行编程
- c# winform捕获全局异常,并记录日志
- C#获取当前程序运行路径的方法集合