gpt4 book ai didi

Matlab:Moore-Penrose伪逆算法实现

转载 作者:太空宇宙 更新时间:2023-11-03 20:13:13 33 4
gpt4 key购买 nike

我正在寻找 Moore-Penrose 算法计算伪逆矩阵的 Matlab 实现。

我尝试了几种算法,这个

http://arxiv.org/ftp/arxiv/papers/0804/0804.4809.pdf

第一眼看上去不错。

然而,问题是,对于大元素,它会生成缩放比例很差的矩阵,并且某些内部操作会失败。它涉及以下步骤:

L=L(:,1:r);
M=inv(L'*L);

我正试图找到一个更强大的解决方案,它可以在我的其他软件中轻松实现。感谢您的帮助。

最佳答案

我使用 Lutz Roeder 的 Mapack 矩阵库在 C# 中重新实现了一个。也许这个版本或 Java 版本对您有用。

/// <summary>
/// The difference between 1 and the smallest exactly representable number
/// greater than one. Gives an upper bound on the relative error due to
/// rounding of floating point numbers.
/// </summary>
const double MACHEPS = 2E-16;

// NOTE: Code for pseudoinverse is from:
// http://the-lost-beauty.blogspot.com/2009/04/moore-penrose-pseudoinverse-in-jama.html

/// <summary>
/// Computes the Moore–Penrose pseudoinverse using the SVD method.
/// Modified version of the original implementation by Kim van der Linde.
/// </summary>
/// <param name="x"></param>
/// <returns>The pseudoinverse.</returns>
public static Matrix MoorePenrosePsuedoinverse(Matrix x)
{
if (x.Columns > x.Rows)
return MoorePenrosePsuedoinverse(x.Transpose()).Transpose();
SingularValueDecomposition svdX = new SingularValueDecomposition(x);
if (svdX.Rank < 1)
return null;
double[] singularValues = svdX.Diagonal;
double tol = Math.Max(x.Columns, x.Rows) * singularValues[0] * MACHEPS;
double[] singularValueReciprocals = new double[singularValues.Length];
for (int i = 0; i < singularValues.Length; ++i)
singularValueReciprocals[i] = Math.Abs(singularValues[i]) < tol ? 0 : (1.0 / singularValues[i]);
Matrix u = svdX.GetU();
Matrix v = svdX.GetV();
int min = Math.Min(x.Columns, u.Columns);
Matrix inverse = new Matrix(x.Columns, x.Rows);
for (int i = 0; i < x.Columns; i++)
for (int j = 0; j < u.Rows; j++)
for (int k = 0; k < min; k++)
inverse[i, j] += v[i, k] * singularValueReciprocals[k] * u[j, k];
return inverse;
}

关于Matlab:Moore-Penrose伪逆算法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330459/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com