コード例 #1
0
 /**
      * Enables to define the arguments (associated with
      * the expression) based on the given arguments names.
      *
      * @param      argumentsNames      the arguments names (variadic)
      *                                 comma separated list
      *
      * @see        Argument
      * @see        RecursiveArgument
      */
 public void defineArguments(params String[] argumentsNames)
 {
     foreach (String argName in argumentsNames) {
             Argument arg = new Argument(argName);
             arg.addRelatedExpression(this);
             argumentsList.Add(arg);
         }
         setExpressionModifiedFlag();
 }
コード例 #2
0
 /**
  * Calculates function f(x0) (given as expression) assigning Argument x = x0;
  *
  *
  * @param      f                   the expression
  * @param      x                   the argument
  * @param      x0                  the argument value
  *
  * @return     f.calculate()
  *
  * @see        Expression
  */
 public static double getFunctionValue(Expression f, Argument x, double x0)
 {
     x.setArgumentValue(x0);
     return(f.calculate());
 }
コード例 #3
0
 /**
      * Enables to define the argument (associated with the expression)
      * based on the argument name and the argument value.
      *
      * @param      argumentName        the argument name
      * @param      argumentValue       the the argument value
      *
      * @see        Argument
      * @see        RecursiveArgument
      */
 public void defineArgument(String argumentName, double argumentValue)
 {
     Argument arg = new Argument(argumentName, argumentValue);
         arg.addRelatedExpression(this);
         argumentsList.Add(arg);
         setExpressionModifiedFlag();
 }
コード例 #4
0
 /**
  * Calculates function f(x0) (given as expression) assigning Argument x = x0;
  *
  *
  * @param      f                   the expression
  * @param      x                   the argument
  * @param      x0                  the argument value
  *
  * @return     f.calculate()
  *
  * @see        Expression
  */
 public static double getFunctionValue(Expression f, Argument x, double x0)
 {
     x.setArgumentValue(x0);
     return f.calculate();
 }
コード例 #5
0
 /**
  * Returns array of double values of the function f(i)
  * calculated on the range: i = from to i = to by step = delta
  *
  * @param f            Function expression
  * @param index        Index argument
  * @param from         'from' value
  * @param to           'to' value
  * @param delta        'delta' step definition
  * @return             Array of function values
  */
 public static double[] getFunctionValues(Expression f, Argument index, double from, double to, double delta)
 {
     if ((Double.IsNaN(delta)) || (Double.IsNaN(from)) || (Double.IsNaN(to)) || (delta == 0))
         return null;
     int n = 0;
     double[] values;
     if ((to >= from) && (delta > 0)) {
         for (double i = from; i < to; i += delta)
             n++;
         n++;
         values = new double[n];
         int j = 0;
         for (double i = from; i < to; i += delta) {
             values[j] = getFunctionValue(f, index, i);
             j++;
         }
         values[j] = getFunctionValue(f, index, to);
     } else if ((to <= from) && (delta < 0)) {
         for (double i = from; i > to; i += delta)
             n++;
         n++;
         values = new double[n];
         int j = 0;
         for (double i = from; i > to; i += delta) {
             values[j] = getFunctionValue(f, index, i);
             j++;
         }
         values[j] = getFunctionValue(f, index, to);
     } else if (from == to) {
         n = 1;
         values = new double[n];
         values[0] = getFunctionValue(f, index, from);
     } else values = null;
     return values;
 }
