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

Metis provides a comprehensive set of math functions that work seamlessly with both numeric (double) and symbolic (SymbolicScalar) types. When called with a metis::SymbolicScalar argument, C++ automatically finds the correct overload via ADL (Argument-Dependent Lookup), so you can write sin(x) or metis::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 <metis/metis.hpp>
// Works with both double and SymbolicScalar
template <typename Scalar>
Scalar my_function(const Scalar& x, const Scalar& y) {
return metis::sin(x) + metis::pow(y, 2.0) + metis::exp(-x * y);
}
// Numeric usage
double result = my_function(1.0, 2.0);
// Symbolic usage
auto x = metis::sym("x");
auto y = metis::sym("y");
auto expr = my_function(x, y);
Umbrella header that includes the entire Metis public API.
T pow(const T &base, const T &exponent)
Computes the power function: base^exponent.
Definition Arithmetic.hpp:72
SymbolicScalar sym(const std::string &name)
Create a named symbolic scalar variable.
Definition MetisTypes.hpp:107
T exp(const T &x)
Computes the exponential function e^x.
Definition Arithmetic.hpp:131
T sin(const T &x)
Computes sine of x.
Definition Trig.hpp:21

Core API

Trigonometric

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

Exponential & Logarithmic

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

Hyperbolic

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

Utility

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

Control Flow

  • metis::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 metis::SymbolicScalar argument, C++ automatically finds the correct function via ADL:

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

For mixed types or explicit calls:

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

Branch-Free Conditionals with metis::where

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

template <typename Scalar>
Scalar safe_sqrt(const Scalar& x) {
return metis::where(x > 0.0, metis::sqrt(x), Scalar(0.0));
}
T sqrt(const T &x)
Computes the square root of a scalar.
Definition Arithmetic.hpp:46
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

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 <metis/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 Metis symbols into scope.
SymbolicScalar sym(const std::string &name)
Create a named symbolic scalar variable.
Definition MetisTypes.hpp:107

See Also