Search in sources :

Example 6 with IComplex

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

the class ComplexSym method powPositive.

/**
 * @param n must be greater equal 0
 * @return
 */
private IComplex powPositive(final long n) {
    if (fReal.isZero()) {
        long modN = n % 4;
        if (fImaginary.isOne()) {
            if (modN == 0) {
                return ONE;
            }
            if (modN == 1) {
                return this;
            }
            if (modN == 2) {
                return MINUS_ONE;
            }
            return NEGATIVE_I;
        }
        if (fImaginary.isMinusOne()) {
            if (modN == 0) {
                return ONE;
            }
            if (modN == 1) {
                return NEGATIVE_I;
            }
            if (modN == 2) {
                return MINUS_ONE;
            }
            return POSITIVE_I;
        }
    }
    long exp = n;
    long b2pow = 0;
    while ((exp & 1) == 0L) {
        b2pow++;
        exp >>= 1;
    }
    IComplex r = this;
    IComplex x = r;
    while ((exp >>= 1) > 0L) {
        x = x.multiply(x);
        if ((exp & 1) != 0) {
            r.checkBitLength();
            r = r.multiply(x);
        }
    }
    r.checkBitLength();
    while (b2pow-- > 0L) {
        r = r.multiply(r);
        r.checkBitLength();
    }
    return r;
}
Also used : IComplex(org.matheclipse.core.interfaces.IComplex)

Example 7 with IComplex

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

the class AbstractArg12 method binaryOperator.