コード例 #6
0
        public static void Start()
        {
            /*
             * Tutorial for the mXparser version 1.0
             * Mariusz Gromada    2010-01-10
             */
            /*
             * Simple & complex arithmetic expressions, large math functions collection
             * User defined arguments, functions, constants
             * Calculus operations (i.e. differentiation, integration)
             * Summation and product operations
             * User defined recursive functions
             * Boolean operators
             * and many more...
             *
             */
            /*
             * Start from the license
             */
            Expression e = new Expression();

            mXparser.consolePrintln(mXparser.LICENSE);
            mXparser.consolePrintln();

            /*
             * Using help
             */
            mXparser.consolePrintln();
            mXparser.consolePrintln(e.getHelp());

            /*
             * Full line searching
             */
            mXparser.consolePrintln();
            mXparser.consolePrintln(e.getHelp("sine"));
            mXparser.consolePrintln();
            mXparser.consolePrintln(e.getHelp("inver"));

            /*
             * Simple expression
             */
            Expression e1 = new Expression("2+1");

            mXparser.consolePrintln(e1.getExpressionString() + " = " + e1.calculate(CancellationToken.None));
            e1.setExpressionString("2-1");
            mXparser.consolePrintln(e1.getExpressionString() + " = " + e1.calculate(CancellationToken.None));
            /* operators */
            Expression e2 = new Expression("2-(32-4)/(23+(4)/(5))-(2-4)*(4+6-98.2)+4");

            mXparser.consolePrintln(e2.getExpressionString() + " = " + e2.calculate(CancellationToken.None));
            /* power function */
            Expression e3 = new Expression("2^3+2^(-3)+2^3^(-4)");

            mXparser.consolePrintln(e3.getExpressionString() + " = " + e3.calculate(CancellationToken.None));

            /*
             * Relations
             */
            Expression e4 = new Expression("2=3");

            mXparser.consolePrintln(e4.getExpressionString() + " = " + e4.calculate(CancellationToken.None));
            Expression e5 = new Expression("2<3");

            mXparser.consolePrintln(e5.getExpressionString() + " = " + e5.calculate(CancellationToken.None));
            Expression e6 = new Expression("(2=3) | (2<3)");

            mXparser.consolePrintln(e6.getExpressionString() + " = " + e6.calculate(CancellationToken.None));
            Expression e7 = new Expression("(2=3) & (2<3)");

            mXparser.consolePrintln(e7.getExpressionString() + " = " + e7.calculate(CancellationToken.None));
            /* 1 arg functions */
            Expression e8 = new Expression("sin(2)-cos(3)");

            mXparser.consolePrintln(e8.getExpressionString() + " = " + e8.calculate(CancellationToken.None));
            /* 2 args functions */
            Expression e9 = new Expression("min(3,4) + max(-2,-1)");

            mXparser.consolePrintln(e9.getExpressionString() + " = " + e9.calculate(CancellationToken.None));
            /* binomial coefficient */
            Expression e10 = new Expression("C(10,5)");

            mXparser.consolePrintln(e10.getExpressionString() + " = " + e10.calculate(CancellationToken.None));
            /* 3 args functions */
            /* conditions */
            Expression e11 = new Expression("if(2<3,1,0)");

            mXparser.consolePrintln(e11.getExpressionString() + " = " + e11.calculate(CancellationToken.None));
            Expression e12 = new Expression("if(3<2,1,0)");

            mXparser.consolePrintln(e12.getExpressionString() + " = " + e12.calculate(CancellationToken.None));
            Expression e13 = new Expression("if(3<2, 1, if(1=1, 5, 0) )");

            mXparser.consolePrintln(e13.getExpressionString() + " = " + e13.calculate(CancellationToken.None));

            /*
             * Free Arguments
             */
            Argument   x   = new Argument("x", 1);
            Argument   y   = new Argument("y", 2);
            Argument   z   = new Argument("z", 3);
            Argument   n   = new Argument("n", 4);
            Expression e14 = new Expression("sin(x+y)-cos(y/z)", x, y, z);

            mXparser.consolePrintln(e14.getExpressionString() + " = " + e14.calculate(CancellationToken.None));
            Expression e15 = new Expression("if(x>y, x-z, if(y<z, sin(x+y), cos(z)) )", x, y, z);

            mXparser.consolePrintln(e15.getExpressionString() + " = " + e15.calculate(CancellationToken.None));
            x.setArgumentValue(5);
            mXparser.consolePrintln(x.getArgumentName() + " = " + x.getArgumentValue(CancellationToken.None));

            /*
             * Dependent arguments
             */
            y = new Argument("y", "2*x+5", x);
            mXparser.consolePrintln(y.getArgumentName() + " = " + y.getArgumentValue(CancellationToken.None));
            y = new Argument("y", "sin(y)-z", y, z);
            mXparser.consolePrintln(y.getArgumentName() + " = " + y.getArgumentValue(CancellationToken.None));
            /* syntax checking */
            y.setArgumentExpressionString("n*sin(y)-z");
            mXparser.consolePrintln(y.getArgumentName() + " = ... \n syntax = " + y.checkSyntax(CancellationToken.None) + "\n message = \n" + y.getErrorMessage());
            y.addArguments(n);
            mXparser.consolePrintln(y.getArgumentName() + " = ... \n syntax = " + y.checkSyntax(CancellationToken.None) + "\n message = \n" + y.getErrorMessage());
            mXparser.consolePrintln(y.getArgumentName() + " = " + y.getArgumentValue(CancellationToken.None));

            /*
             * the same methods could be called
             * for the expression syntax checking
             *
             */
            /*
             * Complex expressions
             */
            /*
             * Summation operator SIGMA
             * sum(index, from, to, expr)
             * sum(index, from, to, expr, by)
             *
             */
            Expression e16 = new Expression("sum(i,1,10,i)");

            mXparser.consolePrintln(e16.getExpressionString() + " = " + e16.calculate(CancellationToken.None));
            Expression e17 = new Expression("sum(i,1,10,i,0.5)");

            mXparser.consolePrintln(e17.getExpressionString() + " = " + e17.calculate(CancellationToken.None));

            /*
             * Product operator SIGMA
             * prod(index, from, to, expr)
             * prod(index, from, to, expr, by)
             */
            /* factorial */
            Expression e18 = new Expression("prod(i,1,5,i)");

            mXparser.consolePrintln(e18.getExpressionString() + " = " + e18.calculate(CancellationToken.None));
            Expression e19 = new Expression("prod(i,1,5,i,0.5)");

            mXparser.consolePrintln(e19.getExpressionString() + " = " + e19.calculate(CancellationToken.None));

            /* Approximation sin(x) by Taylor series
             * ! - factorial
             */
            Expression e20 = new Expression("sin(x)-sum(n,0,10,(-1)^n*(x^(2*n+1))/(2*n+1)!)", x);

            x.setArgumentValue(1);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e20.getExpressionString() + " = " + e20.calculate(CancellationToken.None));
            x.setArgumentValue(5);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e20.getExpressionString() + " = " + e20.calculate(CancellationToken.None));
            x.setArgumentValue(10);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e20.getExpressionString() + " = " + e20.calculate(CancellationToken.None));

            /*
             * calculating pi by integral of
             * sqrt(1-x^2) from -1 to 1
             * using simple summation operator
             */
            Argument   d   = new Argument("d", 0.1);
            Expression e21 = new Expression("2*sum(x, -1, 1, d*sqrt(1-x^2), d)", d);

            mXparser.consolePrintln("d = " + d.getArgumentValue(CancellationToken.None) + ", " + e21.getExpressionString() + " = " + e21.calculate(CancellationToken.None));
            d.setArgumentValue(0.01);
            mXparser.consolePrintln("d = " + d.getArgumentValue(CancellationToken.None) + ", " + e21.getExpressionString() + " = " + e21.calculate(CancellationToken.None));

            /*
             * Derivatives
             *
             * der( f(x,...), x) - general
             * der+( f(x,...), x) - right
             * der-( f(x,...), x) - left
             */
            /* cos(x) as derivative from sin(x) */
            Expression e22 = new Expression("cos(x)-der(sin(x), x)", x);

            mXparser.consolePrintln(e22.getExpressionString() + " = " + e22.calculate(CancellationToken.None));
            /* left and right derivative from |x| */
            x.setArgumentValue(0);
            Expression e23 = new Expression("der-(abs(x), x)", x);

            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e23.getExpressionString() + " = " + e23.calculate(CancellationToken.None));
            Expression e24 = new Expression("der+(abs(x), x)", x);

            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e24.getExpressionString() + " = " + e24.calculate(CancellationToken.None));
            /* derivative from sin(x) as Taylor series (approximation) */
            x.setArgumentValue(1);
            Expression e25 = new Expression("cos(x)-der(sum(n,0,10,(-1)^n*(x^(2*n+1))/(2*n+1)!), x)", x);

            x.setArgumentValue(1);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e25.getExpressionString() + " = " + e25.calculate(CancellationToken.None));
            x.setArgumentValue(5);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e25.getExpressionString() + " = " + e25.calculate(CancellationToken.None));
            x.setArgumentValue(10);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e25.getExpressionString() + " = " + e25.calculate(CancellationToken.None));

            /*
             * Integral
             * int(f(x,...), x, a, b)
             */
            /* calculating PI value */
            Expression e26 = new Expression("2*int(sqrt(1-x^2), x, -1, 1)");

            mXparser.consolePrintln(e26.getExpressionString() + " = " + e26.calculate(CancellationToken.None));

            /*
             * Parser constants
             */
            Expression e27 = new Expression("pi");

            mXparser.consolePrintln(e27.getExpressionString() + " = " + e27.calculate(CancellationToken.None));
            Expression e28 = new Expression("e");

            mXparser.consolePrintln(e28.getExpressionString() + " = " + e28.calculate(CancellationToken.None));
            /* and many many more ...  */

            /*
             * USER DEFINED FUNCTIONS
             * f(x,y) = sin(x+y)
             * f(a,b,c,d,...) = ....
             */
            Function   f   = new Function(CancellationToken.None, "f", "x^2", "x");
            Expression e29 = new Expression("f(2)");

            e29.addFunctions(f);
            mXparser.consolePrintln(e29.getExpressionString() + " = " + e29.calculate(CancellationToken.None));

            /*
             * Please be informed that sequence of
             * arguments names is important. In the below exampe
             * a - 1st argument of f
             * b - 2nd argument of f
             * c - 3rd argument of f
             *
             * In the expressions f will be c alled as f( . , . , . )
             */
            f = new Function(CancellationToken.None, "f", "a+b+c", "a", "b", "c");
            Expression e30 = new Expression("f(1, 2, 3)");

            e30.addFunctions(f);
            mXparser.consolePrintln(e30.getExpressionString() + " = " + e30.calculate(CancellationToken.None));

            /*
             * Using functions in functions
             */
            f = new Function(CancellationToken.None, "f", "x^2", "x");
            Function g = new Function(CancellationToken.None, "g", "f(x)^2", "x");

            g.addFunctions(f);
            mXparser.consolePrintln("g(2) = " + g.calculate(CancellationToken.None, 2));
            Expression e31 = new Expression("f(x)+g(2*x)", x);

            e31.addFunctions(f, g);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e31.getExpressionString() + " = " + e31.calculate(CancellationToken.None));
            x.setArgumentValue(2);
            Expression e32 = new Expression("der(g(x),x)", x);

            e32.addFunctions(g);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e32.getExpressionString() + " = " + e32.calculate(CancellationToken.None));

            /*
             * Fundamental theorem of calculus
             * if F(x) = int_a^x f(t) dt
             * then F'(x) = f(x)
             */
            f = new Function(CancellationToken.None, "f", "sin(x)", "x");
            Function F = new Function(CancellationToken.None, "F", "int(f(t), t, 0, x)", "x");

            F.addFunctions(f);
            Expression e33 = new Expression("f(x) - der(F(x),x)", x);

            e33.addFunctions(f, F);
            mXparser.consolePrintln("x = " + x.getArgumentValue(CancellationToken.None) + ", " + e33.getExpressionString() + " = " + e33.calculate(CancellationToken.None) +
                                    ", computing time : " + e33.getComputingTime() + " s.");

            /*
             * Simple (fast) recursion
             * Only one index n >= 0, n integer
             */
            /* Fibonacci numbers with add base cases method*/
            n = new Argument(CancellationToken.None, "n");
            RecursiveArgument fib1 = new RecursiveArgument("fib1", "fib1(n-1)+fib1(n-2)", n);

            fib1.addBaseCase(0, 0);
            fib1.addBaseCase(1, 1);
            mXparser.consolePrintln("fib1: ");
            for (int i = 0; i <= 10; i++)
            {
                mXparser.consolePrint(fib1.getArgumentValue(CancellationToken.None, i) + ", ");
            }
            mXparser.consolePrintln();
            /* Fibonacci numbers with if statement*/
            RecursiveArgument fib2 = new RecursiveArgument("fib2", "if( n>1, fib2(n-1)+fib2(n-2), if(n=1,1,0) )", n);

            mXparser.consolePrintln("fib2: ");
            for (int i = 0; i <= 10; i++)
            {
                mXparser.consolePrint(fib2.getArgumentValue(CancellationToken.None, i) + ", ");
            }
            mXparser.consolePrintln();
            Expression e34 = new Expression("sum(i, 0, 10, fib1(i))", fib1);

            mXparser.consolePrintln(e34.getExpressionString() + " = " + e34.calculate(CancellationToken.None) +
                                    ", computing time : " + e34.getComputingTime() + " s.");
            Expression e35 = new Expression("sum(i, 0, 10, fib2(i))", fib2);

            mXparser.consolePrintln(e35.getExpressionString() + " = " + e35.calculate(CancellationToken.None) +
                                    ", computing time : " + e35.getComputingTime() + " s.");

            /*
             * Complex recursion (slow)
             * any definition
             *
             *
             */
            /* Fibonacci numbers using complex recursion */
            Function fib3 = new Function(CancellationToken.None, "fib3", "if(n>1, fib3(n-1)+fib3(n-2), if(n>0,1,0))", "n");

            mXparser.consolePrintln("fib2: ");
            for (int i = 0; i <= 10; i++)
            {
                mXparser.consolePrint(fib3.calculate(CancellationToken.None, i) + ", ");
            }
            mXparser.consolePrintln();
            Expression e36 = new Expression("sum(i, 0, 10, fib3(i))");

            e36.addFunctions(fib3);
            mXparser.consolePrintln(e36.getExpressionString() + " = " + e36.calculate(CancellationToken.None) +
                                    ", computing time : " + e36.getComputingTime() + " s.");

            /*
             * Chebyshev polynomials definition using
             * recursive functions
             */
            Function   T   = new Function(CancellationToken.None, "T", "if(n>1, 2*x*T(n-1,x)-T(n-2,x), if(n>0, x, 1) )", "n", "x");
            Argument   k   = new Argument("k", 5);
            Expression e37 = new Expression("T(k,x) - ( (x + sqrt(x^2-1))^k + (x - sqrt(x^2-1))^k)/2", k, x);

            e37.addFunctions(T);
            mXparser.consolePrintln(e37.getExpressionString() + " = " + e37.calculate(CancellationToken.None) +
                                    ", computing time : " + e37.getComputingTime() + " s.");

            /*
             * Binomial coefficient using complex recursion
             */
            Function   Cnk = new Function(CancellationToken.None, "Cnk", "if( k>0, if( k<n, Cnk(n-1,k-1)+Cnk(n-1,k), 1), 1)", "n", "k");
            Expression e38 = new Expression("C(10,5) - Cnk(10,5)");

            e38.addFunctions(Cnk);
            mXparser.consolePrintln(e38.getExpressionString() + " = " + e38.calculate(CancellationToken.None) +
                                    ", computing time : " + e38.getComputingTime() + " s.");

            /*
             * Differences between simple and complex recursion
             *
             * Simple:
             *
             * Advantages
             *    1) fast
             *    2) remembers what was previously calculated
             *    3) calculations are done in a controlled way
             *         - recursion counter (each recursion step increases
             *           recursion counter, for calculating n-th recursion
             *           value maximum n recursion steps are needed
             *         - no negative indexes
             *    4) add base value method
             *
             * Disadvantages
             *    1) limited to one index argument (positive integer index argument)
             *
             * Complex recursion
             *
             * Advantages
             *    1) flexible - no limitations
             *
             * Disadvantages
             *    1) slow
             *    2) no add base values method
             */
            /*
             * User defined constants
             */
            Constant   a   = new Constant("a", 5);
            Constant   b   = new Constant("b", 10);
            Constant   c   = new Constant("c", 15);
            Expression e39 = new Expression("a+b+c");

            e39.addConstants(a, b, c);

            /*
             * For example in verbose mode
             * computing
             */
            e39.setVerboseMode();
            e39.checkSyntax(CancellationToken.None);
            mXparser.consolePrintln();
            mXparser.consolePrintln(e39.getErrorMessage());
            mXparser.consolePrintln(e39.getExpressionString() + " = " + e39.calculate(CancellationToken.None) +
                                    ", computing time : " + e39.getComputingTime() + " s.");

            /*
             * There are much more other features in the mXparser API.
             * Please refer to the API specifications.
             *
             * Please be aware that this is the version 1.0 of the parser
             * - I am pretty sure you will find some bugs - if so please send me
             * more info: [email protected]
             */
        }
