Search in sources :

Example 26 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class HypergeometricJS method hypergeometric0F1.

// public static Complex hypergeometric0F1(Complex a, Complex x) {
// return hypergeometric0F1(a, x, Config.SPECIAL_FUNCTIONS_TOLERANCE);
// }
public static Complex hypergeometric0F1(Complex a, Complex x) {
    final double useAsymptotic = 100;
    if (a.isMathematicalInteger() && a.getReal() <= 0) {
        throw new ArgumentTypeException("hypergeometric function pole");
    }
    // asymptotic form as per Johansson
    if (x.norm() > useAsymptotic) {
        // transform variables for convenience
        Complex b = a.multiply(2).subtract(1);
        a = a.subtract(0.5);
        x = x.sqrt().multiply(4.0);
        // copied from hypergeometric1F1
        Complex t1 = Arithmetic.lanczosApproxGamma(b).multiply(x.negate().pow(a.negate())).multiply(Arithmetic.lanczosApproxGamma(b.subtract(a)).reciprocal());
        t1 = t1.multiply(hypergeometric2F0(a, a.add(b.negate()).add(1), new Complex(-1.0).divide(x)));
        Complex t2 = Arithmetic.lanczosApproxGamma(b).multiply(x.pow(a.subtract(b))).multiply(x.exp()).multiply(Arithmetic.lanczosApproxGamma(a).reciprocal());
        t2 = t2.multiply(hypergeometric2F0(b.subtract(a), Complex.ONE.subtract(a), Complex.ONE.divide(x)));
        return x.divide(-2.0).exp().multiply(t1.add(t2));
    }
    Complex s = Complex.ONE;
    Complex p = Complex.ONE;
    long i = 1;
    long iterationLimit = EvalEngine.get().getIterationLimit();
    while (Math.abs(p.getReal()) > Config.SPECIAL_FUNCTIONS_TOLERANCE || Math.abs(p.getImaginary()) > Config.SPECIAL_FUNCTIONS_TOLERANCE) {
        p = p.multiply(x).multiply(a.reciprocal()).divide(i);
        s = s.add(p);
        a = a.add(1);
        if (i++ > iterationLimit && iterationLimit > 0) {
            IterationLimitExceeded.throwIt(i, S.Hypergeometric0F1);
        }
    }
    return s;
}
Also used : ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) Complex(org.hipparchus.complex.Complex)

Example 27 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class IntervalSym method normalizeArgument.

/**
 * If the argument is a list of 2 elements, try sorting the elements. If the argument is not a
 * list return a new <code>{argOfIntervalList, argOfIntervalList]</code>
 *
 * @param arg
 * @param engine
 * @return
 */
private static IAST normalizeArgument(final IExpr arg, final EvalEngine engine) {
    if (arg.isList()) {
        if (arg.size() == 3) {
            IAST list = (IAST) arg;
            IExpr arg1 = list.arg1();
            IExpr arg2 = list.arg2();
            if (arg1.isReal() && arg2.isReal()) {
                if (arg1.greaterThan(arg2).isTrue()) {
                    return F.list(arg2, arg1);
                }
                return F.NIL;
            }
            IExpr min = arg1.isNumber() ? arg1 : engine.evaluate(arg1);
            IExpr max = arg2.isNumber() ? arg2 : engine.evaluate(arg2);
            if (min.isRealResult() && max.isRealResult()) {
                if (min.greaterThan(max).isTrue()) {
                    return F.list(max, min);
                }
            }
            return F.NIL;
        }
        // The expression `1` is not a valid interval.
        String str = IOFunctions.getMessage("nvld", F.list(arg), engine);
        throw new ArgumentTypeException(str);
    }
    if (arg instanceof INum) {
        if (arg instanceof ApfloatNum) {
            Apfloat apfloat = ((ApfloatNum) arg).fApfloat;
            Apfloat[] values = interval(apfloat);
            return // 
            F.list(// 
            F.num(values[0]), F.num(values[1]));
        }
        double value = ((ISignedNumber) arg).doubleValue();
        return // 
        F.list(// 
        F.num(Math.nextDown(value)), F.num(Math.nextUp(value)));
    }
    return F.list(arg, arg);
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) Apfloat(org.apfloat.Apfloat) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) INum(org.matheclipse.core.interfaces.INum)

Example 28 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class Symbol method reassignSymbolValue.

/**
 * {@inheritDoc}
 */
@Override
public IExpr[] reassignSymbolValue(IASTMutable ast, ISymbol functionSymbol, EvalEngine engine) {
    if (hasAssignedSymbolValue()) {
        IExpr[] result = new IExpr[2];
        result[0] = fValue;
        // if (fReferences > 0 && result[0].isAST()) {
        // result[0] = ((IAST) result[0]).copy();
        // }
        ast.set(1, result[0]);
        // F.binaryAST2(this, symbolValue, value));
        IExpr calculatedResult = engine.evaluate(ast);
        if (calculatedResult != null) {
            assignValue(calculatedResult, false);
            result[1] = calculatedResult;
            return result;
        }
    }
    throw new ArgumentTypeException(functionSymbol.toString() + " - Symbol: " + toString() + " has no value! Reassignment with a new value is not possible");
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 29 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class AbstractAST method toDoubleMatrix.

/**
 * {@inheritDoc}
 */
@Override
public double[][] toDoubleMatrix() {
    int[] dim = isMatrix();
    if (dim == null) {
        return null;
    }
    try {
        double[][] result = new double[dim[0]][dim[1]];
        ISignedNumber signedNumber;
        for (int i = 1; i <= dim[0]; i++) {
            IAST row = (IAST) get(i);
            for (int j = 1; j <= dim[1]; j++) {
                signedNumber = row.get(j).evalReal();
                if (signedNumber != null) {
                    result[i - 1][j - 1] = signedNumber.evalDouble();
                } else {
                    return null;
                }
            }
        }
        return result;
    } catch (ArgumentTypeException rex) {
    // 
    }
    return null;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 30 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class ASTAssociation method appendRule.

/**
 * Adds the specified rule at the end of this association.Existing duplicate rule keys will be
 * replaced by the new rule.
 *
 * @param rule the rule to add at the end of this association
 */
@Override
public final void appendRule(IExpr rule) {
    int index = size();
    if (rule.isRuleAST()) {
        int value = getInt(rule.first());
        if (value == 0) {
            append(rule);
            keyToIndexMap.assoc(rule.first(), index++);
        } else {
            set(value, rule);
        }
    } else if (rule.isEmptyList()) {
    // ignore empty list entries
    } else {
        throw new ArgumentTypeException("rule expression expected instead of " + rule.toString());
    }
}
Also used : ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Aggregations

ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)44 IExpr (org.matheclipse.core.interfaces.IExpr)24 IAST (org.matheclipse.core.interfaces.IAST)16 Complex (org.hipparchus.complex.Complex)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 EvalEngine (org.matheclipse.core.eval.EvalEngine)6 UnaryNumerical (org.matheclipse.core.generic.UnaryNumerical)6 UnivariateDifferentiableFunction (org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction)5 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)5 ArrayList (java.util.ArrayList)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 DSFactory (org.hipparchus.analysis.differentiation.DSFactory)3 FiniteDifferencesDifferentiator (org.hipparchus.analysis.differentiation.FiniteDifferencesDifferentiator)3 Config (org.matheclipse.core.basic.Config)3 F (org.matheclipse.core.expression.F)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 INum (org.matheclipse.core.interfaces.INum)3 BisectionSolver (org.hipparchus.analysis.solvers.BisectionSolver)2 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)2 Gamma (org.hipparchus.special.Gamma)2