Skip to content

Commit

Permalink
0.4.3 in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Jan 15, 2025
1 parent 6288a00 commit 3bf42f7
Show file tree
Hide file tree
Showing 9 changed files with 735 additions and 8 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# cpp11armadillo 0.4.3

* Expanded documentation.
* Added a new vignette "Functions of vectors, matrices, and cubes".
* Added a new vignettes:
* Functions of vectors, matrices, and cubes.
* Statistics and clustering.
* New package template.
* Provides `as_mat()` and `as_col()` as wrappers for `as_Mat()` and `as_Col()`.

# cpp11armadillo 0.4.2

Expand Down
32 changes: 32 additions & 0 deletions cpp11armadillotest/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,38 @@ trig1_ <- function(n) {
.Call(`_cpp11armadillotest_trig1_`, n)
}

mean1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_mean1_`, X, Y)
}

median1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_median1_`, X, Y)
}

stddev1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_stddev1_`, X, Y)
}

var1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_var1_`, X, Y)
}

range1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_range1_`, X, Y)
}

cov1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_cov1_`, X, Y)
}

cor1_ <- function(X, Y) {
.Call(`_cpp11armadillotest_cor1_`, X, Y)
}

hist1_ <- function(n) {
.Call(`_cpp11armadillotest_hist1_`, n)
}

ols_ <- function(x, y) {
.Call(`_cpp11armadillotest_ols_`, x, y)
}
Expand Down
136 changes: 132 additions & 4 deletions cpp11armadillotest/src/08_official_documentation_adapted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,13 +2119,16 @@
return res;
}

// TODO: check later why vec + cx_vec reurns an error
[[cpp11::register]] list roots1_(const int& n) {
// y = p_1*x^n + p_2*x^(n-1) + ... + p_(n-1)*x + p_n
// p_1, ..., p_n are random numbers
mat A(n, 1, fill::randu);
cx_mat B = roots(A);
list res = as_complex_matrix(B);
vec y(n, 1, fill::randu);

// note that mat and cx_mat operate directly
// but vec and cx_vec require conv_to<...>::from()
cx_vec z = roots(conv_to<cx_vec>::from(y));

list res = as_complex_doubles(z);
return res;
}

Expand Down Expand Up @@ -2386,3 +2389,128 @@

return res;
}

[[cpp11::register]] list mean1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

// create a cube with 3 copies of B + random noise
cube C(B.n_rows, B.n_cols, 3);
C.slice(0) = B + 0.1 * randn<mat>(B.n_rows, B.n_cols);
C.slice(1) = B + 0.2 * randn<mat>(B.n_rows, B.n_cols);
C.slice(2) = B + 0.3 * randn<mat>(B.n_rows, B.n_cols);

vec D = mean(A).t();
vec E = mean(A, 1);
vec F = mean(mean(B, 1), 1);

writable::list res(3);
res[0] = as_doubles(D);
res[1] = as_doubles(E);
res[2] = as_doubles(F);

return res;
}

[[cpp11::register]] list median1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

vec C = median(A).t();
vec D = median(A, 1);
vec E = median(median(B, 1), 1);

writable::list res(3);
res[0] = as_doubles(C);
res[1] = as_doubles(D);
res[2] = as_doubles(E);

return res;
}

[[cpp11::register]] list stddev1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

vec C = stddev(A).t();
vec D = stddev(A, 1).t();
vec E = stddev(A, 1, 1);

writable::list res(3);
res[0] = as_doubles(C);
res[1] = as_doubles(D);
res[2] = as_doubles(E);

return res;
}

[[cpp11::register]] list var1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

vec C = var(A).t();
vec D = var(A, 1).t();
vec E = var(A, 1, 1);

writable::list res(3);
res[0] = as_doubles(C);
res[1] = as_doubles(D);
res[2] = as_doubles(E);

return res;
}

[[cpp11::register]] list range1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

vec C = range(A).t();
vec D = range(A, 1);

