Skip to content

Commit

Permalink
Simplify using qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
doccstat committed Apr 25, 2024
1 parent 8fac74f commit 19925f4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 71 deletions.
36 changes: 35 additions & 1 deletion src/fastcpd_class.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
#include "fastcpd_class.h"
#include "RProgress.h"

using ::arma::abs;
using ::arma::accu;
using ::arma::as_scalar;
using ::arma::diff;
using ::arma::dot;
using ::arma::find;
using ::arma::eye;
using ::arma::floor;
using ::arma::index_max;
using ::arma::index_min;
using ::arma::join_cols;
using ::arma::join_rows;
using ::arma::join_slices;
using ::arma::linspace;
using ::arma::max;
using ::arma::mean;
using ::arma::min;
using ::arma::norm;
using ::arma::sign;
using ::arma::solve;
using ::arma::sort;
using ::arma::square;
using ::arma::unique;
using ::arma::zeros;
using ::Rcpp::as;
using ::Rcpp::checkUserInterrupt;
using ::Rcpp::Named;
using ::Rcpp::wrap;

using ::arma::vec;
using ::Rcpp::Environment;
using ::Rcpp::InternalFunction;

using ::Rcpp::Rcout;

#define ERROR(msg) \
Rcout << "error: " << __FILE__ << ":" << __LINE__ << ": " << msg << std::endl

Expand Down Expand Up @@ -187,7 +222,6 @@ List Fastcpd::run() {
update_hessian(pruned_left);
}

// Objective function F(t).
fvec(t) = min_obj;

checkUserInterrupt();
Expand Down
8 changes: 6 additions & 2 deletions src/fastcpd_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#include <memory>

#include "fastcpd_test.h"
#include "fastcpd_types.h"
#include "fastcpd_wrapper.h"
#include "RcppClock.h"
#include "RProgress.h"

using ::arma::cube;
using ::arma::ucolvec;
using ::Rcpp::Function;
using ::Rcpp::List;
using ::std::string;

using ::fastcpd::test::FastcpdTest;

