Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added nimbleModel/.DS_Store
Binary file not shown.
326 changes: 152 additions & 174 deletions nimbleModel/R/declFunBaseClass.R
Original file line number Diff line number Diff line change
@@ -1,190 +1,168 @@
#' @export
declFunBase_nClass <- nClass(
classname = "declFunBase_nClass",
Rpublic = list(
calculate = function(instr) {
calc_op(instr, "calc_one")
},
calculateDiff = function(instr) {
calc_op(instr, "calcDiff_one")
},
getLogProb = function(instr) {
calc_op(instr, "getLogProb_one")
},
calc_op = function(instr, fn) {
if(instr$type == 0) return(calc_0(instr, fn))
if(instr$type == 1) return(calc_1_seq(instr, fn))
if(instr$type == 2) return(calc_1_mat(instr, fn))
if(instr$type == 3) return(calc_1_matp(instr, fn))
return(0)
},
calc_0 = function(instr, fn) {
return(self[[fn]](0))
},
calc_1_seq =
function(instr, fn) {
logProb = 0
iStart <- instr$values[[1]][1] # Values seem to start offset by -1, a bit confusing
for(i in 1:instr$lens[1])
logProb <- logProb + self[[fn]](iStart + i)
return(logProb)
},
calc_1_mat =
function(instr, fn) {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + self[[fn]](instr$values[[1]][i])
return(logProb)
},
calc_1_matp =
function(instr, fn) {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + self[[fn]](instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
return(logProb)
},
simulate = function(instr) {
if(instr$type == 0) return(sim_0(instr))
if(instr$type == 1) return(sim_1_seq(instr))
if(instr$type == 2) return(sim_1_mat(instr))
if(instr$type == 3) return(sim_1_matp(instr))
},
sim_0 = function(instr) {
sim_one(0) ## sim_one will always has `idx` as arg?
},
sim_1_seq = function(instr) {
for(i in 1:instr$lens[1])
sim_one(instr$values[[1]][1]+i)
},
sim_1_mat = function(instr) {
for(i in 1:instr$lens[1])
sim_one(instr$values[[1]][i])
},
sim_1_matp = function(instr) {
for(i in 1:instr$lens[1])
sim_one(instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
}
),
Cpublic = list(
## model = 'modelBase_nClass',
ping = nFunction(
name = "ping",
function() {return(TRUE); returnType(logical())},
compileInfo = list(virtual=TRUE)
),
calculate_cpp = nFunction(
name = "calculate_cpp",
function(instr) {
stop("Uncompiled version of calculate_cpp should not be called.")
},
returnType = 'numericScalar',
compileInfo = list(virtual=TRUE,
C_fun = function(instr = 'instr_nClass') {
cppLiteral('Rprintf("declFunBase_nClass virtual base calculate_cpp should never be called (something is wrong)\\n");')
return(0)
})
),
calculateDiff_cpp = nFunction(
name = "calculateDiff_cpp",
function(instr) {
stop("Uncompiled version of calculateDiff_cpp should not be called.")
},
returnType = 'numericScalar',
compileInfo = list(virtual=TRUE,
C_fun = function(instr = 'instr_nClass') {
cppLiteral('Rprintf("declFunBase_nClass virtual base calculateDiff_cpp should never be called (something is wrong)\\n");')
return(0)
})
),
getLogProb_cpp = nFunction(
name = "getLogProb_cpp",
function(instr) {
stop("Uncompiled version of getLogProb_cpp should not be called.")
},
returnType = 'numericScalar',
compileInfo = list(virtual=TRUE,
C_fun = function(instr = 'instr_nClass') {
cppLiteral('Rprintf("declFunBase_nClass virtual base getLogProb_cpp should never be called (something is wrong)\\n");')
return(0)
})
),
simulate_cpp = nFunction(
name = "simulate_cpp",
function(instr) {
stop("Uncompiled version of simulate_cpp should not be called.")
},
returnType = 'void',
compileInfo = list(virtual=TRUE,
C_fun = function(instr = 'instr_nClass') {
cppLiteral('Rprintf("declFunBase_nClass virtual base simulate_cpp should never be called (something is wrong)\\n");')
})
)



calculate = nFunction(
name = "calculate",
fun = function(instr = 'instr_nClass') {
## TODO: how embed determination of vec and parallel cases here?
if(instr$type == 0) return(calc_0(instr))
if(instr$type == 1) return(calc_1_seq(instr))
if(instr$type == 2) return(calc_1_mat(instr))
if(instr$type == 3) return(calc_1_matp(instr))
return(0) ## Need to error trap/warn if unhandled type requested
}, returnType = 'numericScalar',
compileInfo = list(virtual=TRUE)
),
calc_0 = nFunction(
name = 'calc_0',
function(instr = 'instr_nClass') {
return(calc_one(0)) ## calc_one will always has `idx` as arg?
}, returnType = 'numericScalar'
),
calc_1_seq = nFunction(
name = 'calc_1_seq',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + calc_one(instr$values[[1]][1]+i)
return(logProb)
}, returnType = 'numericScalar'
),
calc_1_mat = nFunction(
name = 'calc_1_mat',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + calc_one(instr$values[[1]][i])
return(logProb)
}, returnType = 'numericScalar'
),
calc_1_matp = nFunction(
name = 'calc_1_mat',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + calc_one(instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
return(logProb)
}, returnType = 'numericScalar'
),

calculateDiff = nFunction(
name = "calculateDiff",
fun = function(instr = 'instr_nClass') {
## TODO: how embed determination of vec and parallel cases here?
if(instr$type == 0) return(calcDiff_0(instr))
if(instr$type == 1) return(calcDiff_1_seq(instr))
if(instr$type == 2) return(calcDiff_1_mat(instr))
if(instr$type == 3) return(calcDiff_1_matp(instr))
return(0) ## Need to error trap/warn if unhandled type requested
}, returnType = 'numericScalar',
compileInfo = list(virtual=TRUE)
),
calcDiff_0 = nFunction(
name = 'calcDiff_0',
function(instr = 'instr_nClass') {
return(calcDiff_one(0)) ## calcDiff_one will always has `idx` as arg?
}, returnType = 'numericScalar'
),
calcDiff_1_seq = nFunction(
name = 'calcDiff_1_seq',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + calcDiff_one(instr$values[[1]][1]+i)
return(logProb)
}, returnType = 'numericScalar'
),
calcDiff_1_mat = nFunction(
name = 'calcDiff_1_mat',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + calcDiff_one(instr$values[[1]][i])
return(logProb)
}, returnType = 'numericScalar'
),
calcDiff_1_matp = nFunction(
name = 'calcDiff_1_mat',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + calcDiff_one(instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
return(logProb)
}, returnType = 'numericScalar'
),


