Represents a function token.
Inheritance: IToken
Beispiel #1
0
        public void EqualsSameObjectTest()
        {
            var token = new FunctionToken(Functions.Sine, 1);

            Assert.True(token.Equals(token));
            Assert.Equal(token, token);
        }
Beispiel #2
0
        public void EqualsNullTest()
        {
            var token = new FunctionToken(Functions.Sine, 1);

            Assert.False(token.Equals(null));
            Assert.NotEqual(null, token);
        }
Beispiel #3
0
        public void EqualsDiffTypeTest()
        {
            var token = new FunctionToken(Functions.Sine, 1);

            Assert.False(token.Equals(1));
            Assert.NotEqual((object)1, token);
        }
Beispiel #4
0
        public void EqualsDiffFuncTest()
        {
            var token1 = new FunctionToken(Functions.Sine, 1);
            var token2 = new FunctionToken(Functions.Cosine, 1);

            Assert.False(token1.Equals(token2));
            Assert.NotEqual(token1, token2);
        }
Beispiel #5
0
        private int _CountParams(List<IToken> tokens, int index)
        {
            var func = (FunctionToken)tokens[index];

            int countOfParams = 0;
            int brackets = 1;
            bool hasBraces = false;
            bool oneParam = true;
            int i = index + 2;
            for (; i < tokens.Count;)
            {
                var token = tokens[i];
                if (token is SymbolToken)
                {
                    var symbol = token as SymbolToken;
                    if (symbol.Symbol == Symbols.OpenBrace)
                        hasBraces = true;

                    if (symbol.Symbol == Symbols.CloseBracket || symbol.Symbol == Symbols.CloseBrace)
                    {
                        brackets--;

                        if (brackets == 0)
                            break;
                    }
                    else if (symbol.Symbol == Symbols.OpenBracket || symbol.Symbol == Symbols.OpenBrace)
                    {
                        brackets++;

                        if (oneParam)
                        {
                            countOfParams++;
                            oneParam = false;
                        }
                    }
                    else if (symbol.Symbol == Symbols.Comma)
                    {
                        oneParam = true;
                    }

                    i++;
                }
                else if (token is FunctionToken)
                {
                    if (oneParam)
                    {
                        countOfParams++;
                        oneParam = false;
                    }

                    var f = (FunctionToken)token;
                    if (f.Function == Functions.Matrix || f.Function == Functions.Vector)
                        hasBraces = true;

                    i = _CountParams(tokens, i) + 1;
                }
                else
                {
                    if (oneParam)
                    {
                        countOfParams++;
                        oneParam = false;
                    }

                    i++;
                }
            }

            if (func.Function == Functions.Vector && hasBraces)
                tokens[index] = new FunctionToken(Functions.Matrix, countOfParams);
            else
                func.CountOfParams = countOfParams;

            return i;
        }
Beispiel #6
0
        public void ToStringTest()
        {
            var token = new FunctionToken(Functions.Sine, 1);

            Assert.Equal("Function: Sine (1)", token.ToString());
        }
Beispiel #7
0
 /// <summary>
 /// Creates an expression object from <see cref="FunctionToken"/>.
 /// </summary>
 /// <param name="token">The function token.</param>
 /// <returns>An expression.</returns>
 protected virtual IExpression CreateFunction(FunctionToken token)
 {
     switch (token.Function)
     {
         case Functions.Absolute:
             return new Abs();
         case Functions.Sine:
             return new Sin();
         case Functions.Cosine:
             return new Cos();
         case Functions.Tangent:
             return new Tan();
         case Functions.Cotangent:
             return new Cot();
         case Functions.Secant:
             return new Sec();
         case Functions.Cosecant:
             return new Csc();
         case Functions.Arcsine:
             return new Arcsin();
         case Functions.Arccosine:
             return new Arccos();
         case Functions.Arctangent:
             return new Arctan();
         case Functions.Arccotangent:
             return new Arccot();
         case Functions.Arcsecant:
             return new Arcsec();
         case Functions.Arccosecant:
             return new Arccsc();
         case Functions.Sqrt:
             return new Sqrt();
         case Functions.Root:
             return new Root();
         case Functions.Ln:
             return new Ln();
         case Functions.Lg:
             return new Lg();
         case Functions.Log:
             return new Log();
         case Functions.Sineh:
             return new Sinh();
         case Functions.Cosineh:
             return new Cosh();
         case Functions.Tangenth:
             return new Tanh();
         case Functions.Cotangenth:
             return new Coth();
         case Functions.Secanth:
             return new Sech();
         case Functions.Cosecanth:
             return new Csch();
         case Functions.Arsineh:
             return new Arsinh();
         case Functions.Arcosineh:
             return new Arcosh();
         case Functions.Artangenth:
             return new Artanh();
         case Functions.Arcotangenth:
             return new Arcoth();
         case Functions.Arsecanth:
             return new Arsech();
         case Functions.Arcosecanth:
             return new Arcsch();
         case Functions.Exp:
             return new Exp();
         case Functions.GCD:
             return new GCD();
         case Functions.LCM:
             return new LCM();
         case Functions.Factorial:
             return new Fact();
         case Functions.Sum:
             return new Sum();
         case Functions.Product:
             return new Product();
         case Functions.Round:
             return new Round();
         case Functions.Floor:
             return new Floor();
         case Functions.Ceil:
             return new Ceil();
         case Functions.Derivative:
             return new Derivative();
         case Functions.Simplify:
             return new Simplify();
         case Functions.Define:
             return new Define();
         case Functions.Vector:
             return new Vector();
         case Functions.Matrix:
             return new Matrix();
         case Functions.Transpose:
             return new Transpose();
         case Functions.Determinant:
             return new Determinant();
         case Functions.Inverse:
             return new Inverse();
         case Functions.If:
             return new If();
         case Functions.For:
             return new For();
         case Functions.While:
             return new While();
         case Functions.Undefine:
             return new Undefine();
         default:
             return null;
     }
 }
