21template <JanusScalar T> T
abs(
const T &x) {
22 if constexpr (std::is_floating_point_v<T>) {
35template <
typename Derived>
auto abs(
const Eigen::MatrixBase<Derived> &x) {
36 return x.array().abs().matrix();
46template <JanusScalar T> T
sqrt(
const T &x) {
47 if constexpr (std::is_floating_point_v<T>) {
60template <
typename Derived>
auto sqrt(
const Eigen::MatrixBase<Derived> &x) {
61 return x.array().sqrt().matrix();
72template <JanusScalar T> T
pow(
const T &base,
const T &exponent) {
73 if constexpr (std::is_floating_point_v<T>) {
74 return std::pow(base, exponent);
76 return pow(base, exponent);
87template <JanusScalar T>
88 requires(!std::is_same_v<T, double>)
89T
pow(
const T &base,
double exponent) {
90 if constexpr (std::is_floating_point_v<T>) {
91 return std::pow(base,
static_cast<T
>(exponent));
93 return janus::pow(base,
static_cast<T
>(exponent));
104template <JanusScalar T>
105 requires(!std::is_same_v<T, double>)
106T
pow(
double base,
const T &exponent) {
108 return janus::pow(
static_cast<T
>(base), exponent);
119template <
typename Derived,
typename Scalar>
120auto pow(
const Eigen::MatrixBase<Derived> &base,
const Scalar &exponent) {
121 return base.array().pow(exponent).matrix();
131template <JanusScalar T> T
exp(
const T &x) {
132 if constexpr (std::is_floating_point_v<T>) {
145template <
typename Derived>
auto exp(
const Eigen::MatrixBase<Derived> &x) {
146 return x.array().exp().matrix();
156template <JanusScalar T> T
log(
const T &x) {
157 if constexpr (std::is_floating_point_v<T>) {
170template <
typename Derived>
auto log(
const Eigen::MatrixBase<Derived> &x) {
171 return x.array().log().matrix();
180template <JanusScalar T> T
log10(
const T &x) {
181 if constexpr (std::is_floating_point_v<T>) {
182 return std::log10(x);
194template <
typename Derived>
auto log10(
const Eigen::MatrixBase<Derived> &x) {
195 return x.array().log10().matrix();
205template <JanusScalar T> T
sinh(
const T &x) {
206 if constexpr (std::is_floating_point_v<T>) {
219template <
typename Derived>
auto sinh(
const Eigen::MatrixBase<Derived> &x) {
220 return x.array().sinh().matrix();
229template <JanusScalar T> T
cosh(
const T &x) {
230 if constexpr (std::is_floating_point_v<T>) {
243template <
typename Derived>
auto cosh(
const Eigen::MatrixBase<Derived> &x) {
244 return x.array().cosh().matrix();
253template <JanusScalar T> T
tanh(
const T &x) {
254 if constexpr (std::is_floating_point_v<T>) {
267template <
typename Derived>
auto tanh(
const Eigen::MatrixBase<Derived> &x) {
268 return x.array().tanh().matrix();
278template <JanusScalar T> T
floor(
const T &x) {
279 if constexpr (std::is_floating_point_v<T>) {
280 return std::floor(x);
292template <
typename Derived>
auto floor(
const Eigen::MatrixBase<Derived> &x) {
293 return x.array().floor().matrix();
302template <JanusScalar T> T
ceil(
const T &x) {
303 if constexpr (std::is_floating_point_v<T>) {
316template <
typename Derived>
auto ceil(
const Eigen::MatrixBase<Derived> &x) {
317 return x.array().ceil().matrix();
326template <JanusScalar T> T
sign(
const T &x) {
327 if constexpr (std::is_floating_point_v<T>) {
329 return (x > 0) ? T(1.0) : ((x < 0) ? T(-1.0) : T(0.0));
341template <
typename Derived>
auto sign(
const Eigen::MatrixBase<Derived> &x) {
342 return x.array().sign().matrix();
353template <JanusScalar T> T
fmod(
const T &x,
const T &y) {
354 if constexpr (std::is_floating_point_v<T>) {
355 return std::fmod(x, y);
371template <
typename Derived,
typename Scalar>
372auto fmod(
const Eigen::MatrixBase<Derived> &x,
const Scalar &y) {
373 if constexpr (std::is_same_v<typename Derived::Scalar, casadi::MX> ||
374 std::is_same_v<Scalar, casadi::MX>) {
376 using ResultScalar = casadi::MX;
377 Eigen::Matrix<ResultScalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime> result(
379 const ResultScalar y_mx =
static_cast<ResultScalar
>(y);
380 for (Eigen::Index i = 0; i < x.rows(); ++i) {
381 for (Eigen::Index j = 0; j < x.cols(); ++j) {
382 result(i, j) =
janus::fmod(
static_cast<ResultScalar
>(x(i, j)), y_mx);
387 return x.binaryExpr(Eigen::MatrixBase<Derived>::Constant(x.rows(), x.cols(), y),
388 [](
double a,
double b) { return std::fmod(a, b); });
399template <JanusScalar T> T
log2(
const T &x) {
400 if constexpr (std::is_floating_point_v<T>) {
404 return log(x) /
log(T(2.0));
414template <
typename Derived>
auto log2(
const Eigen::MatrixBase<Derived> &x) {
415 using Scalar =
typename Derived::Scalar;
416 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
417 return x.unaryExpr([](
const Scalar &v) {
return janus::log2(v); });
419 return (x.array().log() / std::log(2.0)).matrix();
430template <JanusScalar T> T
exp2(
const T &x) {
431 if constexpr (std::is_floating_point_v<T>) {
435 return exp(x *
log(T(2.0)));
445template <
typename Derived>
auto exp2(
const Eigen::MatrixBase<Derived> &x) {
446 using Scalar =
typename Derived::Scalar;
447 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
448 return x.unaryExpr([](
const Scalar &v) {
return janus::exp2(v); });
450 return (x.array() * std::log(2.0)).exp().matrix();
461template <JanusScalar T> T
cbrt(
const T &x) {
462 if constexpr (std::is_floating_point_v<T>) {
467 return sign(x) *
pow(fabs(x), T(1.0 / 3.0));
477template <
typename Derived>
auto cbrt(
const Eigen::MatrixBase<Derived> &x) {
478 using Scalar =
typename Derived::Scalar;
479 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
480 return x.unaryExpr([](
const Scalar &v) {
return janus::cbrt(v); });
482 return x.unaryExpr([](
double v) {
return std::cbrt(v); });
493template <JanusScalar T> T
round(
const T &x) {
494 if constexpr (std::is_floating_point_v<T>) {
495 return std::round(x);
499 return floor(x + T(0.5));
509template <
typename Derived>
auto round(
const Eigen::MatrixBase<Derived> &x) {
510 using Scalar =
typename Derived::Scalar;
511 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
512 return x.unaryExpr([](
const Scalar &v) {
return janus::round(v); });
514 return x.array().round().matrix();
525template <JanusScalar T> T
trunc(
const T &x) {
526 if constexpr (std::is_floating_point_v<T>) {
527 return std::trunc(x);
540template <
typename Derived>
auto trunc(
const Eigen::MatrixBase<Derived> &x) {
541 using Scalar =
typename Derived::Scalar;
542 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
543 return x.unaryExpr([](
const Scalar &v) {
return janus::trunc(v); });
545 return x.unaryExpr([](
double v) {
return std::trunc(v); });
557template <JanusScalar T> T
hypot(
const T &x,
const T &y) {
558 if constexpr (std::is_floating_point_v<T>) {
559 return std::hypot(x, y);
562 return sqrt(x * x + y * y);
573template <JanusScalar T>
574 requires(!std::is_same_v<T, double>)
586template <JanusScalar T>
587 requires(!std::is_same_v<T, double>)
599template <
typename Derived>
600auto hypot(
const Eigen::MatrixBase<Derived> &x,
const Eigen::MatrixBase<Derived> &y) {
601 using Scalar =
typename Derived::Scalar;
602 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
603 return (x.array().square() + y.array().square()).sqrt().matrix();
605 return x.binaryExpr(y, [](
double a,
double b) {
return std::hypot(a, b); });
616template <JanusScalar T> T
expm1(
const T &x) {
617 if constexpr (std::is_floating_point_v<T>) {
618 return std::expm1(x);
621 return exp(x) - T(1.0);
631template <
typename Derived>
auto expm1(
const Eigen::MatrixBase<Derived> &x) {
632 using Scalar =
typename Derived::Scalar;
633 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
634 return x.unaryExpr([](
const Scalar &v) {
return janus::expm1(v); });
636 return x.unaryExpr([](
double v) {
return std::expm1(v); });
647template <JanusScalar T> T
log1p(
const T &x) {
648 if constexpr (std::is_floating_point_v<T>) {
649 return std::log1p(x);
652 return log(T(1.0) + x);
662template <
typename Derived>
auto log1p(
const Eigen::MatrixBase<Derived> &x) {
663 using Scalar =
typename Derived::Scalar;
664 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
665 return x.unaryExpr([](
const Scalar &v) {
return janus::log1p(v); });
667 return x.unaryExpr([](
double v) {
return std::log1p(v); });
679template <JanusScalar T> T
copysign(
const T &x,
const T &y) {
680 if constexpr (std::is_floating_point_v<T>) {
681 return std::copysign(x, y);
684 return sign(y) * fabs(x);
695template <JanusScalar T>
696 requires(!std::is_same_v<T, double>)
708template <JanusScalar T>
709 requires(!std::is_same_v<T, double>)
721template <
typename Derived>
722auto copysign(
const Eigen::MatrixBase<Derived> &x,
const Eigen::MatrixBase<Derived> &y) {
723 using Scalar =
typename Derived::Scalar;
724 if constexpr (std::is_same_v<Scalar, casadi::MX>) {
725 return x.binaryExpr(y,
726 [](
const Scalar &a,
const Scalar &b) {
return janus::copysign(a, b); });
728 return x.binaryExpr(y, [](
double a,
double b) {
return std::copysign(a, b); });
739template <JanusScalar T> T
square(
const T &x) {
return x * x; }
747template <
typename Derived>
auto square(
const Eigen::MatrixBase<Derived> &x) {
748 return x.array().square().matrix();
C++20 concepts constraining valid Janus scalar types.
Definition Diagnostics.hpp:19
T hypot(const T &x, const T &y)
Computes sqrt(x^2 + y^2) without undue overflow/underflow.
Definition Arithmetic.hpp:557
T cbrt(const T &x)
Computes cube root of x.
Definition Arithmetic.hpp:461
T trunc(const T &x)
Truncates x toward zero.
Definition Arithmetic.hpp:525
T tanh(const T &x)
Computes hyperbolic tangent of x.
Definition Arithmetic.hpp:253
T sinh(const T &x)
Computes hyperbolic sine.
Definition Arithmetic.hpp:205
T square(const T &x)
Computes x^2 (more efficient than pow(x, 2)).
Definition Arithmetic.hpp:739
T fmod(const T &x, const T &y)
Computes floating-point remainder of x/y.
Definition Arithmetic.hpp:353
T abs(const T &x)
Computes the absolute value of a scalar.
Definition Arithmetic.hpp:21
T sqrt(const T &x)
Computes the square root of a scalar.
Definition Arithmetic.hpp:46
T copysign(const T &x, const T &y)
Returns magnitude of x with sign of y.
Definition Arithmetic.hpp:679
T log(const T &x)
Computes the natural logarithm of x.
Definition Arithmetic.hpp:156
T log2(const T &x)
Computes base-2 logarithm of x.
Definition Arithmetic.hpp:399
T round(const T &x)
Rounds x to the nearest integer.
Definition Arithmetic.hpp:493
T cosh(const T &x)
Computes hyperbolic cosine of x.
Definition Arithmetic.hpp:229
T sign(const T &x)
Computes sign of x.
Definition Arithmetic.hpp:326
T floor(const T &x)
Computes floor of x.
Definition Arithmetic.hpp:278
T pow(const T &base, const T &exponent)
Computes the power function: base^exponent.
Definition Arithmetic.hpp:72
T exp2(const T &x)
Computes 2^x.
Definition Arithmetic.hpp:430
T ceil(const T &x)
Computes ceiling of x.
Definition Arithmetic.hpp:302
T log10(const T &x)
Computes the base-10 logarithm of x.
Definition Arithmetic.hpp:180
T log1p(const T &x)
Computes log(1 + x), accurate for small x.
Definition Arithmetic.hpp:647
T expm1(const T &x)
Computes exp(x) - 1, accurate for small x.
Definition Arithmetic.hpp:616
T exp(const T &x)
Computes the exponential function e^x.
Definition Arithmetic.hpp:131