Skip to content

Commit c47d9c2

Browse files
committed
refactor: Simplify inheritance for unary/binary ops.
Custom modules need to be refactored.
1 parent 6aba8bb commit c47d9c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+887
-849
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Matthias Krippner
3+
Copyright (c) 2024-2025 Matthias Krippner
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

include/AutoDiff/src/Basic/ops/ArcCos.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,27 +9,27 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class ArcCos : public UnaryOperation<ArcCos<X>, X> {
12+
class ArcCos : public Expression<ArcCos<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<ArcCos<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::acos(Base::xValue());
19+
return std::acos(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
auto const& x = Base::xValue();
25-
return -std::pow(1 - x * x, -0.5) * Base::xPushForward();
24+
auto const& x = Op::xValue();
25+
return -std::pow(1 - x * x, -0.5) * Op::xPushForward();
2626
}
2727

2828
template <typename Derivative>
2929
void _pullBackImpl(Derivative const& derivative)
3030
{
31-
auto const& x = Base::xValue();
32-
Base::xPullBack(derivative * (-std::pow(1 - x * x, -0.5)));
31+
auto const& x = Op::xValue();
32+
Op::xPullBack(derivative * (-std::pow(1 - x * x, -0.5)));
3333
}
3434
};
3535

include/AutoDiff/src/Basic/ops/ArcCot.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,27 +9,27 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class ArcCot : public UnaryOperation<ArcCot<X>, X> {
12+
class ArcCot : public Expression<ArcCot<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<ArcCot<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::atan(1 / Base::xValue());
19+
return std::atan(1 / Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
auto const& x = Base::xValue();
25-
return -1 / (1 + x * x) * Base::xPushForward();
24+
auto const& x = Op::xValue();
25+
return -1 / (1 + x * x) * Op::xPushForward();
2626
}
2727

2828
template <typename Derivative>
2929
void _pullBackImpl(Derivative const& derivative)
3030
{
31-
auto const& x = Base::xValue();
32-
Base::xPullBack(derivative / -(1 + x * x));
31+
auto const& x = Op::xValue();
32+
Op::xPullBack(derivative / -(1 + x * x));
3333
}
3434
};
3535

include/AutoDiff/src/Basic/ops/ArcSin.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,27 +9,27 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class ArcSin : public UnaryOperation<ArcSin<X>, X> {
12+
class ArcSin : public Expression<ArcSin<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<ArcSin<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::asin(Base::xValue());
19+
return std::asin(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
auto const& x = Base::xValue();
25-
return std::pow(1 - x * x, -0.5) * Base::xPushForward();
24+
auto const& x = Op::xValue();
25+
return std::pow(1 - x * x, -0.5) * Op::xPushForward();
2626
}
2727

2828
template <typename Derivative>
2929
void _pullBackImpl(Derivative const& derivative)
3030
{
31-
auto const& x = Base::xValue();
32-
Base::xPullBack(derivative * std::pow(1 - x * x, -0.5));
31+
auto const& x = Op::xValue();
32+
Op::xPullBack(derivative * std::pow(1 - x * x, -0.5));
3333
}
3434
};
3535

include/AutoDiff/src/Basic/ops/ArcTan.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,27 +9,27 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class ArcTan : public UnaryOperation<ArcTan<X>, X> {
12+
class ArcTan : public Expression<ArcTan<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<ArcTan<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::atan(Base::xValue());
19+
return std::atan(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
auto const& x = Base::xValue();
25-
return 1 / (1 + x * x) * Base::xPushForward();
24+
auto const& x = Op::xValue();
25+
return 1 / (1 + x * x) * Op::xPushForward();
2626
}
2727

2828
template <typename Derivative>
2929
void _pullBackImpl(Derivative const& derivative)
3030
{
31-
auto const& x = Base::xValue();
32-
Base::xPullBack(derivative / (1 + x * x));
31+
auto const& x = Op::xValue();
32+
Op::xPullBack(derivative / (1 + x * x));
3333
}
3434
};
3535

include/AutoDiff/src/Basic/ops/Cos.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,25 +9,25 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class Cos : public UnaryOperation<Cos<X>, X> {
12+
class Cos : public Expression<Cos<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<Cos<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::cos(Base::xValue());
19+
return std::cos(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
return -std::sin(Base::xValue()) * Base::xPushForward();
24+
return -std::sin(Op::xValue()) * Op::xPushForward();
2525
}
2626

2727
template <typename Derivative>
2828
void _pullBackImpl(Derivative const& derivative)
2929
{
30-
Base::xPullBack(derivative * (-std::sin(Base::xValue())));
30+
Op::xPullBack(derivative * (-std::sin(Op::xValue())));
3131
}
3232
};
3333

