Skip to content

Commit

Permalink
more progress with vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Dec 25, 2024
1 parent b349e53 commit eb8a200
Show file tree
Hide file tree
Showing 5 changed files with 651 additions and 4 deletions.
44 changes: 44 additions & 0 deletions cpp11armadillotest/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,50 @@ index_min1_ <- function(n) {
.Call(`_cpp11armadillotest_index_min1_`, n)
}

inplace_trans1_ <- function(n) {
.Call(`_cpp11armadillotest_inplace_trans1_`, n)
}

inplace_strans1_ <- function(n) {
.Call(`_cpp11armadillotest_inplace_strans1_`, n)
}

intersect1_ <- function(n) {
.Call(`_cpp11armadillotest_intersect1_`, n)
}

join_rows1_ <- function(n) {
.Call(`_cpp11armadillotest_join_rows1_`, n)
}

join_cubes1_ <- function(n) {
.Call(`_cpp11armadillotest_join_cubes1_`, n)
}

kron1_ <- function(n) {
.Call(`_cpp11armadillotest_kron1_`, n)
}

log_det1_ <- function(n) {
.Call(`_cpp11armadillotest_log_det1_`, n)
}

log_det_sympd1_ <- function(n) {
.Call(`_cpp11armadillotest_log_det_sympd1_`, n)
}

logmat1_ <- function(n) {
.Call(`_cpp11armadillotest_logmat1_`, n)
}

logmat_sympd1_ <- function(n) {
.Call(`_cpp11armadillotest_logmat_sympd1_`, n)
}

max1_ <- function(n) {
.Call(`_cpp11armadillotest_max1_`, n)
}

ols_ <- function(x, y) {
.Call(`_cpp11armadillotest_ols_`, x, y)
}
Expand Down
153 changes: 153 additions & 0 deletions cpp11armadillotest/src/08_official_documentation_adapted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1790,3 +1790,156 @@
// max values in col 1 and row n
return writable::doubles({M(ii(0), 0), M(n - 1, jj(n - 1))});
}

[[cpp11::register]] doubles_matrix<> inplace_trans1_(const int& n) {
mat X(n, n, fill::randu);
inplace_trans(X);
return as_doubles_matrix(X);
}

[[cpp11::register]] list inplace_strans1_(const int& n) {
cx_mat X(n, n, fill::randu);
inplace_strans(X);
return as_complex_matrix(X);
}

[[cpp11::register]] integers intersect1_(const int& n) {
ivec A = regspace<ivec>(n, 1); // n, ..., 1
ivec B = regspace<ivec>(2, n + 1); // 2, ..., n + 1

ivec C = intersect(A, B); // 2, ..., n

return as_integers(C);
}

[[cpp11::register]] list join_rows1_(const int& n) {
mat A(n, 1, fill::randu);
mat B(n, 1, fill::randu);
mat C(n, 1, fill::randu);

mat D = join_rows(A, B, C);
mat E = join_cols(A, B, C);

return writable::list({as_doubles_matrix(A), as_doubles_matrix(B),
as_doubles_matrix(C), as_doubles_matrix(D), as_doubles_matrix(E)});
}

[[cpp11::register]] list join_cubes1_(const int& n) {
cube C(n, n + 1, 3, fill::randu);
cube D(n, n + 1, 4, fill::randu);

cube E = join_slices(C, D);

size_t m = C.n_slices + D.n_slices;

writable::list res(m);

for (size_t i = 0; i < m; ++i) {
res[i] = as_doubles_matrix(E.slice(i));
}

return res;
}

[[cpp11::register]] doubles_matrix<> kron1_(const int& n) {
mat A(n, n + 1, fill::randu);
mat B(n + 1, n, fill::randu);

mat K = kron(A, B);

return as_doubles_matrix(K);
}

[[cpp11::register]] list log_det1_(const int& n) {
mat A(n, n, fill::randu);

cx_double res1 = log_det(A); // form 1

cpp11::writable::list res2;
res2.push_back(writable::doubles({std::real(res1)}));
res2.push_back(writable::doubles({std::imag(res1)}));

double val;
double sign;
bool ok = log_det(val, sign, A); // form 2

writable::list res3(3);
res3[0] = doubles({val});
res3[1] = doubles({sign});
res3[2] = logicals({ok});

writable::list res(2);
res[0] = res2;
res[1] = res3;

return res;
}

