Skip to content

Commit

Permalink
First draft for lazy subsetting
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Nov 29, 2024
1 parent df7b1b5 commit 9083bb0
Show file tree
Hide file tree
Showing 20 changed files with 498 additions and 22 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
^\.docs
^\.development
^index.html
^compile_flags.txt
Empty file added .development/compiling.cpp
Empty file.
12 changes: 12 additions & 0 deletions .development/test_evaluate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#define STANDALONE_ETR
// #define DERIV_ETR
#include "../inst/include/etr.hpp"
using namespace etr;

int main() {
Vec<double> v = coca(1, 2, 3, 4);
Evaluate(v);
Evaluate(coca(1, 2, 3));
auto res = Evaluate(v + v);
print(res);
}
66 changes: 66 additions & 0 deletions .development/test_lazy_subsetting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <optional>
#include <stdexcept>
#include <type_traits>
#define STANDALONE_ETR
// #define DERIV_ETR
#include "../inst/include/etr.hpp"
using namespace etr;

// TODO:
// 1. Place SubsetClass in Vec --> done
// 2. Add tests in subset_test --> done
// a. is rvalue --> done
// b. is arithmetic value --> done
// 3. Dependent on the second test the arguments to subset_test are moved or not
// --> done
// 4. To ponder over:
// a. How to subset matrices --> overload subset function. Calculate indices
// and put them in SubsetClass
// b. Is it maybe a good idea to calculate the
// indices once and use them afterwards?

// TODO: missing subset of subset: auto five = subset_test(v, subset_test(v,
// coca(1, 3)));

// TODO: missing subset a 'subset of subset' with something
// Or subset a calculation with something

void test_subset_test_with_scalar() {
Vec<int> l = coca(1, 2, 3, 4);
int scalar = 2;
auto result1 = subset_test(l, scalar);
static_assert(std::is_same_v<decltype(result1), int>);
auto result2 = subset_test(l, 2.4);
static_assert(std::is_same_v<decltype(result2), int>);
ass<"Expected 2">(result1 == 2);
ass<"Expected 2">(result2 == 2);
auto result3 = subset_test(l, true);
ass<"Expected length 4">(result3.size() == 4);
auto result4 = subset_test(l, false);
ass<"Expected length 4">(result4.size() == 0);
try {
auto result = subset_test(l, 0);
} catch (std::exception &e) {
std::string expected =
"Error: out of boundaries --> value beyond size of vector";
ass(e.what() == expected, "Expected out of boundary error");
}
try {
auto result = subset_test(l, 0.4);
std::cout << result << std::endl;
} catch (std::exception &e) {
std::string expected =
"Error: out of boundaries --> value beyond size of vector";
ass(e.what() == expected, "Expected out of boundary error");
}
auto result5 = subset_test(l + l, 2);
ass<"Expected 4">(result5 == 4);
auto result6 = subset_test(l + 1, 2);
ass<"Expected 3">(result6 == 3);
auto result7 = subset_test(subset_test(l + 1, coca(1, 2)), 1);
ass<"Expected 2">(result7 == 2);
// auto result8 = subset_test(subset_test(l + 1, 1), 1); // Catched by static
// assert
}

int main() { test_subset_test_with_scalar(); }
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ dim_tests <- function() {
invisible(.Call(`_ast2ast_dim_tests`))
}

test_lazy_subsets <- function() {
invisible(.Call(`_ast2ast_test_lazy_subsets`))
}

test_matrices <- function() {
invisible(.Call(`_ast2ast_test_matrices`))
}
Expand Down
5 changes: 2 additions & 3 deletions compile_commands.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[
{
"directory": "/home/konrad/Documents/GitHub/RProjects/ast2ast_supplement/ast2ast",
"command": "clang++ -std=c++20 -DSTANDALONE_ETR -DDERIV_ETR ./src/BaseStore_Tests.cpp -I ./inst/include/",
"file": "./src/BaseStore_Tests.cpp"
"command": "clang++ -std=c++20 -DSTANDALONE_ETR ./.development/test_lazy_subsetting.cpp -I ./inst/include/ -I./inst/include/etr_bits/",
"file": "./.development/test_lazy_subsetting.cpp"
}
]

3 changes: 3 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-Iinst/include
-std=c++20

3 changes: 3 additions & 0 deletions inst/include/etr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ If not see: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html#SEC4
#include "etr_bits/Subsetting.hpp"
#include "etr_bits/UnaryCalculations.hpp"

#include "etr_bits/Subsetting/LazySubsetting.hpp"
#include "etr_bits/Evaluate.hpp"