namespace fastcpd::classes {
Expand Down
44 changes: 29 additions & 15 deletions src/fastcpd_class_nll.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#include "fastcpd_class.h"

using ::arma::as_scalar;
using ::arma::eye;
using ::arma::log_det_sympd;
using ::arma::ones;
using ::arma::reverse;
using ::arma::trace;
using ::arma::zeros;
using ::Rcpp::as;
using ::Rcpp::Named;

using ::Rcpp::Environment;
using ::Rcpp::NumericVector;
using ::Rcpp::S4;

namespace fastcpd::classes {

colvec Fastcpd::get_gradient_arma(
Expand Down Expand Up @@ -362,7 +376,7 @@ CostResult Fastcpd::get_nll_pelt_arma(
par.rows(0, sum(order) - 1) = as<colvec>(out["coef"]);
par(sum(order)) = as<double>(out["sigma2"]);

return {{par}, {as<vec>(out["residuals"])}, -as<double>(out["loglik"])};
return {{par}, {as<colvec>(out["residuals"])}, -as<double>(out["loglik"])};
}

CostResult Fastcpd::get_nll_pelt_custom(
Expand Down Expand Up @@ -391,7 +405,7 @@ CostResult Fastcpd::get_nll_pelt_glm(
const Nullable<colvec>& start
) {
const mat data_segment = data.rows(segment_start, segment_end);
vec y = data_segment.col(0);
colvec y = data_segment.col(0);
Environment fastglm = Environment::namespace_env("fastglm");
Function fastglm_ = fastglm["fastglm"];
List out;
Expand All @@ -403,8 +417,8 @@ CostResult Fastcpd::get_nll_pelt_glm(
mat x = data_segment.cols(1, data_segment.n_cols - 1);
out = fastglm_(x, y, family, Named("start") = start_);
}
vec par = as<vec>(out["coefficients"]);
vec residuals = as<vec>(out["residuals"]);
colvec par = as<colvec>(out["coefficients"]);
colvec residuals = as<colvec>(out["residuals"]);
double value = out["deviance"];
return {{par}, {residuals}, value / 2};
}
Expand Down Expand Up @@ -436,9 +450,9 @@ CostResult Fastcpd::get_nll_pelt_lasso(
Named("type") = "coefficients",
Named("exact") = false
);
vec glmnet_i = as<vec>(out_coef.slot("i"));
vec glmnet_x = as<vec>(out_coef.slot("x"));
vec par = zeros(data_segment.n_cols - 1);
colvec glmnet_i = as<colvec>(out_coef.slot("i"));
colvec glmnet_x = as<colvec>(out_coef.slot("x"));
colvec par = zeros(data_segment.n_cols - 1);
for (unsigned int i = 1; i < glmnet_i.n_elem; i++) {
par(glmnet_i(i) - 1) = glmnet_x(i);
}
Expand All @@ -454,19 +468,19 @@ CostResult Fastcpd::get_nll_pelt_lasso(
Named("family") = "gaussian", Named("lambda") = lambda
);
S4 out_par = out["beta"];
vec par_i = as<vec>(out_par.slot("i"));
vec par_x = as<vec>(out_par.slot("x"));
vec par = zeros(data_segment.n_cols - 1);
colvec par_i = as<colvec>(out_par.slot("i"));
colvec par_x = as<colvec>(out_par.slot("x"));
colvec par = zeros(data_segment.n_cols - 1);
for (unsigned int i = 0; i < par_i.n_elem; i++) {
par(par_i(i)) = par_x(i);
}
double value = as<double>(deviance(out));
vec fitted_values = as<vec>(
colvec fitted_values = as<colvec>(
predict_glmnet(
out, data_segment.cols(1, data_segment.n_cols - 1), Named("s") = lambda
)
);
vec residuals = data_segment.col(0) - fitted_values;
colvec residuals = data_segment.col(0) - fitted_values;
return {{par}, {residuals}, value / 2};
}
}
Expand Down Expand Up @@ -638,7 +652,7 @@ double Fastcpd::get_nll_sen_binomial(
double lambda
) {
mat data_segment = data.rows(segment_start, segment_end);
vec y = data_segment.col(0);
colvec y = data_segment.col(0);
// Calculate negative log likelihood in binomial family
mat x = data_segment.cols(1, data_segment.n_cols - 1);
colvec u = x * theta;
Expand All @@ -663,7 +677,7 @@ double Fastcpd::get_nll_sen_lm(
double lambda
) {
mat data_segment = data.rows(segment_start, segment_end);
vec y = data_segment.col(0);
colvec y = data_segment.col(0);
// Calculate negative log likelihood in gaussian family
double penalty = lambda * accu(abs(theta));
mat x = data_segment.cols(1, data_segment.n_cols - 1);
Expand Down Expand Up @@ -700,7 +714,7 @@ double Fastcpd::get_nll_sen_poisson(
double lambda
) {
mat data_segment = data.rows(segment_start, segment_end);
vec y = data_segment.col(0);
colvec y = data_segment.col(0);
mat x = data_segment.cols(1, data_segment.n_cols - 1);
colvec u = x * theta;
colvec y_factorial(y.n_elem);
Expand Down
2 changes: 1 addition & 1 deletion src/fastcpd_test.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef FASTCPD_TEST_H_
#define FASTCPD_TEST_H_

#include "fastcpd_types.h"
#include "fastcpd_wrapper.h"

using ::fastcpd::classes::CostResult;
using ::Rcpp::Nullable;

namespace fastcpd::test {

Expand Down
51 changes: 0 additions & 51 deletions src/fastcpd_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,12 @@

#include "RcppArmadillo.h"

using ::arma::abs;
using ::arma::accu;
using ::arma::approx_equal;
using ::arma::as_scalar;
using ::arma::clamp;
using ::arma::cov;
using ::arma::diff;
using ::arma::dot;
using ::arma::find;
using ::arma::eye;
using ::arma::floor;
using ::arma::index_max;
using ::arma::index_min;
using ::arma::join_cols;
using ::arma::join_rows;
using ::arma::join_slices;
using ::arma::linspace;
using ::arma::log_det_sympd;
using ::arma::max;
using ::arma::mean;
using ::arma::min;
using ::arma::norm;
using ::arma::ones;
using ::arma::reverse;
using ::arma::sign;
using ::arma::solve;
using ::arma::sort;
using ::arma::square;
using ::arma::trace;
using ::arma::unique;
using ::arma::zeros;
using ::Rcpp::as;
using ::Rcpp::checkUserInterrupt;
using ::Rcpp::Named;
using ::Rcpp::stop;
using ::Rcpp::wrap;

using ::arma::colvec;
using ::arma::conv_to;
using ::arma::cube;
using ::arma::mat;
using ::arma::rowvec;
using ::arma::span;
using ::arma::ucolvec;
using ::arma::uvec;
using ::arma::vec;
using ::Rcpp::Environment;
using ::Rcpp::Function;
using ::Rcpp::InternalFunction;
using ::Rcpp::List;
using ::Rcpp::Nullable;
using ::Rcpp::NumericMatrix;
using ::Rcpp::NumericVector;
using ::Rcpp::S4;
using ::std::function;
using ::std::string;

using ::Rcpp::Rcout;

#endif // FASTCPD_TYPES_H_
6 changes: 5 additions & 1 deletion src/fastcpd_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef FASTCPD_WRAPPER_H_
#define FASTCPD_WRAPPER_H_

#include "fastcpd_types.h"
#include "RcppArmadillo.h"

using ::arma::colvec;
using ::arma::mat;
using ::arma::rowvec;

namespace fastcpd::classes {

Expand Down
3 changes: 3 additions & 0 deletions src/test-fastcpd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "fastcpd_test_constants.h"
#include "testthat.h"

using ::arma::approx_equal;
using ::arma::ones;

using ::fastcpd::classes::CostResult;
using ::fastcpd::test::FastcpdTest;

Expand Down

0 comments on commit 19925f4

Please sign in to comment.