Example #1
0
 /**
  * Real root bound. With f(M) * f(-M) != 0.
  *
  * @param f univariate polynomial.
  * @return M such that -M < root(f) < M.
  */
 public C realRootBound(GenPolynomial<C> f) {
   if (f == null) {
     return null;
   }
   RingFactory<C> cfac = f.ring.coFac;
   C M = cfac.getONE();
   if (f.isZERO() || f.isConstant()) {
     return M;
   }
   C a = f.leadingBaseCoefficient().abs();
   for (C c : f.getMap().values()) {
     C d = c.abs().divide(a);
     if (M.compareTo(d) < 0) {
       M = d;
     }
   }
   // works also without this case, only for optimization
   // to use rational number interval end points
   // can fail if real root is in interval [r,r+1]
   // for too low precision or too big r, since r is approximation
   if ((Object) M instanceof RealAlgebraicNumber) {
     RealAlgebraicNumber Mr = (RealAlgebraicNumber) M;
     BigRational r = Mr.magnitude();
     M = cfac.fromInteger(r.numerator()).divide(cfac.fromInteger(r.denominator()));
   }
   M = M.sum(f.ring.coFac.getONE());
   // System.out.println("M = " + M);
   return M;
 }
Example #2
0
  public static void example13() {
    BigRational fac = new BigRational();
    UnivPowerSeriesRing<BigRational> pfac = new UnivPowerSeriesRing<BigRational>(fac, 11, "y");
    UnivPowerSeries<BigRational> exp = pfac.getEXP();
    System.out.println("exp = " + exp);
    System.out.println("exp(1) = " + exp.evaluate(fac.getONE()));
    System.out.println("exp(0) = " + exp.evaluate(fac.getZERO()));

    BigDecimal dfac = new BigDecimal();
    UnivPowerSeriesRing<BigDecimal> dpfac = new UnivPowerSeriesRing<BigDecimal>(dfac, 11, "z");
    UnivPowerSeries<BigDecimal> dexp = dpfac.getEXP();
    System.out.println("exp = " + dexp);
    System.out.println("exp(1) = " + dexp.evaluate(dfac.getONE()));
    System.out.println("exp(0) = " + dexp.evaluate(dfac.getZERO()));
  }
Example #3
0
  /**
   * Magnitude bound.
   *
   * @param iv interval.
   * @param f univariate polynomial.
   * @return B such that |f(c)| &lt; B for c in iv.
   */
  public C magnitudeBound(Interval<C> iv, GenPolynomial<C> f) {
    if (f == null) {
      return null;
    }
    if (f.isZERO()) {
      return f.ring.coFac.getONE();
    }
    if (f.isConstant()) {
      return f.leadingBaseCoefficient().abs();
    }
    GenPolynomial<C> fa =
        f.map(
            new UnaryFunctor<C, C>() {

              public C eval(C a) {
                return a.abs();
              }
            });
    // System.out.println("fa = " + fa);
    C M = iv.left.abs();
    if (M.compareTo(iv.right.abs()) < 0) {
      M = iv.right.abs();
    }
    // System.out.println("M = " + M);
    RingFactory<C> cfac = f.ring.coFac;
    C B = PolyUtil.<C>evaluateMain(cfac, fa, M);
    // works also without this case, only for optimization
    // to use rational number interval end points
    // can fail if real root is in interval [r,r+1]
    // for too low precision or too big r, since r is approximation
    if ((Object) B instanceof RealAlgebraicNumber) {
      RealAlgebraicNumber Br = (RealAlgebraicNumber) B;
      BigRational r = Br.magnitude();
      B = cfac.fromInteger(r.numerator()).divide(cfac.fromInteger(r.denominator()));
    }
    // System.out.println("B = " + B);
    return B;
  }
