Janus 2.0.0
High-performance C++20 dual-mode numerical framework
Loading...
Searching...
No Matches
Math Functions

Janus provides a comprehensive set of math functions that work seamlessly with both numeric (double) and symbolic (SymbolicScalar) types. When called with a janus::SymbolicScalar argument, C++ automatically finds the correct overload via ADL (Argument-Dependent Lookup), so you can write sin(x) or janus::sin(x) interchangeably. These functions are the foundation for writing generic template code that compiles to both fast numeric execution and symbolic graph construction.

Quick Start

#include <janus/janus.hpp>
// Works with both double and SymbolicScalar
template <typename Scalar>
Scalar my_function(const Scalar& x, const Scalar& y) {
return janus::sin(x) + janus::pow(y, 2.0) + janus::exp(-x * y);
}
// Numeric usage
double result = my_function(1.0, 2.0);
// Symbolic usage
auto x = janus::sym("x");
auto y = janus::sym("y");
auto expr = my_function(x, y);
Umbrella header that includes the entire Janus public API.
T sin(const T &x)
Computes sine of x.
Definition Trig.hpp:21
SymbolicScalar sym(const std::string &name)
Create a named symbolic scalar variable.
Definition JanusTypes.hpp:90
T pow(const T &base, const T &exponent)
Computes the power function: base^exponent.
Definition Arithmetic.hpp:72
T exp(const T &x)
Computes the exponential function e^x.
Definition Arithmetic.hpp:131

Core API

Trigonometric

  • janus::sin(x), janus::cos(x), janus::tan(x)
  • janus::asin(x), janus::acos(x), janus::atan(x), janus::atan2(y, x)

Exponential & Logarithmic

  • janus::exp(x), janus::log(x), janus::log10(x)
  • janus::pow(base, exp), janus::sqrt(x)

Hyperbolic

  • janus::sinh(x), janus::cosh(x), janus::tanh(x)

Utility

  • janus::abs(x), janus::fabs(x)
  • janus::floor(x), janus::ceil(x)
  • janus::fmin(a, b), janus::fmax(a, b)

Control Flow

  • janus::where(condition, if_true, if_false) - Branch-free conditional that works in both numeric and symbolic mode

Usage Patterns

ADL (Argument-Dependent Lookup)

When you call a math function with a janus::SymbolicScalar argument, C++ automatically finds the correct function via ADL:

auto y = sin(x); // Finds janus::sin via ADL
auto z = pow(x, 2); // Finds janus::pow via ADL
casadi::MX SymbolicScalar
CasADi MX symbolic scalar.
Definition JanusTypes.hpp:70
T sin(const T &x)
Computes sine of x.
Definition Trig.hpp:21
T pow(const T &base, const T &exponent)
Computes the power function: base^exponent.
Definition Arithmetic.hpp:72

For mixed types or explicit calls:

auto y = janus::sin(x); // Always works
auto z = janus::pow(x, 2.0); // Mixed symbolic/numeric

Branch-Free Conditionals with janus::where

Use janus::where instead of if/else to ensure your code works in symbolic mode:

template <typename Scalar>
Scalar safe_sqrt(const Scalar& x) {
return janus::where(x > 0.0, janus::sqrt(x), Scalar(0.0));
}
auto where(const Cond &cond, const T1 &if_true, const T2 &if_false)
Select values based on condition (ternary operator) Returns: cond ? if_true : if_false Supports mixed...
Definition Logic.hpp:43
T sqrt(const T &x)
Computes the square root of a scalar.
Definition Arithmetic.hpp:46

In numeric mode, this compiles to efficient branchless select instructions (where possible). In symbolic mode, it builds a valid conditional node in the computation graph.

Convenience Header

For cleaner code, use the convenience header:

#include <janus/using.hpp>
// Now sym, sin, cos, pow, where are in scope
auto x = sym("x");
auto y = sin(x) + pow(x, 2);
Convenience header bringing common Janus symbols into scope.
SymbolicScalar sym(const std::string &name)
Create a named symbolic scalar variable.
Definition JanusTypes.hpp:90

See Also