Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for 4d sums #115

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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
63 changes: 63 additions & 0 deletions inst/include/dust2/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ T reduce(Container x, const dimensions<3>& dim, T init, BinaryOp op,
return tot;
}

// 4 dimensions:
template <typename T, typename Container, typename BinaryOp>
T reduce(Container x, const dimensions<4>& dim, T init, BinaryOp op,
const idx& i, const idx& j, const idx& k, const idx& l) {
T tot = init;
for (size_t ll = l.first; ll <= l.second; ++ll) {
for (size_t kk = k.first; kk <= k.second; ++kk) {
for (size_t jj = j.first; jj <= j.second; ++jj) {
for (size_t ii = i.first; ii <= i.second; ++ii) {
const auto at = ii + jj * dim.mult[1] + kk * dim.mult[2] + ll * dim.mult[3];
tot = op(tot, x[at]);
}
}
}
}
return tot;
}

template <typename T, typename Container, typename BinaryOp>
T reduce(Container x, const dimensions<5>& dim, T init, BinaryOp op,
const idx& i, const idx& j, const idx& k, const idx& l, const idx& i5) {
static_assert("reduction over 5d arrays not yet implemented");
}

template <typename T, typename Container, typename BinaryOp>
T reduce(Container x, const dimensions<6>& dim, T init, BinaryOp op,
const idx& i, const idx& j, const idx& k, const idx& l, const idx& i5, const idx& i6) {
static_assert("reduction over 6d arrays not yet implemented");
}

template <typename T, typename Container, typename BinaryOp>
T reduce(Container x, const dimensions<7>& dim, T init, BinaryOp op,
const idx& i, const idx& j, const idx& k, const idx& l, const idx& i5, const idx& i6, const idx& i7) {
static_assert("reduction over 7d arrays not yet implemented");
}

template <typename T, typename Container, typename BinaryOp>
T reduce(Container x, const dimensions<8>& dim, T init, BinaryOp op,
const idx& i, const idx& j, const idx& k, const idx& l, const idx& i5, const idx& i6, const idx& i7, const idx& i8) {
static_assert("reduction over 8d arrays not yet implemented");
}


// Special case, sum over everything:
template <typename T, typename Container, typename BinaryOp, size_t rank>
T reduce(Container x, const dimensions<rank>& dim, T init, BinaryOp op) {
Expand Down Expand Up @@ -180,6 +223,11 @@ T sum(Container x, const dimensions<3>& dim, const idx& i, const idx& j, const i
return reduce(x, dim, static_cast<T>(0), std::plus<T>(), i, j, k);
}

template <typename T, typename Container>
T sum(Container x, const dimensions<4>& dim, const idx& i, const idx& j, const idx& k, const idx& l) {
return reduce(x, dim, static_cast<T>(0), std::plus<T>(), i, j, k, l);
}

// And products:
template <typename T, typename Container, size_t rank>
T prod(Container x, const dimensions<rank>& dim) {
Expand All @@ -201,6 +249,11 @@ T prod(Container x, const dimensions<3>& dim, const idx& i, const idx& j, const
return reduce(x, dim, static_cast<T>(1), std::multiplies<T>(), i, j, k);
}

template <typename T, typename Container>
T prod(Container x, const dimensions<4>& dim, const idx& i, const idx& j, const idx& k, const idx& l) {
return reduce(x, dim, static_cast<T>(0), std::multiplies<T>(), i, j, k, l);
}

// And min:
template <typename T, typename Container, size_t rank>
T min(Container x, const dimensions<rank>& dim) {
Expand All @@ -225,6 +278,11 @@ T min(Container x, const dimensions<3>& dim, const idx& i, const idx& j, const i
return reduce(x, dim, std::numeric_limits<T>::infinity(), min2, i, j, k);
}

template <typename T, typename Container>
T min(Container x, const dimensions<4>& dim, const idx& i, const idx& j, const idx& k, const idx& l) {
return reduce(x, dim, std::numeric_limits<T>::infinity(), min2, i, j, k, l);
}

// And max
template <typename T, typename Container, size_t rank>
T max(Container x, const dimensions<rank>& dim) {
Expand All @@ -246,5 +304,10 @@ T max(Container x, const dimensions<3>& dim, const idx& i, const idx& j, const i
return reduce(x, dim, -std::numeric_limits<T>::infinity(), max2, i, j, k);
}

template <typename T, typename Container>
T max(Container x, const dimensions<4>& dim, const idx& i, const idx& j, const idx& k, const idx& l) {
return reduce(x, dim, std::numeric_limits<T>::infinity(), max2, i, j, k, l);
}

}
}
3 changes: 3 additions & 0 deletions inst/include/dust2/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ template <typename real_type, size_t rank>
void read_real_array(cpp11::list args, const dust2::array::dimensions<rank>& dim,
real_type * dest, const char *name, bool required);
template <size_t rank>
void read_int_array(cpp11::list args, const dust2::array::dimensions<rank>& dim,
int * dest, const char *name, bool required);
template <size_t rank>
dust2::array::dimensions<rank> read_dimensions(cpp11::list args, const char * name);
template <size_t rank>
void read_dimensions(cpp11::list args, const char * name, dust2::array::dimensions<rank>& dest);
Expand Down
22 changes: 22 additions & 0 deletions inst/include/dust2/r/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,28 @@ void read_real_array(cpp11::list args, const dust2::array::dimensions<rank>& dim
}
}

template <size_t rank>
void read_int_array(cpp11::list args, const dust2::array::dimensions<rank>& dim,
int * dest, const char *name, bool required) {
cpp11::sexp value = args[name];
if (value == R_NilValue) {
if (required) {
cpp11::stop("A value is expected for '%s'", name);
}
} else {
check_dimensions<rank>(value, dim, name);
if (TYPEOF(value) == REALSXP) {
auto value_dbl = cpp11::as_cpp<cpp11::doubles>(value);
std::copy(value_dbl.begin(), value_dbl.end(), dest);
} else if (TYPEOF(value) == INTSXP) {
auto value_int = cpp11::as_cpp<cpp11::integers>(value);
std::copy(value_int.begin(), value_int.end(), dest);
} else {
cpp11::stop("'%s' must be numeric", name);
}
}
}

inline cpp11::sexp packing_to_r(const dust2::packing& packing_info) {
if (packing_info.size() == 0) {
return R_NilValue;
Expand Down
Loading