/// <summary> /// Iterates calculation of derivation /// </summary> /// <param name="next">The "next" msign</param> /// <param name="addition">Associated addition</param> /// <returns>Next derivation iteration</returns> public FormulaMeasurementDerivation Iterate(bool next, AssociatedAddition addition) { string dn = "D" + name; ObjectFormulaTree t = tree.Derivation("d/dt"); IDistribution d = DeltaFunction.GetDistribution(t); AssociatedAddition aa = FormulaMeasurementDerivation.Create(associated); if (next) { FormulaMeasurementDerivation der = null; if (d != null) { der = new FormulaMeasurementDerivationDistribution(t, dn, aa); } else { der = new FormulaMeasurementDerivation(t, dn, aa); } derivation = der; return(der); } if (d != null) { derivation = new FormulaMeasurementDistribution(t, dn, aa); } else { derivation = new FormulaMeasurement(t, dn, aa); } return(null); }
/// <summary> /// Constructor /// </summary> /// <param name="symbol">Formula symbol</param> /// <param name="alias">Alias object</param> /// <param name="name">Alias name</param> public AliasNameVariable(string symbol, IAlias alias, string name) { this.alias = alias; this.name = name; this.symbol = symbol; tree = new ObjectFormulaTree(this, new List <ObjectFormulaTree>()); }
ObjectFormulaTree IDerivationOperation.Derivation(ObjectFormulaTree tree, string s) { if (s.Equals("d/dt")) { return(this.tree); } return(null); }
public FormulaMeasurement(ObjectFormulaTree tree, string name, AssociatedAddition associated) { this.tree = tree; this.name = name; this.associated = associated; par = GetDefaultValue; }
private void UpdateFormulas() { foreach (char c in variables.Keys) { object[] o = variables[c] as object[]; ObjectFormulaTree tree = o[1] as ObjectFormulaTree; o[3] = tree.Result; } }
/// <summary> /// Converts a tree to AliasName /// </summary> /// <param name="tree"></param> /// <returns>AliasName</returns> static public IAliasName ToAliasName(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is AliasNameVariable) { return((op as AliasNameVariable).AliasName); } return(null); }
/// <summary> /// Converts a tree to Measurement /// </summary> /// <param name="tree"></param> /// <returns>Measurement</returns> static public IMeasurement ToMeasurement(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is VariableMeasurement) { return((op as VariableMeasurement).Measurement); } return(null); }
/// <summary> /// Creates array code /// </summary> /// <param name="tree">Base tree</param> /// <param name="ret">Return</param> /// <param name="parameters">Variables</param> /// <param name="variables">Parameters</param> /// <param name="initializers">Initializers</param> /// <returns>List of code strings</returns> protected IList <string> CreateTreeCode(ObjectFormulaTree tree, string ret, string[] parameters, out IList <string> variables, out IList <string> initializers) { List <string> l = new List <string>(); if (tree.ReturnType.IsEmpty()) { variables = new List <string>(); initializers = new List <string>(); return(l); } int n = StaticCodeCreator.GetNumber(this, tree); string curr = "trees[" + n + "]."; // l.Add("currentTree = trees[" + n + "];"); int count = tree.Count; List <string> vari = new List <string>(); if (count > 0) { string ta = "treeArray_" + n; l.Add("currentArray = " + ta + ";"); vari.Add("object[] " + ta + " = new object[" + count + "];"); } for (int i = 0; i < count; i++) { ObjectFormulaTree t = tree[i]; if (t == null) { continue; } if (count > 0) { string id = codeCreator[t]; l.Add("currentArray[" + i + "] = " + id + ";"); } } string ss = ""; string tt = typeCreator.GetType(tree.ReturnType); if (!tt.Equals("object")) { ss = "(" + tt + ")"; } if (count > 0) { l.Add(ret + " = " + ss + curr + "Calculate(currentArray);"); } else { l.Add(ret + " = " + ss + curr + "Calculate();"); } initializers = new List <string>(); variables = vari; return(l); }
/// <summary> /// Creates code from trees /// </summary> /// <param name="trees">The trees</param> /// <param name="creator">Code creator</param> /// <param name="local">Local code reator</param> /// <param name="variables">Strings of variables</param> /// <param name="initializers">Strings of initializers</param> /// <returns>Strings of code</returns> public static IList <string> CreateCode(ObjectFormulaTree[] trees, ICodeCreator creator, out ICodeCreator local, out IList <string> variables, out IList <string> initializers) { List <string> code = new List <string>(); List <string> vari = new List <string>(); List <string> init = new List <string>(); local = creator.Create(trees); IList <ObjectFormulaTree> lt = local.Trees; if (local.Optional.Count > 0) { return(CreateOptionalCode(local, out variables, out initializers)); } foreach (ObjectFormulaTree t in lt) { string ret = local[t]; IList <string> par = new List <string>(); int n = t.Count; for (int i = 0; i < n; i++) { ObjectFormulaTree child = t[i]; if (child == null) { continue; } par.Add(local[child]); } IList <string> lv; IList <string> lp; IList <string> c = local.CreateCode(t, ret, par.ToArray <string>(), out lv, out lp); if (lv != null) { vari.AddRange(lv); } if (lp != null) { init.AddRange(lp); } if (creator.GetConstValue(t) == null) { code.AddRange(c); } else if (creator.GetConstValue(t).Equals("\"\"")) { code.AddRange(c); } } variables = vari; initializers = init; return(code); }
/// <summary> /// Calculates derivation /// </summary> /// <param name="tree">The tree for derivation calculation</param> /// <param name="variableName">Name of variable</param> /// <returns>The derivation tree</returns> ObjectFormulaTree IDerivationOperation.Derivation(ObjectFormulaTree tree, string variableName) { if (variableName.Equals("d/d" + this.variableName)) { // If name of variable is equal to differential variable // Then returns 1 return(new ObjectFormulaTree(new Unity(), new List <ObjectFormulaTree>())); } // Returns 0 return(new ObjectFormulaTree(new Zero(), new List <ObjectFormulaTree>())); }
private void AddTree(ObjectFormulaTree tree, IList <string> init, IList <string> func) { int n = StaticCodeCreator.GetNumber(local, tree); string tid = local[tree]; string f = "Get_" + n; init.Add("dictionary[trees[" + n + "]] = " + f + ";"); func.Add(""); func.Add("object " + f + "()"); func.Add("{"); func.Add("\treturn " + tid + ";"); func.Add("}"); }
private static void GetList(ObjectFormulaTree tree, List <ObjectFormulaTree> l, List <ObjectFormulaTree> busy) { int n = tree.Count; for (int i = 0; i < n; i++) { GetList(tree[i], l, busy); } if (!busy.Contains(tree)) { l.Add(tree); } }
private void init() { arg.Clear(); for (int i = 0; i < 3; i++) { MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, formulaStrings[i]); formulas[i] = f; f = f.FullTransform(null); ObjectFormulaTree t = ObjectFormulaTree.CreateTree(f); trees[i] = t; arg.Add(t); } }
/// <summary> /// Creates code from tree /// </summary> /// <param name="tree">The tree</param> /// <param name="ret">Return identifier</param> /// <param name="parameters">Parameters of tree</param> /// <param name="variables">Variables of tree</param> /// <param name="initializers">Initializers</param> /// <returns>List of code strings</returns> protected IList <string> CreateArrayCode(ObjectFormulaTree tree, string ret, string[] parameters, out IList <string> variables, out IList <string> initializers) { List <string> vari = new List <string>(); List <string> init = new List <string>(); variables = vari; initializers = init; IObjectOperation op = tree.Operation; if (!(op is ArrayOperation)) { return(null); } ArrayOperation ao = op as ArrayOperation; object[] types = ao.Types; string[] par = new string[parameters.Length]; for (int i = 0; i < par.Length; i++) { par[i] = GetModifier(types[i]) + parameters[i]; } ArrayReturnType art = ao.ReturnType as ArrayReturnType; List <ObjectFormulaTree> ch = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { if (tree[i] != null) { ch.Add(tree[i]); } } ObjectFormulaTree t = new ObjectFormulaTree(ao.SingleOperation, ch); List <string> list = new List <string>(); bool success; if (cycle) { ProcessCycleArrayCode(0, tree, t, ret + "[", par, parameters, types, art, list, vari, init, out success); } else { ProcessArrayCode(0, tree, t, ret + "[", par, types, art, list, vari, init, out success); } if (!success) { return(null); } return(list); }
/// <summary> /// Separators /// </summary> /// <param name="tree">Tree</param> /// <returns>operation separators</returns> public new virtual string[] this[ObjectFormulaTree tree] { get { string[] ss = null; IObjectOperation op = tree.Operation; ss = GetMultiSeparator(op); if (ss != null) { return(ss); } return(null); } }
/// <summary> /// Gets constant string representation of value of tree /// </summary> /// <param name="tree">The tree</param> /// <returns>String representation</returns> public override string GetConstValue(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is ElementaryRealConstant) { ElementaryRealConstant co = op as ElementaryRealConstant; return(co.StringValue); } if (op.ReturnType.Equals("")) { return("\"\""); } return(null); }
ObjectFormulaTree IDerivationOperation.Derivation(ObjectFormulaTree tree, string s) { if (derivation != null) { return(derivation); } if (!(measurement is IDerivation)) { throw new Exception("VariableMeasure.Derivation"); } IDerivation d = measurement as IDerivation; VariableMeasurement mea = new VariableMeasurement("", d.Derivation, null); derivation = new ObjectFormulaTree(mea, new List <ObjectFormulaTree>()); return(derivation); }
/// <summary> /// Creates measure /// </summary> /// <param name="tree">Tree</param> /// <param name="n">Number of tree</param> /// <param name="name">Name</param> /// <param name="associated">Associated addition</param> /// <returns>Measurement</returns> public static FormulaMeasurement Create(ObjectFormulaTree tree, int n, string name, AssociatedAddition associated) { object ret = null; try { ret = tree.Result; } catch (Exception ex) { ex.ShowError(-1); } FormulaMeasurement fm; if (n == 0) { IDistribution d = DeltaFunction.GetDistribution(tree); if (d != null) { return(new FormulaMeasurementDistribution(tree, name, associated)); } fm = new FormulaMeasurement(tree, name, associated); fm.ret = ret; return(fm); } string dn = "D" + name; ObjectFormulaTree t = tree.Derivation("d/dt"); if (t == null) { throw new Exception("VariableMeasure.Derivation"); } AssociatedAddition aa = FormulaMeasurementDerivation.Create(associated); FormulaMeasurement der = Create(t, n - 1, dn, aa); fm = new FormulaMeasurementDerivation(tree, der, name, aa); try { fm.ret = t.Result; } catch (Exception exc) { exc.ShowError(10); } return(fm); }
/// <summary> /// Constructor /// </summary> /// <param name="trr">Collection of trees</param> protected AbstractCodeCreator(ObjectFormulaTree[] trr) { codeCreator = this; ObjectFormulaTree[] t = trr; if (t == null) { t = new ObjectFormulaTree[0]; } trees = ObjectFormulaTree.CreateList(t, optional).ToArray(); int i = 0; foreach (ObjectFormulaTree tr in trees) { ident[tr] = "var_" + i; dictionary[tr] = i; ++i; } }
/// <summary> /// Updates measurements data /// </summary> public void UpdateMeasurements() { if (IsUpdated) { return; } try { if (par == null) { throw new Exception(DynamicalParameter.UndefinedParameters); } UpdateChildrenData(); (par as Formula.DynamicalParameter).Set(arg); result = (double)tree.Result; derivation = 0; string str = arg.Variables; foreach (char c in str) { string s = c + ""; if (parameters.ContainsKey(s)) { continue; } IMeasurement m = par[c]; if (!(m is IDerivation)) { derivation = 0; break; } IDerivation p = m as IDerivation; ObjectFormulaTree t = derivations[c] as ObjectFormulaTree; derivation += (double)t.Result * (double)p.Derivation.Parameter(); } isUpdated = true; } catch (Exception e) { e.ShowError(10); this.Throw(e); } }
/// <summary> /// Creates Code from tree /// </summary> /// <param name="tree">The tree</param> /// <param name="ret">Return value</param> /// <param name="parameters">Parameters</param> /// <param name="variables">Variables</param> /// <param name="initializers">Initializers</param> /// <returns>List of code</returns> public virtual IList <string> CreateCode(ObjectFormulaTree tree, string ret, string[] parameters, out IList <string> variables, out IList <string> initializers) { variables = null; initializers = null; if (creators == null) { return(null); } foreach (ICodeCreator cc in creators) { IList <string> l = cc.CreateCode(tree, ret, parameters, out variables, out initializers); if (l != null) { return(l); } } return(null); }
/// <summary> /// Gets number of tree /// </summary> /// <param name="creator">Creator of code</param> /// <param name="tree">The tree</param> /// <returns>Number of tree</returns> public static int GetNumber(ICodeCreator creator, ObjectFormulaTree tree) { try { return(creator.GetNumber(tree)); /* ObjectFormulaTree[] trees = creator.Trees; * for (int i = 0; i < trees.Length; i++) * { * if (trees[i] == tree) * { * return i; * } * }*/ } catch (Exception exception) { throw new Exception("Tree not found"); } }
/// <summary> /// Creates code for array operation /// </summary> /// <param name="tree">Tree</param> /// <param name="ret">Return</param> /// <param name="parameters">Parameters of tree</param> /// <param name="variables">Variables</param> /// <param name="initializers">Initializers</param> /// <returns>List of code strings</returns> protected IList <string> CreateArraySingleCode(ObjectFormulaTree tree, string ret, string[] parameters, out IList <string> variables, out IList <string> initializers) { variables = new List <string>(); initializers = new List <string>(); IObjectOperation op = tree.Operation; if (!(op is ArraySingleOperation)) { return(null); } ArraySingleOperation ars = op as ArraySingleOperation; string ops = (ars.Type == ArraySingleOperationType.Sum) ? " + " : " * "; ObjectFormulaTree t = tree[0]; ArrayReturnType art = t.ReturnType as ArrayReturnType; int n = art.Dimension[0]; string sp = ""; if (art.IsObjectType) { sp = "(double)"; } StringBuilder sb = new StringBuilder(); sb.Append(ret); sb.Append(" = "); string par = sp + parameters[0] + "["; sb.Append(par + "0]"); par = ops + par; for (int i = 1; i < n; i++) { sb.Append(par); sb.Append(i); sb.Append("]"); } sb.Append(";"); return(new List <string> { { sb.ToString() } }); }
void Set(Dictionary <string, string> variables) { MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, formula); measures.Clear(); Dictionary <char, object> table = new Dictionary <char, object>(); foreach (string key in variables.Keys) { IMeasurement m = this.FindMeasurement(variables[key], false); measures[key[0]] = m; table[key[0]] = m.Type; } IFormulaObjectCreator creator = VariableDetector.GetCreator(table); f = f.FullTransform(null); tree = ObjectFormulaTree.CreateTree(f, creator); arg = new ElementaryObjectArgument(); arg.Add(tree); this.variables = variables; }
public virtual void CreateSystem(string formula) { double[] no = new double[nom.Length]; double[] dn = new double[denom.Length]; try { Array.Copy(nom, no, nom.Length); Array.Copy(denom, dn, dn.Length); MathFormula f = AcceptFormula(formula); MathFormula form = f.FullTransform(""); ObjectFormulaTree tree = ObjectFormulaTree.CreateTree(form, creator); IObjectOperation op = tree.Operation; if (!(op is ElementaryFraction)) { throw new Exception("Operation should be fraction"); } double[][] p = new double[2][]; for (int i = 0; i < 2; i++) { ObjectFormulaTree t = tree[i]; p[i] = CreatePolynom(tree[i]); } Set(p[0], p[1]); if (!IsStable & ShouldStable) { throw new Exception("System is not stable"); } this.formula = formula; Solver = DifferentialEquationsPerformer.Default[solver]; } catch (Exception e) { e.ShowError(10); nom = new double[no.Length]; denom = new double[dn.Length]; Array.Copy(no, nom, nom.Length); Array.Copy(dn, denom, dn.Length); this.Throw(e); } }
/// <summary> /// Accepts formulas /// </summary> private void acceptFormulas() { try { AssociatedAddition aa = new AssociatedAddition(this, null); int n = Dimension; measurements = new FormulaMeasurement[n]; result = new object[n, 2]; for (int i = 0; i < n; i++) { MathFormula formula = MathFormula.FromString(MathSymbolFactory.Sizes, formulaString[i]); MathFormula f = formula.FullTransform(null); ObjectFormulaTree t = ObjectFormulaTree.CreateTree(f, ElementaryFunctionsCreator.Object); measurements[i] = FormulaMeasurement.Create(t, deriOrder, Formula_ + (i + 1), aa); } } catch (Exception e) { e.ShowError(10); this.Throw(e); } }
/// <summary> /// Accepts formulas /// </summary> private void acceptFormulas() { output.Clear(); acc.Clear(); foreach (char c in vars.Keys) { Variable v; output.Add(Variable.GetMeasure(c, this, out v)); acc[c + ""] = v; } foreach (char c in parameters.Keys) { IMeasurement m = parameters[c] as IMeasurement; VariableMeasurement v = c.Create(m, this); acc[c + ""] = v; } foreach (char c in aliases.Keys) { AliasNameVariable v = new AliasNameVariable(c, this); acc[c + ""] = v; object[] o = aliases[c] as object[]; } IAlias al = this; IList <string> l = al.AliasNames; foreach (string n in l) { if (n.Length == 1) { } } IFormulaObjectCreator creator = VariableDetector.GetCreator(this); variables.Clear(); foreach (char c in vars.Keys) { variables[c] = new object[4]; } IList <string> an = AliasNames; List <ObjectFormulaTree> tt = new List <ObjectFormulaTree>(); string proh = "\u03B4"; foreach (char c in parameters.Keys) { IMeasurement m = parameters[c]; if (m.Type is IOneVariableFunction) { proh += c; } } foreach (char c in vars.Keys) { object t = null; object[] os = vars[c] as object[]; if (an.Contains(c + "")) { t = GetType(c + ""); } else { t = os[2]; } if (t is IOneVariableFunction) { proh += c; } } foreach (char c in vars.Keys) { object[] os = vars[c] as object[]; object t = null; if (an.Contains(c + "")) { t = GetType(c + ""); } else { t = os[2]; } object[] ol = variables[c] as object[]; string f = os[1] as string; MathFormula form = MathFormula.FromString(MathSymbolFactory.Sizes, f); ObjectFormulaTree tree = ObjectFormulaTree.CreateTree(form.FullTransform(proh), creator); if (!t.Equals(tree.ReturnType)) { throw new Exception("Illegal return type"); } ol[1] = tree; tt.Add(tree); } trees = tt.ToArray(); }
/// <summary> /// Identifier of tree /// </summary> public string this[ObjectFormulaTree tree] { get { return(ident[tree]); } }
int ICodeCreator.GetNumber(ObjectFormulaTree tree) { return(dictionary[tree]); }
/// <summary> /// Gets constant string representation of value of tree /// </summary> /// <param name="tree">The tree</param> /// <returns>String representation</returns> public abstract string GetConstValue(ObjectFormulaTree tree);