use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class Convert method list2ComplexMatrix.
/**
* Returns a <code>FieldMatrix<Complex></code> if possible.
*
* @param expr
* @return <code>null</code> if the conversion isn't possible.
* @throws ClassCastException
* @throws IndexOutOfBoundsException
*/
public static FieldMatrix<Complex> list2ComplexMatrix(IExpr expr) throws ClassCastException, IndexOutOfBoundsException {
if (expr == null) {
return null;
}
int[] dim = expr.isMatrix();
if (dim == null || dim[0] == 0 || dim[1] == 0) {
return null;
}
if (expr.isSparseArray()) {
// TODO optimize for sparse arrays
// ISparseArray array = (ISparseArray) expr;
expr = ((ISparseArray) expr).normal(false);
}
if (expr.isList()) {
try {
IAST list = (IAST) expr;
IAST currInRow = (IAST) list.arg1();
if (currInRow.isAST0()) {
// special case 0-Matrix
Complex[][] array = new Complex[0][0];
return new Array2DRowFieldMatrix<Complex>(array, false);
}
final int rowSize = expr.argSize();
final int colSize = currInRow.argSize();
final Complex[][] elements = new Complex[rowSize][colSize];
for (int i = 1; i < rowSize + 1; i++) {
currInRow = (IAST) list.get(i);
if (currInRow.isVector() < 0 || colSize != currInRow.argSize()) {
return null;
}
for (int j = 1; j < colSize + 1; j++) {
elements[i - 1][j - 1] = currInRow.get(j).evalComplex();
}
}
return new Array2DRowFieldMatrix<Complex>(elements, false);
} catch (ValidateException vex) {
// pass
}
}
return null;
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class HypergeometricJS method hypergeometric1F1.
// public static Complex hypergeometric1F1(Complex a, Complex b, Complex x) {
// return hypergeometric1F1(a, b, x, Config.SPECIAL_FUNCTIONS_TOLERANCE);
// }
public static Complex hypergeometric1F1(Complex a, Complex b, Complex x) {
final double useAsymptotic = 30;
if (b.isMathematicalInteger() && b.getReal() <= 0) {
throw new ArgumentTypeException("hypergeometric function pole");
}
// Kummer transformation
if (x.getReal() < 0) {
return x.exp().multiply(hypergeometric1F1(b.subtract(a), b, x.negate()));
}
// asymptotic form as per Johansson arxiv.org/abs/1606.06977
if (x.norm() > useAsymptotic) {
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.0), 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 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).multiply(b.reciprocal()).divide(i);
s = s.add(p);
a = a.add(1.0);
b = b.add(1.0);
if (i++ > iterationLimit && iterationLimit > 0) {
IterationLimitExceeded.throwIt(i, S.Hypergeometric1F1);
}
}
return s;
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class HypergeometricJS method hypergeometric1F2.
public static Complex hypergeometric1F2(Complex a, Complex b, Complex c, Complex x) {
final int useAsymptotic = 200;
if (x.norm() > useAsymptotic) {
Complex p = a.add(b.negate()).add(c.negate()).add(0.5).divide(2.0);
ArrayList<Complex> ck = new ArrayList<Complex>();
//
ck.add(Complex.ONE);
ck.add(((a.multiply(3.0).add(b).add(c).add(-2.0)).multiply(a.subtract(b.add(c))).multiply(0.5)).add(b.multiply(c).multiply(2)).add(//
-3.0 / 8.0));
ck.add((a.multiply(3.0).add(b).add(c).add(-2.0)).multiply(a.subtract(b.add(c))).multiply(0.25).add(b.multiply(c).add(-3.0 / 16.0)).pow(2).multiply(//
2));
//
ck.add(new Complex(-1.0).multiply(a.multiply(2.0).subtract(3.0)).multiply(b).multiply(c));
ck.add(a.pow(2.0).multiply(-8.0).add(a.multiply(11.0)).add(b).add(c).add(-2.0).multiply(a.subtract(b.add(c))).multiply(//
0.25));
ck.add(new Complex(-3.0 / 16.0));
IntFunction<Complex> w = k -> ck.get(k).multiply(x.negate().pow(-k / 2.0)).divide(Math.pow(2.0, k));
Complex u1 = Complex.I.multiply(p.multiply(Math.PI).add(x.negate().sqrt().multiply(2.0))).exp();
Complex u2 = new Complex(0.0, -1.0).multiply(p.multiply(Math.PI).add(x.negate().sqrt().multiply(2.0))).exp();
Complex wLast = w.apply(2);
Complex w2Negate = wLast.negate();
Complex s = //
u1.multiply(new Complex(0.0, -1.0).multiply(w.apply(1)).add(w2Negate).add(1.0)).add(u2.multiply(Complex.I.multiply(w.apply(1)).add(w2Negate).add(1.0)));
int k = 3;
while (wLast.norm() > w.apply(k).norm()) {
//
ck.add(a.multiply(-6.0).add(b.multiply(2)).add(c.multiply(2.0)).add(-4.0).multiply(k).add(a.pow(a).multiply(3.0)).add(b.subtract(c).pow(2.0).negate()).add(a.multiply(b.add(c).add(-2)).multiply(2.0).negate()).add(0.25).add(3.0 * k * k).multiply(1.0 / (2.0 * k)).multiply(//
ck.get(k - 1)).subtract(a.negate().add(b).add(c.negate()).add(-0.5).add(k).multiply(a.negate().add(b.negate()).add(c).add(-0.5).add(k)).multiply(a.negate().add(b).add(c).add(-2.5).add(k)).multiply(//
ck.get(k - 2))));
wLast = w.apply(k);
s = s.add(//
u1.multiply(new Complex(0.0, -1.0).pow(k)).multiply(wLast).add(u2.multiply(Complex.I.pow(k)).multiply(wLast)));
k++;
}
Complex t1 = Arithmetic.lanczosApproxGamma(a).reciprocal().multiply(x.negate().pow(p)).multiply(s).divide(2.0 * Math.sqrt(Math.PI));
Complex t2 = Arithmetic.lanczosApproxGamma(b.subtract(a)).reciprocal().multiply(Arithmetic.lanczosApproxGamma(c.subtract(a)).reciprocal()).multiply(x.negate().pow(a.negate())).multiply(hypergeometricSeries(new Complex[] { a, a.add(b.negate()).add(1), a.add(c.negate().add(1.0)) }, new Complex[] {}, // , true ) );
x.reciprocal()));
// hypergeometricSeries( [ a, add(a,neg(b),1), add(a,neg(c),1) ], [], inv(x), true ) );
return Arithmetic.lanczosApproxGamma(b).multiply(Arithmetic.lanczosApproxGamma(c)).multiply(t1.add(t2));
}
return hypergeometricSeries(new Complex[] { a }, new Complex[] { b, c }, x);
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class Convert method list2ComplexVector.
public static FieldVector<Complex> list2ComplexVector(IExpr expr) throws ClassCastException {
if (expr == null) {
return null;
}
int dim = expr.isVector();
if (dim <= 0) {
return null;
}
if (expr.isSparseArray()) {
// ISparseArray array = (ISparseArray) expr;
// return array.toFieldVector(false);
expr = ((ISparseArray) expr).normal(false);
}
if (expr.isList()) {
try {
final int rowSize = expr.argSize();
IAST list = (IAST) expr;
final Complex[] elements = new Complex[rowSize];
for (int i = 0; i < rowSize; i++) {
elements[i] = list.get(i + 1).evalComplex();
}
return new ArrayFieldVector<Complex>(elements, false);
} catch (ValidateException vex) {
// pass
}
}
return null;
}
use of org.hipparchus.complex.Complex in project symja_android_library by axkr.
the class BesselJS method airyBiPrime.
public static Complex airyBiPrime(Complex x) {
if (F.isZero(x)) {
return new Complex(Math.pow(3.0, 1.6) / GammaJS.gamma(1.0 / 3.0));
}
if (x.getReal() < 0) {
Complex xMinus = x.negate();
Complex z = xMinus.pow(1.5).multiply(2.0 / 3.0);
return xMinus.multiply(1.0 / Math.sqrt(3.0)).multiply(besselJ(2.0 / 3.0, z).add(besselJ(-2.0 / 3.0, z)));
}
Complex z = x.pow(1.5).multiply(2.0 / 3.0);
return x.multiply(1.0 / Math.sqrt(3.0)).multiply(besselI(2.0 / 3.0, z).add(besselI(-2.0 / 3.0, z)));
}
Aggregations