alib
Universal C Library Collection for Machine Learning & Artificial Intelligence
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions | Variables
calc.h File Reference

A simple calculus library header. More...

#include <math.h>
Include dependency graph for calc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  calc_function
 Structure representing an function and its derivative. More...
 

Typedefs

typedef struct calc_function calc_function
 Structure representing an function and its derivative.
 
typedef double(* calc_function_fn) (double)
 A function pointer type for functions.
 

Functions

double calc_approx_derivative (double(*f)(double), double x, double h)
 Calculate the numerical derivative of a function at a point.
 
double calc_approx_finite_integral (double(*f)(double), double a, double b, int n)
 Calculate the numerical integral of a function over an interval.
 
double calc_function_negation (double x)
 Negate the value of a given double.
 
double calc_function_negation_derivative (double x)
 Calculate the derivative of the negation function.
 
double calc_function_relu (double x)
 Calculate the ReLU (Rectified Linear Unit) function.
 
double calc_function_relu_derivative (double x)
 Calculate the derivative of the ReLU function.
 
double calc_function_sigmoid (double x)
 Calculate the sigmoid function.
 
double calc_function_sigmoid_derivative (double x)
 Calculate the derivative of the sigmoid function.
 
double calc_function_square (double x)
 Calculate the square of a given double.
 
double calc_function_square_derivative (double x)
 Calculate the derivative of the square function.
 
double calc_function_tanh (double x)
 Calculate the Tanh function.
 
double calc_function_tanh_derivative (double x)
 Calculate the derivative of the Tanh function.
 

Variables

const calc_function CALC_FUNCTION_NEGATION
 Predefined negation function.
 
const calc_function CALC_FUNCTION_RELU
 Predefined ReLU function.
 
const calc_function CALC_FUNCTION_SIGMOID
 Predefined Sigmoid function.
 
const calc_function CALC_FUNCTION_SQUARE
 Predefined square function.
 
const calc_function CALC_FUNCTION_TANH
 Predefined Tanh function.
 

Detailed Description

A simple calculus library header.

This header file contains declarations for mathematical calculation functions, including numerical differentiation and integration. The implementations of these functions are included in the compiled library lib/libcalc.so.

Definition in file calc.h.

Typedef Documentation

◆ calc_function

typedef struct calc_function calc_function

Structure representing an function and its derivative.

This structure holds the name of the function, the function itself, and its derivative function.

◆ calc_function_fn

typedef double(* calc_function_fn) (double)

A function pointer type for functions.

This type represents a function that takes a double and returns a double, typically used for functions in neural networks.

Definition at line 22 of file calc.h.

Function Documentation

◆ calc_approx_derivative()

double calc_approx_derivative ( double(*)(double) f,
double x,
double h )

Calculate the numerical derivative of a function at a point.

This function approximates the derivative of a given function \( f \) at a point \( x \) using the central difference method:

