Search in sources :

Example 51 with EvalEngine

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

the class ConvertRubi method main.

public static void main(String[] args) {
    Config.SERVER_MODE = false;
    ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS = false;
    Config.RUBI_CONVERT_SYMBOLS = true;
    EvalEngine engine = new EvalEngine(false);
    engine.getContextPath().add(org.matheclipse.core.expression.Context.RUBI);
    // engine.beginPackage(org.matheclipse.core.expression.Context.RUBI_STR);
    EvalEngine.set(engine);
    addPredefinedSymbols();
    // use same order as in Rubi.m
    String userHome = System.getProperty("user.home");
    String[] fileNames = { // 
    userHome + "/git/symja_android_library/symja_android_library/Rubi/RubiRules4.16.0_FullLHS.m" };
    IASTAppendable listOfRules = F.ListAlloc(10000);
    int fcnt = 0;
    for (int i = 0; i < fileNames.length; i++) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(">>>>> File name: " + fileNames[i]);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
        StringBuffer buffer = new StringBuffer(100000);
        List<ASTNode> list = parseFileToList(fileNames[i]);
        // 2);
        if (list != null) {
            int cnt = 0;
            for (int j = 0; j < list.size(); j++) {
                if (cnt == 0) {
                    buffer = new StringBuffer(100000);
                    buffer.append(HEADER + fcnt + " { \n  public static IAST RULES = List( \n");
                }
                ASTNode astNode = list.get(j);
                cnt++;
                convert(astNode, buffer, cnt == NUMBER_OF_RULES_PER_FILE || j == list.size() - 1, listOfRules);
                if (cnt == NUMBER_OF_RULES_PER_FILE) {
                    buffer.append("  );\n" + FOOTER);
                    writeFile("C:/temp/rubi/IntRules" + fcnt + ".java", buffer);
                    fcnt++;
                    cnt = 0;
                }
            }
            if (cnt != 0) {
                // System.out.println(");");
                buffer.append("  );\n" + FOOTER);
                writeFile("C:/temp/rubi/IntRules" + fcnt + ".java", buffer);
                fcnt++;
                cnt = 0;
            }
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
            System.out.println(">>>>> Number of entries: " + list.size());
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
        }
    }
    // which built-in symbols are used how often?
    for (Map.Entry<String, Integer> entry : AST2Expr.RUBI_STATISTICS_MAP.entrySet()) {
        System.out.println(entry.getKey() + "  >>>  " + entry.getValue());
    }
// File file = new File("./Rubi/IntegrateRules.bin");
// 
// byte[] byteArray = WL.serializeInternal(listOfRules);
// try {
// 
// System.out.println("Creating binary file:" + file.toString());
// com.google.common.io.Files.write(byteArray, file);
// 
// byteArray = com.google.common.io.Files.toByteArray(file);
// IExpr result = WL.deserializeInternal(byteArray);
// // System.out.println(result.toString());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) Map(java.util.Map)

Example 52 with EvalEngine

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

the class ConvertRubiUtilityFunctions method main.

