Esempio n. 1
0
  @Override
  public IExpr evaluate(final IAST ast) {
    if ((ast.size() == 2) && ast.get(1).isList()) {
      final AST lst = (AST) ast.get(1);
      final int len0 = lst.size() - 1;

      final IAST resultList = List();
      final int[] indexArray = new int[2];
      indexArray[0] = len0;
      indexArray[1] = len0;

      final IIndexFunction<IExpr> function =
          new IIndexFunction<IExpr>() {
            public IExpr evaluate(int[] index) {
              return Power(lst.get(index[0] + 1), IntegerSym.valueOf(index[1]));
            }
          };
      final IndexTableGenerator<IExpr, IAST> generator =
          new IndexTableGenerator<IExpr, IAST>(indexArray, resultList, function, AST.COPY);
      final IAST matrix = (IAST) generator.table();
      if (matrix != null) {
        matrix.addEvalFlags(IAST.IS_MATRIX);
      }
      return matrix;
    }

    return null;
  }
Esempio n. 2
0
 @Override
 public IExpr evaluate(final IAST ast) {
   Validate.checkSize(ast, 2);
   if (ast.get(1).isAST()) {
     return ast.get(1).replaceRepeated(REPLACE_RULES);
   }
   return ast.get(1);
 }
Esempio n. 3
0
 @Override
 public IExpr numericEval(final IAST functionList) {
   Validate.checkSize(functionList, 2);
   if (functionList.get(1) instanceof Num) {
     return numericEvalD1((Num) functionList.get(1));
   }
   if (functionList.get(1) instanceof ComplexNum) {
     return numericEvalDC1((ComplexNum) functionList.get(1));
   }
   return numericEvalArg1(functionList.get(1));
 }
Esempio n. 4
0
 /**
  * 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));
 }
Esempio n. 5
0
 @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));
 }
Esempio n. 6
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;
 }
Esempio n. 7
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;
    }
 /**
  * This default implementation just calls {@link #increment} in a loop over the specified portion
  * of the input array.
  *
  * <p>Throws IllegalArgumentException if the input values array is null.
  *
  * @param values array holding values to add
  * @param begin index of the first array element to add
  * @param length number of array elements to add
  * @throws IllegalArgumentException if values is null
  * @see
  *     org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[],
  *     int, int)
  */
 public void incrementAll(IAST values, int begin, int length) {
   if (test(values, begin, length)) {
     int k = begin + length;
     for (int i = begin; i < k; i++) {
       increment(values.get(i));
     }
   }
 }
Esempio n. 9
0
 /**
  * Converts a given function into the corresponding TeX output
  *
  * @param buf StringBuffer for MathML output
  * @param f The math function which should be converted to MathML
  */
 public boolean convert(final StringBuffer buf, final IAST f, final int precedence) {
   if (f.size() != 2) {
     return false;
   }
   buf.append(" \\neg ");
   fFactory.convert(buf, f.get(1), 0);
   return true;
 }
Esempio n. 10
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;
  }
Esempio n. 11
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;
  }
Esempio n. 12
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;
 }
Esempio n. 13
0
 /**
  * Converts a given function into the corresponding MathML output
  *
  * @param buf StringBuffer for MathML output
  * @param f The math function which should be converted to MathML
  */
 public boolean convert(final StringBuffer buf, final IAST f, final int precedence) {
   if (f.size() != 2) {
     return false;
   }
   fFactory.tagStart(buf, "mrow");
   // &LeftFloor; &x0230A;
   fFactory.tag(buf, "mo", "&#x230A;");
   fFactory.convert(buf, f.get(1), 0);
   // &RightFloor; &#0230B;
   fFactory.tag(buf, "mo", "&#230B;");
   fFactory.tagEnd(buf, "mrow");
   return true;
 }
Esempio n. 14
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;
  }
Esempio n. 15
0
 /** @see org.matheclipse.core.expression.AST#hashCode() */
 public int visit(IAST list) {
   try {
     ++currentDepth;
     if (currentDepth <= fMaxDepth) {
       int hash = 0;
       if (list.size() > 1) {
         hash = (31 * list.get(0).hashCode() + list.get(1).accept(this) + list.size());
       } else {
         if (list.size() == 1) {
           hash = (17 * list.get(0).hashCode());
         } else {
           // this case shouldn't happen
           hash = 41;
         }
       }
       return hash;
     } else {
       return (31 * list.head().hashCode() + list.size());
     }
   } finally {
     --currentDepth;
   }
 }
Esempio n. 16
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;
 }
Esempio n. 17
0
  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;
  }
Esempio n. 18
0
  @Override
  public IExpr evaluate(final IAST ast) {
    Validate.checkSize(ast, 2);

    return evaluateArg1(ast.get(1));
  }
Esempio n. 19
0
  @Override
  public IExpr evaluate(final IAST ast) {
    final EvalEngine engine = EvalEngine.get();
    if ((ast.size() == 3) && (ast.get(1)).isList()) {
      final IAST lst = (IAST) ast.get(1);
      final List<IExpr> variables = new ArrayList<IExpr>();
      IExpr result;

      try {
        // remember which local variables we use:
        for (int i = 1; i < lst.size(); i++) {
          if (lst.get(i).isSymbol()) {
            variables.add(lst.get(i));
            ((Symbol) lst.get(i)).pushLocalVariable();
          } else {
            if (lst.get(i).isAST(F.Set, 3)) {
              // lhs = rhs
              final IAST setFun = (IAST) lst.get(i);
              if (setFun.get(1).isSymbol()) {
                variables.add(setFun.get(1));
                ((Symbol) setFun.get(1)).pushLocalVariable(engine.evaluate(setFun.get(2)));
              }
            }
          }
        }

        result = engine.evaluate(ast.get(2));
      } finally {
        // pop all local variables from local variable stack
        for (int i = 0; i < variables.size(); i++) {
          ((Symbol) variables.get(i)).popLocalVariable();
        }
      }

      return result;
    }

    return null;
  }
Esempio n. 20
0
 public IExpr evaluate(final IAST ast) {
   if (ast.size() != 2) {
     return null;
   }
   return F.stringx(new StringBuffer(ast.get(1).fullFormString()));
 }
Esempio n. 21
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;
  }