Package lipstone.joshua.parser.types

Examples of lipstone.joshua.parser.types.BigDec


  private ArrayList<BigDec> getRoots(ConsCell equation) throws ParserException {
    ArrayList<BigDec> roots = new ArrayList<BigDec>();
    ArrayList<ConsCell> terms = getTerms(equation);
    if (terms.size() < 1)
      return roots;
    BigDec highestCo = getCoefficient(terms.get(0)), lowestCo = getCoefficient(terms.get(terms.size() - 1));
    if (!highestCo.isInt()) {
      lowestCo = lowestCo.divide(highestCo);
      highestCo = BigDec.ONE;
      if (!lowestCo.isInt())
        return roots;
    }
    if (!lowestCo.isInt()) {
      highestCo = highestCo.divide(lowestCo);
      lowestCo = BigDec.ONE;
      if (!highestCo.isInt())
        return roots;
    }
   
    ArrayList<BigDec> aN = findFactors(highestCo), a0 = findFactors(lowestCo);
    for (BigDec i : a0)
      for (BigDec a : aN) {
        if (new BigDec(i).divide(new BigDec(a)).getInfinity() != 0)
          continue;
        roots.add(new BigDec(i).divide(new BigDec(a)));
        roots.add(new BigDec(i).divide(new BigDec(a)).multiply(BigDec.MINUSONE));
      }
    return roots;
  }
View Full Code Here


   * @return the fully factored equation if possible.
   * @throws ParserException
   */
  private ConsCell rationalRootsFactoring(ConsCell equation) throws ParserException {
    ConsCell original = equation.clone();
    if (orderOfEquation(equation, parser.getVars()).lt(new BigDec(2)))
      return equation;
    ArrayList<ConsCell> eqn = foldSigns(getTerms(equation));
    BigDec highestCoefficient = getCoefficient(eqn.get(0)), lowestCoefficient = getCoefficient(eqn.get(eqn.size() - 1)), lcm = BigDec.ONE;
    if (!highestCoefficient.isInt() || !lowestCoefficient.isInt()) {
      lcm = ((BigDec) highestCoefficient.toFraction().getCar(2)).lcm((BigDec) lowestCoefficient.toFraction().getCar(2));
      equation = eqn.get(0).getLastConsCell().append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(lcm, ConsType.NUMBER)));
      for (int i = 0; i < eqn.size(); i++)
        equation = equation.append(new ConsCell('+', ConsType.OPERATOR, eqn.get(i).getLastConsCell().append(new ConsCell('*', ConsType.OPERATOR, new ConsCell(lcm, ConsType.NUMBER))).getFirstConsCell().clone()));
      equation = equation.getFirstConsCell();
      equation = simplifyTerms(parser.removeDoubles(equation));
    }
    ArrayList<BigDec> roots = getRoots(equation);
    for (BigDec root : roots) {
      ConsCell output = polynomialDivision(equation, new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER, new ConsCell('+', ConsType.OPERATOR, new ConsCell(root, ConsType.NUMBER))),
          new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER), true);
      ConsCell last = output.getLastConsCell();
      if (last.getCarType() == ConsType.OBJECT && last.getCar().equals("{false}")) { //If the polynomial division succeeded, return the factored form
        last.remove();
        ConsCell result = parser.removeDoubles(new ConsCell(new ConsCell(parser.getVars().get(0), ConsType.IDENTIFIER, new ConsCell('+', ConsType.OPERATOR, new ConsCell(root, ConsType.NUMBER))),
            ConsType.CONS_CELL, new ConsCell('*', ConsType.OPERATOR, new ConsCell(rationalRootsFactoring(output), ConsType.CONS_CELL))));
        if (!highestCoefficient.isInt() && lcm.neq(BigDec.ONE))
          result = new ConsCell(BigDec.ONE, ConsType.NUMBER, new ConsCell('/', ConsType.OPERATOR, new ConsCell(lcm, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR, result))));
        return result;
      }
    }
    return original;
