18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
PseudoInverse.cs 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2023-12-30 10:33 . Switch to file-scoped namespaces (#430)
using System;
using Algorithms.Numeric.Decomposition;
using Utilities.Extensions;
namespace Algorithms.Numeric.Pseudoinverse;
/// <summary>
/// The Moore–Penrose pseudo-inverse A+ of a matrix A,
/// is a general way to find the solution to the following system of linear equations:
/// ~b = A ~y. ~b e R^m; ~y e R^n; A e Rm×n.
/// There are varios methods for construction the pseudo-inverse.
/// This one is based on Singular Value Decomposition (SVD).
/// </summary>
public static class PseudoInverse
{
/// <summary>
/// Return the pseudoinverse of a matrix based on the Moore-Penrose Algorithm.
/// using Singular Value Decomposition (SVD).
/// </summary>
/// <param name="inMat">Input matrix to find its inverse to.</param>
/// <returns>The inverse matrix approximation of the input matrix.</returns>
public static double[,] PInv(double[,] inMat)
{
// To compute the SVD of the matrix to find Sigma.
var (u, s, v) = ThinSvd.Decompose(inMat);
// To take the reciprocal of each non-zero element on the diagonal.
var len = s.Length;
var sigma = new double[len];
for (var i = 0; i < len; i++)
{
sigma[i] = Math.Abs(s[i]) < 0.0001 ? 0 : 1 / s[i];
}
// To construct a diagonal matrix based on the vector result.
var diag = sigma.ToDiagonalMatrix();
// To construct the pseudo-inverse using the computed information above.
var matinv = u.Multiply(diag).Multiply(v.Transpose());
// To Transpose the result matrix.
return matinv.Transpose();
}
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891