include/AutoDiff/src/Basic/ops/Cosh.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,25 +9,25 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class Cosh : public UnaryOperation<Cosh<X>, X> {
12+
class Cosh : public Expression<Cosh<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<Cosh<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::cosh(Base::xValue());
19+
return std::cosh(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
return std::sinh(Base::xValue()) * Base::xPushForward();
24+
return std::sinh(Op::xValue()) * Op::xPushForward();
2525
}
2626

2727
template <typename Derivative>
2828
void _pullBackImpl(Derivative const& derivative)
2929
{
30-
Base::xPullBack(derivative * std::sinh(Base::xValue()));
30+
Op::xPullBack(derivative * std::sinh(Op::xValue()));
3131
}
3232
};
3333

include/AutoDiff/src/Basic/ops/Cot.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,27 +9,27 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class Cot : public UnaryOperation<Cot<X>, X> {
12+
class Cot : public Expression<Cot<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<Cot<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return 1 / std::tan(Base::xValue());
19+
return 1 / std::tan(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
auto const& tan_x = std::tan(Base::xValue());
25-
return (-1 - 1 / (tan_x * tan_x)) * Base::xPushForward();
24+
auto const& tan_x = std::tan(Op::xValue());
25+
return (-1 - 1 / (tan_x * tan_x)) * Op::xPushForward();
2626
}
2727

2828
template <typename Derivative>
2929
void _pullBackImpl(Derivative const& derivative)
3030
{
31-
auto const& tan_x = std::tan(Base::xValue());
32-
Base::xPullBack(derivative * (-1 - 1 / (tan_x * tan_x)));
31+
auto const& tan_x = std::tan(Op::xValue());
32+
Op::xPullBack(derivative * (-1 - 1 / (tan_x * tan_x)));
3333
}
3434
};
3535

include/AutoDiff/src/Basic/ops/Difference.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,35 +9,36 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X, typename Y>
12-
class Difference : public BinaryOperation<Difference<X, Y>, X, Y> {
12+
class Difference : public Expression<Difference<X, Y>>,
13+
public BinaryOperation<X, Y> {
1314
public:
14-
using Base = BinaryOperation<Difference<X, Y>, X, Y>;
15-
using Base::Base;
15+
using Op = BinaryOperation<X, Y>;
16+
using Op::Op;
1617

1718
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1819
{
19-
return Base::xValue() - Base::yValue();
20+
return Op::xValue() - Op::yValue();
2021
}
2122

2223
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2324
{
24-
if constexpr (!Base::hasOperandX) {
25-
return -Base::yPushForward();
26-
} else if constexpr (!Base::hasOperandY) {
27-
return Base::xPushForward();
25+
if constexpr (!Op::hasOperandX) {
26+
return -Op::yPushForward();
27+
} else if constexpr (!Op::hasOperandY) {
28+
return Op::xPushForward();
2829
} else {
29-
return Base::xPushForward() - Base::yPushForward();
30+
return Op::xPushForward() - Op::yPushForward();
3031
}
3132
}
3233

3334
template <typename Derivative>
3435
void _pullBackImpl(Derivative const& derivative)
3536
{
36-
if constexpr (Base::hasOperandX) {
37-
Base::xPullBack(derivative);
37+
if constexpr (Op::hasOperandX) {
38+
Op::xPullBack(derivative);
3839
}
39-
if constexpr (Base::hasOperandY) {
40-
Base::yPullBack(-derivative);
40+
if constexpr (Op::hasOperandY) {
41+
Op::yPullBack(-derivative);
4142
}
4243
}
4344
};

include/AutoDiff/src/Basic/ops/Exp.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Matthias Krippner
1+
// Copyright (c) 2024-2025 Matthias Krippner
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -9,25 +9,25 @@
99
namespace AutoDiff::Basic {
1010

1111
template <typename X>
12-
class Exp : public UnaryOperation<Exp<X>, X> {
12+
class Exp : public Expression<Exp<X>>, public UnaryOperation<X> {
1313
public:
14-
using Base = UnaryOperation<Exp<X>, X>;
15-
using Base::Base;
14+
using Op = UnaryOperation<X>;
15+
using Op::Op;
1616

1717
[[nodiscard]] auto _valueImpl() -> decltype(auto)
1818
{
19-
return std::exp(Base::xValue());
19+
return std::exp(Op::xValue());
2020
}
2121

2222
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
2323
{
24-
return std::exp(Base::xValue()) * Base::xPushForward();
24+
return std::exp(Op::xValue()) * Op::xPushForward();
2525
}
2626

2727
template <typename Derivative>
2828
void _pullBackImpl(Derivative const& derivative)
2929
{
30-
Base::xPullBack(derivative * std::exp(Base::xValue()));
30+
Op::xPullBack(derivative * std::exp(Op::xValue()));
3131
}
3232
};
3333

0 commit comments

Comments
 (0)