Skip to content

Commit cfb7405

Browse files
committed
WIP #1034 NSum, NExpectation
1 parent 116e03f commit cfb7405

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/StatisticsFunctions.java

+30-17
Original file line numberDiff line numberDiff line change
@@ -4106,25 +4106,42 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
41064106
IAST data = (IAST) distribution;
41074107
// Sum( predicate , data ) / data.argSize()
41084108
IASTAppendable sum = F.PlusAlloc(data.size());
4109+
INumber sumValue = F.C0;
4110+
sum.append(sumValue);
41094111
for (int i = 1; i < data.size(); i++) {
4110-
sum.append(F.subst(xExpr, F.Rule(x, data.get(i))));
4112+
IExpr summand = engine.evaluate(F.subst(xExpr, F.Rule(x, data.get(i))));
4113+
if (summand.isNumber()) {
4114+
sumValue = sumValue.plus((INumber) summand);
4115+
} else {
4116+
sum.append(summand);
4117+
}
41114118
}
4112-
return sum.divide(F.ZZ(data.argSize()));
4119+
sum.set(1, sumValue);
4120+
return engine.evaluate(sum.divide(F.ZZ(data.argSize())));
41134121
} else if (distribution.isContinuousDistribution()) {
41144122
IExpr pdf = S.PDF.of(engine, distribution, x);
4115-
return F.NIntegrate(F.Times(ast.arg1(), pdf), F.list(x, F.CNInfinity, F.CInfinity));
4123+
if (pdf.isPresent()) {
4124+
return F.NIntegrate(F.Times(ast.arg1(), pdf), F.list(x, F.CNInfinity, F.CInfinity));
4125+
}
41164126
} else if (distribution.isDiscreteDistribution()) {
4117-
// final IExpr xNegate = EvalEngine.get().evaluate(F.Negate(x));
4118-
// IExpr fNegate = F.subst(ast.arg1(), v -> v.equals(x) ? xNegate : F.NIL);
4119-
// IExpr pdfNegate = S.PDF.of(engine, distribution, xNegate);
4120-
// IAST function = F.Times(fNegate, pdfNegate);
4121-
// IAST nsum1 = F.NSum(function, F.list(x, F.C0, F.CInfinity));
4122-
IDiscreteDistribution dist = getDiscreteDistribution(distribution);
41234127
IExpr pdf = S.PDF.of(engine, distribution, x);
4124-
IAST function = F.Times(ast.arg1(), pdf);
4125-
IExpr lowerBound = dist.lowerBound();
4126-
return NSum.nsum(function, x, lowerBound, F.CInfinity,
4127-
F.NSum(function, F.list(x, lowerBound, F.CInfinity)));
4128+
if (pdf.isPresent()) {
4129+
IDiscreteDistribution dist = getDiscreteDistribution(distribution);
4130+
int supportUpperBound = dist.getSupportUpperBound(distribution);
4131+
if (supportUpperBound < Integer.MAX_VALUE) {
4132+
int supportLowerBound = dist.getSupportLowerBound(distribution);
4133+
if (supportLowerBound > Integer.MIN_VALUE
4134+
&& supportLowerBound < supportUpperBound) {
4135+
IAST function = F.Times(ast.arg1(), pdf);
4136+
int lowerBound = dist.getSupportLowerBound(distribution);
4137+
return NSum.nsum(function, x, F.ZZ(lowerBound), F.ZZ(supportUpperBound),
4138+
F.NSum(function, F.list(x, F.ZZ(lowerBound), F.ZZ(supportUpperBound))));
4139+
}
4140+
}
4141+
IAST function = F.Times(ast.arg1(), pdf);
4142+
return NSum.nsum(function, x, F.CNInfinity, F.CInfinity,
4143+
F.NSum(function, F.list(x, F.CNInfinity, F.CInfinity)));
4144+
}
41284145
}
41294146
}
41304147
} catch (RuntimeException rex) {
@@ -6517,10 +6534,6 @@ public void setUp(final ISymbol newSymbol) {}
65176534
*/
65186535
private static final class PoissonDistribution extends AbstractEvaluator
65196536
implements ICDF, IDiscreteDistribution, IPDF, IStatistics, IRandomVariate {
6520-
@Override
6521-
public IExpr lowerBound() {
6522-
return F.C0;
6523-
}
65246537

65256538
@Override
65266539
public IExpr evaluate(final IAST ast, EvalEngine engine) {

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/reflection/system/NSum.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.matheclipse.core.reflection.system;
22

3-
import java.util.Iterator;
43
import java.util.function.Function;
54
import org.hipparchus.complex.Complex;
65
import org.matheclipse.core.eval.EvalEngine;
@@ -170,14 +169,22 @@ private static IExpr sumStartToInfinity(IExpr function, IExpr variable, IExpr lo
170169
long start = lowerLimit.toLongDefault();
171170
long end = upperLimit.toLongDefault();
172171
if (start != Long.MIN_VALUE && end != Long.MIN_VALUE) {
173-
LongComplexFunction longComplexFunction = new LongComplexFunction(function, variable);
174-
Iterator<Complex> iter = Sequences.toIterable(longComplexFunction, start, end).iterator();
175-
Complex result = Complex.ZERO;
176-
for (long i = start; i < end; i++) {
177-
Complex c = iter.next();
178-
result = result.add(c);
179-
}
180-
return F.complexNum(result);
172+
LongDoubleFunction longDoubleFunction = new LongDoubleFunction(function, variable);
173+
174+
Iterable<Double> iter = Sequences.toIterable(longDoubleFunction, start, end);
175+
SeriesAlgorithm alg = new Ensemble(1e-8, 1000, 5);
176+
SeriesSolution limit = alg.limit(iter, true);
177+
return F.num(limit.limit);
178+
179+
// LongComplexFunction longComplexFunction = new LongComplexFunction(function, variable);
180+
// Iterator<Complex> iter = Sequences.toIterable(longComplexFunction, start,
181+
// end).iterator();
182+
// Complex result = Complex.ZERO;
183+
// for (long i = start; i < end; i++) {
184+
// Complex c = iter.next();
185+
// result = result.add(c);
186+
// }
187+
// return F.complexNum(result);
181188
}
182189
}
183190
return F.NIL;

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/DistributionTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ public void testNExpectation() {
264264
"13.00025");
265265

266266
// check("NExpectation(E^x, Distributed(x, HypergeometricDistribution(20,50,100)))", //
267-
// "-4.77816*10^-10");
267+
// "");
268268
// check("NExpectation(E^x, Distributed(x, GeometricDistribution(0.1)))", //
269-
// "-0.0691346");
269+
// "");
270270
// check("NExpectation(E^x, Distributed(x, DiscreteUniformDistribution({20,40})))", //
271271
// "");
272272
// check(

0 commit comments

Comments
 (0)