Project Title
Header-Only Mathematics library for advanced mathematics.
This project is available on Github.
This project has been discontinued and will not receive updates.
What is Mathlib?
Mathlib is a header-only maths library for C/C++. It goes above and beyond math.h, providing
functionality for advanced mathematics such as Calculus and Vectors.
The library also reimplements several libc functions, such as trigonometric functions and
exponents/logarithms. Functions such as these rely heavily on Taylor Series Expansions[1], meaning an
advanced knowledge of Mathematics and Calculus will be necessary to interpret the source code of
this
library.
What is the purpose of this project?
This project was created to further my knowledge of Mathematics, and improve my grasp on concepts needed for my Mathematics and Further Mathematics A-Levels[2]. Throughout the development of this project, I learned several valuable problem-solving and optimisation skills, as well as furthering my knowledge of advanced mathematic concepts, with the help of my Mathematics teachers, and websites such as Khan Academy.
Implementation
The library is implemented across 6 header files, however the user should only need to include "mathlib.h" into a source file that uses the library. Each file is responsible for a specific part of the library.
- mathlib.h - Main Front-Facing file. Includes all other files.
- mathlib_core.h - Core functionality used by all other files.
- mathlib_discrete.h - Responsible for discrete mathematics such as Binomial Distribution and summations.
- mathlib_pure.h - Responsible for pure mathematics such as calculus and trigonometry.
- mathlib_vector.h - Responsible for vectors and related functions.
- mathlib_complex.h - Responsible for complex numbers and related functions.
Each header file contains structure and function definitions that can be referenced externally to perform operations. mathlib_core defines aliases for common data types that the library uses.
- real - Double-precision float
- integer - Signed integer
- natural - Unsigned integer
- function - Simple function that both accepts and returns a real number.
- complex - Structure containing two real numbers to simulate a complex number in the form a+bi
- vector - Structure containing a real number signifying the dimension, and a dynamic array of elements. Dimensions can range from 1-4
Functions - mathlib_core.h
Arguments are assumed to be real numbers unless otherwise stated.-
power(x, n) - Calculates xn
\-> n -- Natural number
-
fact(n) - Calculates n!
\-> n -- Natural number
-
fact_norecurse(n) - Alternative to fact(n) that uses iteration instead of recursion.
\-> n -- Natural number
-
constant(x) - Simple function that returns the value of x.
-
ml_abs(x) - Returns the modulus of x
-
ml_floor(x) - Rounds x down to an integer value.
-
ml_ceil(x) - Rounds x up to an integer value.
-
ml_ln(x) - Calculates ln(x) using Taylor Series.
-
ml_log(b, x) - Calculates logb(x).
-
ml_exp(x) - Calculates ex using Taylor Series.
-
pow_exp(b, x) - Calculates bx using ml_exp(x).
-
ml_sqrt(x) - Calculates sqrt(x) using pow_exp(b, x).
Functions - mathlib_discrete.h
Arguments are assumed to be real numbers unless otherwise stated.- discrete_sum(f, start, end, step) - Performs a summation over function f with lower bound of start
and upper bound of end.
\-> f -- Function - discrete_product(f, start, end, step) - Similar to discrete_sum, but calculating product instead of
sum.
\-> f -- Function - nCr(n, r) - Calculates nCr
\-> n -- Natural number
\-> r -- Natural Number - nPr(n, r) - Calculates nPr
\-> n -- Natural number
\-> r -- Natural Number - binomialProbability(n, r, p) - Calulates P(X=r) where X~B(n,p)
\-> n -- Natural number
\-> r -- Natural Number - binomialCumulativeProbability(n, r, p) - Calulates P(X<=r) where X~B(n,p)
\-> n -- Natural number
\-> r -- Natural Number - binomialMean(n, p) - Calculates the mean of X where X~B(n,p)
\-> n -- Natural number - binomialMedian(n, p) - Calculates the median of X where X~B(n,p)
\-> n -- Natural number - binomialMode(n, p) - Calculates the mode of X where X~B(n,p)
\-> n -- Natural number - binomialVariance(n, p) - Calculates the variance of X where X~B(n,p)
\-> n -- Natural number - binomialStandardDeviation(n, p) - Calculates the standard deviation of X where X~B(n,p)
\-> n -- Natural number
Functions - mathlib_pure.h
Arguments are assumed to be real numbers unless otherwise stated.- lim(f, a) - Gives the limit of f(x) as x approaches a.
- derivative(f, x) - Gives the value of f'(x).
- integral(f, a, b) - Gives the definite integral of f(x) between a and b.
- degrees_to_radians(x) - Converts x from degrees to radians.
- radians_to_degrees(x) - Converts x from radians to degrees.
- ml_sin(x) - Calculates sin(x) where x is in radians.
- ml_cos(x) - Calculates cos(x) where x is in radians.
- ml_tan(x) - Calculates tan(x) where x is in radians.
- arcsin(x) - Calculates arcsin(x)
- arccos(x) - Calculates arccos(x)
- arctan(x) - Calculates arctan(x)
Functions - mathlib_vector.h
Arguments are assumed to be vectors unless otherwise stated.- createVector(d) - Creates a vector structure with dimension d.
\-> d -- Natural number - destroyVector(*v) - Deallocates a vector structure.
- getVectorComponent(v, i) - Gets vi.
\-> i -- Natural number - setVectorComponent(*v, i, val) - Sets vi
\-> i -- Natural number
\-> val -- Real number - vector_add(a, b) - Adds vectors a and b
- vector_sub(a, b) - Subtracts vector b from vector a
- vector_scalar_multiply(a, b) - Multiplies vector a by scalar b.
\-> b -- Real number - vector_dot(a, b) - Calculates the dot product of a and b.
Functions - mathlib_complex.h
Arguments are assumed to be complex numbers unless otherwise stated.- argument(z) - Calculates arg(z).
- modulus(z) - Calculates |z|.
- conjugute(z) - Returns the complex conjugate of z.
- complex_sqrt(x) - Returns the complex square root of x.
\-> x -- Real number - complex_add(a, b) - Adds a and b.
- complex_sub(a, b) - Subtracts b from a.
- complex_mul(a, b) - Multiplies a and b.
- complex_div(a, b) - Divides a by b.
References
- Talyor Series - Wikipedia | Khan Academy
- A-Levels - Wikipedia