Understand Mathematics With The Help of Programming. Translated Mathematical Notations to JavaScript Code.

3 min read
Understand Mathematics With The Help of Programming. Translated Mathematical Notations to JavaScript Code.
Photo by Resource Database™ / Unsplash

As a self-taught programmer, reading academic papers can be intimidating. However, understanding mathematical notation is essential for many fields, including computer science. That's why I've created this guide to help developers to understand mathematics better by translating it and comparing it to JavaScript code.

One thing to keep in mind when working with math notation is that different symbols can mean different things depending on the context and field of study. It's also important to note that there are several conventions for naming variables, such as using italic lowercase letters for scalars, bold lowercase letters for vectors, and bold uppercase letters for matrices.

In terms of equalities and inequalities, the equals sign = is used for equality, the symbol is used for inequality, and the symbol is used for approximately equal to. The := symbol is used for definition, such as defining a variable or function. In JavaScript, we can use the === and !== operators to check for equality and inequality, respectively. We can also use a function like almostEqual to check for approximately equal values.

When working with square roots and complex numbers, we use the symbol for square roots and the i symbol to represent the imaginary number. In JavaScript, we can use the Math.sqrt function to calculate square roots and represent complex numbers using objects with real and imaginary properties.

Dot and cross products are commonly used in math, and they can be represented using the ·, ×, and symbols. In JavaScript, we can use the dot and cross functions from the gl-vec3 library to calculate these products.

Summation is represented using the Σ symbol, and products of sequences are represented using the Π symbol. These operations can be implemented in JavaScript using loops to iterate over the elements in a sequence and perform the appropriate operation.

Other commonly used symbols in math include the || pipes for absolute value, the hat â symbol for unit vectors, and the and symbols for "element of" and "not an element of", respectively. These concepts can be represented in JavaScript using functions or built-in methods like Math.abs and the in operator.

Functions are a crucial part of math and can be represented using the ƒ symbol. Piecewise functions, which are functions defined using different formulas for different ranges of input, can be represented using the "piecewise" keyword. In JavaScript, we can use functions to represent these concepts and use conditional statements to handle different ranges of input.

There are many common functions used in math, such as trigonometric functions and logarithmic functions, which can be represented using JavaScript's built-in Math object. Function notation can also be represented using the and symbols, and derivatives can be represented using the prime symbol. These concepts can be implemented in JavaScript using functions and the derivative function.

Other commonly used symbols in math include the and symbols for floor and ceiling, respectively, and various arrow symbols for different types of implications and equalities. These concepts can be represented in JavaScript using the Math.floor and Math.ceil functions and conditional statements.

To give you a better idea of how math notation can be translated into JavaScript code, let's look at some specific examples

One common math concept is the dot product of two vectors. In math notation, the dot product is represented using the · symbol, like so:

a · b = c

In JavaScript, we can use the dot function from the gl-vec3 library to calculate the dot product of two vectors:

const vec3 = require('gl-vec3'); 

const a = [1, 2, 3]; 
const b = [4, 5, 6]; 
const c = vec3.dot(a, b); 

console.log(c); // Outputs 32

Another common math concept is the cross product of two vectors. In math notation, the cross product is represented using the × symbol, like so:

a × b = c

In JavaScript, we can use the cross function from the gl-vec3 library to calculate the cross product of two vectors:

const vec3 = require('gl-vec3');

const a = [1, 2, 3]; 
const b = [4, 5, 6]; 
const c = vec3.cross(a, b); 

console.log(c); // Outputs [-3, 6, -3]

Summation is another common operation in math, and it is represented using the Σ symbol. In JavaScript, we can use a loop to iterate over the elements in a sequence and perform the summation operation:

const sequence = [1, 2, 3, 4, 5];
let sum = 0;

for (const element of sequence) {
  sum += element;
}

console.log(sum); // Outputs 15

The absolute value of a number is represented using the || pipes symbol in math. In JavaScript, we can use the Math.abs function to calculate the absolute value of a number:

const number = -5;
const absoluteValue = Math.abs(number);

console.log(absoluteValue); // Outputs 5

These are just a few examples of how math notation can be translated into JavaScript code. By using programming as a different point of view to understand math, we can see how math concepts are applied in a more concrete way and test out different scenarios to see how they affect the result.

Here is a great Github Repository called math-as-code which is a big cheat-sheet for mathematical notation in code form with a plenty of examples!

Happy Calculating!

Harduex blog


Follow