Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad.kraemer committed Jun 26, 2024
1 parent 51561d8 commit e733d6a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
Binary file added development/a.out
Binary file not shown.
18 changes: 12 additions & 6 deletions development/deriv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ int main() {
print(x);
print();

assign_deriv(subset_deriv(y, 2),
get_deriv(subset_deriv(x, 3)) * x(3) +
get_deriv(subset_deriv(x, 3)) * x(3));
y(2) = x(3) * x(3);
// y = x*x = 1, 9, 9
// dy/dx = get_deriv(x) * x + x * get_deriv(x) = 1, 4, 9
// dy/dx = get_deriv(y) * x + y * get_deriv(x) =
// 6 * 3 + 9 * 1 = 27
std::cout << &y.deriv<< std::endl;
auto s = subset_deriv(y, 2);
std::cout << &s.d<< std::endl;

assign_deriv(s,
get_deriv(subset_deriv(y, 3)) * x(3) +
y(3) * get_deriv(subset_deriv(x, 3)));
// y(2) = y(3) * x(3) = 1, 27
y(2) = x(3)*y(3) + 1.1;

print(get_deriv(x));
print(get_deriv(y));
print(y);
Expand Down
2 changes: 1 addition & 1 deletion development/diff.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library(ast2ast)

f <- function(x) x * x
f <- function(x) y * x

ast2ast:::d(f, x)
23 changes: 13 additions & 10 deletions inst/include/etr_bits/Derivatives/DerivsGetAndAssign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace etr {
#ifdef DERIV_ETR

template <typename T>
requires IsVec<T>
inline auto get_deriv(T &v) {
inline auto get_deriv(T&&v) {
Vec<double, Buffer<double>> ret{v.deriv};
return ret;
}
Expand All @@ -25,11 +24,10 @@ inline auto get_deriv(T &&v) {
deriv[i] = v[i];
}
Vec<double, Buffer<double>> ret{deriv};
ret.dep_var = true;
ret.dep_var = v.dep_var;
return ret;
}


template <typename T>
requires IsVec<T>
inline auto set_indep(T &v) {
Expand All @@ -41,13 +39,18 @@ inline auto set_indep(T &v) {
template <typename L, typename R> inline void assign_deriv(L &&l, const R &r) {
using DataTypeOtherVec = typename etr::ExtractDataType<
std::remove_reference_t<decltype(r)>>::RetType;
l.deriv.resize(r.size());
std::cout << &l.d << std::endl;
l.temp.resize(r.size());
for (std::size_t i = 0; i < r.size(); i++) {
// if constexpr (is<DataTypeOtherVec, double>) {
// l.deriv[i] = r[i];
// } else {
// l.deriv[i] = static_cast<double>(r[i]);
// }
if constexpr (is<DataTypeOtherVec, double>) {
l.temp[i] = r[i];
} else {
l.temp[i] = static_cast<double>(r[i]);
}
}
l.deriv.resize(r.size());
for(std::size_t i = 0; i < l.temp.size(); i++) {
l.deriv[i] = l.temp[i];
}
}

Expand Down
2 changes: 2 additions & 0 deletions inst/include/etr_bits/Vector/AssignmentOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Vec &operator=(const TD inp) {
}

Vec &operator=(const Vec<T, R, Trait> &otherVec) {

static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
Expand Down Expand Up @@ -190,6 +191,7 @@ template <typename T2, typename R2, typename Trait2>
requires(IsRVec<const Vec<T2, R2, Trait2>> && std::is_same_v<T, T2> &&
isBuffer::value)
Vec &operator=(Vec<T2, R2, Trait2> &&otherVec) {

static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
Expand Down

0 comments on commit e733d6a

Please sign in to comment.