public static void main(String[] args) {
    Config.SERVER_MODE = false;
    ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS = false;
    Config.RUBI_CONVERT_SYMBOLS = true;
    EvalEngine engine = new EvalEngine(false);
    engine.getContextPath().add(org.matheclipse.core.expression.Context.RUBI);
    EvalEngine.set(engine);
    ConvertRubi.addPredefinedSymbols();
    IASTAppendable listOfRules = F.ListAlloc(10000);
    String userHome = System.getProperty("user.home");
    String[] fileNames = { // 
    userHome + "/git/symja_android_library/symja_android_library/Rubi/IntegrationUtilityFunctions.m" };
    for (int i = 0; i < fileNames.length; i++) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(">>>>> File name: " + fileNames[i]);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
        StringBuffer buffer = new StringBuffer(100000);
        List<ASTNode> list = parseFileToList(fileNames[i]);
        if (list != null) {
            int cnt = 0;
            int fcnt = 0;
            TreeSet<String> functionSet = new TreeSet<String>();
            TreeSet<String> uniqueFunctionSet = new TreeSet<String>();
            for (int j = 0; j < list.size(); j++) {
                if (cnt == 0) {
                    buffer = new StringBuffer(100000);
                    buffer.append(HEADER + fcnt + " { \n  public static IAST RULES = List( \n");
                }
                ASTNode astNode = list.get(j);
                cnt++;
                convert(astNode, buffer, cnt == NUMBER_OF_RULES_PER_FILE || j == list.size(), functionSet, listOfRules);
                if (cnt == NUMBER_OF_RULES_PER_FILE) {
                    buffer.append("  );\n" + FOOTER);
                    writeFile("C:/temp/rubi/UtilityFunctions" + fcnt + ".java", buffer);
                    fcnt++;
                    cnt = 0;
                }
            }
            if (cnt != 0) {
                // System.out.println(");");
                buffer.append("  );\n" + FOOTER);
                writeFile("C:/temp/rubi/UtilityFunctions" + fcnt + ".java", buffer);
            }
            buffer = new StringBuffer(100000);
            for (String str : functionSet) {
                String[] spl = str.split(",");
                String functionName = spl[0];
                uniqueFunctionSet.add(functionName);
            }
            for (String str : uniqueFunctionSet) {
                String functionName = str;
                buffer.append("    F.PREDEFINED_INTERNAL_FORM_STRINGS.put(\"" + functionName + "\", Context.RUBI_STR + \"" + functionName + "\");\n");
            }
            System.out.println(buffer.toString());
            buffer = new StringBuffer(100000);
            for (String str : functionSet) {
                String[] spl = str.split(",");
                String functionName = spl[0];
                int numberOfArguments = Integer.valueOf(spl[1]).intValue() - 1;
                switch(numberOfArguments) {
                    case 1:
                        buffer.append("  public static IAST " + functionName + "(final IExpr a0) {\n");
                        buffer.append("    return unary($rubi(\"" + functionName + "\"), a0);\n");
                        buffer.append("  }\n\n");
                        break;
                    case 2:
                        buffer.append("  public static IAST " + functionName + "(final IExpr a0, final IExpr a1) {\n");
                        buffer.append("    return binary($rubi(\"" + functionName + "\"), a0, a1);\n");
                        buffer.append("  }\n\n");
                        break;
                    case 3:
                        buffer.append("  public static IAST " + functionName + "(final IExpr a0, final IExpr a1, final IExpr a2) {\n");
                        buffer.append("    return ternary($rubi(\"" + functionName + "\"), a0, a1, a2);\n");
                        buffer.append("  }\n\n");
                        break;
                    case 4:
                        buffer.append("  public static IAST " + functionName + "(final IExpr a0, final IExpr a1, final IExpr a2, final IExpr a3) {\n");
                        buffer.append("    return quaternary($rubi(\"" + functionName + "\"), a0, a1, a2, a3);\n");
                        buffer.append("  }\n\n");
                        break;
                    case 5:
                        buffer.append("  public static IAST " + functionName + "(final IExpr a0, final IExpr a1, final IExpr a2, final IExpr a3, final IExpr a4) {\n");
                        buffer.append("    return quinary($rubi(\"" + functionName + "\"), a0, a1, a2, a3, a4);\n");
                        buffer.append("  }\n\n");
                        break;
                    case 6:
                        buffer.append("  public static IAST " + functionName + "(final IExpr a0, final IExpr a1, final IExpr a2, final IExpr a3, final IExpr a4, final IExpr a5) {\n");
                        buffer.append("    return senary($rubi(\"" + functionName + "\"), a0, a1, a2, a3, a4, a5);\n");
                        buffer.append("  }\n\n");
                        break;
                    case Integer.MAX_VALUE:
                        // MAX_VALUE indicates a left-hand-side form with a PetternSequence (i.e. f[x__]:=...)
                        buffer.append("  public static IAST " + functionName + "(final IExpr... a) {\n");
                        buffer.append("    return ast(a, $rubi(\"" + functionName + "\"));\n");
                        buffer.append("  }\n\n");
                        break;
                    default:
                        // System.out.println("ERROR in SWITCH:" + functionName + " - " + numberOfArguments);
                        // public static IAST <functionName>(final IExpr... a) {
                        // return ast(a, Part);
                        // }
                        buffer.append("  public static IAST " + functionName + "(final IExpr... a) {\n");
                        buffer.append("    return ast(a, $rubi(\"" + functionName + "\"));\n");
                        buffer.append("  }\n\n");
                }
            }
            System.out.println(buffer.toString());
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
            System.out.println(">>>>> Number of entries: " + list.size());
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
        // for (int j = 0; j < list.size(); j++) {
        // System.out.println(list.get(j).toString());
        // }
        }
    }
    // which built-in symbols are used how often?
    for (Map.Entry<String, Integer> entry : AST2Expr.RUBI_STATISTICS_MAP.entrySet()) {
        System.out.println(entry.getKey() + "  >>>  " + entry.getValue());
    }
    File file = new File(userHome + "/git/symja_android_library/symja_android_library/Rubi/IntegrationUtilityFunctions.ser");
    byte[] byteArray = WL.serialize(listOfRules);
    try {
        com.google.common.io.Files.write(byteArray, file);
        byteArray = com.google.common.io.Files.toByteArray(file);
        IExpr result = WL.deserialize(byteArray);
        System.out.println(result.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) TreeSet(java.util.TreeSet) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) Map(java.util.Map) File(java.io.File)