Example #4
0
 /**
  * ComplexAlgebraicNumber magnitude.
  *
  * @return |this| as complex rational number.
  */
 public Complex<BigRational> magnitude() {
   try {
     Rectangle<C> v =
         ring.engine.invariantMagnitudeRectangle(
             ring.root, ring.algebraic.modul, number.val, ring.getEps());
     ring.setRoot(v);
     // System.out.println("new v = " + v);
     Complex<C> ev =
         ring.engine.complexRectangleMagnitude(
             v, ring.algebraic.modul, number.val); // , ring.eps);
     // C re = ev.getRe();
     // if ( (Object) re instanceof Rational) { // true by type parameter
     BigRational er = ev.getRe().getRational();
     BigRational ei = ev.getIm().getRational();
     ComplexRing<BigRational> cr = new ComplexRing<BigRational>(er.factory());
     return new Complex<BigRational>(cr, er, ei);
     // } else {
     //    throw new RuntimeException("Rational expected, but was " + ev.getClass());
     // }
   } catch (InvalidBoundaryException e) { // should not happen
     e.printStackTrace();
     throw new RuntimeException(e);
   }
 }
Example #5
0
  /** Test rational coefficient reduction. */
  public void testRatReduction() {
    do {
      a = fac.random(kl, ll, el);
    } while (a.isZERO());
    do {
      b = fac.random(kl, ll, el);
    } while (b.isZERO());
    // System.out.println("a = " + a);
    // System.out.println("b = " + b);

    L = new ArrayList<GenWordPolynomial<BigRational>>();
    L.add(a);

    e = red.normalform(L, a);
    // System.out.println("e = " + e);
    assertTrue("isZERO( e )", e.isZERO());

    L.add(b);
    e = red.normalform(L, a);
    // System.out.println("e = " + e);
    assertTrue("isZERO( e ) some times", e.isZERO());

    L = new ArrayList<GenWordPolynomial<BigRational>>();
    L.add(a);
    assertTrue("isTopRed( a )", red.isTopReducible(L, a));
    assertTrue("isRed( a )", red.isReducible(L, a));
    L.add(b);
    assertTrue("isTopRed( b )", red.isTopReducible(L, b));
    assertTrue("isRed( b )", red.isReducible(L, b));

    c = fac.random(kl, ll, el);
    // System.out.println("c = " + c);
    e = red.normalform(L, c);
    // System.out.println("e = " + e);
    assertTrue("isNF( e )", red.isNormalform(L, e));

    Word u = new Word(wfac, "f");
    Word v = new Word(wfac, "abc");
    c = a.multiply(cfac.getONE(), u, v);
    // System.out.println("c = " + c);
    e = red.normalform(L, c);
    // System.out.println("e = " + e);
    assertTrue("isNF( e )", red.isNormalform(L, e));
    assertTrue("e == 0", e.isZERO());
  }
