DEV Community

Aria Diniz
Aria Diniz

Posted on

Advanced Math Library for C

Hello There!

In this post I will share a C library I created: Advanced Math Library, or libamath. This is a C library that centralizes some of my mathematical implementations, focusing on performance and multithreading.

libamath includes algorithms such as Kendall Correlation, Genetic Algorithms for optimization, Fourier Transforms, and various statistical calculations like mean, median, and standard deviation. I'm also planning to add support for BigInt factorial, which will offer higher precision for Poisson Distribution and other advanced calculations. Many of these functions are optimized with multithread support to handle intensive computational tasks.

Here are some examples of how you can use libamath:

  1. Kendall Correlation:
double data1[] = {1.0, 2.0, 3.0};
double data2[] = {3.0, 2.0, 1.0};
double tau = amath_kcorr(data1, data2, 3);
printf("Kendall's Tau: %f\n", tau);
Enter fullscreen mode Exit fullscreen mode
  1. Genetic Algorithm:
void *fitness_function(Individuals *individuals) {
  // Define fitness logic
  return NULL;
}
Individuals *pop = amath_generate_individuals(100, 0.05, 0.001, 0.25, 4, 0.0, 1.0);
for (int i = 0; i < 1000; i++) {
  amath_fit(pop, fitness_function);
  amath_mutate(pop);
  amath_reproduce(pop);
}
amath_destroy_individuals(pop);
Enter fullscreen mode Exit fullscreen mode
  1. Discrete Fourier Transform (DFT):
double complex data[] = {1.0, 2.0, 3.0, 4.0};
amath_dft(data, 4, 2); // Perform DFT using 2 threads
Enter fullscreen mode Exit fullscreen mode
  1. Mean:
double data[] = {1.0, 2.0, 3.0};
double mean_value = amath_mean(data, 3);
printf("Mean: %f\n", mean_value);
Enter fullscreen mode Exit fullscreen mode

For those familiar with my previous repositories, libamath brings together both the Kendall Correlation (now with performance improvements) and the Genetic Algorithm implementations into one place. This will make it easier to expand and manage the tools over time.

In my free time, I intend to add even more features, including:

  • Variance Calculation: Handy alongside the standard deviation.
  • Covariance: To measure how two datasets vary together.
  • Linear Regression: To model relationships between variables.
  • Binomial Distribution: A great complement to the Poisson distribution.
  • Gamma Distribution: Another versatile probability distribution.

This is something I built some time ago, since I often use these functions in my work, and I decided to share it in case anyone else finds it useful.

You can check out the project and contribute here: https://github.com/ariasdiniz/advanced_math_lib

As always, suggestions and feedback are very welcome!

Top comments (0)