Skip to content

Commit bbffc30

Browse files
committed
Adding some functions for mapping between the integers, natural numbers, and ordered pairs of natural numbers.
1 parent f29ca1e commit bbffc30

File tree

7 files changed

+93
-10
lines changed

7 files changed

+93
-10
lines changed

GameMakerScripts.yyp

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ All scripts are included in the `scripts/` folder of this repository. Each scrip
66

77
The following is a brief description of the included functions, divided into rough categories.
88

9+
## Analysis Functions
10+
11+
A variety of scripts dealing with functions and sets, which may have some uses for managing data structures.
12+
13+
* [`_integer_to_natural`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_integer_to_natural/_integer_to_natural.gml): Converts an integer to a unique natural number using the zig-zagging bijection from _0, 1, -1, 2, -2, ..._ to _0, 1, 2, 3, 4, ..._. The inverse of `_natural_to_integer`.
14+
* [`_natural_to_integer`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_natural_to_integer/_natural_to_integer.gml): Converts a natural number to a unique integer using the zig-zagging bijection from _0, 1, 2, 3, 4, ..._ to _0, 1, -1, 2, -2, ..._. The inverse of `_integer_to_natural`.
15+
* [`_natural_to_pair`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_natural_to_pair/_natural_to_pair.gml): Converts a natural number to a unique ordered pair of natural numbers using the [inverse Cantor pairing function](https://en.wikipedia.org/wiki/Pairing_function#Inverting_the_Cantor_pairing_function). The inverse of `_pair_to_natural`.
16+
* [`_pair_to_natural`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_pair_to_natural/_pair_to_natural.gml): Converts an ordered pair of natural numbers to a unique natural number using the [Cantor pairing function](https://en.wikipedia.org/wiki/Pairing_function#Cantor_pairing_function). The inverse of `_pair_to_natural`.
17+
918
## Array Functions
1019

1120
Common array functions (such as searching and counting).
@@ -23,20 +32,20 @@ Common array functions (such as searching and counting).
2332

2433
Numerical algorithms for computational mathematics.
2534

26-
* [`_cubic_spline_coefficients`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_cubic_spline_coefficients/_cubic_spline_coefficients.gml): Calculates vectors of coefficients for the natural cubic polynomial interpolating a given data set.
35+
* [`_cubic_spline_coefficients`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_cubic_spline_coefficients/_cubic_spline_coefficients.gml): Calculates vectors of polynomial coefficients for the natural [cubic spline](https://en.wikipedia.org/wiki/Spline_interpolation) interpolating a given data set.
2736
* [`_cubic_spline_evaluate`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_cubic_spline_evaluate/_cubic_spline_evaluate.gml): Evaluates a piecewise cubic polynomial at a specific value given a given set of coefficient vectors and intervals.
28-
* [`_root_bisection`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_root_bisection/_root_bisection.gml): Finds a function root on a specified interval using the bisection method.
29-
* [`_root_newton`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_root_newton/_root_newton.gml): Finds a function root given a derivative and an initial guess using Newton's method.
37+
* [`_root_bisection`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_root_bisection/_root_bisection.gml): Finds a function root on a specified interval using the [bisection method](https://en.wikipedia.org/wiki/Bisection_method).
38+
* [`_root_newton`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_root_newton/_root_newton.gml): Finds a function root given a derivative and an initial guess using [Newton's method](https://en.wikipedia.org/wiki/Newton%27s_method).
3039

3140
## Linear Algebra Functions
3241

33-
Common linear algebra algorithms for dealing with matrices and vectors. In all functions, _vectors_ are considered to be 1D arrays while _matrices_ are 2D arrays.
42+
Common linear algebra algorithms for dealing with matrices and vectors. In all functions, _vectors_ are considered to be 1D arrays while _matrices_ are 2D arrays (i.e. arrays of arrays).
3443

35-
* [`_linear_solve`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_linear_solve/_linear_solve.gml): Solves a linear system given a coefficient matrix and righthand vector (using Gaussian elimination with partial pivoting).
44+
* [`_linear_solve`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_linear_solve/_linear_solve.gml): Solves a linear system given a coefficient matrix and righthand vector (using [Gaussian elimination with partial pivoting](https://en.wikipedia.org/wiki/Pivot_element)).
3645
* [`_matrix_multiply`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_matrix_multiply/_matrix_multiply.gml): Performs matrix-matrix, matrix-vector, and vector-vector multiplication.
3746
* [`_matrix_transpose`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_matrix_transpose/_matrix_transpose.gml): Transposes a 2D matrix.
38-
* [`_tridiagonal_solve`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_tridiagonal_solve/_tridiagonal_solve.gml): Solves a tridiagonal system (using the Thomas algorithm).
39-
* [`_triangular_solve`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_triangular_solve/_triangular_solve.gml): Solves an upper or lower triangular system (using forward or back substitution).
47+
* [`_tridiagonal_solve`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_tridiagonal_solve/_tridiagonal_solve.gml): Solves a tridiagonal system (using the [Thomas algorithm](https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm)).
48+
* [`_triangular_solve`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_triangular_solve/_triangular_solve.gml): Solves an upper or lower triangular system (using [forward or back substitution](https://www.cs.cornell.edu/~bindel/class/cs6210-f12/notes/lec08.pdf)).
4049
* [`_unit_vector`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_unit_vector/_unit_vector.gml): Defines a unit direction vector between two coordinates.
4150
* [`_vector_angle`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_vector_angle/_vector_angle.gml): Calculates the acute angle between two vectors.
4251
* [`_vector_distance`](https://github.com/adam-rumpf/game-maker-scripts/blob/master/scripts/_vector_distance/_vector_distance.gml): Calculates the Lp-distance between two vectors.

scripts/_cubic_spline_evaluate/_cubic_spline_evaluate.gml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// @func _cubic_spline_evaluate(a, b, c, d, x, t)
2-
/// @desc Evaluate a natural cubic spline at point t for coefficient vectors a, b, c, and d and input data vector x.
2+
/// @desc Evaluates a natural cubic spline at point t for coefficient vectors a, b, c, and d and input data vector x.
33
/// @param {real[]} a Vector of cubic coefficients [a0, a1, ..., a(n-1)].
44
/// @param {real[]} b Vector of quadratic coefficients [b0, b1, ..., b(n-1)].
55
/// @param {real[]} c Vector of linear coefficients [c0, c1, ..., c(n-1)].
@@ -26,6 +26,7 @@ function _cubic_spline_evaluate(a, b, c, d, xx, t)
2626
while ((t >= xx[ii+1]) && (ii < n-2))
2727
ii++;
2828

29-
// Evaluate the function using the corresponding coefficients
30-
return a[ii]*power((t-xx[ii]), 3) + b[ii]*sqr(t-xx[ii]) + c[ii]*(t-xx[ii]) + d[ii];
29+
// Evaluate the polynomial using Horner's method and the coefficients for the chosen interval
30+
var dx = t - xx[ii]; // position within interval
31+
return ((a[ii]*dx + b[ii])*dx + c[ii])*dx + d[ii];
3132
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// @func _integer_to_natural(n)
2+
/// @desc Returns a unique natural number (beginning with 0) for a given integer, according to the straightforward zig-zagging bijection (0, 1, -1, 2, -2, ...).
3+
/// @param {int} n Integer for which to generate a unique natural number.
4+
/// @return {int} Natural number (at least 0) corresponding to integer n.
5+
/**
6+
* This function is meant to be the inverse of the _nautral_to_integer()
7+
* function. Both are based on the bijection which maps (0, 1, -1, 2, -2, ...)
8+
* to (0, 1, 2, 3, 4, ...).
9+
*
10+
* For the Z -> N bijection we use the following piecewise definition:
11+
* |2n|-1 if n > 0
12+
* |2n| if n <= 0
13+
*/
14+
15+
function _integer_to_natural(n)
16+
{
17+
// Apply piecewise definition depending on the sign of n
18+
if (n > 0)
19+
return floor(2*n - 1);
20+
else
21+
return floor(-2*n);
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// @func _natural_to_integer(n)
2+
/// @desc Returns a unique integer for a given natural number (beginning with 0), according to the straightforward zig-zagging bijection (0, 1, -1, 2, -2, ...).
3+
/// @param {int} n Natural number for which to generate a unique integer.
4+
/// @return {int} Integer corresponding to natural number n.
5+
/**
6+
* This function is meant to be the inverse of the _integer_to_natural()
7+
* function. Both are based on the bijection which maps (0, 1, 2, 3, 4, ...)
8+
* to (0, 1, -1, 2, -2, ...).
9+
*
10+
* For the N -> Z bijection we use the following piecewise definition:
11+
* -n/2 if n is even
12+
* (n+1)/2 if n is odd
13+
*/
14+
15+
function _natural_to_integer(n)
16+
{
17+
// Apply piecewise definition depending on the parity of n
18+
if (n % 2 == 0)
19+
return floor(-n/2);
20+
else
21+
return floor((n+1)/2);
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// @func _natural_to_pair(x)
2+
/// @desc Returns a unique ordered pair of natural numbers (beginning with 0) for a given natural numbers (via the inverse Cantor pairing function).
3+
/// @param {int} x Nonnegative integer for which to find a coordinate.
4+
/// @return {int[]} Array of length 2 containing a pair of natural numbers (at least 0) corresponding to the input number.
5+
6+
function _natural_to_pair(xx)
7+
{
8+
// Apply the inverse Cantor pairing function
9+
var sum = floor((sqrt(8*xx+1)-1)/2);
10+
var triangle = (sqr(sum)+sum)/2;
11+
var yy = xx - triangle;
12+
return [sum - yy, yy];
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// @func _pair_to_natural(m, n)
2+
/// @desc Returns a unique natural number (beginning with 0) for a given ordered pair of natural numbers (via the Cantor pairing function).
3+
/// @param {int} m First nonnegative integer coordinate.
4+
/// @param {int} n Second nonnegative integer coordinate.
5+
/// @return {int} Natural number (at least 0) corresponding to the ordered pair (m, n).
6+
7+
function _pair_to_natural(m, n)
8+
{
9+
// Apply the definition of the Cantor pairing function
10+
return floor(((m+n)*(m+n+1))/2 + n);
11+
}

0 commit comments

Comments
 (0)