Search in sources :

Example 6 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class RulePreprocessor method generateFunctionStrings.

/**
	 * Generate Java files (*.java) from Symja rule files (*.m)
	 * 
	 * @param sourceLocation
	 *            source directory for rule (*.m) files
	 * @param targetLocation
	 *            target directory for the generated Java files
	 * @param ignoreTimestamp
	 *            if <code>false</code> only change the target file (*.java), if
	 *            the source file (*.m) has a newer time stamp than the target
	 *            file.
	 */
public static void generateFunctionStrings(final File sourceLocation, File targetLocation, boolean ignoreTimestamp) {
    if (sourceLocation.exists()) {
        // Get the list of the files contained in the package
        final String[] files = sourceLocation.list();
        if (files != null) {
            StringBuffer buffer;
            EvalEngine engine = new EvalEngine(true);
            for (int i = 0; i < files.length; i++) {
                File sourceFile = new File(sourceLocation, files[i]);
                // we are only interested in .m files
                if (files[i].endsWith(".m")) {
                    ASTNode node = parseFileToList(sourceFile);
                    if (node != null) {
                        buffer = new StringBuffer(100000);
                        PrintWriter out;
                        try {
                            String className = files[i].substring(0, files[i].length() - 2);
                            String symbolName = className.substring(0, className.length() - 5);
                            File targetFile = new File(targetLocation, className + ".java");
                            if (targetFile.exists()) {
                                if (!ignoreTimestamp && (sourceFile.lastModified() <= targetFile.lastModified())) {
                                    // existing ones
                                    continue;
                                }
                            }
                            System.out.println(className);
                            out = new PrintWriter(targetFile.getCanonicalPath());
                            out.print(HEADER);
                            out.print(className);
                            out.print(" {\n");
                            convert(node, "", buffer, out, symbolName, engine);
                            out.println(FOOTER1);
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 7 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class FieldReducedRowEchelonForm method rowReduce.

/**
	 * Create the &quot;reduced row echelon form&quot; of a matrix.
	 * 
	 * See: <a href="http://en.wikipedia.org/wiki/Row_echelon_form">Wikipedia -
	 * Row echelon form</a>.
	 * 
	 * @return
	 */
private FieldMatrix<IExpr> rowReduce() {
    int maxRows = numRows;
    RowColIndex pivot = new RowColIndex(0, 0);
    int submatrix = 0;
    for (int x = 0; x < numCols; x++) {
        pivot = new RowColIndex(pivot.row, x);
        // The pivot position is at the top.
        for (int i = x; i < numCols; i++) {
            if (isColumnZeroFromRow(pivot) == false) {
                break;
            } else {
                pivot.col = i;
            }
        }
        // Step 2
        // Select a nonzero entry in the pivot column with the highest
        // absolute value as a pivot.
        pivot = findPivot(pivot);
        if (isZero(getCoordinate(pivot))) {
            pivot.row++;
            if (pivot.row >= maxRows) {
                break;
            }
            continue;
        }
        // move this row to the top of the submatrix
        if (pivot.row != submatrix) {
            swapRow(new RowColIndex(submatrix, pivot.col), pivot);
        }
        // Force pivot to be 1
        if (!isOne(getCoordinate(pivot))) {
            IExpr scalar = getCoordinate(pivot).inverse();
            scaleRow(pivot, scalar);
        }
        // belowPivot = belowPivot + (Pivot * -belowPivot)
        for (int i = pivot.row; i < numRows; i++) {
            if (i == pivot.row) {
                continue;
            }
            RowColIndex belowPivot = new RowColIndex(i, pivot.col);
            IExpr complement = (getCoordinate(belowPivot).negate().divide(getCoordinate(pivot)));
            multiplyAdd(belowPivot, pivot, complement);
        }
        // above the pivot
        for (int i = pivot.row; i >= 0; i--) {
            if (i == pivot.row) {
                if (!isOne(getCoordinate(pivot))) {
                    scaleRow(pivot, getCoordinate(pivot).inverse());
                }
                continue;
            }
            if (i == pivot.row) {
                continue;
            }
            RowColIndex abovePivot = new RowColIndex(i, pivot.col);
            IExpr complement = (getCoordinate(abovePivot).negate().divide(getCoordinate(pivot)));
            multiplyAdd(abovePivot, pivot, complement);
        }
        // are no more nonzero entries.
        if ((pivot.row + 1) >= maxRows) {
            // {
            break;
        }
        submatrix++;
        pivot.row++;
    }
    EvalEngine engine = EvalEngine.get();
    IEvalStepListener listener = engine.getStepListener();
    if (listener != null) {
        listener.add(Convert.matrix2List(originalMatrix), Convert.matrix2List(rowReducedMatrix), engine.getRecursionCounter(), -1, "ReducedRowEchelonForm");
    }
    return rowReducedMatrix;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IEvalStepListener(org.matheclipse.core.interfaces.IEvalStepListener) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 8 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class Surd method e2DblArg.

@Override
public IExpr e2DblArg(INum d0, INum d1) {
    double val = d0.doubleValue();
    double r = d1.doubleValue();
    if (r == 0.0d) {
        EvalEngine ee = EvalEngine.get();
        ee.printMessage("Surd(a,b) division by zero");
        return F.Indeterminate;
    }
    if (val < 0.0d) {
        return F.num(-Math.pow(-val, 1.0d / r));
    }
    return F.num(Math.pow(val, 1.0d / r));
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine)

Example 9 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class BasicTeXTestCase method setUp.

/**
	 * The JUnit setup method
	 */
protected void setUp() {
    try {
        //			F.initSymbols();
        EvalEngine engine = new EvalEngine();
        texUtil = new TeXUtilities(engine, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) TeXUtilities(org.matheclipse.core.eval.TeXUtilities)

Example 10 with EvalEngine

use of org.matheclipse.core.eval.EvalEngine in project symja_android_library by axkr.

the class Iterator method create.

/**
	 * Iterator specification for functions like <code>Table()</code> or
	 * <code>Sum()</code> or <code>Product()</code>
	 * 
	 * @param list
	 *            a list representing an iterator specification
	 * @param engine
	 *            the evaluation engine
	 * @return the iterator
	 */
public static IIterator<IExpr> create(final IAST list, final EvalEngine engine) {
    EvalEngine evalEngine = engine;
    IExpr lowerLimit;
    IExpr upperLimit;
    IExpr step;
    ISymbol variable;
    boolean fNumericMode;
    // fNumericMode = evalEngine.isNumericMode() ||
    // list.isMember(Predicates.isNumeric(), false);
    boolean localNumericMode = evalEngine.isNumericMode();
    try {
        if (list.hasNumericArgument()) {
            evalEngine.setNumericMode(true);
        }
        fNumericMode = evalEngine.isNumericMode();
        switch(list.size()) {
            case 2:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
                step = F.C1;
                variable = null;
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 3:
                lowerLimit = F.C1;
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                step = F.C1;
                if (list.arg1() instanceof ISymbol) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (upperLimit instanceof Num) {
                    return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (upperLimit.isInteger()) {
                    try {
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, 1, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (upperLimit.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
                }
                break;
            case 4:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
                step = F.C1;
                if (list.arg1().isSymbol()) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (lowerLimit instanceof Num && upperLimit instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational()) {
                    try {
                        return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
                    ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
                    ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
                    return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
                }
                break;
            case 5:
                lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
                upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
                step = evalEngine.evalWithoutNumericReset(list.arg4());
                if (list.arg1() instanceof ISymbol) {
                    variable = (ISymbol) list.arg1();
                } else {
                    variable = null;
                }
                if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
                    return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
                }
                if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
                    try {
                        int iLowerLimit = ((IInteger) lowerLimit).toInt();
                        int iUpperLimit = ((IInteger) upperLimit).toInt();
                        int iStep = ((IInteger) step).toInt();
                        return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
                    try {
                        return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
                    } catch (ArithmeticException ae) {
                    //
                    }
                } else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
                    return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
                }
                break;
            default:
                lowerLimit = null;
                upperLimit = null;
                step = null;
                variable = null;
        }
        return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
    } finally {
        evalEngine.setNumericMode(localNumericMode);
    }
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) INum(org.matheclipse.core.interfaces.INum) Num(org.matheclipse.core.expression.Num) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IRational(org.matheclipse.core.interfaces.IRational) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)32 IExpr (org.matheclipse.core.interfaces.IExpr)15 IAST (org.matheclipse.core.interfaces.IAST)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 IRational (org.matheclipse.core.interfaces.IRational)3 IOException (java.io.IOException)2 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)2 Num (org.matheclipse.core.expression.Num)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 INum (org.matheclipse.core.interfaces.INum)2 Parser (org.matheclipse.parser.client.Parser)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 PrintWriter (java.io.PrintWriter)1 Apint (org.apfloat.Apint)1 UnivariateFunction (org.hipparchus.analysis.UnivariateFunction)1 RombergIntegrator (org.hipparchus.analysis.integration.RombergIntegrator)1 SimpsonIntegrator (org.hipparchus.analysis.integration.SimpsonIntegrator)1 TrapezoidIntegrator (org.hipparchus.analysis.integration.TrapezoidIntegrator)1