Ejemplo n.º 1
0
        public void AssertSystemSolvable(List <Entity> equations, List <VariableEntity> vars, int rootCount = -1)
        {
            var sys = MathS.Equations(equations.ToArray());
            var sol = sys.Solve(vars.ToArray());

            Assert.IsTrue(sol.Shape[0] == rootCount || rootCount == -1, "Got " + sol.Shape[0] + " instead of " + rootCount);
            for (int i = 0; i < sol.Shape[0]; i++)
            {
                foreach (var eq in equations)
                {
                    var eqCopy = eq.DeepCopy();
                    Assert.AreEqual(sol.Shape[1], vars.Count, "Incorrect output of Solve");
                    for (int rootid = 0; rootid < sol.Shape[1]; rootid++)
                    {
                        eqCopy = eqCopy.Substitute(vars[rootid], sol[i, rootid]);
                    }

                    foreach (var uniqvar in MathS.Utils.GetUniqueVariables(eqCopy).FiniteSet())
                    {
                        eqCopy = eqCopy.Substitute(uniqvar.Name, 3);
                    }
                    var E = Number.Abs(eqCopy.Eval());
                    Assert.IsTrue(E.IsDefinite() && E < 0.0001,
                                  "i: " + i + "  eq: " + eq.ToString() + "  E: " + E.ToString());
                }
            }
        }
        public void EqSysLatex()
        {
            var eq = MathS.Equations(
                "x + 3",
                "y + x + 5"
                );

            Assert.True(eq.Latexise().Length > 10);
        }
Ejemplo n.º 3
0
        internal static void AssertSystemSolvable(Entity[] equations, Entity.Variable[] vars, int rootCount, Integer?ToSub = null)
        {
            ToSub ??= 3;
            var sol = MathS.Equations(equations).Solve(vars);

            if (sol is null)
            {
                if (rootCount == 0)
                {
                    return;
                }
                else
                {
                    throw new Xunit.Sdk.XunitException($"{nameof(sol)} is null but {nameof(rootCount)} is {rootCount}");
                }
            }
            if (rootCount != -1)
            {
                Assert.Equal(rootCount, sol.RowCount);
            }
            var substitutions = new Dictionary <Entity.Variable, Entity>();

            for (int i = 0; i < sol.RowCount; i++)
            {
                foreach (var equation in equations)
                {
                    var eqCopy = equation;
                    Assert.Equal(sol.ColumnCount, vars.Length);
                    substitutions.Clear();
                    for (int rootid = 0; rootid < sol.ColumnCount; rootid++)
                    {
                        substitutions.Add(vars[rootid], sol[i, rootid]);
                    }
                    eqCopy = eqCopy.Substitute(substitutions);

                    substitutions.Clear();
                    foreach (var uniqvar in eqCopy.Vars)
                    {
                        substitutions.Add(uniqvar, ToSub);
                    }
                    eqCopy = eqCopy.Substitute(substitutions);
                    var error = eqCopy.EvalNumerical().Abs();
                    Assert.True(error.IsFinite && error < 0.0001,
                                $"\n{nameof(equation)}: {equation.InnerSimplified.Stringize()}\n{nameof(i)}: {i}\n{nameof(error)}: {error.Stringize()}\n{nameof(sol)}: {sol.Stringize()}");
                }
            }
        }
Ejemplo n.º 4
0
 public static Tensor SolveSystem(this Entity[] equations, params VariableEntity[] vars)
 => MathS.Equations(equations).Solve(vars);