38template <
typename T>
auto softmax(
const std::vector<T> &args,
double softness = 1.0) {
42 if (softness <= 0.0) {
47 auto max_val = args[0];
48 for (
size_t i = 1; i < args.size(); ++i) {
57 auto first_term =
janus::exp((args[0] - max_val) / softness);
58 auto sum_exp = first_term;
60 for (
size_t i = 1; i < args.size(); ++i) {
61 sum_exp = sum_exp +
janus::exp((args[i] - max_val) / softness);
65 return max_val + softness *
janus::log(sum_exp);
69template <
typename T1,
typename T2>
auto softmax(
const T1 &a,
const T2 &b,
double softness = 1.0) {
83 auto term1 =
janus::exp((a - max_val) / softness);
84 auto term2 =
janus::exp((b - max_val) / softness);
93 return max_val + softness *
janus::log(term1 + term2);
108template <
typename T>
auto softmin(
const std::vector<T> &args,
double softness = 1.0) {
109 std::vector<T> neg_args;
110 neg_args.reserve(args.size());
111 for (
const auto &arg : args) {
112 neg_args.push_back(-arg);
114 return -
softmax(neg_args, softness);
118template <
typename T1,
typename T2>
auto softmin(
const T1 &a,
const T2 &b,
double softness = 1.0) {
119 return -
softmax(-a, -b, softness);
136 if (hardness <= 0.0) {
137 throw InvalidArgument(std::string(function_name) +
": hardness must be positive");
148 const auto bx = beta * x;
149 const auto shift = bx > 0.0 ? bx : 0.0;
150 return (shift + std::log(std::exp(-shift) + std::exp(bx - shift))) / beta;
154 const auto bx = beta * x;
157 return SymbolicScalar::logsumexp(SymbolicScalar::vertcat({
SymbolicScalar(0.0), bx})) / beta;
161template <JanusScalar T>
auto softplus(
const T &x,
double beta = 1.0,
double = 20.0) {
165template <
typename Derived>
166auto softplus(
const Eigen::MatrixBase<Derived> &x,
double beta = 1.0,
double threshold = 20.0) {
167 using Scalar =
typename Derived::Scalar;
168 return x.derived().unaryExpr(
169 [beta, threshold](
const Scalar &v) {
return janus::softplus(v, beta, threshold); });
186template <
typename T>
auto smooth_abs(
const T &x,
double hardness = 1.0) {
188 return -x +
softplus(2.0 * x, hardness);
202template <
typename T1,
typename T2>
203auto smooth_max(
const T1 &a,
const T2 &b,
double hardness = 1.0) {
205 return b +
softplus(a - b, hardness);
219template <
typename T1,
typename T2>
220auto smooth_min(
const T1 &a,
const T2 &b,
double hardness = 1.0) {
222 return a -
softplus(a - b, hardness);
236template <
typename TX,
typename TLow,
typename THigh>
237auto smooth_clamp(
const TX &x,
const TLow &low,
const THigh &high,
double hardness = 1.0) {
252template <JanusScalar T>
auto ks_max(
const std::vector<T> &values,
double rho = 1.0) {
253 if (values.empty()) {
258 if constexpr (std::is_floating_point_v<T>) {
259 const auto max_val = *std::max_element(values.begin(), values.end());
260 double sum_exp = 0.0;
261 for (
const auto &value : values) {
262 sum_exp += std::exp(rho * (value - max_val));
264 return max_val + std::log(sum_exp) / rho;
266 return SymbolicScalar::logsumexp(rho * SymbolicScalar::vertcat(values)) / rho;
270template <
typename Derived>
271auto ks_max(
const Eigen::MatrixBase<Derived> &values,
double rho = 1.0) {
272 using Scalar =
typename Derived::Scalar;
273 std::vector<Scalar> flattened;
274 flattened.reserve(
static_cast<size_t>(values.size()));
275 for (Eigen::Index i = 0; i < values.size(); ++i) {
276 flattened.push_back(values.derived().coeff(i));
278 return ks_max(flattened, rho);
306 double norm_max = 1.0) {
333 double scale = (norm_max - norm_min) / 2.0;
334 double offset = (norm_max + norm_min) / 2.0;
336 return s * scale + offset;
350template <
typename T>
auto swish(
const T &x,
double beta = 1.0) {
366template <
typename TSwitch,
typename THigh,
typename TLow>
367auto blend(
const TSwitch &switch_val,
const THigh &val_high,
const TLow &val_low) {
371 return val_high * weights + val_low * (1.0 - weights);
Scalar and element-wise arithmetic functions (abs, sqrt, pow, exp, log, etc.).
C++20 concepts constraining valid Janus scalar types.
Custom exception hierarchy for Janus framework.
Conditional selection, comparison, and logical operations.
Trigonometric and inverse trigonometric functions.
Input validation failed (e.g., mismatched sizes, invalid parameters).
Definition JanusError.hpp:31
void validate_rho(double rho)
Definition SurrogateModel.hpp:141
auto softplus_scalar(const T &x, double beta)
Definition SurrogateModel.hpp:147
void validate_hardness(double hardness, const char *function_name)
Definition SurrogateModel.hpp:135
Definition Diagnostics.hpp:19
auto sigmoid(const T &x, SigmoidType type=SigmoidType::Tanh, double norm_min=0.0, double norm_max=1.0)
Sigmoid function with normalization capability.
Definition SurrogateModel.hpp:305
T tanh(const T &x)
Computes hyperbolic tangent of x.
Definition Arithmetic.hpp:253
SigmoidType
Sigmoid shape selection.
Definition SurrogateModel.hpp:288
@ Polynomial
Polynomial: x / sqrt(1 + x^2).
Definition SurrogateModel.hpp:292
@ Arctan
Arc tangent based.
Definition SurrogateModel.hpp:291
@ Logistic
Logistic / standard sigmoid.
Definition SurrogateModel.hpp:290
@ Tanh
Hyperbolic tangent.
Definition SurrogateModel.hpp:289
T sqrt(const T &x)
Computes the square root of a scalar.
Definition Arithmetic.hpp:46
T atan(const T &x)
Computes arc tangent of x.
Definition Trig.hpp:146
T log(const T &x)
Computes the natural logarithm of x.
Definition Arithmetic.hpp:156
auto ks_max(const std::vector< T > &values, double rho=1.0)
Kreisselmeier-Steinhauser smooth maximum aggregator.
Definition SurrogateModel.hpp:252
auto smooth_clamp(const TX &x, const TLow &low, const THigh &high, double hardness=1.0)
Smooth approximation of clamp(x, low, high).
Definition SurrogateModel.hpp:237
auto softplus(const T &x, double beta=1.0, double=20.0)
Definition SurrogateModel.hpp:161
auto swish(const T &x, double beta=1.0)
Swish activation function: x / (1 + exp(-beta * x)).
Definition SurrogateModel.hpp:350
@ Polynomial
Definition AutoDiff.hpp:32
auto smooth_abs(const T &x, double hardness=1.0)
Smooth approximation of absolute value.
Definition SurrogateModel.hpp:186
auto blend(const TSwitch &switch_val, const THigh &val_high, const TLow &val_low)
Smoothly blends between two values based on a switch parameter.
Definition SurrogateModel.hpp:367
auto softmax(const std::vector< T > &args, double softness=1.0)
Computes the smooth maximum (LogSumExp) of a collection of values.
Definition SurrogateModel.hpp:38
auto smooth_max(const T1 &a, const T2 &b, double hardness=1.0)
Pairwise smooth maximum.
Definition SurrogateModel.hpp:203
auto smooth_min(const T1 &a, const T2 &b, double hardness=1.0)
Pairwise smooth minimum.
Definition SurrogateModel.hpp:220
auto softmin(const std::vector< T > &args, double softness=1.0)
Computes the smooth minimum of a collection of values. softmin(x) = -softmax(-x).
Definition SurrogateModel.hpp:108
casadi::MX SymbolicScalar
CasADi MX symbolic scalar.
Definition JanusTypes.hpp:70
auto max(const T1 &a, const T2 &b)
Computes maximum of two values.
Definition Logic.hpp:194
T exp(const T &x)
Computes the exponential function e^x.
Definition Arithmetic.hpp:131