Example #6
0
  public static void example7() {
    final BigRational fac = new BigRational();
    final UnivPowerSeriesRing<BigRational> pfac =
        new UnivPowerSeriesRing<BigRational>(fac, 11, "y");
    UnivPowerSeries<BigRational> exp =
        pfac.fixPoint(
            new UnivPowerSeriesMap<BigRational>() {

              public UnivPowerSeries<BigRational> map(UnivPowerSeries<BigRational> e) {
                return e.integrate(fac.getONE());
              }
            });
    System.out.println("exp = " + exp);
    UnivPowerSeries<BigRational> tan =
        pfac.fixPoint(
            new UnivPowerSeriesMap<BigRational>() {

              public UnivPowerSeries<BigRational> map(UnivPowerSeries<BigRational> t) {
                return t.multiply(t).sum(pfac.getONE()).integrate(fac.getZERO());
              }
            });
    System.out.println("tan = " + tan);
    UnivPowerSeries<BigRational> sin =
        new UnivPowerSeries<BigRational>(
            pfac,
            new Coefficients<BigRational>() {

              @Override
              public BigRational generate(int i) {
                BigRational c;
                if (i == 0) {
                  c = fac.getZERO();
                } else if (i == 1) {
                  c = fac.getONE();
                } else {
                  c = get(i - 2).negate();
                  c = c.divide(fac.fromInteger(i)).divide(fac.fromInteger(i - 1));
                }
                return c;
              }
            });
    System.out.println("sin = " + sin);
    UnivPowerSeries<BigRational> sin1 =
        pfac.fixPoint(
            new UnivPowerSeriesMap<BigRational>() {

              public UnivPowerSeries<BigRational> map(UnivPowerSeries<BigRational> e) {
                return e.negate().integrate(fac.getONE()).integrate(fac.getZERO());
              }
            });
    System.out.println("sin1 = " + sin1);

    UnivPowerSeries<BigRational> cos =
        new UnivPowerSeries<BigRational>(
            pfac,
            new Coefficients<BigRational>() {

              @Override
              public BigRational generate(int i) {
                BigRational c;
                if (i == 0) {
                  c = fac.getONE();
                } else if (i == 1) {
                  c = fac.getZERO();
                } else {
                  c = get(i - 2).negate();
                  c = c.divide(fac.fromInteger(i)).divide(fac.fromInteger(i - 1));
                }
                return c;
              }
            });
    System.out.println("cos = " + cos);
    UnivPowerSeries<BigRational> cos1 =
        pfac.fixPoint(
            new UnivPowerSeriesMap<BigRational>() {

              public UnivPowerSeries<BigRational> map(UnivPowerSeries<BigRational> e) {
                return e.negate().integrate(fac.getZERO()).integrate(fac.getONE());
              }
            });
    System.out.println("cos1 = " + cos1);

    UnivPowerSeries<BigRational> cos2 = pfac.solveODE(sin1.negate(), fac.getONE());
    System.out.println("cos2 = " + cos2);

    UnivPowerSeries<BigRational> sin2 = pfac.solveODE(cos1, fac.getZERO());
    System.out.println("sin2 = " + sin2);

    UnivPowerSeries<BigRational> sinh =
        new UnivPowerSeries<BigRational>(
            pfac,
            new Coefficients<BigRational>() {

              @Override
              public BigRational generate(int i) {
                BigRational c;
                if (i == 0) {
                  c = fac.getZERO();
                } else if (i == 1) {
                  c = fac.getONE();
                } else {
                  c = get(i - 2);
                  c = c.divide(fac.fromInteger(i)).divide(fac.fromInteger(i - 1));
                }
                return c;
              }
            });
    System.out.println("sinh = " + sinh);
    UnivPowerSeries<BigRational> cosh =
        new UnivPowerSeries<BigRational>(
            pfac,
            new Coefficients<BigRational>() {

              @Override
              public BigRational generate(int i) {
                BigRational c;
                if (i == 0) {
                  c = fac.getONE();
                } else if (i == 1) {
                  c = fac.getZERO();
                } else {
                  c = get(i - 2);
                  c = c.divide(fac.fromInteger(i)).divide(fac.fromInteger(i - 1));
                }
                return c;
              }
            } // , null
            );
    System.out.println("cosh = " + cosh);

    UnivPowerSeries<BigRational> sinhcosh = sinh.sum(cosh);
    System.out.println("sinh+cosh = " + sinhcosh);
    System.out.println("sinh+cosh == exp: " + sinhcosh.equals(exp));
  }
  /** Test scalar multiplication. */
  public void testMultiplication() {
    BigRational cfac = new BigRational(1);
    GenVectorModul<BigRational> mfac = new GenVectorModul<BigRational>(cfac, ll);
    BigRational r, s, t;
    GenVector<BigRational> a, b, c, d, e;

    r = cfac.random(kl);
    // System.out.println("r = " + r);
    s = r.inverse();
    // System.out.println("s = " + s);

    a = mfac.random(kl, q);
    // System.out.println("a = " + a);

    c = a.scalarMultiply(r);
    d = c.scalarMultiply(s);
    // System.out.println("c = " + c);
    // System.out.println("d = " + d);
    assertEquals("a*b*(1/b) = a", a, d);

    b = mfac.random(kl, q);
    // System.out.println("b = " + b);

    t = cfac.getONE();
    // System.out.println("t = " + t);
    c = a.linearCombination(b, t);
    d = b.linearCombination(a, t);
    // System.out.println("c = " + c);
    // System.out.println("d = " + d);
    assertEquals("a+1*b = b+1*a", c, d);

    c = a.linearCombination(b, t);
    d = a.sum(b);
    // System.out.println("c = " + c);
    // System.out.println("d = " + d);
    assertEquals("a+1*b = b+1*a", c, d);

    s = t.negate();
    // System.out.println("s = " + s);
    c = a.linearCombination(b, t);
    d = c.linearCombination(b, s);
    // System.out.println("c = " + c);
    // System.out.println("d = " + d);
    assertEquals("a+1*b+(-1)*b = a", a, d);

    c = a.linearCombination(t, b, t);
    d = c.linearCombination(t, b, s);
    // System.out.println("c = " + c);
    // System.out.println("d = " + d);
    assertEquals("a*1+b*1+b*(-1) = a", a, d);

    t = cfac.getZERO();
    // System.out.println("t = " + t);
    c = a.linearCombination(b, t);
    // System.out.println("c = " + c);
    assertEquals("a+0*b = a", a, c);

    d = a.linearCombination(t, b, t);
    // System.out.println("d = " + d);
    assertEquals("0*a+0*b = 0", mfac.getZERO(), d);

    r = a.scalarProduct(b);
    s = b.scalarProduct(a);
    // System.out.println("r = " + r);
    // System.out.println("s = " + s);
    assertEquals("a.b = b.a", r, s);
  }
