Example #1
0
        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);
            }
        }
        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 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 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);
        }
Example #5
0
        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);
        }
Example #6
0
        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());
        }
Example #7
0
 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";
            }
        }
Example #9
0
        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);
            }
        }
Example #10
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]);
            }
            //}
        }
Example #11
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);
        }
Example #12
0
 public static LinearSystem.SolutionSet Solve(this Matrix <Fraction> equations)
 {
     return(LinearSystem.Solve(equations));
 }