public void SimpleLinearSystemTest() { // 3x + 2y - z = 1 // 2x - 2y + 4z = -2 // -x + 0.5y - z = 0 // x = 1, y = -2, z = -2 var ls = new LinearSystem(new Matrix(3, 3, 3.0, 2.0, -1.0, 2.0, -2.0, 4.0, -1.0, 0.5, -1.0), new Matrix(3, 1, 1.0, -2.0, 0.0)); var solution = ls.Solve(); var expected = new Matrix(3, 1, 1.0, -2.0, -2.0); using (new CloseToEqualityChecker()) { Assert.Equal(expected, solution); } }
//--------------------------------------------------------------------------------------- //Решение системы линейных уравнений private double[] SolveLinearSystem(double[ , ] arrayA, double[] arrayB) { LinearSystem linearSystem = new LinearSystem(arrayA, arrayB, 1E-17); double[] solutionX = linearSystem.XVector; return(solutionX); }
public void SolveGauss_Test() { Random rand = new Random(); // different sizes of linear systems for (int n = 2; n <= 5; n++) { // multiple tests on same size for (int k = 0; k < 5; k++) { double[] coeff = new double[n]; for (int i = 0; i < n; i++) { // subtract 0.5 to ensure usage of negative values // multiply by n+1 to generate larger numbers coeff[i] = (rand.NextDouble() - 0.5) * (n + 1); } LinearSystem system = GenerateLinearSystem(coeff); double[] results = system.SolveGauss(15); for (int i = 0; i < n; i++) { double expected = coeff[i]; double actual = results[i]; Assert.AreEqual(expected, actual, 0.0001); } } } }
public void TestAndVerify2() { LinearSystem ls = new LinearSystem(); string input = "1 2 0 4 7\r\n0 2 0 2 8\r\n0 0 -1 4 6\r\n1 2 3 2 3"; string result = ls.Solve(input); Assert.AreEqual("SOLUTION=(-3.8; 2.6; -0.4; 1.4)", result); }
public void TestAndVerify3() { LinearSystem ls = new LinearSystem(); string input = "1 2 1\r\n1 2 0"; string result = ls.Solve(input); Assert.AreEqual("SOLUTION=NONE", result); }
public void TestAndVerify1() { LinearSystem ls = new LinearSystem(); string input = "1 2 0 7\r\n0 2 4 8\r\n0 5 6 9"; string result = ls.Solve(input); Assert.AreEqual("SOLUTION=(10; -1.5; 2.75)", result); }
public void SetRow_CanSetRowValue() { var ls = new LinearSystem(1, 4); ls.SetRow(0, 2, 4, 6, 8); Assert.AreEqual(ls[0, 0], 2); Assert.AreEqual(ls[0, 1], 4); Assert.AreEqual(ls[0, 2], 6); Assert.AreEqual(ls[0, 3], 8); }
public void RowIndexer_ReturnsRequestedRow() { var ls = new LinearSystem(2, 2); ls[1, 0] = 3; ls[1, 1] = 4; var row = ls[1]; Assert.AreEqual(3, row[0]); Assert.AreEqual(4, row[1]); }
/// <summary> /// Подбирает точки Безье для указанного полинома. /// </summary> /// <param name="polynomial"></param> /// <returns></returns> private static double[] GetBezierPoints(Polynomial polynomial) { return(LinearSystem.Resolve(new[] { new Linear(Line1, polynomial[0]), new Linear(Line2, polynomial[1]), new Linear(Line3, polynomial[2]), new Linear(Line4, polynomial[3]), })); }
public void TestAndVerify3() { LinearSystem ls = new LinearSystem(); string input = "1 2 1\r\n1 2 0"; //string result = ls.Solve(input); //string testResult = Tests.testIt(input, result); //if (testResult.Length > 0) Assert.Fail(testResult); else Console.WriteLine("'" + result + "' accepted!"); Assert.IsFalse(false); }
public void TestMultiplyRow() { Matrix mat = new Matrix() { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; LinearSystem ls = new LinearSystem(mat); ls.MultiplyRow(1, 2); Assert.AreEqual(ls.CoefficientMatrix.GetRow(0), new Vector { 1, 2, 3 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(1), new Vector { 8, 10, 12 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(2), new Vector { 7, 8, 9 }); ls.MultiplyRow(0, 1, 2); Assert.AreEqual(ls.CoefficientMatrix.GetRow(0), new Vector { 17, 22, 27 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(1), new Vector { 8, 10, 12 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(2), new Vector { 7, 8, 9 }); ls.MultiplyRow(1, 1.0 / 8.0); Assert.AreEqual(ls.CoefficientMatrix.GetRow(0), new Vector { 17, 22, 27 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(1), new Vector { 1, 10 / 8.0, 6 / 4.0 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(2), new Vector { 7, 8, 9 }); ls.MultiplyRow(0, 1, -17); Assert.AreEqual(ls.CoefficientMatrix.GetRow(0), new Vector { 0, 0.75, 1.5 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(1), new Vector { 1, 10 / 8.0, 6 / 4.0 }); Assert.AreEqual(ls.CoefficientMatrix.GetRow(2), new Vector { 7, 8, 9 }); }
public void Normalize_NormalizesTheGivenRow() { var ls = new LinearSystem(1, 4); ls.SetRow(0, 2, 4, 6, 8); ls.Normalize(0, 0); var normalizedRow = ls[0]; Assert.AreEqual(1, normalizedRow[0]); Assert.AreEqual(2, normalizedRow[1]); Assert.AreEqual(3, normalizedRow[2]); Assert.AreEqual(4, normalizedRow[3]); }
public void TestAndVerify1() { LinearSystem ls = new LinearSystem(); string input = "1 2 0 7\r\n0 2 4 8\r\n0 5 6 9"; //string result = ls.Solve(input); //should be SOLUTION=(10; -1,5; 2,75) //string testResult = Tests.testIt(input, result); //if (testResult.Length > 0) Assert.Fail(testResult); else Console.WriteLine("'" + result + "' accepted!"); Assert.IsFalse(false); }
private void button1_Click(object sender, EventArgs e) { if (Validate()) { try { var linearSystem = new LinearSystem(MatrixA, VectorB); VectorX = linearSystem.XVector; } catch (Exception error) { MessageBox.Show(error.Message); } } }
public void SolveTest() { var a = new double[, ] { { 1, 1 }, { 1, -1 } }; var b = new double[] { 1, 3 }; var x = LinearSystem.Solve(a, b); Assert.AreEqual(new[] { 2.0, -1 }, x); }
private double LeastSquares(double x) { // int k = 4; double[][] FiX = new double[pointCount][]; int iter = 0; foreach (Point p in points) { FiX[iter++] = Fi(p.X); } double[][] C = new double[k][]; for (int j = 0; j < k; j++) { C[j] = new double[k]; for (int m = 0; m < k; m++) { double S = 0.0; for (int i = 0; i < pointCount; i++) { S = S + FiX[i][j] * FiX[i][m]; } C[j][m] = S; } } double[] d = new double[k]; for (int j = 0; j < k; j++) { double S = 0.0; for (int i = 0; i < pointCount; i++) { S = S + points[i].Y * FiX[i][j]; } d[j] = S; } double[][] matr = new double[k][]; for (int i = 0; i < k; i++) { matr[i] = new double[k + 1]; for (int j = 0; j < k; j++) { matr[i][j] = C[i][j]; } matr[i][k] = d[i]; } LinearSystem a = new LinearSystem(matr); //return a.XVector[2]*x*x + a.XVector[1]*x + a.XVector[0]; return(a.XVector[3] * x * x * x + a.XVector[2] * x * x + a.XVector[1] * x + a.XVector[0]); }
public void TestResolve() { Matrix mat = new Matrix() { { 3, 5, 20 }, { -1, 1, 0 } }; LinearSystem ls = new LinearSystem(mat); Dictionary <string, double> dic = ls.Resolve(); Assert.AreEqual(dic["0"], 2.5); Assert.AreEqual(dic["1"], 2.5); }
public static Vector3 FromPlanes(Plane p1, Plane p2, Plane p3) { var ls = new LinearSystem( new Matrix(3, 3, p1.A, p1.B, p1.C, p2.A, p2.B, p2.C, p3.A, p3.B, p3.C), new Matrix(3, 1, p1.D, p2.D, p3.D)); var result = ls.Solve(); return(result?.AsVector3()); }
private void button1_Click(object sender, EventArgs e) { flowLayoutPanel3.Controls.Clear(); LinearSystem ls = new LinearSystem(); for (int i = 0; i < equationControls.Length; i++) ls.Equations.Add(equationControls[i].Equation); char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e' }; double[] result = ls.Solve(); for (int i = 0; i < result.Length; i++) { Label lbl = new Label(); lbl.Margin = new Padding(12, i == 0 ? 8 : 0, 12, 0); lbl.Text = chars[i] + " = " + result[i]; flowLayoutPanel3.Controls.Add(lbl); } }
private void SolveSystem() { float[,] A = new float[_size, _size]; float[] B = new float[_size]; float temp; for (int i = 0; i < _size; ++i) { for (int j = 0; j < _size; ++j) { if (float.TryParse(_A[i, j], out temp)) { A[i, j] = temp; } else { _message = "Table contains not a number"; return; } } if (float.TryParse(_B[i], out temp)) { B[i] = temp; } else { _message = "Table contains not a number"; return; } } float[] X; if (LinearSystem.Solve(A, B, out X)) { for (int i = 0; i < _size; ++i) { _X[i] = X[i].ToString(); } _message = "System successfuly solved"; } else { _message = "System has no solution"; } }
public void Reduce_CanSolveRowEquations() { var ls = new LinearSystem(3, 4); ls[0] = new[] { 2.0, 1.0, -1.0, 8.0 }; ls[1] = new[] { -3.0, -1.0, 2.0, -11.0 }; ls[2] = new[] { -2.0, 1.0, 2.0, -3.0 }; ls.Reduce(); // Echelon form Assert.AreEqual(1, ls[0, 0]); Assert.AreEqual(1, ls[1, 1]); Assert.AreEqual(1, ls[2, 2]); // Values Assert.IsTrue((ls[0, 3]).Epsilon(2.0, 0.000001)); Assert.IsTrue((ls[1, 3]).Epsilon(3.0, 0.000001)); Assert.IsTrue((ls[2, 3]).Epsilon(-1.0, 0.000001)); }
public void TestCreateRowEchelonForm() { Matrix mat = new Matrix() { { 1, 1, 2, 0 }, { 0, 1, -1, 0 }, { 3, 4, 5, 0 }, { 3, 5, 4, 0 } }; LinearSystem ls = new LinearSystem(mat); ls.CreateLowerTriangularCoefficientMatrix(); Assert.AreEqual(ls.CoefficientMatrix, new Matrix { { 1, 1, 2, 0 }, { 0, 1, -1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }); mat = new Matrix() { { 1, 1, -1, 2 }, { -2, 0, 1, -2 }, { 5, -1, 2, 4 }, { 2, 6, -3, 5 } }; ls = new LinearSystem(mat); ls.CreateLowerTriangularCoefficientMatrix(); Assert.AreEqual(ls.CoefficientMatrix, new Matrix { { 1, 1, -1, 2 }, { 0, 1, -0.5, 1 }, { 0, 0, 1, 0 }, { 0, 0, 0, -3 } }); }
private void button1_Click(object sender, EventArgs e) { flowLayoutPanel3.Controls.Clear(); LinearSystem ls = new LinearSystem(); for (int i = 0; i < equationControls.Length; i++) { ls.Equations.Add(equationControls[i].Equation); } char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e' }; double[] result = ls.Solve(); for (int i = 0; i < result.Length; i++) { Label lbl = new Label(); lbl.Margin = new Padding(12, i == 0 ? 8 : 0, 12, 0); lbl.Text = chars[i] + " = " + result[i]; flowLayoutPanel3.Controls.Add(lbl); } }
/// <summary> /// Запуск линейного решения СЛАУ /// </summary> static void StartLinearSystem() { if (matrix != null && vector != null) { startTime = Stopwatch.StartNew(); linearSystem = new LinearSystem(matrix, vector); linearSystem.GaussSolve(); startTime.Stop(); resultTime = startTime.Elapsed; OutputTime(resultTime); OutputVectorResult(linearSystem.xVector); } else { Console.WriteLine("Линейное решение невозможно! Данные о матрице и векторе отсутствуют..."); } }
public void LinearSystemSolutionTest3() { int countX = 2; string[] eq = { "1x0+2x1=6", "2x0+4x1=12" }; LinearSystem LS = new LinearSystem(countX, eq); Matrix ans = LS.SolutionGaussMethod(); Matrix correctAns = new Matrix(1, 2); correctAns[0, 0] = -2; correctAns[0, 1] = 6; for (int i = 0; i < 2; i++) { Assert.AreEqual(correctAns[0, i], ans[0, i]); } }
public void LinearSystemSolutionTest1() { int countX = 2; string[] eq = { "2x0+1x1=3", "4x0+1x1=5" }; LinearSystem LS = new LinearSystem(countX, eq); Matrix ans = LS.SolutionGaussMethod(); Matrix correctAns = new Matrix(2, 1); correctAns[0, 0] = 1; correctAns[1, 0] = 1; for (int i = 0; i < 2; i++) { Assert.AreEqual(correctAns[i, 0], ans[i, 0]); } }
static void Main(string[] args) { LinearSystem system = new LinearSystem(); Console.WriteLine(); system.Print(); //if (!system.IsDominance()) //{ // Console.WriteLine("System doesn't have diagonal dominance. Simple Iterations and Seidel Method not working"); //} //else //{ var result = system.Solve(new SquareRootsMethod()); Console.Write("\nAnswer:"); for (int i = 0; i < result.Length; i++) { Console.Write("\nx{0} = {1} ", i + 1, result[i]); } //} }
public void LinearSystemSolutionTest2() { int countX = 3; string[] eq = { "1x0+1x1+1x2=6", "1x0+2x1+0x2=5", "0x0+1x1+2x2=8" }; LinearSystem LS = new LinearSystem(countX, eq); Matrix ans = LS.SolutionGaussMethod(); Matrix correctAns = new Matrix(3, 1); correctAns[0, 0] = 1; correctAns[1, 0] = 2; correctAns[2, 0] = 3; for (int i = 0; i < 3; i++) { Assert.AreEqual(correctAns[i, 0], ans[i, 0]); } }
private void HandleBallCollision(IBallInternal ball, BallStepInfo info, Dictionary <IBallInternal, NewVelocity> newVelocities) { var validBalls = info.CollidedBalls.Where(b => b.InGame); //prepare linear system wich solutions gives the impulses Matrix A = new Matrix(validBalls.Count()); Vector B = new Vector(validBalls.Count()); foreach (var iBall in validBalls.Select((Value, Idx) => new { Value, Idx })) { var iNorm = info.GetNormalVersor(iBall.Value); //build matrix A foreach (var jBall in Enumerable.Where(validBalls.Select((Value, Idx) => new { Value, Idx }), e => e.Idx <= iBall.Idx)) { if (iBall.Idx == jBall.Idx) { A[iBall.Idx, iBall.Idx] = (1 / ball.Mass + 1 / iBall.Value.Mass); } else { var jNorm = info.GetNormalVersor(jBall.Value); A[iBall.Idx, jBall.Idx] = Coordinates.Dot(iNorm, jNorm) / ball.Mass; A[jBall.Idx, iBall.Idx] = A[iBall.Idx, jBall.Idx]; } } //build known terms vector B[iBall.Idx] = (1 + Math.Min(iBall.Value.Elasticity, ball.Elasticity)) * Coordinates.Dot(Coordinates.Sub(iBall.Value.Velocity, ball.Velocity), iNorm); } //solve the system var x = LinearSystem.Solve(A, B); //apply the impulses foreach (var iBall in validBalls.Select((Value, Idx) => new { Value, Idx })) { var iNorm = info.GetNormalVersor(iBall.Value); newVelocities[ball].Value = Coordinates.Add(newVelocities[ball].Value, x[iBall.Idx] / ball.Mass * iNorm); newVelocities[iBall.Value].Value = Coordinates.Sub(newVelocities[iBall.Value].Value, x[iBall.Idx] / iBall.Value.Mass * iNorm); iBall.Value.SetHitSomethingFlag(true); } ball.SetHitSomethingFlag(validBalls.Count() != 0); }
public static Conic2 FromPoints(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4, Vector2 v5) { var constraints = new Matrix(5, 5, v1.X * v1.X, v1.X * v1.Y, v1.Y * v1.Y, v1.X, v1.Y, v2.X * v2.X, v2.X * v2.Y, v2.Y * v2.Y, v2.X, v2.Y, v3.X * v3.X, v3.X * v3.Y, v3.Y * v3.Y, v3.X, v3.Y, v4.X * v4.X, v4.X * v4.Y, v4.Y * v4.Y, v4.X, v4.Y, v5.X * v5.X, v5.X * v5.Y, v5.Y * v5.Y, v5.X, v5.Y); var ones = new Matrix(5, 1, 1, 1, 1, 1, 1); var result = new LinearSystem(constraints, ones).Solve(); if (result == null) { return(null); } return(new Conic2(result[0, 0], result[1, 0], result[2, 0], result[3, 0], result[4, 0], 1.0)); }
public void TestCreateUpperTriangularCoefficientMatrix() { Matrix mat = new Matrix() { { 1, 1, 2, 0 }, { 0, 1, -1, 0 }, { 3, 4, 5, 0 }, { 3, 5, 4, 0 } }; LinearSystem ls = new LinearSystem(mat); ls.CreateUpperTriangularCoefficientMatrix(); Assert.IsTrue(ls.CoefficientMatrix.Equals( new Matrix { { 0, 0, 0, 0 }, { 1 / 3.0, 1, 0, 0 }, { 3 / 5.0, 4 / 5.0, 1, 0 }, { 0, 0, 0, 0 } })); }
public void Cols_ReturnsNumberOfColumns() { var ls = new LinearSystem(2, 5); Assert.AreEqual(5, ls.Cols); }
public void TestBothTriangularCoefficientMatrix() { Matrix mat = new Matrix() { { 1, 1, -1, 2 }, { -2, 0, 1, -2 }, { 5, -1, 2, 4 }, { 2, 6, -3, 5 } }; LinearSystem ls = new LinearSystem(mat); ls.CreateLowerTriangularCoefficientMatrix(); ls.CreateUpperTriangularCoefficientMatrix(); Assert.AreEqual(ls.CoefficientMatrix, new Matrix { { 1, 0, 0, 1 }, { 0, 1, 0, 1 }, { 0, 0, 1, 0 }, { 0, 0, 0, -3 } }); mat = new Matrix() { { 1, 1, -1, 2 }, { -2, 0, 1, -2 }, { 5, -1, 2, 4 }, { 2, 6, -3, 5 } }; ls = new LinearSystem(mat); ls.CreateUpperTriangularCoefficientMatrix(); ls.CreateLowerTriangularCoefficientMatrix(); Assert.AreEqual(ls.CoefficientMatrix, new Matrix { { 1, 0, 0, 1 }, { 0, 1, 0, 1 }, { 0, 0, 1, 0 }, { 0, 0, 0, -3 } }); mat = new Matrix() { { 2, 3, 4 }, { 3, 2, 2 }, }; ls = new LinearSystem(mat); ls.CreateLowerTriangularCoefficientMatrix(); ls.CreateUpperTriangularCoefficientMatrix(); Assert.IsTrue(ls.CoefficientMatrix.Equals( new Matrix { { 1, 0, -0.4 }, { 0, 1, 1.6 }, })); mat = new Matrix() { { 6, 7, 5 }, { 1, 1, 0 }, }; ls = new LinearSystem(mat); ls.CreateLowerTriangularCoefficientMatrix(); ls.CreateUpperTriangularCoefficientMatrix(); Assert.IsTrue(ls.CoefficientMatrix.Equals( new Matrix { { 1, 0, -5 }, { 0, 1, 5 }, })); mat = new Matrix() { { 2, -3, 9 }, { 0, 1, 2 }, }; ls = new LinearSystem(mat); ls.CreateLowerTriangularCoefficientMatrix(); ls.CreateUpperTriangularCoefficientMatrix(); Assert.IsTrue(ls.CoefficientMatrix.Equals( new Matrix { { 1, 0, 7.5 }, { 0, 1, 2 }, })); }
public void SwapRows_ProducesExpectedOutput() { var ls = new LinearSystem(2, 2); ls.SetRow(0, 1, 2); ls.SetRow(1, 3, 4); ls.SwapRows(0, 1); Assert.AreEqual(3, ls[0, 0]); Assert.AreEqual(4, ls[0, 1]); Assert.AreEqual(1, ls[1, 0]); Assert.AreEqual(2, ls[1, 1]); }
public static LinearSystem.SolutionSet Solve(this Matrix <Fraction> equations) { return(LinearSystem.Solve(equations)); }