public void TestInverse2x2b() { const int n = 2; float[,] a = new float[n, n] { { 1, 2 }, { 3, 4 } }; float[,] aInv = (float[, ])a.Clone(); Lapack.Inverse(aInv, n); float[,] eye = new float[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { eye[i, j] += a[i, k] * aInv[k, j]; } } } AssertArray.AreAlmostEqual(1f, eye[0, 0]); AssertArray.AreAlmostEqual(0f, eye[0, 1]); AssertArray.AreAlmostEqual(0f, eye[1, 0]); AssertArray.AreAlmostEqual(1f, eye[1, 1]); }
public static Array <float> PowerMethod(Array <float> a) { // https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse // init A(0) = (A*A + dI)-1.A* var d = 1e-6f; var result = a.T.Dot(a) + d * NN.Eye(a.Shape[1]); Lapack.Inverse(result.Values, result.Shape[0]); result = result.Dot(a.T); // iterate: A(i+1) = 2A(i) - A(i).A.A(i) for (int i = 0; i < 2; i++) { result = 2 * result - result.Dot(a).Dot(result); } return(result); }