Example 53 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 createMatcher
 * @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 createMatcher, boolean ignoreTimestamp) {
    if (sourceLocation.exists()) {
        // Get the list of the files contained in the package
        final String[] files = sourceLocation.list();
        if (files != null) {
            StringBuilder 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")) {
                    IExpr expr = parseFileToList(sourceFile, engine);
                    if (expr == null) {
                        System.err.println();
                        System.err.println("Abort after not parsed expression.");
                        return;
                    } else {
                        buffer = new StringBuilder(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);
                            if (className.equals("FunctionExpandRules") || className.equals("FunctionRangeRules") || className.equals("PodDefaultsRules")) {
                                out = createHeader(className, targetFile, createMatcher);
                                convertList(expr, "", buffer, out, symbolName, engine);
                                out.println(FOOTER1);
                                out.close();
                            } else {
                                out = createHeader(className, targetFile, createMatcher);
                                convert(expr, "", out, symbolName, createMatcher, engine);
                                out.println(FOOTER1);
                                out.close();
                            }
                        // } catch (IOException e) {
                        // e.printStackTrace();
                        } catch (Exception e) {
                            System.err.println();
                            System.err.println("Abort after exception.");
                            e.printStackTrace();
                            return;
                        }
                    }
                }
            }
        }
    } else {
        System.out.println("source location doesn't exists: " + sourceLocation.toString());
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 54 with EvalEngine

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

the class RulePreprocessor method generateFunctionSerialized.

public static void generateFunctionSerialized(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) {
            StringBuilder 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")) {
                    IExpr expr = parseFileToList(sourceFile, engine);
                    if (expr == null) {
                        System.err.println();
                        System.err.println("Abort after not parsed expression.");
                        return;
                    } else {
                        buffer = new StringBuilder(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 + ".bin");
                            if (targetFile.exists()) {
                                if (!ignoreTimestamp && (sourceFile.lastModified() <= targetFile.lastModified())) {
                                    // existing ones
                                    continue;
                                }
                            }
                            System.out.println(className);
                            if (className.equals("FunctionExpandRules")) {
                                // || className.equals("PodDefaultsRules")) {
                                out = new PrintWriter(targetFile.getCanonicalPath());
                                byte[] binaryData = convertListSerialized(expr, buffer, out, engine);
                                com.google.common.io.Files.write(binaryData, targetFile);
                            }
                        } catch (Exception e) {
                            System.err.println();
                            System.err.println("Abort after exception.");
                            e.printStackTrace();
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 55 with EvalEngine

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

the class DSolve method odeSeparable.

private static IExpr odeSeparable(EvalEngine engine, IExpr m, IExpr n, IExpr x, IExpr y, IExpr C_1) {
    if (n.isOne()) {
        IExpr fxExpr = F.NIL;
        IExpr gyExpr = F.NIL;
        if (m.isFree(y)) {
            gyExpr = F.C1;
            fxExpr = m;
        } else if (m.isTimes()) {
            IAST timesAST = (IAST) m;
            IASTAppendable fx = F.TimesAlloc(timesAST.size());
            IASTAppendable gy = F.TimesAlloc(timesAST.size());
            timesAST.forEach(expr -> {
                if (expr.isFree(y)) {
                    fx.append(expr);
                } else {
                    gy.append(expr);
                }
            });
            fxExpr = engine.evaluate(fx);
            gyExpr = engine.evaluate(gy);
        }
        if (fxExpr.isPresent() && gyExpr.isPresent()) {
            gyExpr = S.Integrate.of(engine, F.Divide(F.C1, gyExpr), y);
            fxExpr = S.Plus.of(engine, F.Integrate(F.Times(F.CN1, fxExpr), x), C_1);
            IExpr yEquation = S.Subtract.of(engine, gyExpr, fxExpr);
            IExpr result = Eliminate.extractVariable(yEquation, y, false, engine);
            if (result.isPresent()) {
                return engine.evaluate(result);
            }
        }
    }
    return F.NIL;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial) ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) ToggleFeature(org.matheclipse.core.basic.ToggleFeature) IInteger(org.matheclipse.core.interfaces.IInteger) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) S(org.matheclipse.core.expression.S) ExprRingFactory(org.matheclipse.core.polynomials.longexponent.ExprRingFactory) Logger(org.apache.logging.log4j.Logger) AbstractFunctionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator) Validate(org.matheclipse.core.eval.exception.Validate) IExpr(org.matheclipse.core.interfaces.IExpr) LogManager(org.apache.logging.log4j.LogManager) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

EvalEngine (org.matheclipse.core.eval.EvalEngine)131 IExpr (org.matheclipse.core.interfaces.IExpr)71 IAST (org.matheclipse.core.interfaces.IAST)39 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)20 ISymbol (org.matheclipse.core.interfaces.ISymbol)20 IOException (java.io.IOException)13 F (org.matheclipse.core.expression.F)12 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)11 S (org.matheclipse.core.expression.S)11 IInteger (org.matheclipse.core.interfaces.IInteger)11 ASTNode (org.matheclipse.parser.client.ast.ASTNode)11 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 AST2Expr (org.matheclipse.core.convert.AST2Expr)9 ExprParser (org.matheclipse.core.parser.ExprParser)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 MathException (org.matheclipse.parser.client.math.MathException)8 ArrayList (java.util.ArrayList)7 Config (org.matheclipse.core.basic.Config)7 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)7