private static ObjectFormulaTree sumSumInverse(ObjectFormulaTree tree) { ObjectFormulaTree tre = tree; IObjectOperation op = tree.Operation; if (op is PolySum) { PolySum ps = op as PolySum; tre = sumSumInverse(ps); op = tre.Operation; } List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); for (int i = 0; i < tre.Count; i++) { l.Add(sumSumInverse(tre[i])); } if (op is ElementaryBinaryOperation) { ElementaryBinaryOperation bo = op as ElementaryBinaryOperation; char c = bo.Symbol; if (c == '+' | c == '-') { if (PolyMult.IsZero(l[1])) { return(l[0]); } if (PolyMult.IsZero(l[0])) { return(signed(l[1], c)); } } } return(new ObjectFormulaTree(op, l)); }
/// <summary> /// Creates formula /// </summary> /// <param name="tree">Operation tree</param> /// <param name="level">Formula level</param> /// <param name="sizes">Sizes of symbols</param> /// <returns>The formula</returns> public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes) { MathFormula f = FormulaCreator.CreateFormula(tree[0], level, sizes); MathFormula p = FormulaCreator.CreateFormula(tree[1], (byte)((int)level + 1), sizes); IObjectOperation op = tree[0].Operation; if (op.IsPowered()) { if (op is ElementaryFunctionOperation) { f.First[0] = p; } else { f.Last[0] = p; } return(f); } MathFormula form = new MathFormula(level, sizes); MathSymbol s = new BracketsSymbol(); s.Append(form); form.First[0] = p; return(form); }
/// <summary> /// Checks whether a tree has a fiction /// </summary> /// <param name="tree">The tree</param> /// <returns>True if operation has fictoin</returns> static public bool HasFiction(this ObjectFormulaTree tree) { if (tree == null) { return(true); } IObjectOperation op = tree.Operation; if (op == null) { return(true); } if (StaticExtensionBaseTypes.HasAttributeBT <Attributes.FictionAttribute>(op)) { return(true); } for (int i = 0; i < tree.Count; i++) { if (tree[i].HasFiction()) { return(true); } } return(false); }
private static ObjectFormulaTree delConst(ObjectFormulaTree tree, ref bool simple) { IObjectOperation op = tree.Operation; if (op is PolySum) { PolySum ps = op as PolySum; return(delConst(ps, ref simple)); } List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { l.Add(delConst(tree[i], ref simple)); } if ((op is ElementaryBrackets) | (op is ElementaryRoot) | (op is ElementaryPowerOperation)) { if (l.Count == 2) { ObjectFormulaTree t = l[1]; if (ElementaryFormulaSimplification.IsConst(t)) { double a = (double)t.Result; if (a == 1) { simple = false; return(l[0]); } } } } return(new ObjectFormulaTree(op, l)); }
/// <summary> /// Accepts operation /// </summary> /// <param name="type">Argument type</param> /// <returns>The operation</returns> public IObjectOperation Accept(object type) { IObjectOperation ownOp = acceptor.Accept(type); if (ownOp != null) { return(ownOp); } if (type is ArrayReturnType) { ArrayReturnType a = type as ArrayReturnType; if (a.IsObjectType) { op = acceptor.Accept(a) as IMultiVariableOperation; if (op == null) { return(null); } types = new object[] { type }; ArrayOperation.CreateAllArrays(op, types, out y, out yy, out rank, out ranks); returnType = new ArrayReturnType(op.ReturnType, rank, true); return(this); } } return(null); }
/// <summary> /// Constructor /// </summary> /// <param name="operation">Tree operation</param> /// <param name="children">Children trees</param> public ObjectFormulaTree(IObjectOperation operation, List <ObjectFormulaTree> children) { if (operation is ICloneable) { ICloneable c = operation as ICloneable; this.operation = c.Clone() as IObjectOperation; } else { this.operation = operation; } if (operation.InputTypes.Length != 0) { if (children == null) { this.children = children; } if (children.Count != operation.InputTypes.Length) { this.children = children; } } this.children = children; y = new object[operation.InputTypes.Length]; CreateResult(); Normalize(); }
static internal bool IsZero(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is ElementaryRealConstant) { ElementaryRealConstant ec = op as ElementaryRealConstant; double a = ec.Value; return(a == 0); } if (!(op is PolyMult)) { return(false); } PolyMult pm = op as PolyMult; for (int i = 0; i < tree.Count; i++) { if (IsZero(tree[i])) { return(true); } } return(false); }
/// <summary> /// Constructor /// </summary> /// <param name="operation">Operation</param> /// <param name="w">Width</param> public PanelUnary(IObjectOperation operation, int w) { Width = w; this.operation = operation; ICategoryObject o = operation as ICategoryObject; label = o.Object as IObjectLabel; PictureBox p = new PictureBox(); p.Image = NamedComponent.GetImage(label); int y = 10; int x = 10; p.Left = x; p.Top = y; Controls.Add(p); y += p.Height + 10; Label lab = new Label(); lab.Text = label.RootName; //NamedComponent.GetText(label) + ""; lab.Left = x; lab.Top = y; Controls.Add(lab); y += lab.Height + 10; cb.Top = y; cb.Left = x; Controls.Add(cb); y += cb.Height + 10; Height = y; }
internal DynamicalArrayBinaryLeftOperation(object typeA, object typeB, IObjectOperation op) { types = new object[] { typeA, typeB }; ArrayReturnType art = typeA as ArrayReturnType; type = new ArrayReturnType(op.ReturnType, art.Dimension, art.IsObjectType); this.op = op; }
/// <summary> /// Calculates derivation /// </summary> /// <param name="tree">The function for derivation calculation</param> /// <param name="variableName">Derivation string</param> /// <returns>The derivation</returns> public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string variableName) { Double a = 0; bool[] b = new bool[] { false, false }; ObjectFormulaTree[] fc = new ObjectFormulaTree[2]; ObjectFormulaTree[] fd = new ObjectFormulaTree[2]; for (int i = 0; i < 2; i++) { fc[i] = tree[1 - i]; fd[i] = fc[i].Derivation(variableName); b[i] = ZeroPerformer.IsZero(fd[i]); } if (b[0] & b[1]) { return(ElementaryRealConstant.RealZero); } IObjectOperation nom = new ElementaryBinaryOperation('-', new object[] { a, a }); List <ObjectFormulaTree> nomList = new List <ObjectFormulaTree>(); for (int i = 0; i < 2; i++) { List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); l.Add(fd[1 - i]); //.Clone() as ObjectFormulaTree); l.Add(fc[i]); //.Clone() as ObjectFormulaTree); IObjectOperation o = new ElementaryBinaryOperation('*', new object[] { a, a }); nomList.Add(new ObjectFormulaTree(o, l)); } List <ObjectFormulaTree> list = new List <ObjectFormulaTree>(); if (b[0] | b[1]) { for (int i = 0; i < 2; i++) { if (b[i]) { List <ObjectFormulaTree> lt = new List <ObjectFormulaTree>(); lt.Add(nomList[i]); list.Add(new ObjectFormulaTree(new ElementaryFunctionOperation('-'), lt)); } } } else { list.Add(new ObjectFormulaTree(nom, nomList)); } IObjectOperation square = ElementaryFunctionsCreator.Object.GetPowerOperation(a, a); List <ObjectFormulaTree> squareList = new List <ObjectFormulaTree>(); squareList.Add(fc[0]); squareList.Add(new ObjectFormulaTree(new ElementaryRealConstant(2), new List <ObjectFormulaTree>())); list.Add(new ObjectFormulaTree(square, squareList)); if (list.Count != 2) { // list = list; } return(new ObjectFormulaTree(new ElementaryFraction(), list)); }
/// <summary> /// Checks whether operation is powered /// </summary> /// <param name="operation">Operation</param> /// <returns>True if operation is powered and false otherwise</returns> public static bool IsPowered(this IObjectOperation operation) { if (operation is IPowered) { IPowered p = operation as IPowered; return(p.IsPowered); } return(false); }
private static ObjectFormulaTree sumSum(ObjectFormulaTree tree) { List <ObjectFormulaTree> l = new List <ObjectFormulaTree>(); for (int i = 0; i < tree.Count; i++) { ObjectFormulaTree t = tree[i]; // t = ElementaryFormulaSimplification.Object.Simplify(t); l.Add(sumSum(t)); } IObjectOperation op = tree.Operation; if (op is ElementaryBinaryOperation) { ElementaryBinaryOperation bo = op as ElementaryBinaryOperation; char c = bo.Symbol; PolySum ps = new PolySum(true); if (c == '+' | c == '-') { bool b = c == '+'; PolySum p = new PolySum(true); Dictionary <ObjectFormulaTree, bool> dic = p.summands; for (int i = 0; i < l.Count; i++) { ObjectFormulaTree t = l[i]; if (PolyMult.IsZero(t)) { continue; } bool kb = true; if (i > 0) { kb = b; } IObjectOperation oo = t.Operation; if (oo is PolySum) { PolySum pss = oo as PolySum; Dictionary <ObjectFormulaTree, bool> d = pss.summands; foreach (ObjectFormulaTree tr in d.Keys) { bool rb = d[tr]; if (!kb) { rb = !rb; } dic[tr] = rb; } continue; } dic[t] = kb; } return(new ObjectFormulaTree(p, new List <ObjectFormulaTree>())); } } return(new ObjectFormulaTree(op, l)); }
IObjectOperation getOtherOperation(object typeA, object typeB) { foreach (IBinaryAcceptor acceptor in acceptors) { IObjectOperation op = acceptor.Accept(typeA, typeB); return(op); } return(null); }
static internal IObjectOperation GetObjectOperation(this object obj, string name) { IObjectOperation op = PropertyOperation.GetPropertyOperation(obj, name); if (op != null) { return(op); } return(FieldOperation.GetFieldOperation(obj, name)); }
/// <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> /// 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); }
internal Variable(IObjectOperation variable, ObjectFormulaTree[] children) { this.variable = variable; this.children = children; type = children[0].ReturnType; types = new object[] { type }; ArrayReturnType art = children[0].ReturnType as ArrayReturnType; retType = art.ElementType; tree = Convert(children[1]); }
/// <summary> /// Constructor /// </summary> /// <param name="f">The f</param> /// <param name="g">The g</param> private OneVariableFuntionCompostion(IOneVariableFunction f, IOneVariableFunction g) { if (!f.ReturnType.Equals(g.VariableType)) { throw new Exception(); } this.f = f; this.g = g; fo = f; go = g; }
/// <summary> /// Crates all necessary arrays /// </summary> /// <param name="op">Base operation</param> /// <param name="types">Types of variables</param> /// <param name="y">Array</param> /// <param name="yy">Array of arrays</param> /// <param name="rank">Rank</param> /// <param name="ranks">Array of ranks</param> public static void CreateAllArrays(IObjectOperation op, object[] types, out object[] y, out object[][] yy, out int[] rank, out int[][] ranks) { rank = getRank(types); ranks = new int[types.Length][]; for (int i = 0; i < ranks.Length; i++) { ranks[i] = getRank(types); } y = createArray(op.ReturnType, rank); yy = createArrays(types, rank, rank.Length); }
/// <summary> /// Gets unary parallel operation /// </summary> /// <param name="type">Type</param> /// <param name="operation">Prototype</param> /// <returns>The parallel operation</returns> static public IObjectOperation GetUnaryParallel(object type, IObjectOperation operation) { if (ParallelFactory != null) { if (ParallelCount > 0) { return(ParallelFactory.GetUnary(type, operation)); } } return(null); }
/// <summary> /// Checks whether tree is zero /// </summary> /// <param name="tree">The tree</param> /// <returns>True in case of zero and false otherwise</returns> static public bool IsZero(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is ISupportsZero) { ISupportsZero sz = op as ISupportsZero; return(sz.IsZero(tree)); } return(false); }
private void Normalize() { if (operation is ElementaryBrackets) { ObjectFormulaTree tr = children[0]; children = tr.children; operation = tr.Operation; y = tr.y; result = tr.result; tag = tr.tag; } }
/// <summary> /// Get dynamical binary right parallel operation /// </summary> /// <param name="typeLeft">Type of left part</param> /// <param name="typeRight">Type of right part</param> /// <param name="operation">Prototype</param> /// <returns>The parallel operation</returns> public static IObjectOperation GetDynamicalBinaryRight(object typeLeft, object typeRight, IObjectOperation operation) { if (ParallelFactory != null) { if (ParallelCount > 0) { return(ParallelFactory.GetDynamicalBinaryRight(typeLeft, typeRight, operation)); } } return(null); }
/// <summary> /// Gets binary operation /// </summary> /// <param name="acceptors">Array of acceptors</param> /// <param name="typeA">Return type of left subtree</param> /// <param name="typeB">Return type of right subtree</param> /// <returns>The operatiob</returns> public static IObjectOperation GetOperation(IBinaryAcceptor[] acceptors, object typeA, object typeB) { foreach (IBinaryAcceptor acc in acceptors) { IObjectOperation op = acc.Accept(typeA, typeB); if (op != null) { return(op); } } return(null); }
private IObjectOperation GetDynamicalOperation(ArrayReturnType type, IObjectOperation operation) { if (type.IsDynamicalArray()) { IObjectOperation op = StaticExtensionFormulaEditor.GetUnaryParallel(type, operation); if (op != null) { return(op); } return(new DynamicalArrayUnaryOperation(type, operation)); } return(null); }
/// <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> /// Accept operation /// </summary> /// <param name="types">Types of operands</param> /// <returns>Operation</returns> public IObjectOperation Accept(object[] types) { this.types = types; if (acceptor is IMultiVariableOperation) { IMultiVariableOperation ma = acceptor as IMultiVariableOperation; IObjectOperation ownOp = ma.Accept(types); if (ownOp != null) { return(ownOp); } } if (types == null) { return(acceptor.Accept(null)); } object[] t = new object[types.Length]; bool isArray = false; for (int i = 0; i < t.Length; i++) { t[i] = ArrayReturnType.GetBaseType(types[i]); if (types[i] is ArrayReturnType) { isArray = true; } } IObjectOperation opr = operation.Accept(t); if (opr == null) { return(null); } if (!isArray) { return(opr); } ArrayOperation.CreateAllArrays(opr, types, out y, out yy, out rank, out ranks); returnType = new ArrayReturnType(opr.ReturnType, rank, true); returnValue = y; if (types != null) { if (types.Length > 0) { yt = new object[types.Length]; } } return(this); }
/// <summary> /// Accepts operation /// </summary> /// <param name="type">Argument type</param> /// <returns>The operation</returns> public IObjectOperation Accept(object type) { IObjectOperation ownOp = acceptor.Accept(type); if (ownOp != null) { return(ownOp); } if (type is ArrayReturnType) { ArrayReturnType a = type as ArrayReturnType; if (a.IsObjectType) { IObjectOperation op = acceptor.Accept(a.ElementType); if (op == null) { return(null); } IObjectOperation dop = GetDynamicalOperation(a, op); if (dop != null) { return(dop); } return(new ArrayOperation(op, new object[] { type })); } } IObjectOperation ope = acceptor.Accept(type); if (ope != null) { return(ope); } if (type is ArrayReturnType) { ArrayReturnType a = type as ArrayReturnType; IObjectOperation op = acceptor.Accept(a.ElementType); if (op == null) { return(null); } IObjectOperation dop = GetDynamicalOperation(a, op); if (dop != null) { return(dop); } return(new ArrayOperation(op, new object[] { type })); } return(null); }
/// <summary> /// Resets distributons of tree /// </summary> /// <param name="tree">The tree</param> static public void Reset(ObjectFormulaTree tree) { IObjectOperation op = tree.Operation; if (op is IDistribution) { IDistribution d = op as IDistribution; d.Reset(); return; } for (int i = 0; i < tree.Count; i++) { Reset(tree[i]); } }