Skip to content

Commit 71df39e

Browse files
committed
WIP #1034 NSum for complex numbers
- added *Complexx.java variants
1 parent 71534ff commit 71df39e

22 files changed

+2695
-778
lines changed

symja_android_library/doc/functions/NSum.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ NSum(expr, {i, imin, imax, di})
1313
> `i` ranges from `imin` to `imax` in steps `di`
1414
1515
See
16+
* [Wikipedia - Series (mathematics)](https://en.wikipedia.org/wiki/Series_(mathematics))
1617
* [Wikipedia - Summation](https://en.wikipedia.org/wiki/Summation)
1718
* [Wikipedia - Convergence_tests](https://en.wikipedia.org/wiki/Convergence_tests)
1819

1920
### Examples
2021

2122
```
22-
>> Num(k, {k, 1, 10})
23+
>> NSum(k, {k, 1, 10})
2324
55
2425
```
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,106 @@
11
package org.matheclipse.core.numerics.series.dp;
22

33
/**
4-
* Implements a modified adaptive Aitken delta^2 process for estimating infinite
5-
* series, based on the method in [1].
4+
* Implements a modified adaptive Aitken delta^2 process for estimating infinite series, based on
5+
* the method in [1].
66
*
77
* <p>
88
* References:
99
* <ul>
10-
* <li>[1] Osada, Naoki. Acceleration methods for slowly convergent sequences
11-
* and their applications. Diss. PhD thesis, Nagoya University, 1993.</li>
10+
* <li>[1] Osada, Naoki. Acceleration methods for slowly convergent sequences and their
11+
* applications. Diss. PhD thesis, Nagoya University, 1993.</li>
1212
* </ul>
1313
* </p>
1414
*/
1515
public final class Aitken extends SeriesAlgorithm {
1616

17-
private int status;
18-
private double xx, dx, dd, xp, alpha;
19-
private final double[] x;
20-
private final double[][] s, ds, ts, dts;
17+
private int status;
18+
private double xx, dx, dd, xp, alpha;
19+
private final double[] x;
20+
private final double[][] s, ds, ts, dts;
2121

22-
public Aitken(final double tolerance, final int maxIters, final int patience) {
23-
super(tolerance, maxIters, patience);
24-
x = new double[maxIters];
25-
s = new double[2][maxIters + 1];
26-
ds = new double[2][maxIters + 1];
27-
ts = new double[2][maxIters + 1];
28-
dts = new double[2][maxIters + 1];
29-
}
22+
public Aitken(final double tolerance, final int maxIters, final int patience) {
23+
super(tolerance, maxIters, patience);
24+
x = new double[maxIters];
25+
s = new double[2][maxIters + 1];
26+
ds = new double[2][maxIters + 1];
27+
ts = new double[2][maxIters + 1];
28+
dts = new double[2][maxIters + 1];
29+
}
3030

31-
@Override
32-
public final double next(final double e, final double term) {
33-
if (myIndex == 0) {
34-
xx = dx = dd = 0.0;
35-
}
36-
final double xxp = xx;
37-
x[myIndex] = xx = term;
38-
if (myIndex == 0) {
39-
dx = xx;
40-
} else if (myIndex == 1) {
41-
final double dxp = dx;
42-
dx = xx - xxp;
43-
dd = dx / (dx - dxp);
44-
} else {
45-
final double dxp = dx;
46-
final double ddp = dd;
47-
dx = xx - xxp;
48-
dd = dx / (dx - dxp);
49-
alpha = 1.0 / (dd - ddp) + 1.0;
50-
alpha = aitken(alpha, ts, dts, myIndex - 1, -2.0);
51-
}
52-
xp = xx;
53-
if (myIndex >= 2) {
54-
for (int m = 1; m <= myIndex + 1; ++m) {
55-
xp = aitken(x[m - 1], s, ds, m, alpha);
56-
}
57-
}
58-
++myIndex;
59-
if (status == 1) {
60-
return Double.NaN;
61-
} else {
62-
return xp;
63-
}
31+
@Override
32+
public final double next(final double e, final double term) {
33+
if (myIndex == 0) {
34+
xx = dx = dd = 0.0;
6435
}
65-
66-
private final double aitken(final double xx, final double[][] s, final double[][] ds, final int n,
67-
final double theta) {
68-
status = 0;
69-
if (n != 1) {
70-
final int kend = (n - 1) >> 1;
71-
System.arraycopy(s[1], 0, s[0], 0, kend + 1);
72-
if ((n & 1) == 0) {
73-
System.arraycopy(ds[1], 0, ds[0], 0, kend);
74-
} else {
75-
System.arraycopy(ds[1], 0, ds[0], 0, kend + 1);
76-
}
77-
}
78-
s[1][0] = xx;
79-
if (n == 1) {
80-
ds[1][0] = xx;
81-
return xx;
82-
}
83-
ds[1][0] = xx - s[0][0];
84-
for (int k = 1; k <= (n >> 1); ++k) {
85-
final double w1 = ds[0][k - 1] * ds[1][k - 1];
86-
final double w2 = ds[1][k - 1] - ds[0][k - 1];
87-
if (Math.abs(w2) < TINY) {
88-
status = 1;
89-
return xx;
90-
}
91-
final int twok = k << 1;
92-
final double coef = ((twok - 1) - theta) / (twok - 2 - theta);
93-
s[1][k] = s[0][k - 1] - coef * w1 / w2;
94-
if (n != twok - 1) {
95-
ds[1][k] = s[1][k] - s[0][k];
96-
}
97-
}
98-
final int kopt = n >> 1;
99-
return s[1][kopt];
36+
final double xxp = xx;
37+
x[myIndex] = xx = term;
38+
if (myIndex == 0) {
39+
dx = xx;
40+
} else if (myIndex == 1) {
41+
final double dxp = dx;
42+
dx = xx - xxp;
43+
dd = dx / (dx - dxp);
44+
} else {
45+
final double dxp = dx;
46+
final double ddp = dd;
47+
dx = xx - xxp;
48+
dd = dx / (dx - dxp);
49+
alpha = 1.0 / (dd - ddp) + 1.0;
50+
alpha = aitken(alpha, ts, dts, myIndex - 1, -2.0);
51+
}
52+
xp = xx;
53+
if (myIndex >= 2) {
54+
for (int m = 1; m <= myIndex + 1; ++m) {
55+
xp = aitken(x[m - 1], s, ds, m, alpha);
56+
}
57+
}
58+
++myIndex;
59+
if (status == 1) {
60+
return Double.NaN;
61+
} else {
62+
return xp;
10063
}
64+
}
10165

102-
@Override
103-
public final String getName() {
104-
return "Modified Aitken";
66+
private final double aitken(final double xx, final double[][] s, final double[][] ds, final int n,
67+
final double theta) {
68+
status = 0;
69+
if (n != 1) {
70+
final int kend = (n - 1) >> 1;
71+
System.arraycopy(s[1], 0, s[0], 0, kend + 1);
72+
if ((n & 1) == 0) {
73+
System.arraycopy(ds[1], 0, ds[0], 0, kend);
74+
} else {
75+
System.arraycopy(ds[1], 0, ds[0], 0, kend + 1);
76+
}
10577
}
78+
s[1][0] = xx;
79+
if (n == 1) {
80+
ds[1][0] = xx;
81+
return xx;
82+
}
83+
ds[1][0] = xx - s[0][0];
84+
for (int k = 1; k <= (n >> 1); ++k) {
85+
final double w1 = ds[0][k - 1] * ds[1][k - 1];
86+
final double w2 = ds[1][k - 1] - ds[0][k - 1];
87+
if (Math.abs(w2) < TINY) {
88+
status = 1;
89+
return xx;
90+
}
91+
final int twok = k << 1;
92+
final double coef = ((twok - 1) - theta) / (twok - 2 - theta);
93+
s[1][k] = s[0][k - 1] - coef * w1 / w2;
94+
if (n != twok - 1) {
95+
ds[1][k] = s[1][k] - s[0][k];
96+
}
97+
}
98+
final int kopt = n >> 1;
99+
return s[1][kopt];
100+
}
101+
102+
@Override
103+
public final String getName() {
104+
return "Modified Aitken";
105+
}
106106
}

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/numerics/series/dp/Cohen.java

+43-44
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,64 @@
44
import org.matheclipse.core.numerics.utils.SimpleMath;
55

66
/**
7-
* This is an implementation of Algorithm 1 in [1] for evaluating infinite
8-
* series whose terms have alternating signs.
7+
* This is an implementation of Algorithm 1 in [1] for evaluating infinite series whose terms have
8+
* alternating signs.
99
*
1010
* <p>
1111
* References:
1212
* <ul>
13-
* <li>[1] Cohen, Henri, Fernando Rodriguez Villegas, and Don Zagier.
14-
* "Convergence acceleration of alternating series." Experimental mathematics
15-
* 9.1 (2000): 3-12.</li>
13+
* <li>[1] Cohen, Henri, Fernando Rodriguez Villegas, and Don Zagier. "Convergence acceleration of
14+
* alternating series." Experimental mathematics 9.1 (2000): 3-12.</li>
1615
* </ul>
1716
* </p>
1817
*/
1918
public final class Cohen extends SeriesAlgorithm {
2019

21-
private final double[] myTab;
22-
private double mySign0;
20+
private final double[] myTab;
21+
private double mySign0;
2322

24-
public Cohen(final double tolerance, final int maxIters, final int patience) {
25-
super(tolerance, maxIters, patience);
26-
myTab = new double[maxIters];
27-
}
23+
public Cohen(final double tolerance, final int maxIters, final int patience) {
24+
super(tolerance, maxIters, patience);
25+
myTab = new double[maxIters];
26+
}
2827

29-
@Override
30-
public final double next(final double e, final double term) {
28+
@Override
29+
public final double next(final double e, final double term) {
3130

32-
// add next element
33-
final int n = myIndex + 1;
34-
myTab[myIndex] = Math.abs(e);
31+
// add next element
32+
final int n = myIndex + 1;
33+
myTab[myIndex] = Math.abs(e);
3534

36-
// record the sign of the first term, since this method
37-
// requires the first term of the sequence to be positive
38-
if (myIndex == 0) {
39-
mySign0 = Math.signum(e);
40-
if (mySign0 == 0.0) {
41-
mySign0 = 1.0;
42-
}
43-
}
35+
// record the sign of the first term, since this method
36+
// requires the first term of the sequence to be positive
37+
if (myIndex == 0) {
38+
mySign0 = Math.signum(e);
39+
if (mySign0 == 0.0) {
40+
mySign0 = 1.0;
41+
}
42+
}
4443

45-
// initialize d value
46-
double d = SimpleMath.pow(3.0 + 2.0 * Constants.SQRT2, n);
47-
d = 0.5 * (d + 1.0 / d);
44+
// initialize d value
45+
double d = SimpleMath.pow(3.0 + 2.0 * Constants.SQRT2, n);
46+
d = 0.5 * (d + 1.0 / d);
4847

49-
// apply the Chebychef polynomial recursively (Algorithm 1)
50-
double b = -1.0;
51-
double c = -d;
52-
double s = 0.0;
53-
for (int k = 0; k < n; ++k) {
54-
c = b - c;
55-
s += c * myTab[k];
56-
final double numer = (k + n) * (k - n);
57-
final double denom = (k + 0.5) * (k + 1);
58-
b *= numer / denom;
59-
}
60-
++myIndex;
61-
return mySign0 * s / d;
48+
// apply the Chebychef polynomial recursively (Algorithm 1)
49+
double b = -1.0;
50+
double c = -d;
51+
double s = 0.0;
52+
for (int k = 0; k < n; ++k) {
53+
c = b - c;
54+
s += c * myTab[k];
55+
final double numer = (k + n) * (k - n);
56+
final double denom = (k + 0.5) * (k + 1);
57+
b *= numer / denom;
6258
}
59+
++myIndex;
60+
return mySign0 * s / d;
61+
}
6362

64-
@Override
65-
public final String getName() {
66-
return "Cohen";
67-
}
63+
@Override
64+
public final String getName() {
65+
return "Cohen";
66+
}
6867
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package org.matheclipse.core.numerics.series.dp;
22

33
/**
4-
* Naively computes the limit of a sequence or series by inspecting elements one
5-
* a time. No transform of the original sequence is performed.
4+
* Naively computes the limit of a sequence or series by inspecting elements one a time. No
5+
* transform of the original sequence is performed.
66
*/
77
public final class Direct extends SeriesAlgorithm {
88

9-
public Direct(final double tolerance, final int maxIters, final int patience) {
10-
super(tolerance, maxIters, patience);
11-
}
9+
public Direct(final double tolerance, final int maxIters, final int patience) {
10+
super(tolerance, maxIters, patience);
11+
}
1212

13-
@Override
14-
public final double next(final double e, final double term) {
15-
++myIndex;
16-
return term;
17-
}
13+
@Override
14+
public final double next(final double e, final double term) {
15+
++myIndex;
16+
return term;
17+
}
1818

19-
@Override
20-
public final String getName() {
21-
return "Direct Sum";
22-
}
19+
@Override
20+
public final String getName() {
21+
return "Direct Sum";
22+
}
2323
}

0 commit comments

Comments
 (0)