@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;
  }
Exemple #2
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));
 }
  /**
   * This method is used by <code>evaluate(double[], int, int)</code> methods to
   * verify that the input parameters designate a subarray of positive length.
   * <p>
   * <ul>
   * <li>returns <code>true</code> iff the parameters designate a subarray of
   * non-negative length</li>
   * <li>throws <code>IllegalArgumentException</code> if the array is null or or
   * the indices are invalid</li>
   * <li>returns <code>false</li> if the array is non-null, but
   * <code>length</code> is 0 unless <code>allowEmpty</code> is
   * <code>true</code>
   * </ul>
   * </p>
   *
   * @param values
   *          the input array
   * @param begin
   *          index of the first array element to include
   * @param length
   *          the number of elements to include
   * @param allowEmpty
   *          if <code>true</code> then zero length arrays are allowed
   * @return true if the parameters are valid
   * @throws IllegalArgumentException
   *           if the indices are invalid or the array is null
   * @since 3.0
   */
  protected boolean test(
      final IAST values, final int begin, final int length, final boolean allowEmpty) {

    if (values == null) {
      throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
      throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
      throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.size()) {
      throw new NumberIsTooLargeException(
          LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END, begin + length, values.size(), true);
    }

    if (length == 0 && !allowEmpty) {
      return false;
    }

    return true;
  }
Exemple #4
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;
    }
Exemple #5
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);
 }
Exemple #6
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;
 }
 @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));
 }
Exemple #8
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;
 }
Exemple #9
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;
 }
 /**
  * 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));
     }
   }
 }
  /**
   * This method is used by <code>evaluate(double[], double[], int, int)</code>
   * methods to verify that the begin and length parameters designate a subarray
   * of positive length and the weights are all non-negative, non-NaN, finite,
   * and not all zero.
   * <p>
   * <ul>
   * <li>returns <code>true</code> iff the parameters designate a subarray of
   * non-negative length and the weights array contains legitimate values.</li>
   * <li>throws <code>IllegalArgumentException</code> if any of the following
   * are true:
   * <ul>
   * <li>the values array is null</li>
   * <li>the weights array is null</li>
   * <li>the weights array does not have the same length as the values array</li>
   * <li>the weights array contains one or more infinite values</li>
   * <li>the weights array contains one or more NaN values</li>
   * <li>the weights array contains negative values</li>
   * <li>the start and length arguments do not determine a valid array</li>
   * </ul>
   * </li>
   * <li>returns <code>false</li> if the array is non-null, but
   * <code>length</code> is 0 unless <code>allowEmpty</code> is
   * <code>true</code>.
   * </ul>
   * </p>
   *
   * @param values
   *          the input array.
   * @param weights
   *          the weights array.
   * @param begin
   *          index of the first array element to include.
   * @param length
   *          the number of elements to include.
   * @param allowEmpty
   *          if {@code true} than allow zero length arrays to pass.
   * @return {@code true} if the parameters are valid.
   * @throws IllegalArgumentException
   *           if the indices are invalid or the array is {@code null}.
   * @since 3.0
   */
  protected boolean test(
      final IAST values,
      final IAST weights,
      final int begin,
      final int length,
      final boolean allowEmpty) {

    if (weights == null) {
      throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (weights.size() != values.size()) {
      throw new DimensionMismatchException(weights.size(), values.size());
    }

    boolean containsPositiveWeight = false;
    for (int i = begin; i < begin + length; i++) {
      // TODO implement exceptions
      // if (Double.isNaN(weights[i])) {
      // throw new
      // MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i);
      // }
      // if (Double.isInfinite(weights[i])) {
      // throw new
      // MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT,
      // weights[i], i);
      // }
      // if ( weights.get(i) < 0) {
      // throw new
      // MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX,
      // i, weights[i]);
      // }
      // if (!containsPositiveWeight && weights[i] > 0.0) {
      // containsPositiveWeight = true;
      // }
    }

    if (!containsPositiveWeight) {
      throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
    }

    return test(values, begin, length, allowEmpty);
  }
Exemple #12
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));
 }
Exemple #13
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;
   }
 }
Exemple #14
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;
 }
Exemple #15
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;
  }
Exemple #16
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;
  }
Exemple #17
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;
  }
Exemple #18
0
 public IExpr evaluate(final IAST ast) {
   if (ast.size() != 2) {
     return null;
   }
   return F.stringx(new StringBuffer(ast.get(1).fullFormString()));
 }
 /**
  * This default implementation calls {@link #clear}, then invokes {@link #increment} in a loop
  * over the the input array, and then uses {@link #getResult} to compute the return value.
  *
  * <p>Note that this implementation changes the internal state of the statistic. Its side effects
  * are the same as invoking {@link #clear} and then {@link #incrementAll(double[])}.
  *
  * <p>Implementations may override this method with a more efficient and possibly more accurate
  * implementation that works directly with the input array.
  *
  * <p>If the array is null, an IllegalArgumentException is thrown.
  *
  * @param values input array
  * @return the value of the statistic applied to the input array
  * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[])
  */
 public IExpr evaluate(final IAST values) {
   if (values == null) {
     throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
   }
   return evaluate(values, 1, values.size() - 1);
 }
Exemple #20
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;
  }
 /**
  * This default implementation just calls {@link #increment} in a loop over the input array.
  *
  * <p>Throws IllegalArgumentException if the input values array is null.
  *
  * @param values values to add
  * @throws IllegalArgumentException if values is null
  * @see
  *     org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[])
  */
 public void incrementAll(IAST values) {
   if (values == null) {
     throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
   }
   incrementAll(values, 1, values.size() - 1);
 }
Exemple #22
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;
  }
Exemple #23
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;
  }
Exemple #24
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;
 }
Exemple #25
0
  @Override
  public IExpr evaluate(final IAST ast) {
    Validate.checkSize(ast, 2);

    return evaluateArg1(ast.get(1));
  }