simulate = nFunction(
name = "simulate",
fun = function(instr = 'instr_nClass') {
## TODO: how embed determination of vec and parallel cases here?
if(instr$type == 0) sim_0(instr)
if(instr$type == 1) sim_1_seq(instr)
if(instr$type == 2) sim_1_mat(instr)
if(instr$type == 3) sim_1_matp(instr)
},
compileInfo = list(virtual=TRUE)
),
sim_0 = nFunction(
name = 'sim_0',
function(instr = 'instr_nClass') {
sim_one(0) ## sim_one will always has `idx` as arg?
}
),
sim_1_seq = nFunction(
name = 'sim_1_seq',
function(instr = 'instr_nClass') {
for(i in 1:instr$lens[1])
sim_one(instr$values[[1]][1]+i)
}
),
sim_1_mat = nFunction(
name = 'sim_1_mat',
function(instr = 'instr_nClass') {
for(i in 1:instr$lens[1])
sim_one(instr$values[[1]][i])
}
),
sim_1_matp = nFunction(
name = 'sim_1_mat',
function(instr = 'instr_nClass') {
for(i in 1:instr$lens[1])
sim_one(instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
}
),
# simulate = nFunction(
# name = "simulate",
# fun = function(instr = 'instr_nClass') {
# ## TODO: how embed determination of vec and parallel cases here?
# if(instr$type == 0) sim_0(instr)
# if(instr$type == 1) sim_1_seq(instr)
# if(instr$type == 2) sim_1_mat(instr)
# if(instr$type == 3) sim_1_matp(instr)
# },
# compileInfo = list(virtual=TRUE)
# ),
# sim_0 = nFunction(
# name = 'sim_0',
# function(instr = 'instr_nClass') {
# sim_one(0) ## sim_one will always has `idx` as arg?
# }
# ),
# sim_1_seq = nFunction(
# name = 'sim_1_seq',
# function(instr = 'instr_nClass') {
# for(i in 1:instr$lens[1])
# sim_one(instr$values[[1]][1]+i)
# }
# ),
# sim_1_mat = nFunction(
# name = 'sim_1_mat',
# function(instr = 'instr_nClass') {
# for(i in 1:instr$lens[1])
# sim_one(instr$values[[1]][i])
# }
# ),
# sim_1_matp = nFunction(
# name = 'sim_1_mat',
# function(instr = 'instr_nClass') {
# for(i in 1:instr$lens[1])
# sim_one(instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
# }
# ),

getLogProb = nFunction(
name = "getLogProb",
fun = function(instr = 'instr_nClass') {
## TODO: how embed determination of vec and parallel cases here?
if(instr$type == 0) return(getLogProb_0(instr))
if(instr$type == 1) return(getLogProb_1_seq(instr))
if(instr$type == 2) return(getLogProb_1_mat(instr))
if(instr$type == 3) return(getLogProb_1_matp(instr))
return(0) ## Need to error trap/warn if unhandled type requested
}, returnType = 'numericScalar',
compileInfo = list(virtual=TRUE)
),
getLogProb_0 = nFunction(
name = 'getLogProb_0',
function(instr = 'instr_nClass') {
return(getLogProb_one(0)) ## getLogProb_one will always has `idx` as arg?
}, returnType = 'numericScalar'
),
getLogProb_1_seq = nFunction(
name = 'getLogProb_1_seq',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + getLogProb_one(instr$values[[1]][1]+i)
return(logProb)
}, returnType = 'numericScalar'
),
getLogProb_1_mat = nFunction(
name = 'getLogProb_1_mat',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + getLogProb_one(instr$values[[1]][i])
return(logProb)
}, returnType = 'numericScalar'
),
getLogProb_1_matp = nFunction(
name = 'getLogProb_1_mat',
function(instr = 'instr_nClass') {
logProb = 0
for(i in 1:instr$lens[1])
logProb <- logProb + getLogProb_one(instr$values[[1]][(instr$dims[1]*(i-1) + 1):(instr$dims[1]*i)]) ## Ok to call with a vector?
return(logProb)
}, returnType = 'numericScalar'
)

),
## We haven't dealt with ensuring a virtual destructor when any method is virtual
Expand Down
Loading