Пример #1
0
 @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;
 }
Пример #2
0
    public boolean apply(IExpr expr) {
      if (expr.isRational()) {
        return true;
      }
      if (expr.isSymbol()) {
        return true;
      }
      if (expr.isTimes() || expr.isPlus()) {
        IAST ast = (IAST) expr;
        for (int i = 1; i < ast.size(); i++) {
          if (!apply(ast.get(i))) {
            return false;
          }
        }
        return true;
      }
      if (expr.isPower() && ((IAST) expr).get(1).isSymbol() && ((IAST) expr).get(2).isInteger()) {
        try {
          int in = ((IInteger) ((IAST) expr).get(2)).toInt();
          if (in > 0) {
            return true;
          }
        } catch (ArithmeticException ae) {

        }
        return false;
      }
      return false;
    }
Пример #3
0
 @Override
 public IExpr e2ObjArg(final IExpr o0, final IExpr o1) {
   if (o0.isList() && o1.isList()) {
     final IAST v0 = (IAST) o0;
     final IAST v1 = (IAST) o1;
     if ((v0.size() == 4) || (v1.size() == 4)) {
       return List(
           Plus(Times(v0.get(2), v1.get(3)), Times(F.CN1, v0.get(3), v1.get(2))),
           Plus(Times(v0.get(3), v1.get(1)), Times(F.CN1, v0.get(1), v1.get(3))),
           Plus(Times(v0.get(1), v1.get(2)), Times(F.CN1, v0.get(2), v1.get(1))));
     }
   }
   return null;
 }
Пример #4
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;
 }
Пример #5
0
 public boolean apply(final IExpr firstArg, final IExpr secondArg) {
   IAST list;
   if (secondArg.isList()) {
     list = (IAST) secondArg;
   } else {
     list = List(secondArg);
   }
   return polynomialQ(firstArg, list);
 }
Пример #6
0
  public IExpr evaluate(final IAST ast) {
    IAST list = ast;
    if (list.size() > 1) {
      IAST resultList = list.copyHead();
      if (AST.COPY.flatten(F.List, list, resultList, 1)) {
        list = resultList;
      }
      // IExpr max = F.Times(F.CN1, ExprFactory.Infinity);
      IExpr max1;
      IExpr max2;
      max1 = list.get(1);
      IAST f = list.copyHead();
      COMPARE_RESULT comp;
      for (int i = 2; i < list.size(); i++) {
        max2 = list.get(i);
        comp = Less.CONST.prepareCompare(max1, max2);

        if (comp == COMPARE_RESULT.TRUE) {
          max1 = max2;
        } else {
          if (comp == COMPARE_RESULT.UNDEFINED) {
            // undetermined
            if (max1.isNumber()) {
              f.add(max2);
            } else {
              f.add(max1);
              max1 = max2;
            }
          }
        }
      }
      if (f.size() > 1) {
        f.add(1, max1);
        if (f.equals(list)) {
          return null;
        }
        return f;
      } else {
        return max1;
      }
    }
    return null;
  }
Пример #7
0
  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);
  }
Пример #8
0
  /**
   * Check if the expression is canonical negative.
   *
   * @return <code>true</code> if the first argument is canonical negative
   */
  public static boolean isNegativeExpression(final IExpr expr) {
    if (expr.isNumber()) {
      if (((INumber) expr).complexSign() < 0) {
        return true;
      }
    } else if (expr.isTimes()) {
      IAST times = (IAST) expr;
      if (times.get(1).isNumber()) {
        if (((INumber) times.get(1)).complexSign() < 0) {
          return true;
        }
      }
    } else if (expr.isPlus()) {
      IAST plus = (IAST) expr;
      if (plus.get(1).isNumber()) {
        if (((INumber) plus.get(1)).complexSign() < 0) {
          return true;
        }
      }
    }

    return false;
  }
Пример #9
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;
  }