public void Test_VandermondeMatrix_4_Inverse() { // (-2, -39), (0, 3), (1, 6), (3, 36) VandermondeMatrix vm = new VandermondeMatrix(-2, 0, 1, 3); Vector y = new Vector(new double[] { -39, 3, 6, 36 }); double determinant = vm.GetDeterminant(); Assert.NotEqual(0, determinant); Matrix m = vm.GetInverse(); Vector actual = m * y; // p(x) = 2x^3-4x^2+5x+3 Assert.Equal(3, actual[0]); Assert.Equal(5, actual[1]); Assert.Equal(-4, actual[2]); Assert.Equal(2, actual[3]); }
private Vector GetCoeffs(double[] data) { Polynomial polynomial = new Polynomial(); int length = data.Length / 2; double[] x = new double[length]; double[] y = new double[length]; for (int i = 0; i < length; i++) { x[i] = data[i * 2]; y[i] = data[i * 2 + 1]; } VandermondeMatrix vm = new VandermondeMatrix(x); Vector v = new Vector(y); return(vm.GetInverse() * v); }
public void Test_VandermondeMatrix_4_Inverse2() { // (0, 3), (1, 6), (3, 36), (4, 9) VandermondeMatrix vm = new VandermondeMatrix(0, 1, 3, 4); Vector y = new Vector(new double[] { 3, 6, 36, 9 }); double determinant = vm.GetDeterminant(); Assert.NotEqual(0, determinant); Matrix m = vm.GetInverse(); Vector actual = m * y; // p(x) = -4.5x^3 + 22x^2 -14.5x + 3 Assert.Equal(3, actual[0]); Assert.Equal(-14.5, actual[1]); Assert.Equal(0, 22 - actual[2], 14); Assert.Equal(-4.5, actual[3]); }