public void TrySolveSystem_1()
        {
            // Input strings containing linear equations
            string linearequation_1 = "-6x+9y+5z=8";
            string linearequation_2 = "-7x-12y+z=-28";
            string linearequation_3 = "5x+6y+z=-28";

            // List of linear equations
            List<LinearPolynomial> lpList = new List<LinearPolynomial>();
            lpList.Add(new LinearPolynomial(linearequation_1));
            lpList.Add(new LinearPolynomial(linearequation_2));
            lpList.Add(new LinearPolynomial(linearequation_3));

            // System of linear equations
            LinearEquationSystem leSystem =
                new LinearEquationSystem(lpList);

            // Actual values
            Dictionary<string, string> actualSolution =
                leSystem.FindSolution_MatrixMethod();

            // Expected values
            Dictionary<string, string> expectedSolution =
                new Dictionary<string, string>() {
                    {"x", "-8,70588235294118"},
                    {"y", "5,80392156862745"},
                    {"z", "-19,2941176470588"},
                };

            // Asserts
            CollectionAssert.AreEqual(expectedSolution, actualSolution);
        }
Beispiel #2
0
        // Method for solving system of linear equations,
        // where count of linear equation = linearequationcount,
        // count of unknown members = unknowncount,
        // strlist - list of linear equations formated as strings
        public string TrySolveLinearEquationSystem(int linearequationcount,
                        int unknowncount, List<string> strlist)
        {
            // Result
            string result = "";

            try
            {
                // Initialize the system of linear equation from strlist
                List<LinearPolynomial> linearequationlist = new List<LinearPolynomial>();
                foreach (string str in strlist)
                {
                    LinearPolynomial linPolynomial =
                        new LinearPolynomial(str);

                    // If str is a linear polynomial
                    if (linPolynomial.Members.Count != 0)
                        linearequationlist.Add(linPolynomial);
                }
                LinearEquationSystem leSystem =
                    new LinearEquationSystem(linearequationlist);

                // If wrong input data
                if (leSystem.LinearEquationCount != linearequationcount
                    || leSystem.UnknownCount != unknowncount
                    || linearequationcount != strlist.Count)
                {
                    result = "Wrong input system. "
                              + "Check the input data for pattern matching "
                              + "(look at the example).";

                    return result;
                }

                // Try to find the solution of the system
                Dictionary<string, string> Solution
                     = leSystem.FindSolution_MatrixMethod();
                foreach (var root in Solution)
                    result += root.Key + ": " + root.Value
                              + " ";
            }
            catch (Exception exception)
            {
                // Write to log
                logger.WriteLog(System.DateTime.Now.ToString()
                    + " "
                    + exception.TargetSite.ToString()
                    + " "
                    + exception.Message);

                result = "Wrong input system. "
                         + "Check the input data for pattern matching "
                         + "(look at the example).";
            }

            return result;
        }
        public void ConstructorTest(string validInputStr_1, string validInputStr_2, 
            string validInputStr_3, int uCount)
        {
            // List of linear equations
            List<LinearPolynomial> lpList = new List<LinearPolynomial>();
            lpList.Add(new LinearPolynomial(validInputStr_1));
            lpList.Add(new LinearPolynomial(validInputStr_2));
            lpList.Add(new LinearPolynomial(validInputStr_3));

            // System of linear equations
            LinearEquationSystem leSystem =
                new LinearEquationSystem(lpList);

            // Actual values
            int actualSystemCount = leSystem.LinearEquationCount;
            int actualSystemUnknownCount = leSystem.UnknownCount;

            // Asserts
            Assert.AreEqual(3, actualSystemCount);
            Assert.AreEqual(uCount, actualSystemUnknownCount);
        }
        public void TrySolveSystem_2()
        {
            // Input strings containing linear equations
            string linearequation_1 = "-6.8x+9y+5z=8";
            string linearequation_2 = "-7.8x-12.6y-z=2";
            string linearequation_3 = "5x+6y+z+6t=-3";

            // List of linear equations
            List<LinearPolynomial> lpList = new List<LinearPolynomial>();
            lpList.Add(new LinearPolynomial(linearequation_1));
            lpList.Add(new LinearPolynomial(linearequation_2));
            lpList.Add(new LinearPolynomial(linearequation_3));

            // System of linear equations
            LinearEquationSystem leSystem =
                new LinearEquationSystem(lpList);

            // Actual values
            Dictionary<string, string> actualSolution =
                leSystem.FindSolution_MatrixMethod();

            // Expected values
            Dictionary<string, string> expectedSolution =
                new Dictionary<string, string>() {
                    {"This system have",
                     "an infinite number of solutions."}                    
                };

            // Asserts
            CollectionAssert.AreEqual(expectedSolution, actualSolution);
        }
        public void TrySolveSystem_3()
        {
            // Input strings containing linear equations
            string linearequation_1 = "4x-3y+2z-t=8";
            string linearequation_2 = "3x-2y+1z-3t=7";
            string linearequation_3 = "5x-3y+z-8t=1";

            // List of linear equations
            List<LinearPolynomial> lpList = new List<LinearPolynomial>();
            lpList.Add(new LinearPolynomial(linearequation_1));
            lpList.Add(new LinearPolynomial(linearequation_2));
            lpList.Add(new LinearPolynomial(linearequation_3));

            // System of linear equations
            LinearEquationSystem leSystem =
                new LinearEquationSystem(lpList);

            // Actual values
            Dictionary<string, string> actualSolution =
                leSystem.FindSolution_MatrixMethod();

            // Expected values
            Dictionary<string, string> expectedSolution =
                new Dictionary<string, string>() {
                    {"This system have",
                     "no solution."}                    
                };

            // Asserts
            CollectionAssert.AreEqual(expectedSolution, actualSolution);
        }