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;
}
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();
}
}
}
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;
}
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;
}
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;
}
Aggregations