Skip to content

math

This was an overly ambitious try, but making simple and trivial math functions was just non-feasible against just simply using math.h So here we are, this file has been removed from the core as of now. If you try to use it here is a (probably) outdated doc. Of course! This is an excellent candidate for documentation. The C code is well-commented, which makes the process much smoother. I’ll create a comprehensive documentation page using MDX, suitable for an Astro project.

SML Math Library

Welcome to the documentation for the SML Math Library! This library provides a wide range of mathematical functions written in C, from basic arithmetic and trigonometry to more advanced concepts like matrix operations and permutations.

Core Concepts

sml_math_t Structure

The core data structure for advanced math operations is sml_math_t. It’s designed to represent various types of numbers, including integers, floats, irrationals (like roots), and imaginary numbers.

typedef enum {
    _int, _float, _irrational, _imaginary, _degree, _radian, _gradian
} sml_num_type;

typedef struct sml_math_t {
    long double value;
    long double degree; // For exponents or root degrees
    sml_num_type type;
    bool is_imaginary;
    struct sml_math_t* related; // For complex expressions
} sml_math_t;

High Accuracy Mode

The library can be compiled with a HIGH_ACCURACY flag. When defined, it uses long double for its calculations, providing higher precision at the cost of performance. Otherwise, it defaults to double.

Creating an sml_math_t Instance

Use sml_math_create to instantiate the structure.

sml_math_t sml_math_create(double value, double degree, sml_num_type type, bool is_imaginary);

Parameters:

  • value: The main numerical value.
  • degree: The degree of the root or exponent.
  • type: The type of number from the sml_num_type enum.
  • is_imaginary: A boolean flag to indicate if the number is imaginary.

Example:

sml_math_t my_num = sml_math_create(5.0, 2.0, _int, false); // Represents 5^2

Basic Arithmetic

A collection of fundamental arithmetic functions.

sml_abs & sml_float_abs

Returns the absolute value of an integer or a floating-point number.

int sml_abs(int x);
double sml_float_abs(long double x);

sml_max & sml_min

Returns the maximum or minimum of two integers or floating-point numbers.

int sml_max(int x, int y);
double sml_float_max(double x, double y);
int sml_min(int x, int y);
double sml_float_min(double x, double y);

sml_is_prime

Checks if a given unsigned long long is a prime number.

bool sml_is_prime(unsigned long long num);

sml_nth_prime

Returns the nth prime number.

sml_size sml_nth_prime(sml_size nth_prime);

sml_greatest_common_divisor (GCD)

Returns the greatest common divisor of two numbers.

sml_size sml_greatest_common_divisor(sml_size num1, sml_size num2);

sml_smallest_common_multiple (SCM)

Returns the smallest common multiple of two numbers.

sml_size sml_smallest_common_multiple(sml_size num1, sml_size num2);

Factorials & Powers

Functions for calculating factorials and exponents.

Factorials

The library provides multiple versions of the factorial function for different data types.

// Iterative versions for sml_size
sml_size sml_fac_size_t(double x);
sml_size sml_fac_size_t_int(sml_size_s x);
sml_size sml_fac_size_t_unsigned(sml_size x);

// Recursive versions for double and int
double sml_fac(double n);
int sml_fac_int(int n);

Powers (sml_pow family)

Functions to calculate the power of a number.

// General purpose power function
long double sml_pow(long double base, long double expo);

// Basic integer exponent
float sml_pow_basic(double x, sml_size expo);
int sml_pow_basic_int(int base, int exp);

// Specialized versions
long double sml_pow2(double x); // x^2
long double sml_pow10(double x); // x^10
long double sml_pow_only_positive(long double base, sml_size expo);
long double sml_pow_only_negative(long double base, long double expo);

Exponential & Logarithmic Functions

sml_exp

Calculates e (Euler’s number) raised to the power of x.

long double sml_exp(double x); // e^x

