Exemplo n.º 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="symbol">Symbol of variable</param>
        /// <param name="measure">Measure</param>
        /// <param name="detector">Detector of variables</param>
        internal VariableMeasurement(string symbol, IMeasurement measure, IVariableDetector detector)
        {
            this.symbol      = symbol;
            this.measurement = measure;
            this.detector    = detector;
            object par = measure.Type;

            if (par is IOneVariableFunction)
            {
                func = par as IOneVariableFunction;
                operationDetector = new OneVariableFunctionDetector(detector);
                funcwrapper       = new OneVariableFunctionDetector(func);
            }
            else if (par is Table2D)
            {
                table2D = par as Table2D;
            }
            else if (par is Table3D)
            {
                table3D = par as Table3D;
            }

            /*!!!   else if (par is FuncReturn)
             * {
             *     funcReturn = par as FuncReturn;
             *     //operationDetector = new FormulaEditor.Func.FuncDetector(detector,  )
             * }*/
            else
            {
                acceptor = this;
            }
            tree = new ObjectFormulaTree(this, new List <ObjectFormulaTree>());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Detects operation
        /// </summary>
        /// <param name="s">First symbol of the formula</param>
        /// <returns>Acceptor of operation</returns>
        public IOperationAcceptor Detect(MathSymbol s)
        {
            IOperationAcceptor acc = detector.Detect(s);

            if (acc == null)
            {
                return(null);
            }
            if (acc is IMultiVariableOperationAcceptor)
            {
                return(new MultiVariableArrayOperationAcceptor(acc as IMultiVariableOperationAcceptor));
            }
            return(new ArrayOperationAcceptor(acc));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds acceptor
        /// </summary>
        /// <param name="type">Return type</param>
        /// <param name="acc">Acceptor to add</param>
        public void Add(object type, IOperationAcceptor acc)
        {
            List <IOperationAcceptor> l = null;

            if (addAcceptors.ContainsKey(type))
            {
                l = addAcceptors[type];
            }
            else
            {
                l = new List <IOperationAcceptor>();
                addAcceptors[type] = l;
            }
            l.Add(acc);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Sets operations to formula
 /// </summary>
 /// <param name="formula">The formula</param>
 /// <param name="table">Table of operations</param>
 public static void SetOperations(MathFormula formula, Dictionary <int, IOperationAcceptor> table)
 {
     for (int i = 0; i < formula.Count; i++)
     {
         MathSymbol s = formula[i];
         for (int j = 0; j < s.Count; j++)
         {
             SetOperations(s[j], table);
         }
         if (!(s is SeriesSymbol))
         {
             continue;
         }
         SeriesSymbol ss  = s as SeriesSymbol;
         int          ind = ss.index;
         ss.acceptor = null;
         if (!table.ContainsKey(ind))
         {
             throw new Exception("Operation with index " + ind + " does not exist");
         }
         IOperationAcceptor acc = table[ind];
         ss.acceptor = acc;
     }
 }
        /// <summary>
        /// Implements the Visitor design pattern.
        /// </summary>
        /// <param name="acceptor">
        /// The object accepting this operation.
        /// </param>
        public override void Visit(IOperationAcceptor acceptor)
        {
            Guard.NotNull(acceptor, nameof(acceptor));

            acceptor.Accept(this);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Implements the Visitor design pattern.
        /// </summary>
        /// <param name="acceptor">
        /// The object accepting this operation.
        /// </param>
        public override void Visit(IOperationAcceptor acceptor)
        {
            Guard.NotNull(acceptor, nameof(acceptor));

            acceptor.Accept(this);
        }
 /// <summary>
 /// Implements the Visitor design pattern.
 /// </summary>
 /// <param name="acceptor">
 /// The object accepting this operation.
 /// </param>
 public override void Visit(IOperationAcceptor acceptor)
 {
 }
Exemplo n.º 8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="acceptor">Prototype acceptor</param>
 public ArrayOperationAcceptor(IOperationAcceptor acceptor)
 {
     this.acceptor = acceptor;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Detects operation
        /// </summary>
        /// <param name="s">First symbol of the formula</param>
        /// <returns>Acceptor of operation</returns>
        public override IOperationAcceptor Detect(MathSymbol s)
        {
            //Double a = 0;
            if (s.Symbol == '~')
            {
                return(new BitwiseOperation());
            }
            if (s.Symbol == '¬')
            {
                return(NegationOperation.Object);
            }
            if (s.Symbol == '-')
            {
                return(Minus);
            }
            if (s.Symbol == '\u03B4')
            {
                return(new DeltaFunction());
            }
            if (s is SeriesSymbol)
            {
                SeriesSymbol ss = s as SeriesSymbol;
                if (ss.Acceptor is IUnary)
                {
                    return(new ElementaryUnaryOperation(ss.Acceptor as IUnary, ss.Index));
                }
                return(ss.Acceptor);
            }
            if (s is TernaryFunctionSymbol)
            {
                if (s.Symbol == '3')
                {
                    return(TernaryBrackets.Singleton);
                }
            }
            if (s is BinaryFunctionSymbol)
            {
                if (s.Symbol == '2')
                {
                    return(BinaryBrackets.Singleton);
                }
                return(ElementaryAtan2.Object);
            }

            if (s is RootSymbol)
            {
                return(new ElementaryRoot());
            }
            if (s is FractionSymbol)
            {
                return(ElementaryFraction.Object);
            }
            if (s is BracketsSymbol)
            {
                return(Brakets);
            }
            if (s is SubscriptedSymbol)
            {
                SubscriptedSymbol sym = s as SubscriptedSymbol;
                if (table != null)
                {
                }
                return(new ElementaryObjectVariable(sym.Pair, table));
            }
            if (s.SymbolType == (byte)FormulaConstants.Variable)
            {
                if (s.Symbol == '%')
                {
                    return(new TranscredentRealConstant('p'));
                }
                if (s.Symbol == 'e')
                {
                    return(new TranscredentRealConstant('e'));
                }
                if (s.String.Equals("True"))
                {
                    return(new BooleanConstant(true));
                }
                if (s.String.Equals("False"))
                {
                    return(new BooleanConstant(false));
                }
                IOperationAcceptor var = detector.Detect(s);
                if (var != null)
                {
                    return(var);
                }
                else
                {
                    return(null);
                }
            }
            if (s.SymbolType == (byte)FormulaConstants.Number)
            {
                if (s.Symbol == '?')
                {
                    return(new ElementaryRealConstant(s.DoubleValue));
                }
                else
                {
                    return(new ElementaryULongConstant(s.ULongValue));
                }
            }
            if (s.SymbolType == (byte)FormulaConstants.Unary & s.Symbol != '\u2211' & !(s.Symbol + "").Equals("'"))
            {
                string str = s.Symbol + "";
                if (str.Equals(str.ToLower()))
                {
                    if (s.Symbol == 'w')
                    {
                        return(new TimeOperation());
                    }
                    if (s.Symbol == 'o')
                    {
                        return(TimeToDoubleOperation.Object);
                    }
                    return(new ElementaryFunctionOperation(s.Symbol));
                }
                return(new ElementaryIntegerOperation(s.Symbol));
            }
            return(null);
        }
Exemplo n.º 10
0
        /// <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>
 /// Implements the Visitor design pattern.
 /// </summary>
 /// <param name="acceptor">
 /// The object accepting this operation.
 /// </param>
 public abstract void Visit(IOperationAcceptor acceptor);