Skip to content

Commit 89e1597

Browse files
committed
Add trigonometric and hyperbolic scalar functions
1 parent 82ec9b8 commit 89e1597

20 files changed

+497
-6
lines changed

docs/modules/basic.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ x + 3; // right-hand side literal
4444
The following operations are currently supported:
4545

4646
- `+`, `-`, `*`, `/`: Arithmetic operations.
47-
- `pow`: Power function.
48-
- `sin`: Sine function.
49-
- `cos`: Cosine function.
47+
- `sin`, `cos`, `tan`, `cot`: Trigonometric functions.
48+
- `asin`, `acos`, `atan`, `acot`: Inverse trigonometric functions.
49+
- `sinh`, `cosh`, `tanh`: Hyperbolic functions.
5050
- `exp`: Exponential function.
5151
- `log`: Natural logarithm.
52-
- `sqrt`: Square root.
52+
- `pow`: Power function.
5353
- `square`: Square function.
54-
- `min`: Minimum of a scalar expression and zero.
55-
- `max`: Maximum of a scalar expression and zero.
54+
- `sqrt`: Square root.
55+
- `min`, `max`: Minimum, maximum of a scalar expression and zero.
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_ARC_COS_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_ARC_COS_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class ArcCos : public UnaryOperation<ArcCos<X>, X> {
13+
public:
14+
using Base = UnaryOperation<ArcCos<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::acos(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
auto const& x = Base::xValue();
25+
return -std::pow(1 - x * x, -0.5) * Base::xPushForward();
26+
}
27+
28+
template <typename Derivative>
29+
void _pullBackImpl(Derivative const& derivative)
30+
{
31+
auto const& x = Base::xValue();
32+
Base::xPullBack(derivative * (-std::pow(1 - x * x, -0.5)));
33+
}
34+
};
35+
36+
} // namespace AutoDiff::Basic
37+
38+
namespace AutoDiff {
39+
40+
AUTODIFF_MAKE_BASIC_UNARY_OP(acos, Basic::ArcCos)
41+
42+
} // namespace AutoDiff
43+
44+
#endif // AUTODIFF_SRC_BASIC_OPS_ARC_COS_HPP
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_ARC_COT_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_ARC_COT_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class ArcCot : public UnaryOperation<ArcCot<X>, X> {
13+
public:
14+
using Base = UnaryOperation<ArcCot<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::atan(1 / Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
auto const& x = Base::xValue();
25+
return -1 / (1 + x * x) * Base::xPushForward();
26+
}
27+
28+
template <typename Derivative>
29+
void _pullBackImpl(Derivative const& derivative)
30+
{
31+
auto const& x = Base::xValue();
32+
Base::xPullBack(derivative / -(1 + x * x));
33+
}
34+
};
35+
36+
} // namespace AutoDiff::Basic
37+
38+
namespace AutoDiff {
39+
40+
AUTODIFF_MAKE_BASIC_UNARY_OP(acot, Basic::ArcCot)
41+
42+
} // namespace AutoDiff
43+
44+
#endif // AUTODIFF_SRC_BASIC_OPS_ARC_COT_HPP
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_ARC_SIN_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_ARC_SIN_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class ArcSin : public UnaryOperation<ArcSin<X>, X> {
13+
public:
14+
using Base = UnaryOperation<ArcSin<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::asin(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
auto const& x = Base::xValue();
25+
return std::pow(1 - x * x, -0.5) * Base::xPushForward();
26+
}
27+
28+
template <typename Derivative>
29+
void _pullBackImpl(Derivative const& derivative)
30+
{
31+
auto const& x = Base::xValue();
32+
Base::xPullBack(derivative * std::pow(1 - x * x, -0.5));
33+
}
34+
};
35+
36+
} // namespace AutoDiff::Basic
37+
38+
namespace AutoDiff {
39+
40+
AUTODIFF_MAKE_BASIC_UNARY_OP(asin, Basic::ArcSin)
41+
42+
} // namespace AutoDiff
43+
44+
#endif // AUTODIFF_SRC_BASIC_OPS_ARC_SIN_HPP
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_ARC_TAN_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_ARC_TAN_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class ArcTan : public UnaryOperation<ArcTan<X>, X> {
13+
public:
14+
using Base = UnaryOperation<ArcTan<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::atan(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
auto const& x = Base::xValue();
25+
return 1 / (1 + x * x) * Base::xPushForward();
26+
}
27+
28+
template <typename Derivative>
29+
void _pullBackImpl(Derivative const& derivative)
30+
{
31+
auto const& x = Base::xValue();
32+
Base::xPullBack(derivative / (1 + x * x));
33+
}
34+
};
35+
36+
} // namespace AutoDiff::Basic
37+
38+
namespace AutoDiff {
39+
40+
AUTODIFF_MAKE_BASIC_UNARY_OP(atan, Basic::ArcTan)
41+
42+
} // namespace AutoDiff
43+
44+
#endif // AUTODIFF_SRC_BASIC_OPS_ARC_TAN_HPP
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_COSH_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_COSH_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class Cosh : public UnaryOperation<Cosh<X>, X> {
13+
public:
14+
using Base = UnaryOperation<Cosh<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::cosh(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
return std::sinh(Base::xValue()) * Base::xPushForward();
25+
}
26+
27+
template <typename Derivative>
28+
void _pullBackImpl(Derivative const& derivative)
29+
{
30+
Base::xPullBack(derivative * std::sinh(Base::xValue()));
31+
}
32+
};
33+
34+
} // namespace AutoDiff::Basic
35+
36+
namespace AutoDiff {
37+
38+
AUTODIFF_MAKE_BASIC_UNARY_OP(cosh, Basic::Cosh)
39+
40+
} // namespace AutoDiff
41+
42+
#endif // AUTODIFF_SRC_BASIC_OPS_COSH_HPP
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_COT_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_COT_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class Cot : public UnaryOperation<Cot<X>, X> {
13+
public:
14+
using Base = UnaryOperation<Cot<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return 1 / std::tan(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
auto const& tan_x = std::tan(Base::xValue());
25+
return (-1 - 1 / (tan_x * tan_x)) * Base::xPushForward();
26+
}
27+
28+
template <typename Derivative>
29+
void _pullBackImpl(Derivative const& derivative)
30+
{
31+
auto const& tan_x = std::tan(Base::xValue());
32+
Base::xPullBack(derivative * (-1 - 1 / (tan_x * tan_x)));
33+
}
34+
};
35+
36+
} // namespace AutoDiff::Basic
37+
38+
namespace AutoDiff {
39+
40+
AUTODIFF_MAKE_BASIC_UNARY_OP(cot, Basic::Cot)
41+
42+
} // namespace AutoDiff
43+
44+
#endif // AUTODIFF_SRC_BASIC_OPS_COT_HPP
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_SINH_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_SINH_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class Sinh : public UnaryOperation<Sinh<X>, X> {
13+
public:
14+
using Base = UnaryOperation<Sinh<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::sinh(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
return std::cosh(Base::xValue()) * Base::xPushForward();
25+
}
26+
27+
template <typename Derivative>
28+
void _pullBackImpl(Derivative const& derivative)
29+
{
30+
Base::xPullBack(derivative * std::cosh(Base::xValue()));
31+
}
32+
};
33+
34+
} // namespace AutoDiff::Basic
35+
36+
namespace AutoDiff {
37+
38+
AUTODIFF_MAKE_BASIC_UNARY_OP(sinh, Basic::Sinh)
39+
40+
} // namespace AutoDiff
41+
42+
#endif // AUTODIFF_SRC_BASIC_OPS_SINH_HPP
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024 Matthias Krippner
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
#ifndef AUTODIFF_SRC_BASIC_OPS_TAN_HPP
7+
#define AUTODIFF_SRC_BASIC_OPS_TAN_HPP
8+
9+
namespace AutoDiff::Basic {
10+
11+
template <typename X>
12+
class Tan : public UnaryOperation<Tan<X>, X> {
13+
public:
14+
using Base = UnaryOperation<Tan<X>, X>;
15+
using Base::Base;
16+
17+
[[nodiscard]] auto _valueImpl() -> decltype(auto)
18+
{
19+
return std::tan(Base::xValue());
20+
}
21+
22+
[[nodiscard]] auto _pushForwardImpl() -> decltype(auto)
23+
{
24+
auto const& tan_x = std::tan(Base::xValue());
25+
return (1 + tan_x * tan_x) * Base::xPushForward();
26+
}
27+
28+
template <typename Derivative>
29+
void _pullBackImpl(Derivative const& derivative)
30+
{
31+
auto const& tan_x = std::tan(Base::xValue());
32+
Base::xPullBack(derivative * (1 + tan_x * tan_x));
33+
}
34+
};
35+
36+
} // namespace AutoDiff::Basic
37+
38+
namespace AutoDiff {
39+
40+
AUTODIFF_MAKE_BASIC_UNARY_OP(tan, Basic::Tan)
41+
42+
} // namespace AutoDiff
43+
44+
#endif // AUTODIFF_SRC_BASIC_OPS_TAN_HPP

0 commit comments

Comments
 (0)