Exemplo n.º 1
0
  @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;
  }
Exemplo n.º 2
0
 @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;
 }
Exemplo n.º 3
0
  @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;
  }