internal Inverser(AlgebraOperation op) { Op = op; }
private void AddDependents(AlgebraOperation op) { AddDependents(op.Left); AddDependents(op.Right); }
/// <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"); } }