Skip to content

Commit

Permalink
Rewrite the parameters of the whole package for performance gain
Browse files Browse the repository at this point in the history
  • Loading branch information
doccstat committed Apr 3, 2024
1 parent 54be621 commit 024c4ad
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 351 deletions.
60 changes: 41 additions & 19 deletions src/fastcpd_class_cost.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,55 @@ ColMat::operator rowvec() const {
return data.as_col().t();
}

CostFunction::CostFunction(Function cost) : cost(cost) {}

CostResult CostFunction::operator()( // # nocov
mat data,
Nullable<colvec> theta,
double lambda, // UNUSED
bool cv, // UNUSED
Nullable<colvec> start // UNUSED
) {
CostFunction::CostFunction(
const Function& cost,
const mat& data
) : cost(cost),
data(data) {}

CostResult CostFunction::operator() ( // # nocov
const unsigned int segment_start,
const unsigned int segment_end,
const Nullable<colvec>& theta,
const double lambda, // UNUSED
const bool cv, // UNUSED
const Nullable<colvec>& start // UNUSED
) const {
DEBUG_RCOUT(data.n_rows);
SEXP value =
theta.isNull() ? cost(data) : cost(data, as<colvec>(theta)); // # nocov
SEXP value = theta.isNull() ?
cost(data.rows(segment_start, segment_end)) :
cost(data.rows(segment_start, segment_end), as<colvec>(theta)); // # nocov
return {{colvec()}, {colvec()}, as<double>(value)}; // # nocov
}

CostGradient::CostGradient(Function cost_gradient) :
cost_gradient(cost_gradient) {}
CostGradient::CostGradient(
const Function& cost_gradient,
const mat& data
) : cost_gradient(cost_gradient),
data(data) {}

colvec CostGradient::operator()(mat data, colvec theta) {
return as<colvec>(cost_gradient(data, theta));
const colvec CostGradient::operator() (
const unsigned int segment_start,
const unsigned int segment_end,
const colvec& theta
) const {
return as<colvec>(
cost_gradient(data.rows(segment_start, segment_end), theta)
);
}

CostHessian::CostHessian(Function cost_hessian) :
cost_hessian(cost_hessian) {}
CostHessian::CostHessian(
const Function& cost_hessian,
const mat& data
) : cost_hessian(cost_hessian),
data(data) {}

mat CostHessian::operator()(mat data, colvec theta) {
return as<mat>(cost_hessian(data, theta));
const mat CostHessian::operator() (
const unsigned int segment_start,
const unsigned int segment_end,
const colvec& theta
) const {
return as<mat>(cost_hessian(data.rows(segment_start, segment_end), theta));
}

} // namespace fastcpd::classes
Loading

0 comments on commit 024c4ad

Please sign in to comment.