# number-pairings

JavaScript number pairings functions

## Stats

StarsIssuesVersionUpdatedCreatedSize
number-pairings
1.7.56 years ago6 years ago

number-pairings
JavaScript number pairing functions
This is a fun project about pairing functions, the coding of two natural numbers into one and vice versa. Now implemented are:
• Pairing for finite fields (both dimensions are limited) (`field`).
• Pairings for one finite and one infinite dimension (`stack_y`, `stack_x`).
• Cantor pairing (`Cantor`), and a half variant for unordered pairs (`half`).
• elegant pairing (`elegant`).
• POTO pairing (`poto`). The formulas are derived from here.

There is also a composition operator available that can mix arbitrary (finite and infinite) pairings recursively. With this operator it is possible to encode lists of natural numbers into a single number and back.

## Installation

Prerequisite: Node.js needs to be installed.
1. Make a folder
2. Open a console in that new folder
3. Install locally with: `npm install number-pairings`

## Usage

``````const np = require( "number-pairings" )
let pair = np.Cantor()
console.log( pair.split( 100 ) )
// => [ 4, 9 ] ( = [x, y] )
console.log( pair.join( 4, 9 ) )
// => 100 ( = z )``````

Replace `np.Cantor` with e.g. `np.elegant` to try out the elegant pairing.

## Build

Build from source: `npm run build`.

## Limitations

• There are no overflow checks. Since `z` (result of the `join` functions) is always in the order of a multiplication of `x` and `y` (results of the `split` functions) the library only works up to some numbers. There are no warnings given. Use with caution.
• There are other possible number pairings that could be included. Some of them are not "dense", i.e. more than one pair may encode a number. Here the focus is on dense pairings.

MIT, see license file in the repository.

## Examples

``````const np = require("number-pairings")
let p = np.Cantor()
p.join( 10, 34 ) // => 1024
p.split( 1024 ) // => [ 10, 34 ]
p = np.elegant()
p.join( 10, 34 ) // => 1200
p.split( 1200 ) // => [ 10, 34 ]
p = np.poto()
p.join( 10, 34 ) // => 70655
p.split( 70655 ) // => [ 10, 34 ]
p = np.half()
p.join( 10, 34 ) // => 605
p.join( 34, 10 ) // => 605
p.split( 605 ) // => [ 10, 34 ]
p = np.field( 2, 3 )
p.join( 1, 2 ) // => 5
p.join( 2, 2 ) // => undefined (out of bound)
p.split( 5 ) // => [ 1, 2 ]
p = np.stack_x( 5 )
p.join( 2, 4 ) // => 14
p.join( 2, 5 ) // => undefined (out of bound)
// note: use stack_y the same way as stack_x
p.split( 14 ) // => [ 2, 4 ]
p = np.composition( [ 2, 3, 4, 5 ] )
p.join( [ 0, 1, 2, 3 ] ) // => 86
p.split( 86 ) // => [ 0, 1, 2, 3 ]
p.join( [ 1, 0, 0, 0 ] ) // => 1
p.join( [ 0, 1, 0, 0 ] ) // => 2
p.join( [ 0, 0, 1, 0 ] ) // => 6
p.join( [ 0, 0, 0, 1 ] ) // => 24``````