writable::list res(2);
res[0] = as_doubles(C);
res[1] = as_doubles(D);

return res;
}

[[cpp11::register]] list cov1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

mat C = cov(A, B);
mat D = cov(A, B, 1);

writable::list res(2);
res[0] = as_doubles_matrix(C);
res[1] = as_doubles_matrix(D);

return res;
}

[[cpp11::register]] list cor1_(const doubles_matrix<>& X, const doubles_matrix<>& Y) {
mat A = as_Mat(X);
mat B = as_Mat(Y);

mat C = cor(A, B);
mat D = cor(A, B, 1);

writable::list res(2);
res[0] = as_doubles_matrix(C);
res[1] = as_doubles_matrix(D);

return res;
}

[[cpp11::register]] list hist1_(const int& n) {
vec A = randu<vec>(n);

uvec h1 = hist(A, 11);
uvec h2 = hist(A, linspace<vec>(-2, 2, 11));

writable::list res(2);
res[0] = as_integers(h1);
res[1] = as_integers(h2);

return res;
}
64 changes: 64 additions & 0 deletions cpp11armadillotest/src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,62 @@ extern "C" SEXP _cpp11armadillotest_trig1_(SEXP n) {
return cpp11::as_sexp(trig1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list mean1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_mean1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(mean1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list median1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_median1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(median1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list stddev1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_stddev1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(stddev1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list var1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_var1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(var1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list range1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_range1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(range1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list cov1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_cov1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(cov1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list cor1_(const doubles_matrix<>& X, const doubles_matrix<>& Y);
extern "C" SEXP _cpp11armadillotest_cor1_(SEXP X, SEXP Y) {
BEGIN_CPP11
return cpp11::as_sexp(cor1_(cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(X), cpp11::as_cpp<cpp11::decay_t<const doubles_matrix<>&>>(Y)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list hist1_(const int& n);
extern "C" SEXP _cpp11armadillotest_hist1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(hist1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 09_regression.cpp
doubles ols_(const doubles_matrix<>& x, const doubles& y);
extern "C" SEXP _cpp11armadillotest_ols_(SEXP x, SEXP y) {
Expand Down Expand Up @@ -1414,6 +1470,8 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_conj1_", (DL_FUNC) &_cpp11armadillotest_conj1_, 1},
{"_cpp11armadillotest_conv_to1_", (DL_FUNC) &_cpp11armadillotest_conv_to1_, 1},
{"_cpp11armadillotest_copy_size1_", (DL_FUNC) &_cpp11armadillotest_copy_size1_, 1},
{"_cpp11armadillotest_cor1_", (DL_FUNC) &_cpp11armadillotest_cor1_, 2},
{"_cpp11armadillotest_cov1_", (DL_FUNC) &_cpp11armadillotest_cov1_, 2},
{"_cpp11armadillotest_cross1_", (DL_FUNC) &_cpp11armadillotest_cross1_, 1},
{"_cpp11armadillotest_cube1_", (DL_FUNC) &_cpp11armadillotest_cube1_, 2},
{"_cpp11armadillotest_cumprod1_", (DL_FUNC) &_cpp11armadillotest_cumprod1_, 1},
Expand Down Expand Up @@ -1450,6 +1508,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_for_each1_", (DL_FUNC) &_cpp11armadillotest_for_each1_, 1},
{"_cpp11armadillotest_has_inf1_", (DL_FUNC) &_cpp11armadillotest_has_inf1_, 1},
{"_cpp11armadillotest_has_nan1_", (DL_FUNC) &_cpp11armadillotest_has_nan1_, 1},
{"_cpp11armadillotest_hist1_", (DL_FUNC) &_cpp11armadillotest_hist1_, 1},
{"_cpp11armadillotest_imag1_", (DL_FUNC) &_cpp11armadillotest_imag1_, 1},
{"_cpp11armadillotest_imbue1_", (DL_FUNC) &_cpp11armadillotest_imbue1_, 1},
{"_cpp11armadillotest_imbue2_", (DL_FUNC) &_cpp11armadillotest_imbue2_, 1},
Expand Down Expand Up @@ -1493,6 +1552,8 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_matrix2_", (DL_FUNC) &_cpp11armadillotest_matrix2_, 1},
{"_cpp11armadillotest_max1_", (DL_FUNC) &_cpp11armadillotest_max1_, 1},
{"_cpp11armadillotest_maxmin1_", (DL_FUNC) &_cpp11armadillotest_maxmin1_, 1},
{"_cpp11armadillotest_mean1_", (DL_FUNC) &_cpp11armadillotest_mean1_, 2},
{"_cpp11armadillotest_median1_", (DL_FUNC) &_cpp11armadillotest_median1_, 2},
{"_cpp11armadillotest_memptr1_", (DL_FUNC) &_cpp11armadillotest_memptr1_, 1},
{"_cpp11armadillotest_misc1_", (DL_FUNC) &_cpp11armadillotest_misc1_, 1},
{"_cpp11armadillotest_nonzeros1_", (DL_FUNC) &_cpp11armadillotest_nonzeros1_, 1},
Expand All @@ -1519,6 +1580,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_randu1_", (DL_FUNC) &_cpp11armadillotest_randu1_, 1},
{"_cpp11armadillotest_randu2_", (DL_FUNC) &_cpp11armadillotest_randu2_, 1},
{"_cpp11armadillotest_randu3_", (DL_FUNC) &_cpp11armadillotest_randu3_, 1},
{"_cpp11armadillotest_range1_", (DL_FUNC) &_cpp11armadillotest_range1_, 2},
{"_cpp11armadillotest_rank1_", (DL_FUNC) &_cpp11armadillotest_rank1_, 1},
{"_cpp11armadillotest_rcond1_", (DL_FUNC) &_cpp11armadillotest_rcond1_, 1},
{"_cpp11armadillotest_regspace1_", (DL_FUNC) &_cpp11armadillotest_regspace1_, 1},
Expand Down Expand Up @@ -1553,6 +1615,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_sprandu1_", (DL_FUNC) &_cpp11armadillotest_sprandu1_, 1},
{"_cpp11armadillotest_sqrtmat1_", (DL_FUNC) &_cpp11armadillotest_sqrtmat1_, 1},
{"_cpp11armadillotest_sqrtmat_sympd1_", (DL_FUNC) &_cpp11armadillotest_sqrtmat_sympd1_, 1},
{"_cpp11armadillotest_stddev1_", (DL_FUNC) &_cpp11armadillotest_stddev1_, 2},
{"_cpp11armadillotest_sub2ind1_", (DL_FUNC) &_cpp11armadillotest_sub2ind1_, 1},
{"_cpp11armadillotest_subview1_", (DL_FUNC) &_cpp11armadillotest_subview1_, 1},
{"_cpp11armadillotest_subview2_", (DL_FUNC) &_cpp11armadillotest_subview2_, 1},
Expand Down Expand Up @@ -1580,6 +1643,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_typedef_SpMat_int", (DL_FUNC) &_cpp11armadillotest_typedef_SpMat_int, 1},
{"_cpp11armadillotest_typedef_uvec", (DL_FUNC) &_cpp11armadillotest_typedef_uvec, 1},
{"_cpp11armadillotest_unique1_", (DL_FUNC) &_cpp11armadillotest_unique1_, 1},
{"_cpp11armadillotest_var1_", (DL_FUNC) &_cpp11armadillotest_var1_, 2},
{"_cpp11armadillotest_vecnorm1_", (DL_FUNC) &_cpp11armadillotest_vecnorm1_, 1},
{"_cpp11armadillotest_vectorise1_", (DL_FUNC) &_cpp11armadillotest_vectorise1_, 1},
{"_cpp11armadillotest_zeros1_", (DL_FUNC) &_cpp11armadillotest_zeros1_, 1},
Expand Down
Loading

0 comments on commit 3bf42f7

Please sign in to comment.