コード例 #7
0
 /**
  * Constructor - creates recursive argument.
  *
  * @param      argumentName                  the argument name
  * @param      recursiveExpressionString     the recursive expression string
  * @param      n                             the index argument
  * @param      elements                      Optional elements list (variadic - comma
  *                                           separated) of types: Argument, Constant, Function
  *
  * @see        PrimitiveElement
  * @see        Argument
  */
 public RecursiveArgument(String argumentName, String recursiveExpressionString, Argument n, params PrimitiveElement[] elements)
     : base(argumentName, recursiveExpressionString)
 {
     if (argumentName.Equals(this.getArgumentName())) {
         this.argumentType = RECURSIVE_ARGUMENT;
         baseValues = new List<Double>();
         this.n = n;
         base.argumentExpression.addArguments(n);
         base.argumentExpression.addArguments(this);
         base.argumentExpression.addDefinitions(elements);
         base.argumentExpression.setDescription(argumentName);
         recursiveCounter = -1;
     }
 }
コード例 #8
0
 /**
  * Creates cloned object of the this argument.''
  *
  * @return     clone of the argument.
  */
 public Argument clone()
 {
     Argument newArg = new Argument(this.argumentName);
     newArg.argumentExpression = this.argumentExpression;
     newArg.argumentType = this.argumentType;
     newArg.argumentValue = this.argumentValue;
     newArg.description = this.description;
     newArg.n = this.n;
     return newArg;
 }
