public void SolveWithGaussSeidel_Test() { Matrix inputMatrix = new Matrix(new double[3, 3] { { 10, -4, -2 }, { -4, 10, -4 }, { -6, -2, 12 } }); Vector rightSideVector = new Vector(new double[3] { 2, 3, 1 }); Vector startValue = new Vector(new double[3] { 0, 0, 0 }); LGS lgs = new LGS(inputMatrix, rightSideVector); Vector result = lgs.Solve(Enums.ESolveAlgorithm.eGaußSeidel, startValue); Vector expected = new Vector(new double[3] { 0.598, 0.741, 0.506 }); Assert.AreEqual(expected, result); }
public static Function CreateRegression(List <Point> points, int degreeOfPolynominal) { if (degreeOfPolynominal < 1) { throw new ArgumentException("Degree of polynominal must be at least 1"); } Matrix regressionMatrix = CreateRegressionMatrix(points, degreeOfPolynominal + 1); Vector regressionVector = CreateRegressionVector(points, degreeOfPolynominal + 1); Parser parser = new Parser(); LGS lgs = new LGS(regressionMatrix, regressionVector); string function = CreateRegressionFunction(lgs.Solve(Enums.ESolveAlgorithm.eGaussianElimination), degreeOfPolynominal + 1); return(parser.ParseFunction(function)); }
public void SolveWithGaussianElimination_Test() { Matrix m = new Matrix(new double[4, 4] { { 1, 1, 1, 1 }, { 1, 1, -1, -1 }, { 1, -1, 1, 1 }, { 1, -1, -1, 1 } }); Vector v = new Vector(new double[4] { 0, 1, 3, -1 }); LGS lgs = new LGS(m, v); Vector result = lgs.Solve(Enums.ESolveAlgorithm.eGaussianElimination); Vector expected = new Vector(new double[6] { 1, -2, 3, 4, 2, -1 }); Assert.AreEqual(expected, result); }
private void Plot() { //Make asynchronous List <double> hList = CalculateH(); List <double> gList = CalculateG(hList); Math_Collection.LinearAlgebra.Matrices.Matrix m = CreateSplineMatrix(hList); double[] gListAsArray = gList.ToArray(); LGS lgs = new LGS(m, new Math_Collection.LinearAlgebra.Vectors.Vector(gListAsArray)); //Make asynchronous Math_Collection.LinearAlgebra.Vectors.Vector outcome = lgs.Solve(LGS.SolveAlgorithm.Gauß); string sOutcome = outcome.ToString(); List <double> bList = CalculateB(outcome, hList); List <double> dList = CalculateD(outcome, hList); SolveSplineFunctions(CreateSplineFunctions(bList, outcome, dList)); }
/// <summary> /// Calculates all polynominal parts of the spline /// </summary> /// <returns>A List of Functions that represents all polynominals</returns> /// <seealso cref="http://www.tm-mathe.de/Themen/html/funnatsplines.html"/> private List <Function> CalculateSplineParts() { if (_sourcePoints != null && _sourcePoints.Count < 3) { return(null); } // 3.Degree Polynomial := f(x) = a + bx + cx^2 + dx^3 // f'(x) = b + 2cx + 3dx^2 // f''(x) = 2c + 6dx Parser parser = new Parser(); double[] xs = _sourcePoints.Keys.ToArray(); double[] ys = _sourcePoints.Values.ToArray(); #region Calculate Help Variable hi and gi double[] hi = CalculateH(xs); double[] gi = CalcualteG(ys, hi); #endregion #region Calculate all c's Matrix coefficientMatrix = GetCoefficientMatrix(hi); Math_Collection.LinearAlgebra.Vectors.Vector coefficientVector = GetCoefficientVector(gi); LGS lgs = new LGS(coefficientMatrix, coefficientVector); Math_Collection.LinearAlgebra.Vectors.Vector resultFromLGS = lgs.Solve(Math_Collection.Enums.ESolveAlgorithm.eGaussianElimination); double[] ci = new double[_amountOfSplineParts + 2]; for (int i = 2; i <= _amountOfSplineParts + 1; i++) { ci[i] = resultFromLGS[i - 1]; } #endregion #region Calculates all a values // a's = yi-1 double[] ai = new double[_amountOfSplineParts + 1]; for (int i = 1; i <= _amountOfSplineParts; i++) { ai[i] = ys[i - 1]; } #endregion #region Calculate all b's // bi = (yi - yi-1) / hi - (hi / 3) * (2*ci + ci+1) double[] bi = new double[_amountOfSplineParts + 1]; for (int i = 1; i <= _amountOfSplineParts; i++) { bi[i] = ((ys[i] - ys[i - 1]) / hi[i]) - (hi[i] / 3) * (2 * ci[i] + ci[i + 1]); } #endregion #region Calculate all d's // di = (ci+1 - ci) / 3 * hi double[] di = new double[_amountOfSplineParts + 1]; for (int i = 1; i <= _amountOfSplineParts; i++) { di[i] = (ci[i + 1] - ci[i]) / (3 * hi[i]); } #endregion #region Generate all Functions Function[] functionParts = new Function[_amountOfSplineParts]; for (int i = 1; i <= _amountOfSplineParts; i++) { string f = ai[i] + "+" + bi[i] + "*x+" + ci[i] + "*x^2+" + di[i] + "*x^3"; functionParts[i - 1] = parser.ParseFunction(f); } #endregion return(functionParts.ToList()); }
/// <summary> /// Berechnet die Regression für die gegebenen Punkte /// </summary> /// <param name="?"></param> /// <param name="degreeOfPolynominal"></param> /// <returns></returns> public static Function CalculateRegression(List <Point> points, int degreeOfPolynominal = 1) { if (points == null || points.Count < 2) { return(null); } Matrix inputMatrix = new Matrix(new double[degreeOfPolynominal + 1, degreeOfPolynominal + 1]); Math_Collection.LinearAlgebra.Vectors.Vector outputVector = new Math_Collection.LinearAlgebra.Vectors.Vector(new double[degreeOfPolynominal + 1]); int maxPower = degreeOfPolynominal * 2; int actualPower = maxPower; // Input Matrix aufbauen for (int i = 0; i < inputMatrix.RowCount; i++) { for (int k = 0; k < inputMatrix.ColumnCount; k++) { actualPower = maxPower - (i + k); inputMatrix[i, k] = SumXValues(points, actualPower); } } maxPower = degreeOfPolynominal; // Output Vector aufbauen for (int m = 0; m < outputVector.Size; m++) { outputVector[m] = SumXYValues(points, maxPower); maxPower--; } // LGS Lösen LGS lgs = new LGS(inputMatrix, outputVector); Math_Collection.LinearAlgebra.Vectors.Vector resultVector = lgs.Solve(); // Function aufbauen // ax^1 + b; ax^2+bx^1+c string functionString = ""; maxPower = degreeOfPolynominal; for (int o = 0; o <= degreeOfPolynominal; o++) { if (o != degreeOfPolynominal) { functionString += string.Format(resultVector[o] + "*x^{0}+", maxPower); } else { functionString += resultVector[o]; } maxPower--; } functionString = functionString.Replace(",", "."); Parser parser = new Parser(); return(parser.ParseFunction(functionString)); }