Search in sources :

Example 6 with IFraction

use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.

the class AbstractFractionSym method pow.

/** {@inheritDoc} */
@Override
public final IFraction pow(final long n) throws ArithmeticException {
    if (n == 0L) {
        if (!this.isZero()) {
            return AbstractFractionSym.ONE;
        }
        throw new ArithmeticException("Indeterminate: 0^0");
    } else if (n == 1L) {
        return this;
    } else if (n == -1L) {
        return inverse();
    }
    long exp = n;
    if (n < 0) {
        exp *= -1;
    }
    int b2pow = 0;
    while ((exp & 1) == 0) {
        b2pow++;
        exp >>= 1;
    }
    IFraction r = this;
    IFraction x = r;
    while ((exp >>= 1) > 0) {
        x = x.mul(x);
        if ((exp & 1) != 0) {
            r = r.mul(x);
        }
    }
    while (b2pow-- > 0) {
        r = r.mul(r);
    }
    if (n < 0) {
        return r.inverse();
    }
    return r;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction)

Example 7 with IFraction

use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.

the class JASConvert method jas2Numeric.

public static INumber jas2Numeric(edu.jas.poly.Complex<BigRational> c, double epsilon) {
    IFraction re = F.fraction(c.getRe().numerator(), c.getRe().denominator());
    double red = re.doubleValue();
    IFraction im = F.fraction(c.getIm().numerator(), c.getIm().denominator());
    double imd = im.doubleValue();
    return F.chopNumber(F.complexNum(red, imd), epsilon);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction)

Example 8 with IFraction

use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.

the class JASIExpr method expr2IExprPoly.

private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        GenPolynomial<IExpr> result = fPolyFactory.getZERO();
        GenPolynomial<IExpr> p = fPolyFactory.getZERO();
        if (ast.isPlus()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.sum(p);
            }
            return result;
        } else if (ast.isTimes()) {
            IExpr expr = ast.arg1();
            result = expr2IExprPoly(expr);
            for (int i = 2; i < ast.size(); i++) {
                expr = ast.get(i);
                p = expr2IExprPoly(expr);
                result = result.multiply(p);
            }
            return result;
        } else if (ast.isPower()) {
            final IExpr expr = ast.arg1();
            if (expr instanceof ISymbol) {
                ExpVector leer = fPolyFactory.evzero;
                int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
                if (ix >= 0) {
                    int exponent = -1;
                    try {
                        exponent = Validate.checkPowerExponent(ast);
                    } catch (WrongArgumentType e) {
                    //
                    }
                    if (exponent < 0) {
                        throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                    }
                    ExpVector e = ExpVector.create(fVariables.size(), ix, exponent);
                    return fPolyFactory.getONE().multiply(e);
                }
            }
        } else if (fNumericFunction) {
            if (ast.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, ast);
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        ExpVector leer = fPolyFactory.evzero;
        int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
        if (ix >= 0) {
            ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
            return fPolyFactory.getONE().multiply(e);
        }
        if (fNumericFunction) {
            if (exprPoly.isNumericFunction()) {
                return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
            }
            throw new ClassCastException(exprPoly.toString());
        } else {
            return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
        }
    } else if (exprPoly instanceof IInteger) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else if (exprPoly instanceof IFraction) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    }
    if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
        return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
    } else {
        for (int i = 0; i < fVariables.size(); i++) {
            if (fVariables.get(i).equals(exprPoly)) {
                ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
                return fPolyFactory.getONE().multiply(e);
            }
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) GenPolynomial(edu.jas.poly.GenPolynomial) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) ExpVector(edu.jas.poly.ExpVector) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 9 with IFraction

use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.

the class JASConvert method jas2Complex.

public static IComplex jas2Complex(edu.jas.poly.Complex<BigRational> c) {
    IFraction re = F.fraction(c.getRe().numerator(), c.getRe().denominator());
    IFraction im = F.fraction(c.getIm().numerator(), c.getIm().denominator());
    return F.complex(re, im);
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction)

