Skip to content

Commit

Permalink
WIP #1034 NSum for complex numbers
Browse files Browse the repository at this point in the history
- added *Complexx.java variants
  • Loading branch information
axkr committed Aug 10, 2024
1 parent 71534ff commit 71df39e
Show file tree
Hide file tree
Showing 22 changed files with 2,695 additions and 778 deletions.
3 changes: 2 additions & 1 deletion symja_android_library/doc/functions/NSum.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ NSum(expr, {i, imin, imax, di})
> `i` ranges from `imin` to `imax` in steps `di`
See
* [Wikipedia - Series (mathematics)](https://en.wikipedia.org/wiki/Series_(mathematics))
* [Wikipedia - Summation](https://en.wikipedia.org/wiki/Summation)
* [Wikipedia - Convergence_tests](https://en.wikipedia.org/wiki/Convergence_tests)

### Examples

```
>> Num(k, {k, 1, 10})
>> NSum(k, {k, 1, 10})
55
```
Original file line number Diff line number Diff line change
@@ -1,106 +1,106 @@
package org.matheclipse.core.numerics.series.dp;

/**
* Implements a modified adaptive Aitken delta^2 process for estimating infinite
* series, based on the method in [1].
* Implements a modified adaptive Aitken delta^2 process for estimating infinite series, based on
* the method in [1].
*
* <p>
* References:
* <ul>
* <li>[1] Osada, Naoki. Acceleration methods for slowly convergent sequences
* and their applications. Diss. PhD thesis, Nagoya University, 1993.</li>
* <li>[1] Osada, Naoki. Acceleration methods for slowly convergent sequences and their
* applications. Diss. PhD thesis, Nagoya University, 1993.</li>
* </ul>
* </p>
*/
public final class Aitken extends SeriesAlgorithm {

private int status;
private double xx, dx, dd, xp, alpha;
private final double[] x;
private final double[][] s, ds, ts, dts;
private int status;
private double xx, dx, dd, xp, alpha;
private final double[] x;
private final double[][] s, ds, ts, dts;

public Aitken(final double tolerance, final int maxIters, final int patience) {
super(tolerance, maxIters, patience);
x = new double[maxIters];
s = new double[2][maxIters + 1];
ds = new double[2][maxIters + 1];
ts = new double[2][maxIters + 1];
dts = new double[2][maxIters + 1];
}
public Aitken(final double tolerance, final int maxIters, final int patience) {
super(tolerance, maxIters, patience);
x = new double[maxIters];
s = new double[2][maxIters + 1];
ds = new double[2][maxIters + 1];
ts = new double[2][maxIters + 1];
dts = new double[2][maxIters + 1];
}

@Override
public final double next(final double e, final double term) {
if (myIndex == 0) {
xx = dx = dd = 0.0;
}
final double xxp = xx;
x[myIndex] = xx = term;
if (myIndex == 0) {
dx = xx;
} else if (myIndex == 1) {
final double dxp = dx;
dx = xx - xxp;
dd = dx / (dx - dxp);
} else {
final double dxp = dx;
final double ddp = dd;
dx = xx - xxp;
dd = dx / (dx - dxp);
alpha = 1.0 / (dd - ddp) + 1.0;
alpha = aitken(alpha, ts, dts, myIndex - 1, -2.0);
}
xp = xx;
if (myIndex >= 2) {
for (int m = 1; m <= myIndex + 1; ++m) {
xp = aitken(x[m - 1], s, ds, m, alpha);
}
}
++myIndex;
if (status == 1) {
return Double.NaN;
} else {
return xp;
}
@Override
public final double next(final double e, final double term) {
if (myIndex == 0) {
xx = dx = dd = 0.0;
}

private final double aitken(final double xx, final double[][] s, final double[][] ds, final int n,
final double theta) {
status = 0;
if (n != 1) {
final int kend = (n - 1) >> 1;
System.arraycopy(s[1], 0, s[0], 0, kend + 1);
if ((n & 1) == 0) {
System.arraycopy(ds[1], 0, ds[0], 0, kend);
} else {
System.arraycopy(ds[1], 0, ds[0], 0, kend + 1);
}
}
s[1][0] = xx;
if (n == 1) {
ds[1][0] = xx;
return xx;
}
ds[1][0] = xx - s[0][0];
for (int k = 1; k <= (n >> 1); ++k) {
final double w1 = ds[0][k - 1] * ds[1][k - 1];
final double w2 = ds[1][k - 1] - ds[0][k - 1];
if (Math.abs(w2) < TINY) {
status = 1;
return xx;
}
final int twok = k << 1;
final double coef = ((twok - 1) - theta) / (twok - 2 - theta);
s[1][k] = s[0][k - 1] - coef * w1 / w2;
if (n != twok - 1) {
ds[1][k] = s[1][k] - s[0][k];
}
}
final int kopt = n >> 1;
return s[1][kopt];
final double xxp = xx;
x[myIndex] = xx = term;
if (myIndex == 0) {
dx = xx;
} else if (myIndex == 1) {
final double dxp = dx;
dx = xx - xxp;
dd = dx / (dx - dxp);
} else {
final double dxp = dx;
final double ddp = dd;
dx = xx - xxp;
dd = dx / (dx - dxp);
alpha = 1.0 / (dd - ddp) + 1.0;
alpha = aitken(alpha, ts, dts, myIndex - 1, -2.0);
}
xp = xx;
if (myIndex >= 2) {
for (int m = 1; m <= myIndex + 1; ++m) {
xp = aitken(x[m - 1], s, ds, m, alpha);
}
}
++myIndex;
if (status == 1) {
return Double.NaN;
} else {
return xp;
}
}

@Override
public final String getName() {
return "Modified Aitken";
private final double aitken(final double xx, final double[][] s, final double[][] ds, final int n,
final double theta) {
status = 0;
if (n != 1) {
final int kend = (n - 1) >> 1;
System.arraycopy(s[1], 0, s[0], 0, kend + 1);
if ((n & 1) == 0) {
System.arraycopy(ds[1], 0, ds[0], 0, kend);
} else {
System.arraycopy(ds[1], 0, ds[0], 0, kend + 1);
}
}
s[1][0] = xx;
if (n == 1) {
ds[1][0] = xx;
return xx;
}
ds[1][0] = xx - s[0][0];
for (int k = 1; k <= (n >> 1); ++k) {
final double w1 = ds[0][k - 1] * ds[1][k - 1];
final double w2 = ds[1][k - 1] - ds[0][k - 1];
if (Math.abs(w2) < TINY) {
status = 1;
return xx;
}
final int twok = k << 1;
final double coef = ((twok - 1) - theta) / (twok - 2 - theta);
s[1][k] = s[0][k - 1] - coef * w1 / w2;
if (n != twok - 1) {
ds[1][k] = s[1][k] - s[0][k];
}
}
final int kopt = n >> 1;
return s[1][kopt];
}

@Override
public final String getName() {
return "Modified Aitken";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,64 @@
import org.matheclipse.core.numerics.utils.SimpleMath;

/**
* This is an implementation of Algorithm 1 in [1] for evaluating infinite
* series whose terms have alternating signs.
* This is an implementation of Algorithm 1 in [1] for evaluating infinite series whose terms have
* alternating signs.
*
* <p>
* References:
* <ul>
* <li>[1] Cohen, Henri, Fernando Rodriguez Villegas, and Don Zagier.
* "Convergence acceleration of alternating series." Experimental mathematics
* 9.1 (2000): 3-12.</li>
* <li>[1] Cohen, Henri, Fernando Rodriguez Villegas, and Don Zagier. "Convergence acceleration of
* alternating series." Experimental mathematics 9.1 (2000): 3-12.</li>
* </ul>
* </p>
*/
public final class Cohen extends SeriesAlgorithm {

private final double[] myTab;
private double mySign0;
private final double[] myTab;
private double mySign0;

public Cohen(final double tolerance, final int maxIters, final int patience) {
super(tolerance, maxIters, patience);
myTab = new double[maxIters];
}
public Cohen(final double tolerance, final int maxIters, final int patience) {
super(tolerance, maxIters, patience);
myTab = new double[maxIters];
}

@Override
public final double next(final double e, final double term) {
@Override
public final double next(final double e, final double term) {

// add next element
final int n = myIndex + 1;
myTab[myIndex] = Math.abs(e);
// add next element
final int n = myIndex + 1;
myTab[myIndex] = Math.abs(e);

// record the sign of the first term, since this method
// requires the first term of the sequence to be positive
if (myIndex == 0) {
mySign0 = Math.signum(e);
if (mySign0 == 0.0) {
mySign0 = 1.0;
}
}
// record the sign of the first term, since this method
// requires the first term of the sequence to be positive
if (myIndex == 0) {
mySign0 = Math.signum(e);
if (mySign0 == 0.0) {
mySign0 = 1.0;
}
}

// initialize d value
double d = SimpleMath.pow(3.0 + 2.0 * Constants.SQRT2, n);
d = 0.5 * (d + 1.0 / d);
// initialize d value
double d = SimpleMath.pow(3.0 + 2.0 * Constants.SQRT2, n);
d = 0.5 * (d + 1.0 / d);

// apply the Chebychef polynomial recursively (Algorithm 1)
double b = -1.0;
double c = -d;
double s = 0.0;
for (int k = 0; k < n; ++k) {
c = b - c;
s += c * myTab[k];
final double numer = (k + n) * (k - n);
final double denom = (k + 0.5) * (k + 1);
b *= numer / denom;
}
++myIndex;
return mySign0 * s / d;
// apply the Chebychef polynomial recursively (Algorithm 1)
double b = -1.0;
double c = -d;
double s = 0.0;
for (int k = 0; k < n; ++k) {
c = b - c;
s += c * myTab[k];
final double numer = (k + n) * (k - n);
final double denom = (k + 0.5) * (k + 1);
b *= numer / denom;
}
++myIndex;
return mySign0 * s / d;
}

@Override
public final String getName() {
return "Cohen";
}
@Override
public final String getName() {
return "Cohen";
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package org.matheclipse.core.numerics.series.dp;

/**
* Naively computes the limit of a sequence or series by inspecting elements one
* a time. No transform of the original sequence is performed.
* Naively computes the limit of a sequence or series by inspecting elements one a time. No
* transform of the original sequence is performed.
*/
public final class Direct extends SeriesAlgorithm {

public Direct(final double tolerance, final int maxIters, final int patience) {
super(tolerance, maxIters, patience);
}
public Direct(final double tolerance, final int maxIters, final int patience) {
super(tolerance, maxIters, patience);
}

@Override
public final double next(final double e, final double term) {
++myIndex;
return term;
}
@Override
public final double next(final double e, final double term) {
++myIndex;
return term;
}

@Override
public final String getName() {
return "Direct Sum";
}
@Override
public final String getName() {
return "Direct Sum";
}
}
Loading

0 comments on commit 71df39e

Please sign in to comment.