@Override public IExpr evaluate(final IAST ast) { Validate.checkRange(ast, 2, 3); IExpr arg = F.evalExpandAll(ast.get(1)); try { if (arg.isTimes() || arg.isPower()) { IExpr[] parts = Apart.getFractionalParts(arg); if (parts != null) { if (parts[0].isPlus() && parts[1].isPlus()) { IAST[] numParts = ((IAST) parts[0]).split(new PolynomialPredicate()); IAST[] denParts = ((IAST) parts[1]).split(new PolynomialPredicate()); IExpr denParts0 = F.eval(denParts[0]); if (!denParts0.equals(F.C1)) { IExpr[] result = Cancel.cancelGCD(numParts[0], denParts0); if (result != null) { return F.Times( result[0], numParts[1], F.Power(F.Times(result[1], denParts[1]), F.CN1)); } } } } } } catch (JASConversionException jce) { if (Config.DEBUG) { jce.printStackTrace(); } } return arg; }
@Override public IExpr evaluate(final IAST lst) { if (lst.size() >= 3) { try { if (lst.get(1).isVector() < 0) { return null; } if (lst.get(2).isVector() < 0) { return null; } if (lst.size() == 3) { IAST vars = (IAST) lst.get(2); if (vars.size() <= 1) { return null; } List<ISymbol> varList = new ArrayList<ISymbol>(vars.size() - 1); String[] pvars = new String[vars.size() - 1]; for (int i = 1; i < vars.size(); i++) { if (!vars.get(i).isSymbol()) { return null; } varList.add((ISymbol) vars.get(i)); pvars[i - 1] = ((ISymbol) vars.get(i)).toString(); } GroebnerBasePartial<BigRational> gbp = new GroebnerBasePartial<BigRational>(); IAST polys = (IAST) lst.get(1); List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(polys.size() - 1); JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO); for (int i = 1; i < polys.size(); i++) { IExpr expr = F.evalExpandAll(polys.get(i)); GenPolynomial<BigRational> poly = jas.expr2JAS(expr); polyList.add(poly); } OptimizedPolynomialList<BigRational> opl = gbp.partialGB(polyList, pvars); // System.out.println(opl); IAST resultList = F.List(); for (GenPolynomial<BigRational> p : opl.list) { // System.out.println(p); resultList.add(jas.poly2Expr(p, null)); } return resultList; } } catch (JASConversionException e) { if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } } return null; }
@Override public IExpr evaluate(final IAST functionList) { if (functionList.size() != 2) { throw new WrongNumberOfArguments(functionList, 1, functionList.size() - 1); } return F.bool(functionList.get(1).isVector() != (-1)); }
@Override public IExpr evaluate(final IAST ast) { if (ast.size() != 2) { return null; } IExpr temp = roots(ast, true); if (temp == null || !temp.isList()) { return null; } IAST list = (IAST) temp; IAST result = F.List(); for (int i = 1; i < list.size(); i++) { result.add(F.evaln(list.get(i))); } return result; }
public static boolean polynomialQ(final IExpr polnomialExpr, final IAST variables) { try { IExpr expr = F.evalExpandAll(polnomialExpr); ASTRange r = new ASTRange(variables, 1); JASConvert<IExpr> jas = new JASConvert<IExpr>(r.toList(), new ExprRingFactory()); return jas.expr2IExprJAS(expr) != null; } catch (JASConversionException e) { // exception will be thrown if the expression is not a JAS polynomial } return false; }
public IExpr evaluate(final IAST ast) { Validate.checkSize(ast, 3); if (ast.get(1).isSymbol()) { if (ast.get(2).isAST("Blank")) { IAST blank = (IAST) ast.get(2); if (blank.size() == 1) { return F.$p((ISymbol) ast.get(1)); } if (blank.size() == 2) { return F.$p((ISymbol) ast.get(1), blank.get(1)); } } if (ast.get(2).isPattern()) { IPattern blank = (IPattern) ast.get(2); if (blank.isBlank()) { return F.$p((ISymbol) ast.get(1), blank.getCondition()); } } } return null; }
/** * Returns <code>True</code> if the given expression is a polynoomial object; <code>False</code> * otherwise */ @Override public IExpr evaluate(final IAST ast) { if (ast.size() != 3) { throw new WrongNumberOfArguments(ast, 2, ast.size() - 1); } IAST list; if (ast.get(2).isList()) { list = (IAST) ast.get(2); } else { list = List(ast.get(2)); } return F.bool(polynomialQ(ast.get(1), list)); }
public static void generateCoreRules(final String pckgname) { String name = pckgname; if (!name.startsWith("/")) { name = "/" + name; } name = name.replace('.', '/'); // Get a File object for the package final URL url = SourceGenerator.class.getResource(name); final File directory = new File(url.getFile()); if (directory.exists()) { // Get the list of the files contained in the package final String[] files = directory.list(); for (int i = 0; i < files.length; i++) { // we are only interested in .class files if (files[i].endsWith(".class")) { // removes the .class extension final String classname = files[i].substring(0, files[i].length() - 6); try { // Try to create an instance of the object Class.forName(pckgname + "." + classname).newInstance(); System.out.println(" f.createSymbol(\"" + classname + "\");"); F.$s(classname); } catch (final ClassNotFoundException cnfex) { System.err.println(cnfex); } catch (final InstantiationException iex) { // We try to instantiate an interface // or an object that does not have a // default constructor } catch (final IllegalAccessException iaex) { // The class is not public } } } } }
public static void main(final String[] args) { F.initSymbols(); // generateTemplates("org.matheclipse.core.reflection.system"); // return; // generateCoreRules("org.matheclipse.core.reflection.system"); // System.out.println("public static void init() {"); // System.out.println(" ExpressionFactory f = // ExpressionFactory.get();"); // // generateCoreRules("org.matheclipse.core.reflection.system"); // System.out.println("}"); // // System.out.println("\n--------------------\n"); // generate list of symbols generateJFlexKeywords("org.matheclipse.core.reflection.system", 1); // generate javascript: System.out.println("private final String[] COMPLETIONS = { "); generateJavaScript("org.matheclipse.core.reflection.system", false); System.out.println("};"); System.out.println("\n--------------------\n"); generateDeclareSymbols("org.matheclipse.core.reflection.system"); System.out.println("\n--------------------\n"); generateCreateSymbols("org.matheclipse.core.reflection.system"); System.out.println("\n--------------------\n"); generateFunctions("org.matheclipse.core.reflection.system"); System.out.println("\n--------------------\n"); generateEvalFunctions("org.matheclipse.core.reflection.system"); }
public IExpr evaluate(final IAST ast) { if (ast.size() != 2) { return null; } return F.stringx(new StringBuffer(ast.get(1).fullFormString())); }
@Override public IExpr evaluate(final IAST ast) { Validate.checkRange(ast, 3); if (ast.size() == 3 && ast.get(2).isList() && ((IAST) ast.get(2)).size() == 4) { IAST list = (IAST) ast.get(2); if (ast.get(1).isPlus()) { return ((IAST) ast.get(1)).map(Functors.replace1st(F.Sum(F.Null, ast.get(2)))); } if (list.get(1).isSymbol() && list.get(2).isInteger() && list.get(3).isSymbol()) { final ISymbol var = (ISymbol) list.get(1); final IInteger from = (IInteger) list.get(2); final ISymbol to = (ISymbol) list.get(3); if (ast.get(1).isFree(var, true) && ast.get(1).isFree(to, true)) { if (from.equals(F.C1)) { return F.Times(to, ast.get(1)); } if (from.equals(F.C0)) { return F.Times(Plus(to, C1), ast.get(1)); } } else { if (ast.get(1).isTimes()) { // Sum[ Times[a,b,c,...], {var, from, to} ] IAST filterCollector = F.Times(); IAST restCollector = F.Times(); ((IAST) ast.get(1)) .filter( filterCollector, restCollector, new Predicate<IExpr>() { @Override public boolean apply(IExpr input) { return input.isFree(var, true) && input.isFree(to, true); } }); if (filterCollector.size() > 1) { if (restCollector.size() == 2) { filterCollector.add(F.Sum(restCollector.get(1), ast.get(2))); } else { filterCollector.add(F.Sum(restCollector, ast.get(2))); } return filterCollector; } } if (from.equals(F.C0)) { IExpr repl = ast.get(1).replaceAll(F.List(F.Rule(var, F.Slot(F.C1)), F.Rule(to, F.Slot(F.C2)))); if (repl != null) { IExpr temp = MAP_0_N.get(repl); if (temp != null) { return temp.replaceAll(F.Rule(F.Slot(F.C1), to)); } } } } if (from.isPositive()) { return F.Subtract( F.Sum(ast.get(1), F.List(var, C0, to)), F.Sum(ast.get(1), F.List(var, C0, from.minus(F.C1)))); } } } IAST resultList = Plus(); IExpr temp = evaluateTable(ast, resultList, C0); if (temp.equals(resultList)) { return null; } return temp; }
public void testHashValueVisitor() { HashValueVisitor v = new HashValueVisitor(1); IExpr expr = F.Power(F.Sin(F.Log(F.C1)), F.C2); int hash = expr.accept(v); assertEquals(hash, -1895901688); v.setUp(); expr = F.Power(F.Sin(F.Cos(F.C3)), F.C2); hash = expr.accept(v); assertEquals(hash, -1895901688); v.setUp(); expr = F.Power(F.Sin(F.$p("x")), F.C2); hash = expr.accept(v); assertEquals(hash, -1895901688); v.setUp(); expr = F.Power(F.Cos(F.Sin(F.C3)), F.C2); hash = expr.accept(v); assertEquals(hash, -1896372423); }
public VisitorTestCase(String name) { super(name); F.initSymbols(null, null, false); }