[[cpp11::register]] list log_det_sympd1_(const int& n) {
mat A(n, n, fill::randu);
A = A * A.t(); // make A symmetric positive definite

double val = log_det_sympd(A); // form 1

double val2;
bool ok = log_det_sympd(val2, A); // form 2

writable::list res(2);
res[0] = doubles({val});

writable::list res2(2);
res2[0] = doubles({val2});
res2[1] = logicals({ok});
res[1] = res2;

return res;
}

[[cpp11::register]] list logmat1_(const int& n) {
mat A(n, n, fill::randu);
cx_mat B = logmat(A);
return as_complex_matrix(B);
}

[[cpp11::register]] list logmat_sympd1_(const int& n) {
mat A(n, n, fill::randu);
mat B = A * A.t(); // make symmetric matrix

mat C = logmat_sympd(B);

writable::list res(2);

res[0] = as_doubles_matrix(C);

mat D;
bool ok = logmat_sympd(D, B);

writable::list res2(2);
res2[0] = as_doubles_matrix(D);
res2[1] = logicals({ok});

res[1] = res2;

return res;
}

[[cpp11::register]] list max1_(const int& n) {
mat M(n, n, fill::randu);

rowvec a = max(M);
rowvec b = max(M, 0);
colvec c = max(M, 1);

// element-wise maximum
mat X(n, n, fill::randu);
mat Y(n, n, fill::randu);
mat Z = arma::max(X, Y); // use arma:: prefix to distinguish from std::max()

writable::list res(4);
res[0] = as_doubles(a.t());
res[1] = as_doubles(b.t());
res[2] = as_doubles(c);
res[3] = as_doubles_matrix(Z);

return res;
}
88 changes: 88 additions & 0 deletions cpp11armadillotest/src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,83 @@ extern "C" SEXP _cpp11armadillotest_index_min1_(SEXP n) {
return cpp11::as_sexp(index_min1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
doubles_matrix<> inplace_trans1_(const int& n);
extern "C" SEXP _cpp11armadillotest_inplace_trans1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(inplace_trans1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list inplace_strans1_(const int& n);
extern "C" SEXP _cpp11armadillotest_inplace_strans1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(inplace_strans1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
integers intersect1_(const int& n);
extern "C" SEXP _cpp11armadillotest_intersect1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(intersect1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list join_rows1_(const int& n);
extern "C" SEXP _cpp11armadillotest_join_rows1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(join_rows1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list join_cubes1_(const int& n);
extern "C" SEXP _cpp11armadillotest_join_cubes1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(join_cubes1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
doubles_matrix<> kron1_(const int& n);
extern "C" SEXP _cpp11armadillotest_kron1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(kron1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list log_det1_(const int& n);
extern "C" SEXP _cpp11armadillotest_log_det1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(log_det1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list log_det_sympd1_(const int& n);
extern "C" SEXP _cpp11armadillotest_log_det_sympd1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(log_det_sympd1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list logmat1_(const int& n);
extern "C" SEXP _cpp11armadillotest_logmat1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(logmat1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list logmat_sympd1_(const int& n);
extern "C" SEXP _cpp11armadillotest_logmat_sympd1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(logmat_sympd1_(cpp11::as_cpp<cpp11::decay_t<const int&>>(n)));
END_CPP11
}
// 08_official_documentation_adapted.cpp
list max1_(const int& n);
extern "C" SEXP _cpp11armadillotest_max1_(SEXP n) {
BEGIN_CPP11
return cpp11::as_sexp(max1_(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 @@ -1129,9 +1206,12 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_index_maxmin1_", (DL_FUNC) &_cpp11armadillotest_index_maxmin1_, 1},
{"_cpp11armadillotest_index_min1_", (DL_FUNC) &_cpp11armadillotest_index_min1_, 1},
{"_cpp11armadillotest_initialization1_", (DL_FUNC) &_cpp11armadillotest_initialization1_, 1},
{"_cpp11armadillotest_inplace_strans1_", (DL_FUNC) &_cpp11armadillotest_inplace_strans1_, 1},
{"_cpp11armadillotest_inplace_trans1_", (DL_FUNC) &_cpp11armadillotest_inplace_trans1_, 1},
{"_cpp11armadillotest_insert_columns1_", (DL_FUNC) &_cpp11armadillotest_insert_columns1_, 1},
{"_cpp11armadillotest_insert_rows1_", (DL_FUNC) &_cpp11armadillotest_insert_rows1_, 1},
{"_cpp11armadillotest_insert_slices1_", (DL_FUNC) &_cpp11armadillotest_insert_slices1_, 1},
{"_cpp11armadillotest_intersect1_", (DL_FUNC) &_cpp11armadillotest_intersect1_, 1},
{"_cpp11armadillotest_inverse1_", (DL_FUNC) &_cpp11armadillotest_inverse1_, 2},
{"_cpp11armadillotest_is_diagonal1_", (DL_FUNC) &_cpp11armadillotest_is_diagonal1_, 1},
{"_cpp11armadillotest_is_empty1_", (DL_FUNC) &_cpp11armadillotest_is_empty1_, 1},
Expand All @@ -1148,10 +1228,18 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11armadillotest_iterators2_", (DL_FUNC) &_cpp11armadillotest_iterators2_, 1},
{"_cpp11armadillotest_iterators3_", (DL_FUNC) &_cpp11armadillotest_iterators3_, 1},
{"_cpp11armadillotest_iterators4_", (DL_FUNC) &_cpp11armadillotest_iterators4_, 1},
{"_cpp11armadillotest_join_cubes1_", (DL_FUNC) &_cpp11armadillotest_join_cubes1_, 1},
{"_cpp11armadillotest_join_rows1_", (DL_FUNC) &_cpp11armadillotest_join_rows1_, 1},
{"_cpp11armadillotest_kron1_", (DL_FUNC) &_cpp11armadillotest_kron1_, 1},
{"_cpp11armadillotest_linspace1_", (DL_FUNC) &_cpp11armadillotest_linspace1_, 1},
{"_cpp11armadillotest_log_det1_", (DL_FUNC) &_cpp11armadillotest_log_det1_, 1},
{"_cpp11armadillotest_log_det_sympd1_", (DL_FUNC) &_cpp11armadillotest_log_det_sympd1_, 1},
{"_cpp11armadillotest_logmat1_", (DL_FUNC) &_cpp11armadillotest_logmat1_, 1},
{"_cpp11armadillotest_logmat_sympd1_", (DL_FUNC) &_cpp11armadillotest_logmat_sympd1_, 1},
{"_cpp11armadillotest_logspace1_", (DL_FUNC) &_cpp11armadillotest_logspace1_, 1},
{"_cpp11armadillotest_matrix1_", (DL_FUNC) &_cpp11armadillotest_matrix1_, 1},
{"_cpp11armadillotest_matrix2_", (DL_FUNC) &_cpp11armadillotest_matrix2_, 1},
{"_cpp11armadillotest_max1_", (DL_FUNC) &_cpp11armadillotest_max1_, 1},
{"_cpp11armadillotest_maxmin1_", (DL_FUNC) &_cpp11armadillotest_maxmin1_, 1},
{"_cpp11armadillotest_memptr1_", (DL_FUNC) &_cpp11armadillotest_memptr1_, 1},
{"_cpp11armadillotest_ols_", (DL_FUNC) &_cpp11armadillotest_ols_, 2},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,38 @@ test_that("examples derived from official documentation", {
res126 <- index_min1_(2)
expect_type(res126, "double")
expect_equal(length(res126), 2)

res127 <- inplace_trans1_(2)
expect_type(res127, "double")
expect_equal(dim(res127), c(2, 2))

res128 <- inplace_strans1_(2)
expect_type(res128, "list")

res129 <- intersect1_(5)
expect_type(res129, "integer")

res130 <- join_rows1_(2)
expect_type(res130, "list")

res131 <- join_cubes1_(2)
expect_type(res131, "list")

res132 <- kron1_(2)
expect_type(res132, "double")

res133 <- log_det1_(2)
expect_type(res133, "list")

res134 <- log_det_sympd1_(2)
expect_type(res134, "list")

res135 <- logmat1_(2)
expect_type(res135, "list")

res136 <- logmat_sympd1_(2)
expect_type(res136, "list")

res137 <- max1_(2)
expect_type(res137, "list")
})
Loading

0 comments on commit eb8a200

Please sign in to comment.