\[ f'(x) \approx \frac{f(x + h) - f(x - h)}{2h} \]

Parameters
fPointer to the function to differentiate.
xThe point at which to evaluate the derivative.
hA small value for the finite difference approximation.
Returns
The numerical derivative of the function at the point \( x \).

◆ calc_approx_finite_integral()

double calc_approx_finite_integral ( double(*)(double) f,
double a,
double b,
int n )

Calculate the numerical integral of a function over an interval.

This function approximates the integral of a given function \( f \) over the interval \([a, b]\) using the trapezoidal rule:

\[ \int_a^b f\left(x\right) \, dx \approx \frac{b - a}{2n} \sum_{i=0}^{n-1} \left(f\left(x_i\right) + f\left(x_{i+1}\right)\right) \]

Parameters
fPointer to the function to integrate.
aThe lower limit of integration.
bThe upper limit of integration.
nThe number of subintervals.
Returns
The numerical integral of the function over the interval \([a, b]\).

◆ calc_function_negation()

double calc_function_negation ( double x)

Negate the value of a given double.

This function takes a double value and returns its negation.

Example usage:

double value = 5.0;
double neg_value = calc_function_negation(value);
printf("Negation: %f\n", neg_value);
double calc_function_negation(double x)
Negate the value of a given double.
Parameters
xThe value to be negated.
Returns
The negated value.

◆ calc_function_negation_derivative()

double calc_function_negation_derivative ( double x)

Calculate the derivative of the negation function.

This function calculates the derivative of the negation function, which is \( -1 \) for any value of \( x \).

Parameters
xThe value at which to evaluate the derivative.
Returns
The derivative of the negation function, which is \( -1 \).

◆ calc_function_relu()

double calc_function_relu ( double x)

Calculate the ReLU (Rectified Linear Unit) function.

The ReLU function is defined as: \[ \text{ReLU}(x) = \max(0, x) \]

This function is commonly used as an function in neural networks.

Parameters
xThe input value.
Returns
The result of the ReLU function.

Example:

double result = calc_function_relu(0.5); // result = 0.5
result = calc_function_relu(-0.5); // result = 0.0
double calc_function_relu(double x)
Calculate the ReLU (Rectified Linear Unit) function.

◆ calc_function_relu_derivative()

double calc_function_relu_derivative ( double x)

Calculate the derivative of the ReLU function.

The derivative of the ReLU function is: \[ \text{ReLU}'(x) = \begin{cases} 1 & \text{if } x > 0 \\ 0 & \text{if } x \leq 0 \end{cases} \]

Parameters
xThe input value.
Returns
The derivative of the ReLU function.

Example:

double result = calc_function_relu_derivative(0.5); // result = 1.0
result = calc_function_relu_derivative(-0.5); // result = 0.0
double calc_function_relu_derivative(double x)
Calculate the derivative of the ReLU function.

◆ calc_function_sigmoid()

double calc_function_sigmoid ( double x)

Calculate the sigmoid function.

The sigmoid function, \( \sigma(x) \), is defined as: \[ \sigma(x) = \frac{1}{1 + e^{-x}} \]

The sigmoid function maps any real-valued number \( x \) to a value in the range (0, 1). It is commonly used in logistic regression and as an activation function in neural networks.

Parameters
xThe input value.
Returns
The result of the sigmoid function \( \sigma(x) \).

Example:

double result = calc_function_sigmoid(0.5); // result = 0.622
result = calc_function_sigmoid(-0.5); // result = 0.378
double calc_function_sigmoid(double x)
Calculate the sigmoid function.

◆ calc_function_sigmoid_derivative()

double calc_function_sigmoid_derivative ( double x)

Calculate the derivative of the sigmoid function.

The derivative of the sigmoid function is defined as: \[ \sigma'(x) = \sigma(x) \cdot (1 - \sigma(x)) \]

This function is used during the backpropagation step in neural networks.

Parameters
xThe input value.
Returns
The derivative of the sigmoid function.

Example:

double result = calc_function_sigmoid_derivative(0.5); // result = 0.235
result = calc_function_sigmoid_derivative(-0.5); // result = 0.235
double calc_function_sigmoid_derivative(double x)
Calculate the derivative of the sigmoid function.

◆ calc_function_square()

double calc_function_square ( double x)

Calculate the square of a given double.

This function takes a double value and returns its square.

Example usage:

double value = 5.0;
double square_value = calc_function_square(value);
printf("Square: %f\n", square_value);
double calc_function_square(double x)
Calculate the square of a given double.
Parameters
xThe value to be squared.
Returns
The squared value.

◆ calc_function_square_derivative()

double calc_function_square_derivative ( double x)

Calculate the derivative of the square function.

This function calculates the derivative of the square function, which is \( 2x \).

Parameters
xThe value at which to evaluate the derivative.
Returns
The derivative of the square function, which is \( 2x \).

◆ calc_function_tanh()

double calc_function_tanh ( double x)

Calculate the Tanh function.

The Tanh function is defined as: \[ \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} \]

This function maps any real-valued number \( x \) to a value in the range (-1, 1). It is commonly used as an function in neural networks.

Parameters
xThe input value.
Returns
The result of the Tanh function.

Example:

double result = calc_function_tanh(0.5); // result = 0.462
result = calc_function_tanh(-0.5); // result = -0.462
double calc_function_tanh(double x)
Calculate the Tanh function.

◆ calc_function_tanh_derivative()

double calc_function_tanh_derivative ( double x)

Calculate the derivative of the Tanh function.

The derivative of the Tanh function is defined as: \[ \tanh'(x) = 1 - \tanh^2(x) \]

This function is used during the backpropagation step in neural networks.

Parameters
xThe input value.
Returns
The derivative of the Tanh function.

Example:

double result = calc_function_tanh_derivative(0.5); // result = 0.786
result = calc_function_tanh_derivative(-0.5); // result = 0.786
double calc_function_tanh_derivative(double x)
Calculate the derivative of the Tanh function.

Variable Documentation

◆ CALC_FUNCTION_NEGATION

const calc_function CALC_FUNCTION_NEGATION
extern

Predefined negation function.

This constant represents the negation function and its derivative.

◆ CALC_FUNCTION_RELU

const calc_function CALC_FUNCTION_RELU
extern

Predefined ReLU function.

This constant represents the ReLU function and its derivative.

◆ CALC_FUNCTION_SIGMOID

const calc_function CALC_FUNCTION_SIGMOID
extern

Predefined Sigmoid function.

This constant represents the Sigmoid function and its derivative.

◆ CALC_FUNCTION_SQUARE

const calc_function CALC_FUNCTION_SQUARE
extern

Predefined square function.

This constant represents the square function and its derivative.

◆ CALC_FUNCTION_TANH

const calc_function CALC_FUNCTION_TANH
extern

Predefined Tanh function.

This constant represents the Tanh function and its derivative.