public VariablesMulriplication(FunctionElement[] variables, double constant = 1) { this.variables = new List<FunctionElement>(); this.Constant = constant; foreach (var v in variables) { this.AddVariable(v); } }
public Function SetVariableValue(string name, FunctionElement value) { if (this.variables.Count == 0) return new Function(); Function f = new Function(1); this.variables.ForEach(delegate(FunctionElement v) { var temp = v.Clone() as FunctionElement; if (!temp.IsDouble() && temp.HasVariable(name)) { f *= temp.SetVariableValue(name, value); } else { f *= temp; } }); return f * this.Constant; }
public override Dictionary<string, FunctionElement> GetVariablesByConstant(FunctionElement e) { var l = e.ToLeaf() as ListVariable; if (l == null || l.Values.Length != this.Values.Length) { return null; } Dictionary<string, FunctionElement> d = new Dictionary<string, FunctionElement>(); for (int i = 0; i < this.Values.Length; i++) { if (!this.Values[i].IsConstant()) { var d1 = this.Values[i].GetVariablesByConstant(l.Values[i]); if (d1 == null) { return null; } foreach (var item in d1) { if (d.ContainsKey(item.Key)) { if (!d[item.Key].Equals(item.Value)) { return null; } } else { d.Add(item.Key, item.Value); } } } else { if (!this.Values[i].Equals(l.Values[i])) { return null; } } } return d; }
public override FunctionElement SetVariableValue(string name, FunctionElement value) { List<FunctionElement> fs = new List<FunctionElement>(); foreach (var v in this.Values) { fs.Add(v.SetVariableValue(name, value)); } return new ListVariable(fs.ToArray()); }
public void ReplaceThisWithElement(FunctionElement el) { this._initProperties(); this._isModified = true; if (el is Function) { this.variables = new List<VariablesMulriplication>((el as Function).variables); this.CopyFunctions(el); } else { this.AddVariablesMul(new VariablesMulriplication(el)); this.CopyFunctions(el); } }
public override Dictionary<string, FunctionElement> GetVariablesByConstant(FunctionElement e) { if (this.IsLeaf()) { return this.ToLeaf().GetVariablesByConstant(e); } return new Equation(this, new Function(e)).RigorousSolution(); //throw new NotImplementedException(); }
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; }
private static Function _mul(FunctionElement e1, FunctionElement e2) { return e1.ToVariableMultiplication() * e2; }
public FunctionElement ApplyFunctions(FunctionElement e) { var f = this.Clone() as FunctionElement; foreach (var mf in e.MathFunctions) { FunctionElement[] _params = new FunctionElement[mf.Item2.Length]; for (int i = 0; i < mf.Item2.Length; i++) { _params[i] = mf.Item2[i].Clone() as FunctionElement; } f = f.ApplyFunction(mf.Item1.FunctionName, _params); } return f; }
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 override Dictionary<string, FunctionElement> GetVariablesByConstant(FunctionElement e) { return new Dictionary<string, FunctionElement>() { { this.Name, this.ReverseAllFunctionsTo(e).Item2 } }; }
public Function GetRandomValue() { switch (this.Type) { case ColorSetType.Int: if (this.ParsedAttributes[ColorSet.MinValueAttribute] == ColorSet.EmptyAttribute || this.ParsedAttributes[ColorSet.MaxValueAttribute] == ColorSet.EmptyAttribute) { throw new Exception(); } return new Function(r.Next(Int32.Parse(this.ParsedAttributes[ColorSet.MinValueAttribute]), Int32.Parse(this.ParsedAttributes[ColorSet.MaxValueAttribute]))); case ColorSetType.String: throw new Exception(); case ColorSetType.Enum: return new Function(this.ParsedAttributes[Function.KeyOfIndex(r.Next(1, this.ParsedAttributes.Count))]); case ColorSetType.Bool: return new Function(r.Next(0, 1) == 0 ? ColorSet.TrueDefault : ColorSet.FalseDefault); case ColorSetType.Unit: return new Function(ColorSet.UnitDefault); case ColorSetType.Index: return new Function(String.Format("{0}[{1}]", this.Name, r.Next(Int32.Parse(ColorSet.FromAttribute), Int32.Parse(ColorSet.ToAttribute)))); case ColorSetType.Record: StringBuilder sb = new StringBuilder(); foreach (var attr in this.ParsedAttributes) { sb.AppendFormat("{0}, ", this.Collection.GetColorSet(attr.Value).GetRandomValue()); } sb = sb.Remove(sb.Length - 2, 2); return new Function(String.Format("{1} {0} {2}", sb.ToString(), "{", "}")); case ColorSetType.List: if (this.ParsedAttributes[ColorSet.MinLengthAttribute] == ColorSet.EmptyAttribute || this.ParsedAttributes[ColorSet.MaxLengthAttribute] == ColorSet.EmptyAttribute) { throw new Exception(); } int count = r.Next( Int32.Parse(this.ParsedAttributes[ColorSet.MinLengthAttribute]), Int32.Parse(this.ParsedAttributes[ColorSet.MaxLengthAttribute])); FunctionElement[] elements = new FunctionElement[count]; for (int i = 0; i < count; i++) { elements[i] = this.Collection.GetColorSet(this.ParsedAttributes[ColorSet.ElementsColor]).GetRandomValue(); } return new Function(new ListVariable(elements)); default: throw new Exception(); } }
public FunctionElement CalculateRevers(FunctionElement d1, FunctionElement d2) { return base.CalculateReverse(d1, d2); }
private string getVarString(FunctionElement e) { //var degree = Math.Abs(e.Degree); //if (degree == 1) //{ // return e.Name; //} ///*if (degree == 0.5) //{ // return String.Format("sqrt({0})", e.Name); //}*/ //if (e is Variable) //{ // return String.Format("{0}^{1}", e.Name, degree); //} //else //{ // if ((e as Function).IsNeedBracketsForPower()) // { // return String.Format("({0})^{1}", e.Name, degree); // } // else // { // return String.Format("{0}^{1}", e.Name, degree); // } //} return e.ToString(); }
private string getVarMLString(FunctionElement e, bool needBrackets = true) { //MagicLibrary.MathUtils.MathFunctions.PowerFunction p = new MathFunctions.PowerFunction(e.Degree); //var format = p.FormatPower(); //var name = e.ToMathMLShort(); //if (e is Function && (e as Function).IsNeedBracketsForPower() && ( // p.IsNeedBrackets() || // needBrackets // )) //{ // name = String.Format("<mfenced><mrow><mi>{0}</mi></mrow></mfenced>", name); //} //else //{ // name = String.Format("<mi>{0}</mi>", name); //} //return String.Format(format, name); return e.ToMathML(); }
public abstract FunctionElement SetVariableValue(string name, FunctionElement value);
private static Function _div(double d, FunctionElement e) { return new VariablesMulriplication(d) / e; }
public double Calculate(double d) { double result = d; foreach (var mf in this.MathFunctions) { FunctionElement[] _params = new FunctionElement[mf.Item2.Length + 1]; _params[0] = new Function(result); for (int i = 0; i < mf.Item2.Length; i++) { _params[i + 1] = mf.Item2[i]; } result = mf.Item1.Calculate(_params).ToDouble(); } return result; }
private static Function _mul(FunctionElement e, double d) { return e.ToVariableMultiplication() * d; }
public void CopyFunctions(FunctionElement e) { this._isModified = true; this.MathFunctions.Clear(); this.ForceAddFunctions(e); }
public override FunctionElement SetVariableValue(string name, FunctionElement e) { Function newF = new Function(); this.variables.ForEach(v => newF += v.SetVariableValue(name, e)); //newF.Degree = this.Degree; //newF.Functions = new List<MathFunctions.IMathFunction>(this.Functions); newF.ForceAddFunctions(this); foreach (var mf in newF.MathFunctions) { for (int i = 0; i < mf.Item2.Length; i++) { mf.Item2[i] = mf.Item2[i].SetVariableValue(name, e); } } return newF.Recalc(); }
/// <summary> /// Force append of function of anouther function element /// </summary> /// <param name="e"></param> public void ForceAddFunctions(FunctionElement e) { this._isModified = true; foreach (var mf in e.MathFunctions) { FunctionElement[] _params = new FunctionElement[mf.Item2.Length]; for (int i = 0; i < mf.Item2.Length; i++) { _params[i] = mf.Item2[i].Clone() as FunctionElement; } this.ForceAddFunction(mf.Item1.FunctionName, _params); } }
public Function(FunctionElement el) { this.ReplaceThisWithElement(el); }
public abstract Dictionary<string, FunctionElement> GetVariablesByConstant(FunctionElement e);
private static Function _fromStringWithoutBracers(string func, List<Function> fs) { string f = func.Trim(); string _fMask = String.Format("^{0}$", Regex.Escape(String.Format(Function._funcMask, "```")).Replace("```", "(?<index>\\d+)")); string mask = _fMask;//@"^\[_f(?<index>\d+)\]$"; var m = Regex.Match(f, mask); if (m.Success) { var index = Int32.Parse(m.Groups["index"].Value); if (index >= fs.Count) { throw new InvalidFunctionStringException(func); } return fs[index]; } // check math functions foreach (var mf in Function._allFunctions) { string[] _paramsIndexes = new string[mf.ParamsCount]; for (int i = 0; i < mf.ParamsCount; i++) { _paramsIndexes[i] = String.Format(@"\s*(?<p{0}>.+)\s*", i); } mask = String.Format(String.Format("^{0}$", mf.ToStringFormat), _paramsIndexes); m = Regex.Match(f, mask); if (m.Success) { try { FunctionElement[] _params = new FunctionElement[mf.ParamsCount]; for (int i = 0; i < mf.ParamsCount; i++) { _params[i] = Function._fromStringWithoutBracers(m.Groups[String.Format("p{0}", i)].Value, fs); } try { return new Function(mf.Calculate(_params)); } // Skip this, because it can be other function with same signature.. Блять, прям перегрузка функций catch (InvalidMathFunctionParameters) { } } catch (InvalidFunctionStringException) { } } } // Digit mask = @"^\s*(?<value>(?<minus>-?)\s*(?<digit>\d+([\.\,]\d+)?))\s*$"; m = Regex.Match(f, mask); if (m.Success) { return new Function(Double.Parse(m.Groups["minus"].Value + m.Groups["digit"].Value)); } // Variable try { var v = new Variable(""); v.ParseFromString(f); return new Function(v); } catch { } try { var s = new StringVariable(""); s.ParseFromString(f); return new Function(s); } catch { } try { if (f.Contains('{')) { for (int i = 0; i < fs.Count; i++) { if (f.IndexOf(String.Format(Function._funcMask, i)) != -1) { f = f.Replace(String.Format(Function._funcMask, i), String.Format("({0})", fs[i].ToString())); } } var v = new RecordVariable(f); return new Function(v); } } catch { } try { if (f.Contains(ListVariable.OpenBracer)) { for (int i = 0; i < fs.Count; i++) { if (f.IndexOf(String.Format(Function._funcMask, i)) != -1) { f = f.Replace(String.Format(Function._funcMask, i), String.Format("({0})", fs[i].ToString())); } } var v = new ListVariable(f); return new Function(v); } } catch { } throw new InvalidFunctionStringException(func); }
public void OverrideFunction(FunctionElement e) { this.MathFunctions.Clear(); this.OverrideFunction(this.ApplyFunctions(e)); }
public ListVariable(FunctionElement[] values) : base() { this.MathFunctions = new List<Tuple<MathFunctions.IMathFunction, FunctionElement[]>>(); this.Values = values; }
/// <summary> /// It's work like this /// x^2 = y /// this = x^2 /// other = y /// this.ReverseAllFunctionsTo(other) /// => this = x /// other = y ^ -2 /// It change current instance! /// </summary> /// <param name="e"></param> /// <returns>Item1 - modified original object, Item2 - modified e param</returns> public Tuple<FunctionElement, FunctionElement> ReverseAllFunctionsTo(FunctionElement e) { FunctionElement e1; if (this.IsLeaf()) { e1 = this.ToLeaf(); } else { e1 = this.Clone() as FunctionElement; } var e2 = e.Clone() as FunctionElement; for (int i = e1.MathFunctions.Count - 1; i >= 0; i--) { if (!e1.MathFunctions[i].Item1.HasReverse()) { break; } List<FunctionElement> pars = new List<FunctionElement>(); pars.Add(e2); pars.AddRange(e1.MathFunctions[i].Item2); e2 = e1.MathFunctions[i].Item1.CalculateReverse(pars.ToArray()); //e2 = e2.ApplyFunction(e1.MathFunctions[i].Item1.FunctionName, e1.MathFunctions[i].Item2); e1.MathFunctions.RemoveAt(i); } Function e11 = new Function(e1); return new Tuple<FunctionElement,FunctionElement>(new Function(e1), e2); }
public FunctionMatrixElement(FunctionElement fe) { this.Function = new Function(fe); }
public bool SameMathFunctionsWith(FunctionElement e) { if (this.MathFunctions.Count != e.MathFunctions.Count) { return false; } for (int i = 0; i < this.MathFunctions.Count; i++) { if (this.MathFunctions[i].Item1 != e.MathFunctions[i].Item1 || this.MathFunctions[i].Item2.Length != e.MathFunctions[i].Item2.Length) { return false; } for (int j = 0; j < this.MathFunctions[i].Item2.Length; j++) { if (!this.MathFunctions[i].Item2[j].Equals(e.MathFunctions[i].Item2[j])) { return false; } } } return true; }