public IExpr binaryOperator(IAST ast, final IExpr o0, final IExpr o1) {
    IExpr result = e2ObjArg(o0, o1);
    if (result.isPresent()) {
        return result;
    }
    if (o0.isInexactNumber() && o1.isInexactNumber()) {
        try {
            EvalEngine engine = EvalEngine.get();
            INumber arg1 = ((INumber) o0).evaluatePrecision(engine);
            INumber arg2 = ((INumber) o1).evaluatePrecision(engine);
            if (arg1 instanceof ApcomplexNum) {
                if (arg2.isNumber()) {
                    result = e2ApcomplexArg((ApcomplexNum) arg1, arg2.apcomplexNumValue());
                }
            } else if (arg2 instanceof ApcomplexNum) {
                if (arg1.isNumber()) {
                    result = e2ApcomplexArg(arg1.apcomplexNumValue(), (ApcomplexNum) arg2);
                }
            } else if (arg1 instanceof ComplexNum) {
                if (arg2.isNumber()) {
                    result = e2DblComArg((ComplexNum) arg1, arg2.complexNumValue());
                }
            } else if (arg2 instanceof ComplexNum) {
                if (arg1.isNumber()) {
                    result = e2DblComArg(arg1.complexNumValue(), (ComplexNum) arg2);
                }
            }
            if (arg1 instanceof ApfloatNum) {
                if (arg2.isReal()) {
                    result = e2ApfloatArg((ApfloatNum) arg1, ((ISignedNumber) arg2).apfloatNumValue());
                }
            } else if (arg2 instanceof ApfloatNum) {
                if (arg1.isReal()) {
                    result = e2ApfloatArg(((ISignedNumber) arg1).apfloatNumValue(), (ApfloatNum) arg2);
                }
            } else if (arg1 instanceof Num) {
                if (arg2.isReal()) {
                    result = e2DblArg((Num) arg1, ((ISignedNumber) arg2).numValue());
                }
            } else if (arg2 instanceof Num) {
                if (arg1.isReal()) {
                    result = e2DblArg(((ISignedNumber) arg1).numValue(), (Num) arg2);
                }
            }
            if (result.isPresent()) {
                return result;
            }
        } catch (LimitException le) {
            throw le;
        } catch (RuntimeException rex) {
            LOGGER.debug("AbstractArg12.binaryOperator() failed", rex);
            return F.NIL;
        }
    }
    if (o0 instanceof IInteger) {
        if (o1 instanceof IInteger) {
            return e2IntArg((IInteger) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IFraction) {
        if (o1 instanceof IInteger) {
            return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
        }
        if (o1 instanceof IFraction) {
            return e2FraArg((IFraction) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IComplex) {
        if (o1 instanceof IInteger) {
            return eComIntArg((IComplex) o0, (IInteger) o1);
        }
        if (o1 instanceof IFraction) {
            return eComFraArg((IComplex) o0, (IFraction) o1);
        }
        if (o1 instanceof IComplex) {
            return e2ComArg((IComplex) o0, (IComplex) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof ISymbol) {
        if (o1 instanceof ISymbol) {
            return e2SymArg((ISymbol) o0, (ISymbol) o1);
        }
        return F.NIL;
    }
    if (o0 instanceof IAST) {
        if (o1 instanceof IInteger) {
            return eFunIntArg((IAST) o0, (IInteger) o1);
        }
        if (o1 instanceof IAST) {
            return e2FunArg((IAST) o0, (IAST) o1);
        }
    }
    return F.NIL;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) ComplexNum(org.matheclipse.core.expression.ComplexNum) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) INum(org.matheclipse.core.interfaces.INum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) IComplex(org.matheclipse.core.interfaces.IComplex) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) INumber(org.matheclipse.core.interfaces.INumber) IInteger(org.matheclipse.core.interfaces.IInteger) EvalEngine(org.matheclipse.core.eval.EvalEngine) IExpr(org.matheclipse.core.interfaces.IExpr) LimitException(org.matheclipse.core.eval.exception.LimitException) IAST(org.matheclipse.core.interfaces.IAST) ApfloatNum(org.matheclipse.core.expression.ApfloatNum)

Example 8 with IComplex

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

the class JASConvert method complex2Poly.

/**
 * Convert a complex number into a jas polynomial. If the conversion isn't possible this method
 * throws a <code>JASConversionException</code>.
 *
 * @param complexValue the complex value containing a rational real and imaginary part
 * @return a jas polynomial
 * @throws JASConversionException
 */
private GenPolynomial<C> complex2Poly(final IComplex complexValue) throws JASConversionException {
    IRational reRational = complexValue.reRational();
    IRational imRational = complexValue.imRational();
    BigRational nre = new BigRational(reRational.toBigNumerator());
    BigRational dre = new BigRational(reRational.toBigDenominator());
    BigRational re = nre.divide(dre);
    BigRational nim = new BigRational(imRational.toBigNumerator());
    BigRational dim = new BigRational(imRational.toBigDenominator());
    BigRational im = nim.divide(dim);
    if (fRingFactory instanceof ComplexRing) {
        ComplexRing ring = (ComplexRing) fRingFactory;
        Complex<BigRational> c = new Complex<BigRational>(ring, re, im);
        return new GenPolynomial(fPolyFactory, c);
    } else {
        // "ComplexRing expected"
        throw new JASConversionException();
    }
}
Also used : GenPolynomial(edu.jas.poly.GenPolynomial) BigRational(edu.jas.arith.BigRational) ComplexRing(edu.jas.poly.ComplexRing) IRational(org.matheclipse.core.interfaces.IRational) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) IComplex(org.matheclipse.core.interfaces.IComplex) Complex(edu.jas.poly.Complex)

Example 9 with IComplex

use of org.matheclipse.core.interfaces.IComplex 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 JASConversionException
 */
private GenPolynomial<C> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, JASConversionException {
    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.base().isSymbol()) {
                final ISymbol base = (ISymbol) ast.base();
                int exponent = ast.exponent().toIntDefault();
                if (exponent < 0) {
                    throw new JASConversionException();
                // "JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(base.getSymbolName(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            } else if (ast.isPower() && ast.arg1().isSlot()) {
                final IAST base = (IAST) ast.arg1();
                int exponent = ast.exponent().toIntDefault();
                if (exponent < 0) {
                    throw new JASConversionException();
                // throw new ArithmeticException(
                // "JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
                }
                try {
                    return fPolyFactory.univariate(base.toString(), exponent);
                } catch (IllegalArgumentException iae) {
                // fall through
                }
            }
        }
    } else if (exprPoly instanceof ISymbol) {
        try {
            if (exprPoly.isIndeterminate()) {
                throw new JASConversionException();
            }
            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 IComplex) {
        // may throw JASConversionException
        return complex2Poly((IComplex) 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 JASConversionException();
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ISymbol(org.matheclipse.core.interfaces.ISymbol) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) IComplex(org.matheclipse.core.interfaces.IComplex) 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)

Example 10 with IComplex

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

the class DoubleFormFactory method convertInternal.

private void convertInternal(final StringBuilder buf, final IExpr o, final int precedence, boolean isASTHead) {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        // }
        if (list.head().isSymbol()) {
            ISymbol head = (ISymbol) list.head();
            final Operator operator = getOperator(head);
            if (operator != null) {
                if (operator instanceof PostfixOperator) {
                    if (list.isAST1()) {
                        convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
                        return;
                    }
                } else {
                    if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
                        return;
                    }
                }
            }
        // int functionID = head.ordinal();
        // if (functionID > ID.UNKNOWN) {
        // switch (functionID) {
        // case ID.Quantity:
        // // if (head.equals(F.SeriesData) && (list.size() == 7)) {
        // if (list instanceof IQuantity) {
        // if (convertQuantityData(buf, (IQuantity) list, precedence)) {
        // return;
        // }
        // }
        // break;
        // case ID.SeriesData:
        // // if (head.equals(F.SeriesData) && (list.size() == 7)) {
        // if (list instanceof ASTSeriesData) {
        // if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
        // return;
        // }
        // }
        // break;
        // case ID.List:
        // convertList(buf, list);
        // return;
        // case ID.Part:
        // if (list.size() >= 3) {
        // convertPart(buf, list);
        // return;
        // }
        // break;
        // case ID.Slot:
        // if (list.isAST1() && list.arg1().isInteger()) {
        // convertSlot(buf, list);
        // return;
        // }
        // break;
        // case ID.SlotSequence:
        // if (list.isAST1() && list.arg1().isInteger()) {
        // convertSlotSequence(buf, list);
        // return;
        // }
        // break;
        // case ID.Defer:
        // case ID.HoldForm:
        // if (list.isAST1()) {
        // convertInternal(buf, list.arg1());
        // return;
        // }
        // break;
        // case ID.DirectedInfinity:
        // if (list.isDirectedInfinity()) { // head.equals(F.DirectedInfinity))
        // if (list.isAST0()) {
        // append(buf, "ComplexInfinity");
        // return;
        // }
        // if (list.isAST1()) {
        // if (list.arg1().isOne()) {
        // append(buf, "Infinity");
        // return;
        // } else if (list.arg1().isMinusOne()) {
        // if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
        // append(buf, "(");
        // }
        // append(buf, "-Infinity");
        // if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
        // append(buf, ")");
        // }
        // return;
        // } else if (list.arg1().isImaginaryUnit()) {
        // append(buf, "I*Infinity");
        // return;
        // } else if (list.arg1().isNegativeImaginaryUnit()) {
        // append(buf, "-I*Infinity");
        // return;
        // }
        // }
        // }
        // break;
        // case ID.Optional:
        // if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
        // convertInternal(buf, list.arg1());
        // buf.append(":");
        // convertInternal(buf, list.arg2());
        // return;
        // }
        // break;
        // }
        // } else {
        // if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
        // convertList(buf, list);
        // return;
        // }
        // }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
        return;
    }
    convertString(buf, o.toString());
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) Operator(org.matheclipse.parser.client.operator.Operator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IComplex (org.matheclipse.core.interfaces.IComplex)22 IExpr (org.matheclipse.core.interfaces.IExpr)18 IAST (org.matheclipse.core.interfaces.IAST)13 ISymbol (org.matheclipse.core.interfaces.ISymbol)11 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)10 IInteger (org.matheclipse.core.interfaces.IInteger)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 INum (org.matheclipse.core.interfaces.INum)8 IRational (org.matheclipse.core.interfaces.IRational)6 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)4 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)4 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)3 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 Num (org.matheclipse.core.expression.Num)2 INumber (org.matheclipse.core.interfaces.INumber)2 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 Operator (org.matheclipse.parser.client.operator.Operator)2