public void TestXPlus2Equals4() { DependentVariable <AlgebrableInt> x = new DependentVariable <AlgebrableInt>(); _(x + 2 == 4); Assert.AreEqual(2, x.Value); }
public void TestMultipleEquations() { DependentVariable <AlgebrableInt> x = new DependentVariable <AlgebrableInt>(); DependentVariable <AlgebrableInt> y = new DependentVariable <AlgebrableInt>(); _((y + 3) * 6 == 42, x + y == 10); Assert.AreEqual(6, x.Value); }
/// <summary> /// Determines if an expression contains the specified dependent variable. /// </summary> /// <returns><c>true</c> if an expression contains the specified dependent variable; otherwise, <c>false</c>.</returns> /// <param name="expression">The expression to search.</param> /// <param name="dv">The dependent variable to find.</param> /// <typeparam name="T">The type of the dependent variable.</typeparam> public static bool HasDV <T>(Algebrable expression, DependentVariable <T> dv) where T : Algebrable { if (expression is AlgebraOperation) { return(HasDV((AlgebraOperation)expression, dv)); } else { return(((object)expression) == ((object)dv)); } }
public void TestFlippingExpressions() { DependentVariable <AlgebrableInt> x = new DependentVariable <AlgebrableInt>(); _(x + 2 == 4); Assert.AreEqual(2, x.Value); DependentVariable <AlgebrableInt> y = new DependentVariable <AlgebrableInt>(); _(2 + y == 4); Assert.AreEqual(2, y.Value); }
public void TestIndependentVariables() { IndependentVariable <AlgebrableInt> width = (AlgebrableInt)0; IndependentVariable <AlgebrableInt> height = (AlgebrableInt)0; DependentVariable <AlgebrableInt> area = new DependentVariable <AlgebrableInt>(); _(area == width * height); Assert.AreEqual(0, area.Value); width.Value = 42; height.Value = 4; Assert.AreEqual(168, area.Value); }
/// <summary> /// Determines if an expression contains the specified dependent variable. /// </summary> /// <returns><c>true</c> if an expression contains the specified dependent variable; otherwise, <c>false</c>.</returns> /// <param name="expression">The expression to search.</param> /// <param name="dv">The dependent variable to find.</param> /// <typeparam name="T">The type of the dependent variable.</typeparam> public static bool HasDV <T>(AlgebraOperation expression, DependentVariable <T> dv) where T : Algebrable { return(HasDV(expression.Left, dv) || HasDV(expression.Right, dv)); }
/// <summary> /// Solved this equation for the specified dependent variable. /// </summary> /// <param name="dv">The dependent variable to solve for.</param> /// <typeparam name="T">The type of the dependent variable.</typeparam> public T Solve <T>(DependentVariable <T> dv) where T : Algebrable { if (HasDV(Left, dv)) { if (HasDV(Right, dv)) { throw new NotImplementedException("A DV on both sides of an equation is not yet supported"); } else { Algebrable left = Left; Algebrable right = Right; while (((object)left) != ((object)dv)) { if (left is AlgebraOperation) { AlgebraOperation op = (AlgebraOperation)left; Algebrable subLeft; Algebrable subRight; if (HasDV(op.Left, dv)) { if (HasDV(op.Right, dv)) { throw new NotImplementedException("Only 1 DV is currently supported"); } else { subLeft = op.Left; subRight = op.Right; } } else if (HasDV(op.Right, dv)) { op = op.Flip; subLeft = op.Left; subRight = op.Right; } else { throw new InvalidOperationException("Internal error"); } left = subLeft; right = op.Inverse[right]; } else { throw new NotImplementedException("Unknown equation type"); } } return((T)right.Evaluate()); } } else if (HasDV(Right, dv)) { return(Inverse.Solve(dv)); } else { throw new InvalidOperationException("DV does not exist in equation"); } }