コード例 #9
0
 /*=================================================
  *
  * Constructors
  *
  *=================================================
  */
 /**
  * Default constructor - creates argument based on the argument definition string.
  *
  * @param      argumentDefinitionString        Argument definition string, i.e.:
  *                                             <ul>
  *                                                <li>'x' - only argument name
  *                                                <li>'x=5' - argument name and argument value
  *                                                <li>'x=2*5' - argument name and argument value given as simple expression
  *                                                <li>'x=2*y' - argument name and argument expression (dependent argument 'x' on argument 'y')
  *                                             </ul>
  *
  * @param      elements   Optional parameters (comma separated) such as Arguments, Constants, Functions
  */
 public Argument(String argumentDefinitionString, params PrimitiveElement[] elements)
     : base(Argument.TYPE_ID)
 {
     if (mXparser.regexMatch(argumentDefinitionString, ParserSymbol.nameOnlyTokenRegExp)) {
         argumentName = argumentDefinitionString;
         argumentValue = ARGUMENT_INITIAL_VALUE;
         argumentType = FREE_ARGUMENT;
         argumentExpression = new Expression(elements);
     }
     else if (mXparser.regexMatch(argumentDefinitionString, ParserSymbol.constArgDefStrRegExp)) {
         HeadEqBody headEqBody = new HeadEqBody(argumentDefinitionString);
         argumentName = headEqBody.headTokens[0].tokenStr;
         Expression bodyExpr = new Expression(headEqBody.bodyStr);
         double bodyValue = bodyExpr.calculate();
         if ((bodyExpr.getSyntaxStatus() == Expression.NO_SYNTAX_ERRORS) && (bodyValue != Double.NaN)) {
             argumentExpression = new Expression();
             argumentValue = bodyValue;
             argumentType = FREE_ARGUMENT;
         }
         else {
             argumentExpression = bodyExpr;
             addDefinitions(elements);
             argumentType = DEPENDENT_ARGUMENT;
         }
     }
     else if (mXparser.regexMatch(argumentDefinitionString, ParserSymbol.functionDefStrRegExp)) {
         HeadEqBody headEqBody = new HeadEqBody(argumentDefinitionString);
         argumentName = headEqBody.headTokens[0].tokenStr;
         argumentExpression = new Expression(headEqBody.bodyStr, elements);
         argumentExpression.setDescription(headEqBody.headStr);
         argumentValue = ARGUMENT_INITIAL_VALUE;
         argumentType = DEPENDENT_ARGUMENT;
         n = new Argument(headEqBody.headTokens[2].tokenStr);
     }
     else {
         argumentValue = ARGUMENT_INITIAL_VALUE;
         argumentType = FREE_ARGUMENT;
         argumentExpression = new Expression();
         argumentExpression.setSyntaxStatus(SYNTAX_ERROR_OR_STATUS_UNKNOWN, "[" + argumentDefinitionString + "] " + "Invalid argument definition (patterns: 'x', 'x=5', 'x=5+3/2', 'x=2*y').");
     }
     setSilentMode();
     description = "";
 }