Example 10 with IFraction

use of org.matheclipse.core.interfaces.IFraction in project symja_android_library by axkr.

the class JASConvert method expr2Poly.

/**
	 * Convert the given expression into a
	 * <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial
	 * 
	 * @param exprPoly
	 * @param numeric2Rational
	 *            if <code>true</code>, <code>INum</code> double values are
	 *            converted to <code>BigRational</code> internally
	 * 
	 * @return
	 * @throws ArithmeticException
	 * @throws ClassCastException
	 */
private GenPolynomial<C> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
    if (exprPoly instanceof IAST) {
        final IAST ast = (IAST) exprPoly;
        if (ast.isSlot()) {
            try {
                return fPolyFactory.univariate(ast.toString(), 1L);
            } catch (IllegalArgumentException iae) {
            // fall through
            }
        } else {
            GenPolynomial<C> result = fPolyFactory.getZERO();
            GenPolynomial<C> p = fPolyFactory.getZERO();
            if (ast.isPlus()) {
                IExpr expr = ast.arg1();
                result = expr2Poly(expr, numeric2Rational);
                for (int i = 2; i < ast.size(); i++) {
                    expr = ast.get(i);
                    p = expr2Poly(expr, numeric2Rational);
                    result = result.sum(p);
                }
                return result;
            } else if (ast.isTimes()) {
                IExpr expr = ast.arg1();
                result = expr2Poly(expr, numeric2Rational);
                for (int i = 2; i < ast.size(); i++) {
                    expr = ast.get(i);
                    p = expr2Poly(expr, numeric2Rational);
                    result = result.multiply(p);
                }
                return result;
            } else if (ast.isPower() && ast.arg1().isSymbol()) {
                final ISymbol expr = (ISymbol) ast.arg1();
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(expr.getSymbolName(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            } else if (ast.isPower() && ast.arg1().isSlot()) {
                final IAST expr = (IAST) ast.arg1();
                int exponent = -1;
                try {
                    exponent = Validate.checkPowerExponent(ast);
                } catch (WrongArgumentType e) {
                }
                if (exponent < 0) {
                    throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(expr.toString(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        try {
            return fPolyFactory.univariate(((ISymbol) exprPoly).getSymbolName(), 1L);
        } catch (IllegalArgumentException iae) {
        // fall through
        }
    } else if (exprPoly instanceof IInteger) {
        return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
    } else if (exprPoly instanceof IFraction) {
        return fraction2Poly((IFraction) exprPoly);
    } else if (exprPoly instanceof INum && numeric2Rational) {
        IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
        return fraction2Poly(frac);
    } else if (exprPoly instanceof IComplexNum && numeric2Rational) {
        if (F.isZero(((IComplexNum) exprPoly).getImaginaryPart())) {
            // the imaginary part is zero
            IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
            return fraction2Poly(frac);
        }
    }
    throw new ClassCastException(exprPoly.toString());
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Aggregations

IFraction (org.matheclipse.core.interfaces.IFraction)29 IInteger (org.matheclipse.core.interfaces.IInteger)16 IExpr (org.matheclipse.core.interfaces.IExpr)14 IAST (org.matheclipse.core.interfaces.IAST)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)9 BigInteger (java.math.BigInteger)8 INum (org.matheclipse.core.interfaces.INum)7 IComplex (org.matheclipse.core.interfaces.IComplex)6 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)6 GenPolynomial (edu.jas.poly.GenPolynomial)3 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)3 ExpVector (edu.jas.poly.ExpVector)2 BigFraction (org.hipparchus.fraction.BigFraction)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 IRational (org.matheclipse.core.interfaces.IRational)2 BigInteger (edu.jas.arith.BigInteger)1 BigRational (edu.jas.arith.BigRational)1 ModLong (edu.jas.arith.ModLong)1 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)1