Skip to content

Commit

Permalink
merged scalar operator=s
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Jun 3, 2024
1 parent 4b83a61 commit 4086ffc
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 16 deletions.
120 changes: 120 additions & 0 deletions include/etr_bits/Core/Traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ struct PlusDerivTrait {
}
};

struct MinusDerivTrait {
template <typename L, typename R>
static inline std::common_type<L, R>::type f(L l, R r) {
return l - r;
}

template <typename L, typename R>
static inline std::common_type<L, R>::type fDeriv(L l, R r) {
return l - r;
}
};

struct TimesDerivTrait {
template <typename L, typename R>
static inline std::common_type<L, R>::type f(L l, R r) {
Expand All @@ -65,8 +77,116 @@ struct TimesDerivTrait {
}
};

struct DivideDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static inline auto f(L l, R r) {
if constexpr (std::is_integral_v<L> && std::is_integral_v<R>) {
return static_cast<BaseType>(static_cast<BaseType>(l) /
static_cast<BaseType>(r));
} else if constexpr (std::is_integral_v<L> && !std::is_integral_v<R>) {
return static_cast<BaseType>(l) / r;
} else if constexpr (!std::is_integral_v<L> && std::is_integral_v<R>) {
return l / static_cast<BaseType>(r);
} else {
static_assert(std::is_arithmetic_v<L> && std::is_arithmetic_v<R>,
"Type not supported by operation /");
return l / r;
}
}

template <typename L, typename R, typename LDeriv, typename RDeriv>
static inline 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 ...
}
};

struct PowDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static inline std::common_type<L, R>::type f(L l, R r) {
return std::pow(l, r);
}

template <typename L, typename R, typename LDeriv, typename RDeriv>
static inline std::common_type<L, R>::type fDeriv(L l, R r, LDeriv ld,
RDeriv rd) {
return r * std::pow(l, r - 1) * ld;
}
};

struct EqualDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static inline bool
f(L a,
R b) { // issue: add this to documentationion for package authors
if (fabs(a - b) < 1E-3) {
return true;
} else {
return false;
}
}

static inline bool fDeriv() { return false; }
};
struct SmallerDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static bool f(L a, R b) {
if (a < b) {
return true;
} else {
return false;
}
}
static inline bool fDeriv() { return false; }
};
struct SmallerEqualDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static bool f(L a, R b) {
if (a <= b) {
return true;
} else {
return false;
}
}
static inline bool fDeriv() { return false; }
};
struct LargerDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static bool f(L a, R b) {
if (a > b) {
return true;
} else {
return false;
}
}
static inline bool fDeriv() { return false; }
};
struct LargerEqualDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static bool f(L a, R b) {
if (a >= b) {
return true;
} else {
return false;
}
}
};
struct UnEqualDerivTrait {
template <typename L = BaseType, typename R = BaseType>
static bool f(L a, R b) {
if (fabs(a - b) > 1E-3) {
return true;
} else {
return false;
}
}
static inline bool fDeriv() { return false; }
};

struct SinusDerivTrait {
template <typename L> static inline L f(L l) { return sin(l); }
template <typename L> static inline L fDeriv(L l) { return cos(l); }
};

struct PlusTrait {
Expand Down
62 changes: 46 additions & 16 deletions include/etr_bits/Vector/AssignmentOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,67 @@
#define ASSING2VEC_ETR_H

#include "VectorClass.hpp"
#include <type_traits>

template <typename TD>
requires std::is_same_v<TD, T>
requires std::is_arithmetic_v<TD>
Vec &operator=(const TD inp) {
// std::cout << "test1" << std::endl;
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = inp;
static_assert(!isRVec::value,
"Cannot assign to an r value. E.g. c(1, 2, 3) <- 1");
if constexpr (is<TD, T>) {
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = inp;
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = inp;
} else {
d.resize(1);
d[0] = inp;
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = inp;
return *this;
} else {
d.resize(1);
d[0] = inp;
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = static_cast<T>(inp);
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = static_cast<T>(inp);
} else {
d.resize(1);
d[0] = static_cast<T>(inp);
}
return *this;
}
return *this;
}

/*
template <typename TD>
requires std::is_same_v<TD, int>
Vec &operator=(const TD inp) {
// std::cout << "operator= test2" << std::endl;
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
"Cannot assign to an r value. E.g. c(1, 2, 3) <- 1");
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = static_cast<BaseType>(inp);
d[i] = static_cast<T>(inp);
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = static_cast<BaseType>(inp);
d[0] = static_cast<T>(inp);
} else if constexpr (isVarPointer::value) {
d.resize(1);
d[0] = inp;
} else {
d.resize(1);
d[0] = static_cast<BaseType>(inp);
d[0] = static_cast<T>(inp);
}
return *this;
}
Expand All @@ -52,24 +73,29 @@ Vec &operator=(const TD inp) {
// std::cout << "operator= test3" << std::endl;
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
"Cannot assign to an r value. E.g. c(1, 2, 3) <- 1");
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = static_cast<BaseType>(inp);
d[i] = static_cast<T>(inp);
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = static_cast<BaseType>(inp);
d[0] = static_cast<T>(inp);
} else {
d.resize(1);
d[0] = static_cast<BaseType>(inp);
d[0] = static_cast<T>(inp);
}
return *this;
}
*/

Vec &operator=(Vec<BaseType> &other) {
// std::cout << "test4" << std::endl;
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
"Cannot assign to an r value. E.g. c(1, 2, 3) <- 1");
if constexpr (isSubset::value) {
ass(other.size() == d.ind.size(),
"number of items to replace is not a multiple of replacement length");
Expand All @@ -94,6 +120,8 @@ Vec &operator=(const Vec<T, R, Trait> &otherVec) {
// printTAST<decltype(otherVec)>();
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
"Cannot assign to an r value. E.g. c(1, 2, 3) <- 1");
if constexpr (isBuffer::value) {
Buffer<T> temp(otherVec.size()); // issue: create Buffer<T> as attribute
for (std::size_t i = 0; i < otherVec.size(); i++)
Expand Down Expand Up @@ -139,6 +167,8 @@ Vec &operator=(const Vec<T2, R2, Trait2> &otherVec) {
// std::cout << "test6" << std::endl;
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
"Cannot assign to an r value. E.g. c(1, 2, 3) <- 1");
if constexpr (isBuffer::value) {
Buffer<T> temp(otherVec.size()); // issue: define temp as own attribute!
using RetTypeOtherVec =
Expand Down
1 change: 1 addition & 0 deletions tests/Derivatives_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ int main() {
}
// NOTE: Derivatives Test Nr.2
{
std::cout << "block Nr. 2" << std::endl;
etr::AllVars<2, 0, 0, 4> av(0, 0); // deriv with respect tp variable 1 = vp1
Vec<double, VarPointer<decltype(av), 0, 0>, VariableTypeTrait> vp1(av);
Vec<double, VarPointer<decltype(av), 1, 0>, VariableTypeTrait> vp2(av);
Expand Down

0 comments on commit 4086ffc

Please sign in to comment.