Beispiel #8
0
        /// <summary>
        /// Creates an expression object from <see cref="FunctionToken"/>.
        /// </summary>
        /// <param name="token">The function token.</param>
        /// <returns>An expression.</returns>
        protected virtual IExpression CreateFunction(FunctionToken token)
        {
            IExpression exp;

            switch (token.Function)
            {
                case Functions.Add:
                    exp = new Add(); break;
                case Functions.Sub:
                    exp = new Sub(); break;
                case Functions.Mul:
                    exp = new Mul(); break;
                case Functions.Div:
                    exp = new Div(); break;
                case Functions.Pow:
                    exp = new Pow(); break;
                case Functions.Absolute:
                    exp = new Abs(); break;
                case Functions.Sine:
                    exp = new Sin(); break;
                case Functions.Cosine:
                    exp = new Cos(); break;
                case Functions.Tangent:
                    exp = new Tan(); break;
                case Functions.Cotangent:
                    exp = new Cot(); break;
                case Functions.Secant:
                    exp = new Sec(); break;
                case Functions.Cosecant:
                    exp = new Csc(); break;
                case Functions.Arcsine:
                    exp = new Arcsin(); break;
                case Functions.Arccosine:
                    exp = new Arccos(); break;
                case Functions.Arctangent:
                    exp = new Arctan(); break;
                case Functions.Arccotangent:
                    exp = new Arccot(); break;
                case Functions.Arcsecant:
                    exp = new Arcsec(); break;
                case Functions.Arccosecant:
                    exp = new Arccsc(); break;
                case Functions.Sqrt:
                    exp = new Sqrt(); break;
                case Functions.Root:
                    exp = new Root(); break;
                case Functions.Ln:
                    exp = new Ln(); break;
                case Functions.Lg:
                    exp = new Lg(); break;
                case Functions.Lb:
                    exp = new Lb(); break;
                case Functions.Log:
                    exp = new Log(); break;
                case Functions.Sineh:
                    exp = new Sinh(); break;
                case Functions.Cosineh:
                    exp = new Cosh(); break;
                case Functions.Tangenth:
                    exp = new Tanh(); break;
                case Functions.Cotangenth:
                    exp = new Coth(); break;
                case Functions.Secanth:
                    exp = new Sech(); break;
                case Functions.Cosecanth:
                    exp = new Csch(); break;
                case Functions.Arsineh:
                    exp = new Arsinh(); break;
                case Functions.Arcosineh:
                    exp = new Arcosh(); break;
                case Functions.Artangenth:
                    exp = new Artanh(); break;
                case Functions.Arcotangenth:
                    exp = new Arcoth(); break;
                case Functions.Arsecanth:
                    exp = new Arsech(); break;
                case Functions.Arcosecanth:
                    exp = new Arcsch(); break;
                case Functions.Exp:
                    exp = new Exp(); break;
                case Functions.GCD:
                    exp = new GCD(); break;
                case Functions.LCM:
                    exp = new LCM(); break;
                case Functions.Factorial:
                    exp = new Fact(); break;
                case Functions.Sum:
                    exp = new Sum(); break;
                case Functions.Product:
                    exp = new Product(); break;
                case Functions.Round:
                    exp = new Round(); break;
                case Functions.Floor:
                    exp = new Floor(); break;
                case Functions.Ceil:
                    exp = new Ceil(); break;
                case Functions.Derivative:
                    exp = new Derivative(); break;
                case Functions.Simplify:
                    exp = new Simplify(); break;
                case Functions.Del:
                    exp = new Del(); break;
                case Functions.Define:
                    exp = new Define(); break;
                case Functions.Vector:
                    exp = new Vector(); break;
                case Functions.Matrix:
                    exp = new Matrix(); break;
                case Functions.Transpose:
                    exp = new Transpose(); break;
                case Functions.Determinant:
                    exp = new Determinant(); break;
                case Functions.Inverse:
                    exp = new Inverse(); break;
                case Functions.If:
                    exp = new If(); break;
                case Functions.For:
                    exp = new For(); break;
                case Functions.While:
                    exp = new While(); break;
                case Functions.Undefine:
                    exp = new Undefine(); break;
                case Functions.Im:
                    exp = new Im(); break;
                case Functions.Re:
                    exp = new Re(); break;
                case Functions.Phase:
                    exp = new Phase(); break;
                case Functions.Conjugate:
                    exp = new Conjugate(); break;
                case Functions.Reciprocal:
                    exp = new Reciprocal(); break;
                case Functions.Min:
                    exp = new Min(); break;
                case Functions.Max:
                    exp = new Max(); break;
                case Functions.Avg:
                    exp = new Avg(); break;
                case Functions.Count:
                    exp = new Count(); break;
                case Functions.Var:
                    exp = new Var(); break;
                case Functions.Varp:
                    exp = new Varp(); break;
                case Functions.Stdev:
                    exp = new Stdev(); break;
                case Functions.Stdevp:
                    exp = new Stdevp(); break;
                default:
                    exp = null; break;
            }

            var diff = exp as DifferentParametersExpression;
            if (diff != null)
                diff.ParametersCount = token.CountOfParams;

            return exp;
        }