Search in sources :

Example 1 with ComplexNum

use of org.matheclipse.core.expression.ComplexNum in project symja_android_library by axkr.

the class AbstractArg12 method binaryOperator.

public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
    IExpr result = F.NIL;
    if (o0 instanceof ApcomplexNum) {
        if (o1.isNumber()) {
            result = e2ApcomplexArg((ApcomplexNum) o0, ((INumber) o1).apcomplexNumValue(((ApcomplexNum) o0).precision()));
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof ApcomplexNum) {
        if (o0.isNumber()) {
            result = e2ApcomplexArg(((INumber) o0).apcomplexNumValue(((ApcomplexNum) o1).precision()), (ApcomplexNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o0 instanceof ComplexNum) {
        if (o1.isNumber()) {
            result = e2DblComArg((ComplexNum) o0, ((INumber) o1).complexNumValue());
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof ComplexNum) {
        if (o0.isNumber()) {
            result = e2DblComArg(((INumber) o0).complexNumValue(), (ComplexNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    }
    if (o0 instanceof ApfloatNum) {
        if (o1.isSignedNumber()) {
            result = e2ApfloatArg((ApfloatNum) o0, ((ISignedNumber) o1).apfloatNumValue(((ApfloatNum) o0).precision()));
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof ApfloatNum) {
        if (o0.isSignedNumber()) {
            result = e2ApfloatArg(((ISignedNumber) o0).apfloatNumValue(((ApfloatNum) o1).precision()), (ApfloatNum) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o0 instanceof Num) {
        if (o1.isSignedNumber()) {
            result = e2DblArg((Num) o0, ((ISignedNumber) o1).numValue());
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    } else if (o1 instanceof Num) {
        if (o0.isSignedNumber()) {
            result = e2DblArg(((ISignedNumber) o0).numValue(), (Num) o1);
        }
        if (result.isPresent()) {
            return result;
        }
        return e2ObjArg(o0, o1);
    }
    result = e2ObjArg(o0, o1);
    if (result.isPresent()) {
        return result;
    }
    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);
        }
    }
    if (o0 instanceof ISymbol) {
        if (o1 instanceof ISymbol) {
            return e2SymArg((ISymbol) o0, (ISymbol) o1);
        }
    }
    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) IComplex(org.matheclipse.core.interfaces.IComplex) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ISymbol(org.matheclipse.core.interfaces.ISymbol) INumber(org.matheclipse.core.interfaces.INumber) IInteger(org.matheclipse.core.interfaces.IInteger) 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) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum)

Example 2 with ComplexNum

use of org.matheclipse.core.expression.ComplexNum 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 3 with ComplexNum

use of org.matheclipse.core.expression.ComplexNum in project symja_android_library by axkr.

the class EvalComplex method evalAST.

public static double[] evalAST(final DoubleStack stack, final int top, final IAST ast) {
    final int newTop = top;
    final ISymbol symbol = (ISymbol) ast.get(0);
    if (symbol.isBuiltInSymbol()) {
        final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
        if (module instanceof INumericComplex) {
            // fast evaluation path
            stack.ensureCapacity(top + ast.size() * 2);
            for (int i = 1; i < ast.size(); i++) {
                final double[] result = eval(stack, newTop, ast.get(i));
                stack.push(result[0]);
                stack.push(result[1]);
            }
            return ((INumericComplex) module).evalComplex(stack, ast.argSize());
        }
    }
    // slow evaluation path
    final IExpr result = F.evaln(ast);
    if (result instanceof ComplexNum) {
        final double[] res = new double[2];
        res[0] = ((ComplexNum) result).getRealPart();
        res[1] = ((ComplexNum) result).getImaginaryPart();
        return res;
    }
    if (result instanceof Num) {
        final double[] res = new double[2];
        res[0] = ((Num) result).doubleValue();
        res[1] = 0.0;
        return res;
    }
    throw new UnsupportedOperationException();
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) INumericComplex(org.matheclipse.core.eval.interfaces.INumericComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) ComplexNum(org.matheclipse.core.expression.ComplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 4 with ComplexNum

use of org.matheclipse.core.expression.ComplexNum in project symja_android_library by axkr.

the class EvalComplex method evalSymbol.

public static double[] evalSymbol(final ISymbol symbol) {
    IExpr expr = symbol.assignedValue();
    if (expr != null) {
        // final IExpr expr = symbol.get();
        if (expr instanceof ISignedNumber) {
            final double[] result = new double[2];
            result[0] = ((ISignedNumber) expr).doubleValue();
            result[1] = 0.0;
            return result;
        }
        if (expr instanceof ComplexNum) {
            final double[] result = new double[2];
            result[0] = ((ComplexNum) expr).getRealPart();
            result[1] = ((ComplexNum) expr).getImaginaryPart();
            return result;
        }
    }
    if (symbol.isRealConstant()) {
        // fast evaluation path
        final double[] result = new double[2];
        result[0] = ((ISignedNumberConstant) ((IBuiltInSymbol) symbol).getEvaluator()).evalReal();
        result[1] = 0.0;
        return result;
    }
    if (symbol.isBuiltInSymbol()) {
        final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
        if (module instanceof INumericComplexConstant) {
            // fast evaluation path
            return ((INumericComplexConstant) module).evalComplex();
        }
    }
    // slow evaluation path
    final IExpr result = F.evaln(symbol);
    if (result instanceof ComplexNum) {
        final double[] res = new double[2];
        res[0] = ((ComplexNum) result).getRealPart();
        res[1] = ((ComplexNum) result).getImaginaryPart();
        return res;
    }
    if (result instanceof Num) {
        final double[] res = new double[2];
        res[0] = ((Num) result).doubleValue();
        res[1] = 0.0;
        return res;
    }
    throw new UnsupportedOperationException();
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ComplexNum(org.matheclipse.core.expression.ComplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IExpr(org.matheclipse.core.interfaces.IExpr) INumericComplexConstant(org.matheclipse.core.eval.interfaces.INumericComplexConstant)

Example 5 with ComplexNum

use of org.matheclipse.core.expression.ComplexNum in project symja_android_library by axkr.

the class NumberTest method testComplexNum.

public void testComplexNum() {
    // test for Android bug:
    // https://github.com/tranleduy2000/symja_android_library/commit/2f03d0b6c8095c2c71b1f56c8e5fc5f0b30f927d
    // 3802951800684688204490109616127/1267650600228229401496703205376
    IFraction f = AbstractFractionSym.valueOf(new BigInteger("3802951800684688204490109616127"), new BigInteger("1267650600228229401496703205376"));
    ComplexNum cn = f.complexNumValue();
    assertEquals(cn.toString(), "(3.0)");
    // 2535301200456458802993406410751/1267650600228229401496703205376
    f = AbstractFractionSym.valueOf(new BigInteger("2535301200456458802993406410751"), new BigInteger("1267650600228229401496703205376"));
    cn = f.complexNumValue();
    assertEquals(cn.toString(), "(2.0)");
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) ComplexNum(org.matheclipse.core.expression.ComplexNum) BigInteger(java.math.BigInteger)

Aggregations

ComplexNum (org.matheclipse.core.expression.ComplexNum)5 Num (org.matheclipse.core.expression.Num)4 IExpr (org.matheclipse.core.interfaces.IExpr)4 IFraction (org.matheclipse.core.interfaces.IFraction)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 IAST (org.matheclipse.core.interfaces.IAST)2 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 INum (org.matheclipse.core.interfaces.INum)2 INumber (org.matheclipse.core.interfaces.INumber)2 BigInteger (java.math.BigInteger)1 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 LimitException (org.matheclipse.core.eval.exception.LimitException)1 INumericComplex (org.matheclipse.core.eval.interfaces.INumericComplex)1