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;
}
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);
}
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());
}
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);
}
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());
}
Aggregations