@@ -72,6 +72,7 @@ Matdist <- R6Class("Matdist",
72
72
support = Set $ new(1 , class = " numeric" )^ " n" ,
73
73
type = Reals $ new()^ " n"
74
74
)
75
+ private $ .ndists <- nrow(gprm(self , " pdf" ))
75
76
invisible (self )
76
77
},
77
78
@@ -80,7 +81,7 @@ Matdist <- R6Class("Matdist",
80
81
# ' @param n `(integer(1))` \cr
81
82
# ' Ignored.
82
83
strprint = function (n = 2 ) {
83
- " Matdist() "
84
+ sprintf( " Matdist(%s) " , private $ .ndists )
84
85
},
85
86
86
87
# stats
@@ -128,7 +129,7 @@ Matdist <- R6Class("Matdist",
128
129
" *" %= % gprm(self , c(" x" , " pdf" ))
129
130
mean <- self $ mean()
130
131
131
- vnapply(seq(nrow( pdf ) ), function (i ) {
132
+ vnapply(seq_len( private $ .ndists ), function (i ) {
132
133
if (mean [[i ]] == Inf ) {
133
134
Inf
134
135
} else {
@@ -149,7 +150,7 @@ Matdist <- R6Class("Matdist",
149
150
mean <- self $ mean()
150
151
sd <- self $ stdev()
151
152
152
- vnapply(seq(nrow( pdf ) ), function (i ) {
153
+ vnapply(seq_len( private $ .ndists ), function (i ) {
153
154
if (mean [[i ]] == Inf ) {
154
155
Inf
155
156
} else {
@@ -171,7 +172,7 @@ Matdist <- R6Class("Matdist",
171
172
mean <- self $ mean()
172
173
sd <- self $ stdev()
173
174
174
- kurt <- vnapply(seq(nrow( pdf ) ), function (i ) {
175
+ kurt <- vnapply(seq_len( private $ .ndists ), function (i ) {
175
176
if (mean [[i ]] == Inf ) {
176
177
Inf
177
178
} else {
@@ -209,8 +210,8 @@ Matdist <- R6Class("Matdist",
209
210
if (length(t ) == 1 ) {
210
211
mgf <- apply(pdf , 1 , function (.y ) sum(exp(x * t ) * .y ))
211
212
} else {
212
- stopifnot(length(z ) == nrow( pdf ) )
213
- mgf <- vnapply(seq(nrow( pdf ) ),
213
+ stopifnot(length(z ) == private $ .ndists )
214
+ mgf <- vnapply(seq_len( private $ .ndists ),
214
215
function (i ) sum(exp(x * t [[i ]]) * pdf [i , ]))
215
216
}
216
217
@@ -228,8 +229,8 @@ Matdist <- R6Class("Matdist",
228
229
if (length(t ) == 1 ) {
229
230
cf <- apply(pdf , 1 , function (.y ) sum(exp(x * t * 1i ) * .y ))
230
231
} else {
231
- stopifnot(length(z ) == nrow( pdf ) )
232
- cf <- vnapply(seq(nrow( pdf ) ),
232
+ stopifnot(length(z ) == private $ .ndists )
233
+ cf <- vnapply(seq_len( private $ .ndists ),
233
234
function (i ) sum(exp(x * t [[i ]] * 1i ) * pdf [i , ]))
234
235
}
235
236
@@ -247,8 +248,8 @@ Matdist <- R6Class("Matdist",
247
248
if (length(z ) == 1 ) {
248
249
pgf <- apply(pdf , 1 , function (.y ) sum((z ^ x ) * .y ))
249
250
} else {
250
- stopifnot(length(z ) == nrow( pdf ) )
251
- pgf <- vnapply(seq(nrow( pdf ) ),
251
+ stopifnot(length(z ) == private $ .ndists )
252
+ pgf <- vnapply(seq_len( private $ .ndists ),
252
253
function (i ) sum((z [[i ]]^ x ) * pdf [i , ]))
253
254
}
254
255
@@ -271,7 +272,7 @@ Matdist <- R6Class("Matdist",
271
272
.pdf = function (x , log = FALSE ) {
272
273
" pdf, data" %= % gprm(self , c(" pdf" , " x" ))
273
274
out <- t(C_Vec_WeightedDiscretePdf(
274
- x , matrix (data , ncol(pdf ), nrow( pdf ) ),
275
+ x , matrix (data , ncol(pdf ), private $ .ndists ),
275
276
t(pdf )
276
277
))
277
278
if (log ) {
@@ -306,7 +307,8 @@ Matdist <- R6Class("Matdist",
306
307
307
308
# traits
308
309
.traits = list (valueSupport = " discrete" , variateForm = " univariate" ),
309
- .improper = FALSE
310
+ .improper = FALSE ,
311
+ .ndists = 0
310
312
)
311
313
)
312
314
@@ -392,7 +394,12 @@ c.Matdist <- function(...) {
392
394
# ' m[1:2]
393
395
# ' @export
394
396
" [.Matdist" <- function (md , i ) {
395
- if (length(i ) == 1 ) {
397
+ if (is.logical(i )) {
398
+ i <- which(i )
399
+ }
400
+ if (length(i ) == 0 ) {
401
+ stop(" Can't create an empty distribution." )
402
+ } else if (length(i ) == 1 ) {
396
403
pdf <- gprm(md , " pdf" )[i , ]
397
404
dstr(" WeightedDiscrete" , x = as.numeric(names(pdf )), pdf = pdf ,
398
405
decorators = md $ decorators )
0 commit comments