use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class HypergeometricJS method hypergeometric2F0.
public static Complex hypergeometric2F0(Complex a, Complex b, Complex x, double tolerance) {
int terms = 50;
Complex s = Complex.ONE;
Complex p = Complex.ONE;
Complex pLast = p;
// first few terms can be larger than unity
boolean converging = false;
int i = 1;
while (Math.abs(p.getReal()) > tolerance || Math.abs(p.getImaginary()) > tolerance) {
p = p.multiply(x).multiply(a).multiply(b).divide(i);
if (p.norm() > pLast.norm() && converging) {
// prevent runaway sum
break;
}
if (p.norm() < pLast.norm()) {
converging = true;
}
if (i > terms) {
throw new ArgumentTypeException("not converging after " + terms + " terms");
}
s = s.add(p);
a = a.add(1.0);
b = b.add(1.0);
i++;
pLast = p;
}
return s;
}
use of org.hipparchus.complex.Complex 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;
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class NumericArrayExpr method arrayComplexFloatRecursive.
private static boolean arrayComplexFloatRecursive(IAST nestedListsOfValues, int level, float[] floatArr, int[] index) throws RangeException, TypeException {
level--;
for (int i = 1; i < nestedListsOfValues.size(); i++) {
IExpr arg = nestedListsOfValues.get(i);
if (level == 0) {
if (arg.isList()) {
return false;
}
Complex value = arg.evalComplex();
floatArr[index[0]++] = (float) value.getReal();
floatArr[index[0]++] = (float) value.getImaginary();
} else {
if (!arg.isList() || !arrayComplexFloatRecursive((IAST) arg, level, floatArr, index)) {
return false;
}
}
}
return true;
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class ComplexNum method quotientRemainder.
/**
* Return the quotient and remainder as an array <code>[quotient, remainder]</code> of the
* division of <code>Complex</code> numbers <code>c1, c2</code>.
*
* <p>
* See
*
* <ul>
* <li><a href="https://en.wikipedia.org/wiki/Gaussian_integer">Wikipedia - Gaussian integer</a>
* <li><a href=
* "http://fermatslasttheorem.blogspot.com/2005/06/division-algorithm-for-gaussian.html">Division
* Algorithm for Gaussian Integers </a>
* </ul>
*
* @param cn1
* @param cn2
* @return the quotient and remainder as an array <code>[quotient, remainder]</code>
*/
public static ComplexNum[] quotientRemainder(ComplexNum cn1, ComplexNum cn2) {
Complex c1 = cn1.fComplex;
Complex c2 = cn2.fComplex;
Complex[] arr = quotientRemainder(c1, c2);
return new ComplexNum[] { valueOf(arr[0]), valueOf(arr[1]) };
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class ComplexFormFactory 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;
}
}
}
}
convertAST(buf, list);
return;
}
if (o instanceof ISignedNumber) {
double d = o.evalDouble();
if (fPackagePrefix) {
buf.append("org.hipparchus.complex.");
}
buf.append("Complex.valueOf(" + d + ")");
return;
}
if (o instanceof INumber) {
Complex c = o.evalComplex();
if (c != null) {
if (fPackagePrefix) {
buf.append("org.hipparchus.complex.");
}
buf.append("Complex.valueOf(" + c.getReal() + ", " + c.getImaginary() + ")");
} else {
buf.append("Complex.valueOf(" + o.toString() + ")");
}
return;
}
if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
return;
}
if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
return;
}
convertString(buf, o.toString());
}
Aggregations