コード例 #10
0
 public static void Start()
 {
     /*
      * Tutorial for the mXparser version 1.0
      * Mariusz Gromada    2010-01-10
      */
     /*
      * Simple & complex arithmetic expressions, large math functions collection
      * User defined arguments, functions, constants
      * Calculus operations (i.e. differentiation, integration)
      * Summation and product operations
      * User defined recursive functions
      * Boolean operators
      * and many more...
      *
      */
     /*
      * Start from the license
      */
     Expression e = new Expression();
     mXparser.consolePrintln(e.getLicense());
     mXparser.consolePrintln();
     /*
      * Using help
      */
     mXparser.consolePrintln();
     mXparser.consolePrintln(e.getHelp());
     /*
      * Full line searching
      */
     mXparser.consolePrintln();
     mXparser.consolePrintln(e.getHelp("sine"));
     mXparser.consolePrintln();
     mXparser.consolePrintln(e.getHelp("inver"));
     /*
      * Simple expression
      */
     Expression e1 = new Expression("2+1");
     mXparser.consolePrintln(e1.getExpressionString() + " = " + e1.calculate());
     e1.setExpressionString("2-1");
     mXparser.consolePrintln(e1.getExpressionString() + " = " + e1.calculate());
     /* operators */
     Expression e2 = new Expression("2-(32-4)/(23+(4)/(5))-(2-4)*(4+6-98.2)+4");
     mXparser.consolePrintln(e2.getExpressionString() + " = " + e2.calculate());
     /* power function */
     Expression e3 = new Expression("2^3+2^(-3)+2^3^(-4)");
     mXparser.consolePrintln(e3.getExpressionString() + " = " + e3.calculate());
     /*
      * Relations
      */
     Expression e4 = new Expression("2=3");
     mXparser.consolePrintln(e4.getExpressionString() + " = " + e4.calculate());
     Expression e5 = new Expression("2<3");
     mXparser.consolePrintln(e5.getExpressionString() + " = " + e5.calculate());
     Expression e6 = new Expression("(2=3) | (2<3)");
     mXparser.consolePrintln(e6.getExpressionString() + " = " + e6.calculate());
     Expression e7 = new Expression("(2=3) & (2<3)");
     mXparser.consolePrintln(e7.getExpressionString() + " = " + e7.calculate());
     /* 1 arg functions */
     Expression e8 = new Expression("sin(2)-cos(3)");
     mXparser.consolePrintln(e8.getExpressionString() + " = " + e8.calculate());
     /* 2 args functions */
     Expression e9 = new Expression("min(3,4) + max(-2,-1)");
     mXparser.consolePrintln(e9.getExpressionString() + " = " + e9.calculate());
     /* binomial coefficient */
     Expression e10 = new Expression("C(10,5)");
     mXparser.consolePrintln(e10.getExpressionString() + " = " + e10.calculate());
     /* 3 args functions */
     /* conditions */
     Expression e11 = new Expression("if(2<3,1,0)");
     mXparser.consolePrintln(e11.getExpressionString() + " = " + e11.calculate());
     Expression e12 = new Expression("if(3<2,1,0)");
     mXparser.consolePrintln(e12.getExpressionString() + " = " + e12.calculate());
     Expression e13 = new Expression("if(3<2, 1, if(1=1, 5, 0) )");
     mXparser.consolePrintln(e13.getExpressionString() + " = " + e13.calculate());
     /*
      * Free Arguments
      */
     Argument x = new Argument("x", 1);
     Argument y = new Argument("y", 2);
     Argument z = new Argument("z", 3);
     Argument n = new Argument("n", 4);
     Expression e14 = new Expression("sin(x+y)-cos(y/z)", x, y, z);
     mXparser.consolePrintln(e14.getExpressionString() + " = " + e14.calculate());
     Expression e15 = new Expression("if(x>y, x-z, if(y<z, sin(x+y), cos(z)) )", x, y, z);
     mXparser.consolePrintln(e15.getExpressionString() + " = " + e15.calculate());
     x.setArgumentValue(5);
     mXparser.consolePrintln(x.getArgumentName() + " = " + x.getArgumentValue());
     /*
      * Dependent arguments
      */
     y = new Argument("y","2*x+5", x);
     mXparser.consolePrintln(y.getArgumentName() + " = " + y.getArgumentValue());
     y = new Argument("y","sin(y)-z", y, z);
     mXparser.consolePrintln(y.getArgumentName() + " = " + y.getArgumentValue());
     /* syntax checking */
     y.setArgumentExpressionString("n*sin(y)-z");
     mXparser.consolePrintln(y.getArgumentName() + " = ... \n syntax = " + y.checkSyntax() + "\n message = \n" + y.getErrorMessage());
     y.addArguments(n);
     mXparser.consolePrintln(y.getArgumentName() + " = ... \n syntax = " + y.checkSyntax() + "\n message = \n" + y.getErrorMessage());
     mXparser.consolePrintln(y.getArgumentName() + " = " + y.getArgumentValue());
     /*
      * the same methods could be called
      * for the expression syntax checking
      *
      */
     /*
      * Complex expressions
      */
     /*
      * Summation operator SIGMA
      * sum(index, from, to, expr)
      * sum(index, from, to, expr, by)
      *
      */
     Expression e16 = new Expression("sum(i,1,10,i)");
     mXparser.consolePrintln(e16.getExpressionString() + " = " + e16.calculate());
     Expression e17 = new Expression("sum(i,1,10,i,0.5)");
     mXparser.consolePrintln(e17.getExpressionString() + " = " + e17.calculate());
     /*
      * Product operator SIGMA
      * prod(index, from, to, expr)
      * prod(index, from, to, expr, by)
      */
     /* factorial */
     Expression e18 = new Expression("prod(i,1,5,i)");
     mXparser.consolePrintln(e18.getExpressionString() + " = " + e18.calculate());
     Expression e19 = new Expression("prod(i,1,5,i,0.5)");
     mXparser.consolePrintln(e19.getExpressionString() + " = " + e19.calculate());
     /* Approximation sin(x) by Taylor series
      * ! - factorial
      */
     Expression e20 = new Expression("sin(x)-sum(n,0,10,(-1)^n*(x^(2*n+1))/(2*n+1)!)", x);
     x.setArgumentValue(1);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e20.getExpressionString() + " = " + e20.calculate());
     x.setArgumentValue(5);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e20.getExpressionString() + " = " + e20.calculate());
     x.setArgumentValue(10);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e20.getExpressionString() + " = " + e20.calculate());
     /*
      * calculating pi by integral of
      * sqrt(1-x^2) from -1 to 1
      * using simple summation operator
      */
     Argument d = new Argument("d",0.1);
     Expression e21 = new Expression("2*sum(x, -1, 1, d*sqrt(1-x^2), d)", d);
     mXparser.consolePrintln("d = " + d.getArgumentValue() + ", " + e21.getExpressionString() + " = " + e21.calculate());
     d.setArgumentValue(0.01);
     mXparser.consolePrintln("d = " + d.getArgumentValue() + ", " + e21.getExpressionString() + " = " + e21.calculate());
     /*
      * Derivatives
      *
      * der( f(x,...), x) - general
      * der+( f(x,...), x) - right
      * der-( f(x,...), x) - left
      */
     /* cos(x) as derivative from sin(x) */
     Expression e22 = new Expression("cos(x)-der(sin(x), x)", x);
     mXparser.consolePrintln(e22.getExpressionString() + " = " + e22.calculate());
     /* left and right derivative from |x| */
     x.setArgumentValue(0);
     Expression e23 = new Expression("der-(abs(x), x)", x);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e23.getExpressionString() + " = " + e23.calculate());
     Expression e24 = new Expression("der+(abs(x), x)", x);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e24.getExpressionString() + " = " + e24.calculate());
     /* derivative from sin(x) as Taylor series (approximation) */
     x.setArgumentValue(1);
     Expression e25 = new Expression("cos(x)-der(sum(n,0,10,(-1)^n*(x^(2*n+1))/(2*n+1)!), x)", x);
     x.setArgumentValue(1);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e25.getExpressionString() + " = " + e25.calculate());
     x.setArgumentValue(5);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e25.getExpressionString() + " = " + e25.calculate());
     x.setArgumentValue(10);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e25.getExpressionString() + " = " + e25.calculate());
     /*
      * Integral
      * int(f(x,...), x, a, b)
      */
     /* calculating PI value */
     Expression e26 = new Expression("2*int(sqrt(1-x^2), x, -1, 1)");
     mXparser.consolePrintln(e26.getExpressionString() + " = " + e26.calculate());
     /*
      * Parser constants
      */
     Expression e27 = new Expression("pi");
     mXparser.consolePrintln(e27.getExpressionString() + " = " + e27.calculate());
     Expression e28 = new Expression("e");
     mXparser.consolePrintln(e28.getExpressionString() + " = " + e28.calculate());
     /* and many many more ...  */
     /*
      * USER DEFINED FUNCTIONS
      * f(x,y) = sin(x+y)
      * f(a,b,c,d,...) = ....
      */
     Function f = new Function("f", "x^2", "x");
     Expression e29 = new Expression("f(2)");
     e29.addFunctions(f);
     mXparser.consolePrintln(e29.getExpressionString() + " = " + e29.calculate());
     /*
      * Please be informed that sequence of
      * arguments names is important. In the below exampe
      * a - 1st argument of f
      * b - 2nd argument of f
      * c - 3rd argument of f
      *
      * In the expressions f will be c alled as f( . , . , . )
      */
     f = new Function("f", "a+b+c", "a", "b", "c");
     Expression e30 = new Expression("f(1, 2, 3)");
     e30.addFunctions(f);
     mXparser.consolePrintln(e30.getExpressionString() + " = " + e30.calculate());
     /*
      * Using functions in functions
      */
     f = new Function("f", "x^2", "x");
     Function g = new Function("g", "f(x)^2", "x");
     g.addFunctions(f);
     mXparser.consolePrintln("g(2) = " + g.calculate(2));
     Expression e31 = new Expression("f(x)+g(2*x)", x);
     e31.addFunctions(f, g);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e31.getExpressionString() + " = " + e31.calculate());
     x.setArgumentValue(2);
     Expression e32 = new Expression("der(g(x),x)", x);
     e32.addFunctions(g);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e32.getExpressionString() + " = " + e32.calculate());
     /*
      * Fundamental theorem of calculus
      * if F(x) = int_a^x f(t) dt
      * then F'(x) = f(x)
      */
     f = new Function("f", "sin(x)", "x");
     Function F = new Function("F", "int(f(t), t, 0, x)", "x");
     F.addFunctions(f);
     Expression e33 = new Expression("f(x) - der(F(x),x)", x);
     e33.addFunctions(f, F);
     mXparser.consolePrintln("x = " + x.getArgumentValue() + ", " + e33.getExpressionString() + " = " + e33.calculate() +
         ", computing time : " + e33.getComputingTime() + " s.");
     /*
      * Simple (fast) recursion
      * Only one index n >= 0, n integer
      */
     /* Fibonacci numbers with add base cases method*/
     n = new Argument("n");
     RecursiveArgument fib1 = new RecursiveArgument("fib1", "fib1(n-1)+fib1(n-2)", n);
     fib1.addBaseCase(0, 0);
     fib1.addBaseCase(1, 1);
     mXparser.consolePrintln("fib1: ");
     for (int i = 0; i <= 10; i++ )
     mXparser.consolePrint(fib1.getArgumentValue(i) + ", ");
     mXparser.consolePrintln();
     /* Fibonacci numbers with if statement*/
     RecursiveArgument fib2 = new RecursiveArgument("fib2", "if( n>1, fib2(n-1)+fib2(n-2), if(n=1,1,0) )", n);
     mXparser.consolePrintln("fib2: ");
     for (int i = 0; i <= 10; i++ )
     mXparser.consolePrint(fib2.getArgumentValue(i) + ", ");
     mXparser.consolePrintln();
     Expression e34 = new Expression("sum(i, 0, 10, fib1(i))", fib1);
     mXparser.consolePrintln(e34.getExpressionString() + " = " + e34.calculate() +
         ", computing time : " + e34.getComputingTime() + " s.");
     Expression e35 = new Expression("sum(i, 0, 10, fib2(i))", fib2);
     mXparser.consolePrintln(e35.getExpressionString() + " = " + e35.calculate() +
         ", computing time : " + e35.getComputingTime() + " s.");
     /*
      * Complex recursion (slow)
      * any definition
      *
      *
      */
     /* Fibonacci numbers using complex recursion */
     Function fib3 = new Function("fib3","if(n>1, fib3(n-1)+fib3(n-2), if(n>0,1,0))", "n");
     mXparser.consolePrintln("fib2: ");
     for (int i = 0; i <= 10; i++ )
     mXparser.consolePrint(fib3.calculate(i) + ", ");
     mXparser.consolePrintln();
     Expression e36 = new Expression("sum(i, 0, 10, fib3(i))");
     e36.addFunctions(fib3);
     mXparser.consolePrintln(e36.getExpressionString() + " = " + e36.calculate() +
         ", computing time : " + e36.getComputingTime() + " s.");
     /*
      * Chebyshev polynomials definition using
      * recursive functions
      */
     Function T = new Function("T","if(n>1, 2*x*T(n-1,x)-T(n-2,x), if(n>0, x, 1) )", "n", "x");
     Argument k = new Argument("k", 5);
     Expression e37 = new Expression("T(k,x) - ( (x + sqrt(x^2-1))^k + (x - sqrt(x^2-1))^k)/2", k, x);
     e37.addFunctions(T);
     mXparser.consolePrintln(e37.getExpressionString() + " = " + e37.calculate() +
         ", computing time : " + e37.getComputingTime() + " s.");
     /*
      * Binomial coefficient using complex recursion
      */
     Function Cnk = new Function("Cnk","if( k>0, if( k<n, Cnk(n-1,k-1)+Cnk(n-1,k), 1), 1)","n", "k");
     Expression e38 = new Expression("C(10,5) - Cnk(10,5)");
     e38.addFunctions(Cnk);
     mXparser.consolePrintln(e38.getExpressionString() + " = " + e38.calculate() +
         ", computing time : " + e38.getComputingTime() + " s.");
     /*
      * Differences between simple and complex recursion
      *
      * Simple:
      *
      * Advantages
      *    1) fast
      *    2) remembers what was previously calculated
      *    3) calculations are done in a controlled way
      *         - recursion counter (each recursion step increases
      *           recursion counter, for calculating n-th recursion
      *           value maximum n recursion steps are needed
      *         - no negative indexes
      *    4) add base value method
      *
      * Disadvantages
      *    1) limited to one index argument (positive integer index argument)
      *
      * Complex recursion
      *
      * Advantages
      *    1) flexible - no limitations
      *
      * Disadvantages
      *    1) slow
      *    2) no add base values method
      */
     /*
      * User defined constants
      */
     Constant a = new Constant("a", 5);
     Constant b = new Constant("b", 10);
     Constant c = new Constant("c", 15);
     Expression e39 = new Expression("a+b+c");
     e39.addConstants(a, b, c);
     /*
      * For example in verbose mode
      * computing
      */
     e39.setVerboseMode();
     e39.checkSyntax();
     mXparser.consolePrintln();
     mXparser.consolePrintln(e39.getErrorMessage());
     mXparser.consolePrintln(e39.getExpressionString() + " = " + e39.calculate() +
         ", computing time : " + e39.getComputingTime() + " s.");
     /*
      * There are much more other features in the mXparser API.
      * Please refer to the API specifications.
      *
      * Please be aware that this is the version 1.0 of the parser
      * - I am pretty sure you will find some bugs - if so please send me
      * more info: [email protected]
      */
 }