View Full Code Here

      return new ConsCell("{false}", ConsType.OBJECT);
   
    int resultOrder = dividendCoefficients.length - divisorCoefficients.length;
    ConsCell result = new ConsCell();
    for (int i = 0; i <= resultOrder; i++) {
      BigDec resultCoefficient = dividendCoefficients[i].divide(divisorCoefficients[0]);
      dividendCoefficients[i] = BigDec.ZERO;
      for (int j = 1; j < divisorCoefficients.length; j++)
        dividendCoefficients[i + j] = dividendCoefficients[i + j].subtract(divisorCoefficients[j].multiply(resultCoefficient));
      result = result.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(resultCoefficient, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR, new ConsCell(var, ConsType.CONS_CELL,
          new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(resultOrder - i), ConsType.NUMBER)))))));
    }
    ConsCell remainder = new ConsCell();
    for (int i = 0; i < dividendCoefficients.length; i++)
      if (dividendCoefficients[i].neq(BigDec.ZERO))
        remainder = remainder.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(dividendCoefficients[i], ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR,
            new ConsCell(var, ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(dividendCoefficients.length - 1 - i), ConsType.NUMBER)))))));
    remainder = remainder.getFirstConsCell();
    if (!remainder.isNull()) {
      remainder = remainder.remove();
      remainder = new ConsCell(remainder, ConsType.CONS_CELL, new ConsCell('/', ConsType.OPERATOR, new ConsCell(divisor.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL);
      result = result.append(new ConsCell('+', ConsType.OPERATOR, remainder, ConsType.CONS_CELL));
View Full Code Here

  private BigDec[] getSyntheticCoefficients(ConsCell equation, ConsCell var) throws ParserException {
    ArrayList<String> vars = new ArrayList<String>();
    vars.add(var.toString());
    ArrayList<PairedList<ConsCell, ConsCell>> termOrders = new ArrayList<PairedList<ConsCell, ConsCell>>();
    ArrayList<ConsCell> terms = foldSigns(getTerms(simplifyTerms(equation)));
    BigDec equationOrder = BigDec.ZERO;
    for (ConsCell term : terms)
      termOrders.add(orderOfTerm(term, vars));
    for (PairedList<ConsCell, ConsCell> order : termOrders) {
      ConsCell temp = order.containsKey(var) ? parser.run(order.get(var)) : new ConsCell(BigDec.ZERO, ConsType.NUMBER);
      if (temp.length() != 1 || temp.getCarType() != ConsType.NUMBER || ((BigDec) temp.getCar()).lt(BigDec.ZERO) || !((BigDec) temp.getCar()).isInt())
        return null;
      if (((BigDec) temp.getCar()).gt(equationOrder))
        equationOrder = (BigDec) temp.getCar();
    }
    int highestOrder = equationOrder.intValue();
    BigDec[] equationCoefficients = new BigDec[highestOrder + 1];
    for (int i = 0; i < equationCoefficients.length; i++)
      equationCoefficients[i] = BigDec.ZERO;
    for (int i = 0; i < terms.size(); i++) {
      ConsCell temp = termOrders.get(i).containsKey(var) ? parser.run(termOrders.get(i).get(var)) : new ConsCell(BigDec.ZERO, ConsType.NUMBER);
View Full Code Here

   * @return the prime factorization of this equation
   * @throws ParserException
   */
  public ConsCell primeFactor(ConsCell equation) throws ParserException {
    ArrayList<Long> factors = new ArrayList<>();
    BigDec num = (BigDec) parser.run(equation).getCar();
    long[] primes = getPrimes(num.getInternal().longValue());
    for (int i = 0; i < primes.length && num.gt(BigDec.ONE); i++) {
      BigDec prime = new BigDec(primes[i]);
      while (num.mod(prime).eq(BigDec.ZERO)) {
        factors.add(primes[i]);
        num = num.divide(prime);
      }
    }
    ConsCell output = new ConsCell();
    for (long integer : factors)
      output = output.append(new ConsCell(",", ConsType.SEPARATOR)).append(new ConsCell(new BigDec(integer), ConsType.NUMBER));
    output = output.getFirstConsCell();
    if (output.getCarType() == ConsType.SEPARATOR)
      output = output.remove();
    return output;
  }
View Full Code Here

   *         {@link #primeFactor(ConsCell) primeFactor(num)} to get the primeFactorizaiton of the num
   * @throws UndefinedResultException
   */
  public ArrayList<BigDec> findFactors(BigDec num, boolean onlyPrimes) throws UndefinedResultException {
    ArrayList<BigDec> output = new ArrayList<BigDec>();
    BigDec number = BigDec.ONE;
    long[] primes = getPrimes(num.getInternal().longValue());
    ArrayList<BigDec> primeFactors = new ArrayList<>();
    for (int i = 0; i < primes.length && num.gt(BigDec.ONE); i++) {
      BigDec prime = new BigDec(primes[i]);
      while (num.mod(prime).eq(BigDec.ZERO)) {
        primeFactors.add(prime);
        num = num.divide(prime);
      }
    }
View Full Code Here

    if (!num.isInt())
      return false;
    if (num.eq(num.ONE))
      return true;
    try {
      if (new BigDec(num).divide(new BigDec(2)).isInt())
        return false;
    }
    catch (UndefinedResultException e) {}
    long[] primes = getPrimes(num.getInternal().longValue());
    for (int i = 0; i < primes.length && primes[i] <= num.intValue(); i++)
View Full Code Here

    return false;
  }
 
  private void setDomain(BigDec dMin, BigDec dMax) throws UndefinedResultException {
    if (dMin.gt(dMax)) {
      BigDec temp = new BigDec(dMax);
      dMax = new BigDec(dMin);
      dMin = temp;
    }
    xMin = (dMax.add(dMin)).divide(new BigDec(2));
    xMax = dMax.subtract(xMin);
    domainMax = dMax;
    domainMin = dMin;
    method = "domain";
  }
View Full Code Here

          denominator = denominator.append(new ConsCell('*', ConsType.OPERATOR, segments.get(i)));
      }
    }
    numerator = numerator.getFirstConsCell();
    denominator = denominator.getFirstConsCell();
    BigDec num = new BigDec(parser.run(numerator.length() > 0 ? numerator.remove() : new ConsCell(BigDec.ONE, ConsType.NUMBER)).toString());
    BigDec den = new BigDec(parser.run(denominator.length() > 0 ? denominator.remove() : new ConsCell(BigDec.ONE, ConsType.NUMBER)).toString()), gcd = num.gcd(den);
    num = num.divide(gcd);
    den = den.divide(gcd);
    ConsCell output = new ConsCell(num, ConsType.NUMBER, new ConsCell('/', ConsType.OPERATOR, new ConsCell(den, ConsType.NUMBER))).getLastConsCell();
    for (ConsCell key : orders) {
      ConsCell exp = orders.get(key);
      if (exp.length() == 1 && exp.getCarType() == ConsType.NUMBER) { //If the exponent is a real number
        if (((BigDec) exp.getCar()).eq(BigDec.ZERO))
View Full Code Here

      if (term.getCarType() == ConsType.OPERATOR && (Character) term.getCar() == '-')
        return BigDec.MINUSONE;
      return BigDec.ONE;
    }
   
    BigDec coefficient = BigDec.ONE;
    ConsCell current = term;
    boolean negative = false;
    do {
      if (current.getCarType() == ConsType.OPERATOR && (Character) current.getCar() == '-')
        negative = !negative;
      if (!(current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '*' || (Character) current.getCar() == '/'))) {
        boolean divide = current.getPreviousConsCell().getCarType() == ConsType.OPERATOR && (Character) current.getPreviousConsCell().getCar() == '/';
        ConsCell eqn = current.singular();
        while (!(current = current.getNextConsCell()).isNull() && !(current.getCarType() == ConsType.OPERATOR && ((Character) current.getCar() == '*' || (Character) current.getCar() == '/')))
          eqn = eqn.append(current.singular());
        eqn = eqn.getFirstConsCell();
        if (parser.containsVariables(eqn, vars))
          continue;
        eqn = parser.run(eqn);
        if (eqn.length() == 1 && eqn.getCarType() == ConsType.NUMBER)
          coefficient = divide ? coefficient.divide((BigDec) eqn.getCar()) : coefficient.multiply((BigDec) eqn.getCar());
      }
    } while (!(current = current.getNextConsCell()).isNull());
    return negative ? coefficient.multiply(BigDec.MINUSONE) : coefficient;
  }
View Full Code Here

TOP

Related Classes of lipstone.joshua.parser.types.BigDec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.