/// <summary> /// Moore–Penrose pseudoinverse /// If A = U • Σ • VT is the singular value decomposition of A, then A† = V • Σ† • UT. /// For a diagonal matrix such as Σ, we get the pseudoinverse by taking the reciprocal of each non-zero element /// on the diagonal, leaving the zeros in place, and transposing the resulting matrix. /// In numerical computation, only elements larger than some small tolerance are taken to be nonzero, /// and the others are replaced by zeros. For example, in the MATLAB or NumPy function pinv, /// the tolerance is taken to be t = ε • max(m,n) • max(Σ), where ε is the machine epsilon. (Wikipedia) /// </summary> /// <param name="M">The matrix to pseudoinverse</param> /// <returns>The pseudoinverse of this Matrix</returns> public static Matrix PseudoInverse(this Matrix M) { Svd D = M.Svd(true); Matrix W = (Matrix)D.W(); Vector s = (Vector)D.S(); // The first element of W has the maximum value. double tolerance = Precision.EpsilonOf(2) * Math.Max(M.RowCount, M.ColumnCount) * W[0, 0]; for (int i = 0; i < s.Count; i++) { if (s[i] < tolerance) { s[i] = 0; } else { s[i] = 1 / s[i]; } } W.SetDiagonal(s); // (U * W * VT)T is equivalent with V * WT * UT return((Matrix)(D.U() * W * D.VT()).Transpose()); }
private static Matrix <double> PseudoInverse(Svd <double> svd) { Matrix <double> W = svd.W(); Vector <double> s = svd.S(); // The first element of W has the maximum value. double tolerance = Precision.EpsilonOf(2) * Math.Max(svd.U().RowCount, svd.VT().ColumnCount) * W[0, 0]; for (int i = 0; i < s.Count; i++) { if (s[i] < tolerance) { s[i] = 0; } else { s[i] = 1 / s[i]; } } W.SetDiagonal(s); // (U * W * VT)T is equivalent with V * WT * UT return((svd.U() * W * svd.VT()).Transpose()); }
public void TestMakeLowRankMatrix() { Matrix <double> x = SampleGenerator.MakeLowRankMatrix( numSamples: 50, numFeatures: 25, effectiveRank: 5, tailStrength: 0.01, randomState: new Random(0)); Assert.AreEqual(50, x.RowCount, "X shape mismatch"); Assert.AreEqual(25, x.ColumnCount, "X shape mismatch"); Svd svd = x.Svd(true); double sum = svd.S().Sum(); Assert.IsTrue(Math.Abs(sum - 5) < 0.1, "X rank is not approximately 5"); }
public override void Estimate(List <Point3D> datas) { double sum_x = 0; double sum_y = 0; double sum_z = 0; foreach (Point3D temp in datas) { sum_x += temp.x; sum_y += temp.y; sum_z += temp.z; } sum_x /= datas.Count; sum_y /= datas.Count; sum_z /= datas.Count; DenseMatrix jacobian = new DenseMatrix(datas.Count, 3); foreach (Point3D temp in datas) { Vector <double> gradient = new DenseVector(3); gradient[0] = temp.x - sum_x; gradient[1] = temp.y - sum_y; gradient[2] = temp.z - sum_z; jacobian.SetRow(datas.IndexOf(temp), gradient); } Svd svd = jacobian.Svd(true); // get matrix of left singular vectors with first n columns of U Matrix <double> U1 = svd.U().SubMatrix(0, datas.Count, 0, 3); // get matrix of singular values Matrix <double> S = new DiagonalMatrix(3, 3, svd.S().ToArray()); // get matrix of right singular vectors Matrix <double> V = svd.VT().Transpose(); Vector <double> parameters = new DenseVector(3); parameters = V.Column(0); x = sum_x; y = sum_y; z = sum_z; i = parameters[0]; j = parameters[1]; k = parameters[2]; }