Skip to content

Section 1: Math

OrderOfInfinity edited this page Jan 1, 2025 · 4 revisions

Math

in pryttier.math


1. summation(n: float | int, i: float | int, expr: Callable) -> float

This function takes an expression expr , repeats it from n to i and sums all the iteration, it is similar to:

$$ \sum^{i}_{n} (expr) $$

The reason why expr parameter is of type Callable is because it is a function that takes the current iteration into account

Examples:

summation(0, 5, lambda n: 1)

is the same as:

$$ \sum^{5}_{n=0} 1 = 1 + 1 + 1 + 1 + 1 = 6 $$

and

summation(0, 5, lambda n: n + 1)

is the same as:

$$ \sum^{5}_{n=0} (n + 1) = (0 + 1) + (1 + 1) + (2 + 1) + (3 + 1) + (4 + 1) + (5 + 1) = 1 + 2 + 3 + 4 + 5 + 6 = 21 $$


2. product(n: int, i: int, expr: Callable) -> float

This function is similar to the summation but instead of adding, it multiplies the iterations, It is similar to:

$$ \prod^{i}_{n}(expr) $$

Example:

product(0, 3, lambda n: 2)

is the same as:

$$ \prod^{3}_{n=0} 2 = 2 \cdot 2 \cdot 2 \cdot 2 = 16 $$

and

product(0, 5, lambda n: 2
n)

is the same as:

$$ \prod^{5}_{n=1} 2n = 2(1) \cdot 2(2) \cdot 2(3) = 2 \cdot 4 \cdot 6 = 48 $$


3. clamp(num: float, low: float, high: float) -> float

This function takes in a number num and returns it if it is in a given range from low to high (both inclusive). if the number is below the range, it will return low and if the number is above the range, it will return high.

Example:

clamp(2, 0, 5)  # This will return 2 itself because it is in the range from 0 to 5.
clamp(-1, 0, 5)  # This will return 0 (which is `low`) as -1 is smaller than 0 and not in the range from 0 to 5.
clamp(10, 0, 5)  # This will return 5 (which is `high`) as 10 is greater than 5 and not in the range from 0 to 5.

4. sign(num: float) -> int

This function will simply return the sign of the given number num

Example:

sign(-5)  # Will return -1, representing negative sign
sign(7)  # Will return 1, representing positive sign

5. factorial(num: int) -> int

This function returns the factorial of a given number num.

Example:

factorial(5)

is the same as:

$$ 5! = 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 = 120 $$


6. binToDec(num: int) -> int

This function converts a binary (Base 2) number to decimal

Example:

binToDec(101)  # Will return 5
binToDec(10001)  # Will return 17
binToDec(101101)  # Will return 45

7. mapRange(value: int | float, min1: float, max1: float, min2: float, max2: float) -> float

This function takes a given number value in a range from min1 to max1 and converts it into a number in the range min2 from max2

It maps a value from one range to another

For example: 5 in range 0 to 10, when mapped to range 0 to 100, will be 50

Examples:

mapRange(5, 0, 10, 0, 100)  # Will output 50

8. Vector2(x: float | int, y: float | int)

This is a Vector2 class having x and y values as inputs.

You can add, subtract, and multiply Vectors.

Example:

Vector2(5, 1)

9. Vector3(x: float | int, y: float | int)

The Vector3 class is similar to Vector2 but with an additional z input and has the same operations as Vector2.

Example:

Vector3(4, 5, 3)

10. cross(a: Vector2 | Vector3, b: Vector2 | Vector3) -> Vector2 | Vector3 (Not Implemented)

This function takes in two vectors (Vector2 or Vector3 but not both at the same time) and cross multiplies them.

cross(Vector2(5, 5), Vector2(1, 2))  # will return -2
cross(Vector3(1, 2, 3), Vector3(2, 3, 4))  # will return Vector3(-1, 2, -1)
cross(Vector2(5, 5), Vector3(4, 5, 2))  # will give an error because cannot multiply Vector2 with Vector3

11. distance(a: Vector2 | Vector3 | Coord, b: Vector2 | Vector3 | Coord)

This function returns the distance between two vectors

Example:

distance(Vector2(1, 2), Vector2(5, 2))  # Will return 4
distance(Vector2(2, 3), Vector2(4, 3))  # Will return 2
distance(Vector2(7, 8), Vector2(0, 0))  # Will return 10.63014581273465
And that should be it for the 'Math' section. Let's Move on!