DEV Community

Cover image for How I Built a NumPy-Like Library in Pure JavaScript
Prasoon  Jadon
Prasoon Jadon

Posted on

How I Built a NumPy-Like Library in Pure JavaScript

How I Built a NumPy-Like Library in Pure JavaScript

While learning data science with Python, one library impressed me more than any other: NumPy.

NumPy contains many essential numerical and statistical functions that make working with data much easier. Instead of writing complex mathematical logic from scratch, you can simply call built-in functions such as mean, sum, or dot.

As I continued learning, a question came to my mind:

“Can I build something similar using pure JavaScript?”

This curiosity led me to explore how NumPy works internally and how its core ideas could be recreated in another language. After studying the concepts and experimenting with JavaScript, I started building a small numerical utility library inspired by NumPy.

In this essay, I will explain how I built that library and the logic behind some of its functions.


Starting with Basic Arithmetic

After understanding the core idea, I started with the simplest building blocks: basic arithmetic operations.

These functions work on numerical values and form the foundation for more advanced statistical calculations.

1. The add() Function

I began with the simplest operation: addition.

function add(x, y) {
  return x + y;
}
Enter fullscreen mode Exit fullscreen mode

This function takes two numbers as input and returns their sum.

Although the logic is simple, it demonstrates the basic structure of a numerical utility function. Using the same idea, I also implemented other arithmetic operations such as:

  • subtraction
  • multiplication
  • division
  • power

Each function follows the same pattern: take inputs, perform a mathematical operation, and return the result.


Moving Toward Statistics

After implementing basic arithmetic functions, I moved toward statistical functions.

A good starting point for statistics is the mean.

The mean is essentially the average of a set of numbers, and its mathematical formula is:

[
\text{Mean} = \frac{\sum x}{n}
]

Where:

  • ∑x represents the sum of all values
  • n represents the number of values

Before implementing the mean function, we first need a sum function that works with arrays.


Implementing the sum() Function

The sum() function adds all numbers inside an array.

function sum(arr) {
  return arr.reduce((a, b) => a + b, 0);
}
Enter fullscreen mode Exit fullscreen mode

Here we use JavaScript's built-in reduce() method.

The reduce() function processes each element in the array and combines them into a single result. In this case, it repeatedly adds numbers together until the final total is produced.

For example:

sum([1,2,3,4])
// Output: 10
Enter fullscreen mode Exit fullscreen mode

This function also simulates the sigma (∑) notation used in mathematics.


Implementing the mean() Function

Once we have a sum() function, implementing the mean becomes very simple.

function mean(arr) {
  return sum(arr) / arr.length;
}
Enter fullscreen mode Exit fullscreen mode

This function follows the mathematical formula exactly:

  1. Calculate the sum of the array
  2. Divide it by the number of elements

Example:

mean([1,2,3,4])
// Output: 2.5
Enter fullscreen mode Exit fullscreen mode

By combining small functions like sum() and mean(), we can gradually build a library that performs many useful statistical calculations.


What This Project Demonstrates

This small project shows that many numerical tools used in libraries like NumPy are built from simple mathematical ideas.

By implementing these functions in JavaScript, we can better understand:

  • how numerical libraries work internally
  • how statistical formulas translate into code
  • how to design reusable utility functions

Although this library is much simpler than NumPy, it demonstrates the core concepts behind numerical computing.


Conclusion

Building a NumPy-like library in JavaScript was both a learning exercise and a programming challenge. It helped me understand the logic behind many statistical functions and how mathematical ideas can be translated into code.

Projects like this show that even complex libraries are ultimately built from small, simple functions combined together.

By exploring these concepts, developers can gain a deeper understanding of both programming and mathematics.


Top comments (0)