Skip to content

alg

Arithematic functions and solvers. More...

Classes

Name
struct alg::ans

Functions

Name
template <typename D ,typename LAMBDA >
std::vector< D >
root(LAMBDA f, D tol =D(1000))
template <typename D ,typename LAMBDA >
std::vector< D >
newton_root(LAMBDA f, D x0)
template <typename D >
ans< D >
poly_root(int n, std::vector< D > a)
template <typename D >
D
linear_root(std::vector< D > eq)
Linear root through inputing Ax+C such that it is represented as vector {A, C}.
template <typename D ,typename LAMBDA >
D
bisection_root(LAMBDA f, D min, D max, D tol =D(0.00001))
Used to get real roots through bisection method.
template <typename D >
std::vector< D >
linear_sim(var::matrix< D > eq)
Solves a system of linear equations expressed as a matrix.

Detailed Description

Arithematic functions and solvers.

Short for ALGebra

Functions Documentation

function root

template <typename D ,
typename LAMBDA >
std::vector< D > root(
    LAMBDA f,
    D tol =D(1000)
)

function newton_root

template <typename D ,
typename LAMBDA >
std::vector< D > newton_root(
    LAMBDA f,
    D x0
)

function poly_root

template <typename D >
ans< D > poly_root(
    int n,
    std::vector< D > a
)

function linear_root

template <typename D >
D linear_root(
    std::vector< D > eq
)

Linear root through inputing Ax+C such that it is represented as vector {A, C}.

Parameters:

  • eq

Template Parameters:

  • D

Return: D

Usage:

// answer of x+2 = 0;
auto ans = linear({1, 2});

function bisection_root

template <typename D ,
typename LAMBDA >
D bisection_root(
    LAMBDA f,
    D min,
    D max,
    D tol =D(0.00001)
)

Used to get real roots through bisection method.

Parameters:

  • f function
  • min minimum value
  • max maximum value
  • tol tolerance

Template Parameters:

  • D
  • LAMBDA

Return: D

Usage:

// roots between -5 and -1
auto ans = bisection_root<double>(f, -5.0, -1.0);

Exception

Will throw exceptions as follows:

  1. stdruntime_error \(\rightarrow \min\) is not smaller than \(\max\)

  2. `stdlogic_error \(\rightarrow \text{sign}(f(\min)) == \text{sign}(f(\max))\) which means solution doesn't exist

function linear_sim

template <typename D >
std::vector< D > linear_sim(
    var::matrix< D > eq
)

Solves a system of linear equations expressed as a matrix.

Parameters:

  • eq matrix

Template Parameters:

  • D

Return: std::vector

Representing the input

\[ \displaylines{\underbrace{\left[\begin{array} \ a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \\ \end{array}\right]}_A \ \underbrace{\left[\begin{array} \ x_1 \\ x_2 \\ x_3 \\ \end{array}\right]}_x = \underbrace{\left[\begin{array} \ d_1 \\ d_2 \\ d_3 \end{array}\right]}_b \\ eq = \left[\begin{array}{rrr:r} a_1 & b_1 & c_1 & d_1 \\ a_2 & b_2 & c_2 & d_2 \\ a_3 & b_3 & c_3 & d_3 \\ \end{array}\right]} \]

Updated on 4 September 2022 at 18:30:41 EEST