Example #8
0
  /** Test rational coefficient reduction with recording. */
  public void testRatReductionRecording() {
    List<GenWordPolynomial<BigRational>> lrow, rrow = null;
    do {
      a = fac.random(kl, ll, el);
    } while (a.isZERO());
    do {
      b = fac.random(kl, ll, el);
    } while (b.isZERO());
    c = fac.random(kl, ll, el);
    d = fac.random(kl, ll, el);
    // System.out.println("a = " + a);
    // System.out.println("b = " + b);
    // System.out.println("c = " + c);
    // System.out.println("d = " + d);

    L = new ArrayList<GenWordPolynomial<BigRational>>();
    L.add(a);
    lrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    rrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    e = fac.getZERO();
    for (int m = 0; m < L.size(); m++) {
      lrow.add(e);
      rrow.add(e);
    }
    e = red.normalform(lrow, rrow, L, a);
    // System.out.println("e = " + e);
    // System.out.println("lrow = " + lrow);
    // System.out.println("rrow = " + rrow);
    assertTrue("isZERO( e )", e.isZERO());
    assertTrue("is Reduction ", red.isReductionNF(lrow, rrow, L, a, e));

    L.add(b);
    lrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    rrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    e = fac.getZERO();
    for (int m = 0; m < L.size(); m++) {
      lrow.add(e);
      rrow.add(e);
    }
    e = red.normalform(lrow, rrow, L, b);
    // System.out.println("e = " + e);
    // System.out.println("lrow = " + lrow);
    // System.out.println("rrow = " + rrow);
    assertTrue("is Reduction ", red.isReductionNF(lrow, rrow, L, b, e));

    L.add(c);
    lrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    rrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    e = fac.getZERO();
    for (int m = 0; m < L.size(); m++) {
      lrow.add(e);
      rrow.add(e);
    }
    e = red.normalform(lrow, rrow, L, c);
    // System.out.println("e = " + e);
    // System.out.println("lrow = " + lrow);
    // System.out.println("rrow = " + rrow);
    assertTrue("is Reduction ", red.isReductionNF(lrow, rrow, L, c, e));

    L.add(d);
    lrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    rrow = new ArrayList<GenWordPolynomial<BigRational>>(L.size());
    e = fac.getZERO();
    for (int m = 0; m < L.size(); m++) {
      lrow.add(e);
      rrow.add(e);
    }
    Word u = new Word(wfac, "f");
    Word v = new Word(wfac, "abc");
    d = a.multiply(cfac.random(3), u, v);
    // System.out.println("d = " + d);
    e = red.normalform(lrow, rrow, L, d);
    // System.out.println("e = " + e);
    // System.out.println("lrow = " + lrow);
    // System.out.println("rrow = " + rrow);
    assertTrue("is Reduction ", red.isReductionNF(lrow, rrow, L, d, e));
  }