/// <summary> /// Constructor /// </summary> /// <param name="creator">Base formula creator</param> public FormulaArrayObjectCreator(IFormulaObjectCreator creator) { this.creator = creator; for (int i = 0; i < creator.BinaryCount; i++) { binary.Add(new BinaryArrayOperationDetector(creator.GetBinaryDetector(i))); } for (int i = 0; i < creator.MultiOperationCount; i++) { multi.Add(new MultiArrayOperationDetector(creator.GetMultiOperationDetector(i))); } for (int i = 0; i < creator.OperationCount; i++) { IOperationDetector d = creator.GetDetector(i); TypeInfo dt = d.ToTypeInfo(); TreeTransformationAttribute da = dt.GetAttribute <TreeTransformationAttribute>(); if (da != null) { detectors.Add(d); goto detectorSelected; } detectors.Add(new ArrayOperationDetector(d)); detectorSelected: continue; } detectors.Add(ArraySingleDetector.Object); }
/// <summary> /// Initialization /// </summary> protected void Init() { update = () => { foreach (FormulaMeasurement m in measurements) { m.Update(); } }; creator = VariableDetector.GetCreator(this); }
/// <summary> /// Creates tree from formula /// </summary> /// <param name="formula">The formula</param> /// <param name="creator">The creator</param> /// <returns>The tree</returns> public static ObjectFormulaTree CreateTree(MathFormula formula, IFormulaObjectCreator creator) { ObjectFormulaTree tree = new ObjectFormulaTree(formula, creator); IObjectOperation op = tree.Operation; if (op is ITreeCreator) { ITreeCreator tc = op as ITreeCreator; ObjectFormulaTree tp = tc.Tree; if (tp != null) { return(tp); } } if (op == null) { return(null); } return(tree); }
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; }
/// <summary> /// Default constructor /// </summary> protected RationalTransformControlSystemFunctionWrapper() : base(new double[] { 1 }, new double[] { 1 }) { creator = ExtendedFormulaCreator.GetCreator(this, new IOperationDetector[] { new DerivationDetector("p", "p") }, new IBinaryDetector[0]); }
/// <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> /// Constructor /// </summary> /// <param name="creator">Creator of formula</param> public ElementaryBrackets(IFormulaObjectCreator creator) { this.creator = creator; }
internal ExtendedOperationDetector(IVariableDetector detector, IFormulaObjectCreator creator) : base(detector) { this.creator = creator; }
internal ExtendedOperationDetector(Dictionary <char, object> table, IFormulaObjectCreator creator) : base(table) { this.creator = creator; }
private void init(MathFormula formula, IFormulaObjectCreator creator) { if (formula.Count == 0) { return; } if (creator.IsBra(formula[0]) & creator.IsKet(formula[formula.Count - 1])) { MathSymbol sym = formula[formula.Count - 1]; if (sym.Count > 0) { if (sym[0].Count > 0) { MathFormula fp = new MathFormula(formula, 1, formula.Count - 2); ObjectFormulaTree tp = CreateTree(fp, creator); object rp = tp.ReturnType; IObjectOperation opp = tp.Operation; ObjectFormulaTree pow = CreateTree(sym[0], creator); IObjectOperation powOp = creator.GetPowerOperation(tp.ReturnType, pow.ReturnType); if (powOp == null) { return; } operation = powOp; children.Add(tp); children.Add(pow); y = new object[2]; return; } } if (formula[formula.Count - 1].Count >= 0) { if (formula.Count > 2) { int m = 1; for (int i = 1; i < formula.Count - 1; i++) { if (creator.IsBra(formula[i])) { ++m; } else if (creator.IsKet(formula[i])) { --m; } if (m == 0) { goto calc; } } MathFormula f = new MathFormula(formula, 1, formula.Count - 2); init(f, creator); return; } } } calc: if (processMultiOperation(formula, creator)) { return; } if (processBinary(formula, creator)) { return; } if (processOperation(formula, creator)) { return; } Normalize(); }
/// <summary> /// Processes ordinary operation /// </summary> /// <param name="formula">Formula</param> /// <param name="creator">Formula creator</param> /// <returns>True if operation exists and false otherwise</returns> protected bool processOperation(MathFormula formula, IFormulaObjectCreator creator) { int braCount = 0; ObjectFormulaTree tree = null; for (int i = 0; i < creator.OperationCount; i++) { IOperationDetector detector = creator.GetDetector(i); IOperationAcceptor acceptor = detector.Detect(formula[braCount]); if (acceptor == null) { continue; } TypeInfo dt = detector.ToTypeInfo(); TreeTransformationAttribute attr = detector.GetAttribute <TreeTransformationAttribute>(); if (attr != null) { ITreeTransformation trans = acceptor as ITreeTransformation; if (tree == null) { tree = CreateTree(new MathFormula(formula, 2 * braCount + 1, formula.Count - 1), creator); } if (tree != null) { ObjectFormulaTree tr = trans.Transform(tree); if (tr != null) { operation = tr.operation; children = tr.children; y = tr.y; return(true); } } } object type = null; if (formula.Count > 1) { if (tree == null) { tree = CreateTree(new MathFormula(formula, 2 * braCount + 1, formula.Count - 1), creator); } type = tree.ReturnType; } if (acceptor is IMultiVariableOperationAcceptor) { IMultiVariableOperationAcceptor ma = acceptor as IMultiVariableOperationAcceptor; IMultiVariableOperation multiOp = ma.AcceptOperation(formula[0]); MathSymbol s = formula[0]; ObjectFormulaTree[] trees = null; object[] types = null; if (s.HasChildren) { int n = 0; for (int j = 0; j < s.Count; j++) { if (s[j] != null) { if (!s[j].IsEmpty) { ++n; } } } trees = new ObjectFormulaTree[n]; types = new object[n]; n = 0; for (int j = 0; j < s.Count; j++) { if (s[j] != null) { if (!s[j].IsEmpty) { ObjectFormulaTree tr = CreateTree(s[j], creator); if (tr == null) { operation = null; goto mo; } trees[j] = tr; types[j] = trees[j].ReturnType; ++n; } } } } operation = multiOp.Accept(types); mo: if (operation == null) { continue; } y = new object[types.Length]; if (trees != null) { foreach (ObjectFormulaTree t in trees) { if (t != null) { children.Add(t); } } } return(true); } IObjectOperation op = acceptor.Accept(type); if (op != null) { if (op.IsPowered()) { if (formula.First.HasChildren) { ObjectFormulaTree pow = CreateTree(formula.First[0], creator); ObjectFormulaTree val = new ObjectFormulaTree(); val.operation = op; val.y = new object[op.InputTypes.Length]; val.CreateResult(); if (tree != null) { val.children.Add(tree); } IObjectOperation powOp = creator.GetPowerOperation(val.ReturnType, pow.ReturnType); if (powOp == null) { return(false); } operation = powOp; children.Add(val); children.Add(pow); y = new object[2]; return(true); } } operation = op; if (tree != null) { children.Add(tree); y = new object[1]; } return(true); } } return(false); }
/// <summary> /// Processes multi operation /// </summary> /// <param name="formula">Formula</param> /// <param name="creator">Formula creator</param> /// <returns>True if operation exists and false otherwise</returns> protected bool processMultiOperation(MathFormula formula, IFormulaObjectCreator creator) { for (int n = 0; n < creator.MultiOperationCount; n++) { IMultiOperationDetector detector = creator.GetMultiOperationDetector(n); int j = 0; int m = 0; int i = 0; //int opened = 0; int k = 0; List <ObjectFormulaTree> list = null; for (; i < formula.Count; i++) { MathSymbol symbol = formula[i]; if (creator.IsBra(symbol)) { ++m; continue; } if (creator.IsKet(symbol)) { --m; continue; } if (m != 0) { continue; } if (!detector.Detect(k, symbol)) { continue; } if (list == null) { list = new List <ObjectFormulaTree>(); } MathFormula f = new MathFormula(formula, j, i - 1); list.Add(CreateTree(f, creator)); ++k; j = i + 1; if (k == detector.Count) { f = new MathFormula(formula, j, formula.Count - 1); list.Add(CreateTree(f, creator)); object[] types = new object[list.Count]; int nOp = 0; foreach (ObjectFormulaTree tree in list) { types[nOp] = tree.ReturnType; ++nOp; } IObjectOperation op = detector.Accept(types); if (op != null) { operation = op; children = list; y = new object[list.Count]; return(true); } } } } return(false); }
/// <summary> /// Processes binary operation /// </summary> /// <param name="formula">Input formula</param> /// <param name="creator">Creator of tree</param> protected bool processBinary(MathFormula formula, IFormulaObjectCreator creator) { Dictionary <int, ObjectFormulaTree[]> dic = new Dictionary <int, ObjectFormulaTree[]>(); for (int i = 0; i < creator.BinaryCount; i++) { IBinaryDetector detector = creator.GetBinaryDetector(i); ObjectFormulaTree tA = null; ObjectFormulaTree tB = null; if (detector.AssociationDirection == BinaryAssociationDirection.LeftRight) { int m = 0; for (int j = 0; j < formula.Count - 1; j++) { MathSymbol symbol = formula[j]; if (creator.IsBra(symbol)) { ++m; continue; } if (creator.IsKet(symbol)) { --m; continue; } if (m < 0) { //FormulaEditorPerformer.ThrowErrorException(FormulaTree.ERRORS[1]); } if (j == 0 | m != 0) { continue; } IBinaryAcceptor acceptor = detector.Detect(symbol); if (acceptor == null) { continue; } if (dic.ContainsKey(j)) { tA = dic[j][0]; tB = dic[j][1]; } else { tA = CreateTree(new MathFormula(formula, 0, j - 1), creator); tB = CreateTree(new MathFormula(formula, j + 1, formula.Count - 1), creator); if ((tA == null) | (tB == null)) { continue; } dic[j] = new ObjectFormulaTree[] { tA, tB }; } if (acceptor is IChildTreeCreator) { IChildTreeCreator cc = acceptor as IChildTreeCreator; ObjectFormulaTree t = cc[new ObjectFormulaTree[] { tA, tB }]; if (t != null) { Copy(t); y = new object[children.Count]; return(true); } } operation = acceptor.Accept(tA.ReturnType, tB.ReturnType); if (operation != null) { goto start; } } } else { int m = 0; for (int j = formula.Count - 1; j > 0; j--) { MathSymbol symbol = formula[j]; if (creator.IsKet(symbol)) { ++m; continue; } if (creator.IsBra(symbol)) { --m; continue; } if (m < 0) { //!!! FormulaEditorPerformer.ThrowErrorException(FormulaTree.ERRORS[1]); } if (m != 0) { continue; } IBinaryAcceptor acceptor = detector.Detect(symbol); if (acceptor == null) { continue; } if (dic.ContainsKey(j)) { tA = dic[j][0]; tB = dic[j][1]; } else { tA = CreateTree(new MathFormula(formula, 0, j - 1), creator); tB = CreateTree(new MathFormula(formula, j + 1, formula.Count - 1), creator); if ((tA == null) | (tB == null)) { continue; } dic[j] = new ObjectFormulaTree[] { tA, tB }; } if (acceptor is IChildTreeCreator) { IChildTreeCreator cc = acceptor as IChildTreeCreator; ObjectFormulaTree t = cc[new ObjectFormulaTree[] { tA, tB }]; if (t != null) { Copy(t); y = new object[children.Count]; return(true); } } operation = acceptor.Accept(tA.ReturnType, tB.ReturnType); if (operation != null) { goto start; } } } start: if (operation != null) { children.Add(tA); children.Add(tB); y = new object[2]; return(true); } } return(false); }
/// <summary> /// Consructor from formula /// </summary> /// <param name="formula">The formula</param> /// <param name="creator">The formula object creator</param> private ObjectFormulaTree(MathFormula formula, IFormulaObjectCreator creator) { init(formula, creator); CreateResult(); Normalize(); }