#include "etr_bits/BufferVector.hpp"
#include "etr_bits/Derivatives/DerivsGetAndAssign.hpp"
typedef etr::Vec<etr::BaseType> sexp;
Expand Down
2 changes: 1 addition & 1 deletion inst/include/etr_bits/Allocation/Colon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
const Vec is an Operation
*/
#include "../BufferVector.hpp"
#include "../Core/Traits.hpp"
#include "AllocationUtils.hpp"
#include "etr_bits/Core/Traits.hpp"
#include <type_traits>

namespace etr {
Expand Down
2 changes: 1 addition & 1 deletion inst/include/etr_bits/BinaryCalculations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct BinaryOperation {
} else {
return l.im() || r.im();
}
return false; // issue: correct?
return false; // TODO: correct?
}
std::size_t nc() const {
if constexpr (std::is_arithmetic_v<L>) {
Expand Down
2 changes: 0 additions & 2 deletions inst/include/etr_bits/Core/Concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,10 @@ concept IsVarPointer = requires {
};

template <typename T> struct ExtractDType;

template <typename T, typename R, typename Trait>
struct ExtractDType<Vec<T, R, Trait>> {
using type = R;
};

template <typename T> using ExtractedDType = typename ExtractDType<T>::type;

template <typename T>
Expand Down
1 change: 1 addition & 0 deletions inst/include/etr_bits/Core/Header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <math.h>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <type_traits>
#include <unordered_map>
Expand Down
18 changes: 10 additions & 8 deletions inst/include/etr_bits/Core/Traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ struct TimesDerivTrait {
}

template <typename L, typename R, typename LDeriv, typename RDeriv>
static inline typename std::common_type<L, R>::type fDeriv(L l, R r, LDeriv ld,
RDeriv rd) {
static inline typename std::common_type<L, R>::type
fDeriv(L l, R r, LDeriv ld, RDeriv rd) {
return ld * r + rd * l;
}
};
Expand All @@ -95,8 +95,8 @@ struct DivideDerivTrait {
}

template <typename L, typename R, typename LDeriv, typename RDeriv>
static inline typename std::common_type<L, R>::type fDeriv(L l, R r, LDeriv ld,
RDeriv rd) {
static inline typename std::common_type<L, R>::type
fDeriv(L l, R r, LDeriv ld, RDeriv rd) {
return (ld * r - l * rd) /
std::pow(rd, 2); // TODO: add check for integral ...
}
Expand All @@ -120,8 +120,8 @@ struct DivideByConstantDerivTrait {
}

template <typename L, typename R, typename LDeriv, typename RDeriv>
static inline typename std::common_type<L, R>::type fDeriv(L l, R r, LDeriv ld,
RDeriv rd) {
static inline typename std::common_type<L, R>::type
fDeriv(L l, R r, LDeriv ld, RDeriv rd) {
return ld / r;
}
};
Expand All @@ -133,8 +133,8 @@ struct PowDerivTrait {
}

template <typename L, typename R, typename LDeriv, typename RDeriv>
static inline typename std::common_type<L, R>::type fDeriv(L l, R r, LDeriv ld,
RDeriv rd) {
static inline typename std::common_type<L, R>::type
fDeriv(L l, R r, LDeriv ld, RDeriv rd) {
return r * std::pow(l, r - 1) * ld;
}
};
Expand Down Expand Up @@ -232,6 +232,8 @@ struct SinusDerivTrait {
template <typename L> static inline L fDeriv(L l) { return cos(l); }
};

struct SubsetClassTrait {};

struct PlusTrait {
template <typename L = BaseType, typename R = BaseType>
static inline typename std::common_type<L, R>::type f(L l, R r) {
Expand Down
33 changes: 33 additions & 0 deletions inst/include/etr_bits/Evaluate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "BufferVector.hpp"
#include "Core.hpp"
#include <cstddef>

#ifndef EVALUATE_HPP
#define EVALUATE_HPP


namespace etr {
// NOTE: evaluates a Subset or a calculation and returns the result
// If something else is passed as argument it is forwarded

template<typename T>
inline auto Evaluate(T && obj) {
using CaseTrait =
typename std::remove_reference<decltype(obj.d)>::type::CaseTrait;
if constexpr(std::is_same_v<CaseTrait, UnaryTrait> ||
std::is_same_v<CaseTrait, BinaryTrait> ||
std::is_same_v<CaseTrait, SubsetClassTrait>) {
using RetType = typename std::remove_reference<decltype(obj)>::type::RetType;
Vec<RetType, Buffer<RetType, BufferTrait>, VectorTrait> res(SI{obj.size()});
for (size_t i = 0; i < res.size(); i++) {
res[i] = obj.d[i];
}
return res;
} else {
return std::forward<decltype(obj)>(obj);
}
}

}

#endif
Loading

0 comments on commit 9083bb0

Please sign in to comment.