sml_log family

Calculates logarithms with different bases. The core function, sml_nlog, can calculate the logarithm for any base n.

// Calculates log base n of x
long double sml_nlog(double x, double n);

// Common logarithms
long double sml_log2(double x);     // Base 2
long double sml_log10(double x);    // Base 10
long double sml_log_ln(double x);   // Natural log (base e)
long double sml_log_pi(double x);   // Base pi

Roots

sml_sqrt

A fast implementation of the square root function using the Babylonian method.

Imaginary Numbers

This function returns -1 and prints a warning if the input is negative, as it cannot return an imaginary number in a double. Use sml_nth_root for complex results.

double sml_sqrt(double n);

sml_nth_root & sml_nth_root_double

These functions calculate the nth root of a number.

  • sml_nth_root: Returns an sml_math_t struct, which can represent irrational or imaginary roots symbolically.
  • sml_nth_root_double: Returns the numerical result as a double, but only for non-negative inputs.
sml_math_t sml_nth_root(double num, double n);
double sml_nth_root_double(double num, sml_size_s p);

Trigonometric Functions

All standard trigonometric functions expect the input angle in degrees.

Angle Conversions

double sml_rad_to_deg(double Rad);
double sml_deg_to_rad(double d);

Standard Trigonometry

These functions use a Taylor series approximation for sin and derive the others from it.

float sml_sin(int deg);
float sml_cos(int deg);
float sml_tan(int deg);
float sml_cot(int deg);
float sml_sec(int deg);
float sml_csc(int deg);

Inverse Trigonometry

double sml_arc_sin(double x);
double sml_arc_cos(double x);

Hyperbolic Functions

double sml_sinh(double x);
double sml_cosh(double x);
double sml_tanh(double x);
double sml_coth(double x);

Rounding Functions

Standard functions for rounding floating-point numbers.

double sml_ceil(double x);  // Rounds up to the nearest integer
double sml_floor(double x); // Rounds down to the nearest integer
double sml_round(double x); // Rounds to the nearest integer

Combinations & Permutations

Functions for solving common combinatorial problems.

CPR and CCR Humor

The comments for sml_cpr and sml_ccr contain humor. Please do not consult this library in a medical emergency.

// Standard Permutation (nPr)
double sml_npr(double n, double r);

// Standard Combination (nCr)
double sml_ncr(double n, double r);

// Repeated Permutation
unsigned long long sml_rpr(int n, int r);

// Repeated Combination
unsigned long long sml_sml_rcr(int n, int r);

// Circular Permutation
unsigned long long sml_cpr(int n, int r);

// Circular Combination
unsigned long long sml_ccr(int n, int r);

Miscellaneous Functions

sml_fib

Calculates the nth Fibonacci number using an iterative approach.

sml_size sml_fib(sml_size n);

sml_fmod

Returns the floating-point remainder of x / y.

double sml_fmod(double x, double y);

Matrix Operations

The library includes basic support for creating, manipulating, and destroying matrices of sml_math_t types.

sml_matrix_create

Allocates memory for a new matrix of rows x cols dimensions.

sml_matrix_t *sml_matrix_create(sml_size rows, sml_size cols);

sml_matrix_destroy

Frees the memory associated with a matrix.

void sml_matrix_destroy(sml_matrix_t *matrix);

sml_matrix_print

Prints a formatted representation of the matrix to the console.

void sml_matrix_print(sml_matrix_t *matrix);

sml_matrix_zero_init

Initializes all elements of a matrix to zero.

void sml_matrix_zero_init(sml_matrix_t *matrix);

sml_matrix_dup

Creates a deep copy of a source matrix.

sml_matrix_t *sml_matrix_dup(sml_matrix_t *src);

sml_matrix_transpose

Returns a new matrix that is the transpose of the input matrix.

sml_matrix_t *sml_matrix_transpose(sml_matrix_t *matrix);