Search in sources :

Example 1 with WrongNumberOfArguments

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

the class Import method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (!(ast.arg1() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    if (!(ast.arg2() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 2, ast.size() - 1);
    }
    IStringX arg1 = (IStringX) ast.arg1();
    IStringX arg2 = (IStringX) ast.arg2();
    FileReader reader = null;
    try {
        reader = new FileReader(arg1.toString());
        if (arg2.contentEquals("Table")) {
            AST2Expr ast2Expr = AST2Expr.CONST;
            if (engine.isRelaxedSyntax()) {
                ast2Expr = AST2Expr.CONST_LC;
            }
            final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
            CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(' ');
            Iterable<CSVRecord> records = csvFormat.parse(reader);
            IAST rowList = F.List();
            for (CSVRecord record : records) {
                IAST columnList = F.List();
                for (String string : record) {
                    final ASTNode node = parser.parse(string);
                    IExpr temp = ast2Expr.convert(node, engine);
                    columnList.append(temp);
                }
                rowList.append(columnList);
            }
            return rowList;
        }
    } catch (IOException ioe) {
        engine.printMessage("Import: file " + arg1.toString() + " not found!");
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
    return F.NIL;
}
Also used : IOException(java.io.IOException) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with WrongNumberOfArguments

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

the class Package method loadPackage.

/**
	 * Load a package from the given reader
	 * 
	 * @param is
	 */
public static void loadPackage(final EvalEngine engine, final Reader is) {
    String record = null;
    // new
    final BufferedReader r = new BufferedReader(is);
    try {
        StringBuilder builder = new StringBuilder(2048);
        while ((record = r.readLine()) != null) {
            builder.append(record);
            builder.append('\n');
        }
        IExpr parsedExpression = engine.parse(builder.toString());
        if (parsedExpression != null && parsedExpression.isAST()) {
            IAST ast = (IAST) parsedExpression;
            if (ast.size() != 4 || !(ast.arg1() instanceof IStringX) || !ast.arg2().isList() || !ast.arg3().isList()) {
                throw new WrongNumberOfArguments(ast, 3, ast.size() - 1);
            }
            IAST symbols = (IAST) ast.arg2();
            IAST list = (IAST) ast.arg3();
            evalPackage(symbols, list, engine);
        }
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        try {
            r.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : BufferedReader(java.io.BufferedReader) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) IOException(java.io.IOException) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) IOException(java.io.IOException)

Example 3 with WrongNumberOfArguments

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

the class Get method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    if (!(ast.arg1() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    IStringX arg1 = (IStringX) ast.arg1();
    FileReader reader;
    try {
        reader = new FileReader(arg1.toString());
        return loadPackage(engine, reader);
    } catch (FileNotFoundException e) {
        engine.printMessage("Get: file " + arg1.toString() + " not found!");
    }
    return F.Null;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IStringX(org.matheclipse.core.interfaces.IStringX) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments)

Example 4 with WrongNumberOfArguments

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

the class Package method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    if (!(ast.arg1() instanceof IStringX) || !ast.arg2().isList() || !ast.arg3().isList()) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    if (Config.SERVER_MODE) {
        throw new RuleCreationError(null);
    }
    IAST symbols = (IAST) ast.arg2();
    IAST list = (IAST) ast.arg3();
    evalPackage(symbols, list, engine);
    return F.Null;
}
Also used : IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) RuleCreationError(org.matheclipse.core.eval.exception.RuleCreationError)

Example 5 with WrongNumberOfArguments

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

the class FromContinuedFraction method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    if (!ast.arg1().isList()) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    IAST list = (IAST) ast.arg1();
    if (list.size() < 2) {
        return F.NIL;
    }
    int size = list.size() - 1;
    IExpr result = list.get(size--);
    for (int i = size; i >= 1; i--) {
        result = F.Plus(list.get(i), F.Power(result, F.CN1));
    }
    return result;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments)

Aggregations

WrongNumberOfArguments (org.matheclipse.core.eval.exception.WrongNumberOfArguments)5 IAST (org.matheclipse.core.interfaces.IAST)4 IStringX (org.matheclipse.core.interfaces.IStringX)4 IExpr (org.matheclipse.core.interfaces.IExpr)3 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVRecord (org.apache.commons.csv.CSVRecord)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 RuleCreationError (org.matheclipse.core.eval.exception.RuleCreationError)1 Parser (org.matheclipse.parser.client.Parser)1 ASTNode (org.matheclipse.parser.client.ast.ASTNode)1