@@ -4106,25 +4106,42 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
4106
4106
IAST data = (IAST ) distribution ;
4107
4107
// Sum( predicate , data ) / data.argSize()
4108
4108
IASTAppendable sum = F .PlusAlloc (data .size ());
4109
+ INumber sumValue = F .C0 ;
4110
+ sum .append (sumValue );
4109
4111
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
+ }
4111
4118
}
4112
- return sum .divide (F .ZZ (data .argSize ()));
4119
+ sum .set (1 , sumValue );
4120
+ return engine .evaluate (sum .divide (F .ZZ (data .argSize ())));
4113
4121
} else if (distribution .isContinuousDistribution ()) {
4114
4122
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
+ }
4116
4126
} 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 );
4123
4127
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
+ }
4128
4145
}
4129
4146
}
4130
4147
} catch (RuntimeException rex ) {
@@ -6517,10 +6534,6 @@ public void setUp(final ISymbol newSymbol) {}
6517
6534
*/
6518
6535
private static final class PoissonDistribution extends AbstractEvaluator
6519
6536
implements ICDF , IDiscreteDistribution , IPDF , IStatistics , IRandomVariate {
6520
- @ Override
6521
- public IExpr lowerBound () {
6522
- return F .C0 ;
6523
- }
6524
6537
6525
6538
@ Override
6526
6539
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
0 commit comments