public override FunctionElement SetVariableValue(string name, FunctionElement value) { if (value.IsDouble()) { return this.SetVariableValue(name, value.ToDouble()); } if (this.Name.Equals(name)) { var temp = value.Clone() as FunctionElement; temp.ForceAddFunctions(this); return temp; } return this.Clone() as FunctionElement; }
public Function ForceMul(FunctionElement e) { if (e.IsDouble() && e.ToDouble() == 1) { return this.Clone() as Function; } if (this.MathFunctions.Count != 0) { return this * e; } Function f = new Function(); this.variables.ForEach(vs => f += vs * e); f.CopyFunctions(this); return f; }
public VariablesMulriplication AddVariable(FunctionElement variable) { if (this.HasVariable(variable.Name)) { var v = this.GetVariableByName(variable.Name); if (variable.MathFunctions.Count == 0 && v.MathFunctions.Count == 0) { // x * x v.ApplyFunction("power", new Function(2)); } else { if (variable.MathFunctions.Count == 1 && variable.MathFunctions.Last() == Function.GetMathFunction("power") && v.MathFunctions.Count == 1 && v.MathFunctions.Last() == Function.GetMathFunction("power")) { // sum powers v.MathFunctions[0] = new Tuple<MathFunctions.IMathFunction, FunctionElement[]>(Function.GetMathFunction("power"), new FunctionElement[] { v.MathFunctions[0].Item2[0] + variable.MathFunctions[0].Item2[0] }); } else { if (variable.MathFunctions.Count == 0 && v.MathFunctions.Count == 1 && v.MathFunctions[0] == Function.GetMathFunction("power")) { variable.ApplyFunction("power", v.MathFunctions[0].Item2[0] + 1); } else { if (v.MathFunctions.Count == 0 && variable.MathFunctions.Count == 1 && variable.MathFunctions[0] == Function.GetMathFunction("power")) { variable.MathFunctions[0] = new Tuple<MathFunctions.IMathFunction, FunctionElement[]>(Function.GetMathFunction("power"), new FunctionElement[] { variable.MathFunctions[0].Item2[0] + 1 }); } else { this.variables.Add(variable); } } } } } else { if (variable.IsDouble()) { if (this.variables.Count == 0) { this.variables.Add(new Variable("")); } this.Constant *= variable.ToDouble(); } else { if (variable is Function && variable.IsVariableMultiplication()) { var temp = variable.ToVariableMultiplication(); temp.variables.ForEach(v => this.AddVariable(v)); this.Constant *= temp.Constant; } else { this.variables.Add(variable); } } } return this; }