use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or <code>Sum()</code> or <code>
* Product()</code>
*
* @param list a list representing an iterator specification
* @param symbol the variable symbol
* @param engine the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, final ISymbol symbol, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
if (symbol != null && (!symbol.isVariable() || symbol.isProtected())) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(symbol), EvalEngine.get()));
}
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = symbol;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(symbol, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(symbol, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isQuantity()) {
return new QuantityIterator(symbol, (IQuantity) upperLimit);
} else if (upperLimit.isReal()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 3:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
variable = symbol;
if (upperLimit.isList()) {
if (variable != null) {
if (!variable.isVariable() || variable.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(variable), EvalEngine.get()));
}
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
return new ExprListIterator(variable, (IAST) upperLimit, evalEngine);
}
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(symbol, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity()) {
return new QuantityIterator(symbol, (IQuantity) lowerLimit, (IQuantity) upperLimit);
} else if (lowerLimit.isReal() && upperLimit.isReal()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg1());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = evalEngine.evalWithoutNumericReset(list.arg3());
variable = symbol;
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(symbol, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(symbol, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity() && step.isQuantity()) {
return new QuantityIterator(symbol, (IQuantity) lowerLimit, (IQuantity) upperLimit, (IQuantity) step);
} else if (lowerLimit.isReal() && upperLimit.isReal() && step.isReal()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
lowerLimit = null;
upperLimit = null;
step = null;
variable = null;
}
return new ExprIterator(variable, lowerLimit, upperLimit, step, fNumericMode, evalEngine);
} catch (LimitException le) {
throw le;
} catch (ArgumentTypeException atex) {
throw atex;
} catch (RuntimeException rex) {
throw new ClassCastException();
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or <code>Sum()</code> or <code>
* Product()</code>
*
* @param list a list representing an iterator specification
* @param position the position of the list in the argument sequence, for printing an error if
* list cannot be converted into an iterator form
* @param engine the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, int position, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
// fNumericMode = evalEngine.isNumericMode() ||
// list.isMember(Predicates.isNumeric(), false);
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
int iterationLimit = evalEngine.getIterationLimit();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = null;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
if (iUpperLimit > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(iUpperLimit, upperLimit);
}
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
int iUpperLimit = ((IRational) upperLimit).floor().toInt();
if (iUpperLimit > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(iUpperLimit, upperLimit);
}
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) upperLimit);
} else if (upperLimit.isReal()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
if (!list.arg1().isVariable()) {
throw new ArgumentTypeException(IOFunctions.getMessage("vloc", F.list(list.arg1()), EvalEngine.get()));
}
break;
case 3:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
if (list.arg1() instanceof ISymbol) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
}
variable = sym;
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
if (upperLimit.isList()) {
return new ExprListIterator(variable, (IAST) upperLimit, evalEngine);
}
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) upperLimit);
} else if (upperLimit.isReal()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = F.C1;
if (list.arg1().isSymbol()) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
}
variable = sym;
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) lowerLimit, (IQuantity) upperLimit);
} else if (lowerLimit.isReal() && upperLimit.isReal()) {
ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
}
break;
case 5:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = evalEngine.evalWithoutNumericReset(list.arg4());
if (list.arg1() instanceof ISymbol) {
ISymbol sym = (ISymbol) list.arg1();
if (!sym.isVariable() || sym.isProtected()) {
// Cannot assign to raw object `1`.
throw new ArgumentTypeException(IOFunctions.getMessage("setraw", F.list(sym), EvalEngine.get()));
}
variable = sym;
} else {
// Raw object `1` cannot be used as an iterator.
throw new ArgumentTypeException(IOFunctions.getMessage("itraw", F.list(list.arg1()), EvalEngine.get()));
}
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isQuantity() && upperLimit.isQuantity() && step.isQuantity()) {
return new QuantityIterator(variable, (IQuantity) lowerLimit, (IQuantity) upperLimit, (IQuantity) step);
} else if (lowerLimit.isReal() && upperLimit.isReal() && step.isReal()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
// Argument `1` at position `2` does not have the correct form for an iterator.
String str = IOFunctions.getMessage("itform", F.list(list, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
}
return new ExprIterator(variable, lowerLimit, upperLimit, step, fNumericMode, evalEngine);
} catch (LimitException le) {
throw le;
} catch (ArgumentTypeException atex) {
throw atex;
} catch (RuntimeException rex) {
// Argument `1` at position `2` does not have the correct form for an iterator.
String str = IOFunctions.getMessage("itform", F.list(list, F.ZZ(position)), EvalEngine.get());
throw new ArgumentTypeException(str);
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.
the class EllipticFunctionsJS method jacobiTheta.
public static Complex jacobiTheta(int n, Complex x, Complex q, double tolerance) {
if (q.norm() >= 1) {
throw new ArgumentTypeException("unsupported elliptic nome");
}
if (n < 1 || n > 4) {
throw new ArgumentTypeException("undefined Jacobi theta index");
}
if (F.isZero(q)) {
switch(n) {
case 1:
case 2:
return Complex.ZERO;
case 3:
case 4:
return Complex.ONE;
}
}
Complex piTau = q.log().divide(Complex.I);
// dlmf.nist.gov/20.2 to reduce overflow
if (Math.abs(x.getImaginary()) > Math.abs(piTau.getImaginary()) || Math.abs(x.getReal()) > Math.PI) {
double pt = trunc(x.getImaginary() / piTau.getImaginary());
x = x.subtract(piTau.multiply(pt));
double p = trunc(x.getReal() / Math.PI);
x = x.subtract(p * Math.PI);
Complex qFactor = q.pow(-pt * pt);
Complex eFactor = x.multiply(Complex.I).multiply(-2 * pt).exp();
// factors can become huge, so chop spurious parts first
switch(n) {
case 1:
return qFactor.multiply(eFactor).multiply(F.chopComplex(jacobiTheta(n, x, q), tolerance)).multiply(Math.pow((-1), (p + pt)));
case 2:
return qFactor.multiply(eFactor).multiply(F.chopComplex(jacobiTheta(n, x, q), tolerance)).multiply(Math.pow((-1), p));
case 3:
return qFactor.multiply(eFactor).multiply(F.chopComplex(jacobiTheta(n, x, q), tolerance));
case 4:
return qFactor.multiply(eFactor).multiply(F.chopComplex(jacobiTheta(n, x, q), tolerance)).multiply(Math.pow((-1), pt));
}
}
Complex s = Complex.ZERO;
Complex p = Complex.ONE;
long iterationLimit = EvalEngine.get().getIterationLimit();
int i = 0;
switch(n) {
case 1:
while (Math.abs(p.getReal()) > tolerance || Math.abs(p.getImaginary()) > tolerance) {
p = q.pow(i * i + i).multiply(x.multiply(2 * i + 1).sin()).multiply(Math.pow(-1, i));
s = s.add(p);
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return q.pow(0.25).multiply(s).multiply(2);
case 2:
while (Math.abs(p.getReal()) > tolerance || Math.abs(p.getImaginary()) > tolerance) {
p = q.pow(i * i + i).multiply(x.multiply(2 * i + 1).cos());
s = s.add(p);
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return q.pow(0.25).multiply(s).multiply(2);
case 3:
i = 1;
while (Math.abs(p.getReal()) > tolerance || Math.abs(p.getImaginary()) > tolerance) {
p = q.pow(i * i).multiply(x.multiply(2 * i).cos());
s = s.add(p);
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return s.multiply(2.0).add(1.0);
case 4:
i = 1;
while (Math.abs(p.getReal()) > tolerance || Math.abs(p.getImaginary()) > tolerance) {
p = q.negate().pow(i * i).multiply(x.multiply(2 * i).cos());
s = s.add(p);
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return s.multiply(2.0).add(1.0);
}
throw new ArgumentTypeException("undefined Jacobi theta index");
}
use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.
the class EllipticFunctionsJS method jacobiTheta.
public static Complex jacobiTheta(int n, double x, double q, double tolerance) {
if (Math.abs(q) >= 1) {
throw new ArgumentTypeException("unsupported elliptic nome");
}
if (n < 1 || n > 4) {
throw new ArgumentTypeException("undefined Jacobi theta index");
}
if (F.isZero(q)) {
switch(n) {
case 1:
case 2:
return Complex.ZERO;
case 3:
case 4:
return Complex.ONE;
}
}
// dlmf.nist.gov/20.2 to reduce overflow
if (Math.abs(x) > Math.PI) {
double p = trunc(x / Math.PI);
x = x - p * Math.PI;
switch(n) {
case 1:
case 2:
return new Complex(Math.pow(-1, p)).multiply(jacobiTheta(n, x, q));
case 3:
case 4:
return jacobiTheta(n, x, q);
}
}
long iterationLimit = EvalEngine.get().getIterationLimit();
switch(n) {
case 1:
if (q < 0) {
return jacobiTheta(n, new Complex(x), new Complex(q));
}
double s = 0;
double p = 1;
int i = 0;
while (Math.abs(p) > tolerance) {
p = Math.pow(-1, i) * Math.pow(q, (i * i + i)) * Math.sin((2 * i + 1) * x);
s += p;
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return new Complex(2 * Math.pow(q, 0.25) * s);
case 2:
if (q < 0) {
return jacobiTheta(n, new Complex(x), new Complex(q));
}
s = 0;
p = 1;
i = 0;
while (Math.abs(p) > tolerance) {
p = Math.pow(q, (i * i + i)) * Math.cos((2 * i + 1) * x);
s += p;
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return new Complex(2 * Math.pow(q, 0.25) * s);
case 3:
s = 0;
p = 1;
i = 1;
while (Math.abs(p) > tolerance) {
p = Math.pow(q, (i * i)) * Math.cos(2 * i * x);
s += p;
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return new Complex(1 + 2 * s);
case 4:
s = 0;
p = 1;
i = 1;
while (Math.abs(p) > tolerance) {
p = Math.pow(-q, (i * i)) * Math.cos(2 * i * x);
s += p;
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.EllipticTheta);
}
}
return new Complex(1 + 2 * s);
}
throw new ArgumentTypeException("undefined Jacobi theta index");
}
Aggregations