Giter Site home page Giter Site logo

smikhalevski / algomatic Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 1002 KB

πŸ”’β€‚Various algorithms and math utilities.

Home Page: https://smikhalevski.github.io/algomatic/

License: MIT License

TypeScript 97.38% JavaScript 2.62%
int uint byte number math spline interpolation cspline binary-search binary

algomatic's Introduction

Algomatic build

Various algorithms and utilities.

  • High performance and low memory consumption: in-out arguments and no heap allocations;
  • Lightweight and tree-shakable;
  • Thoroughly tested.

Algomatic is used in Paint Bucket, an extremely fast color manipulation library, check out its performance.

πŸ”’β€‚API documentation is available here.

npm install --save-prod algomatic

Arrays

Utilities copyOver range swap asc desc

binarySearch

Searches the specified array for the specified value using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements prior to making this call. If it is not sorted, the results are undefined.

Returns the index of the searched value, if it is contained in the array; otherwise, -(insertion point) - 1. The insertion point is defined as the point at which the searched value would be inserted into the array: the index of the first element greater than the searched value, or array length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be β‰₯ 0 if and only if the searched value is found.

binarySearch([10, 20, 30, 40], 20); // β†’ 1

binarySearch([10, 20, 30, 40], 25); // β†’ -3

sort

Sorts the array in-place using an optional comparator and invokes a callback after a pair of elements was swapped.

sort(
    arr, // Mutable array that would be sorted
    (i, j) => {
      // Called when i and j elements of arr were swapped
      // Use this to sort multiple arrays in parallel
    },
    (a, b) => 0, // Comparator works the same way as in Array.sort
);

sort uses a non-recursive Quicksort algorithm. In contrast to Array.sort, sort doesn't convert array elements to strings before comparison and uses comparison operators directly. So numeric arrays are sorted in natural order with sort(arr). You can provide an element comparator to change the sorting order.

sort is order of magnitude faster than Array.sort on both small and big arrays. The plot below uses a log scale and shows the dependency of number of operations per second from the input array length.

sort vs Array.sort performance comparison

Interpolation

lerp

Creates a linear interpolator:

const f = lerp(xs, ys);
const y = f(x);

Here xs is the array of X coordinates of pivot points in ascending order, and ys is the array of corresponding Y coordinates of pivot points.

cspline

Creates a cubic spline interpolator for given pivot points:

const f = cspline(xs, ys);
const y = f(x);

More control over spline caching and computation:

// Pre-allocate an array of spline components that can be later reused
// to avoid excessive memory allocations
const splines = new Float32Array(xs.length * 3);

createCSplines(xs, ys, xs.length, splines); // β†’ splines
// or
// const splines = createCSplines(xs, ys, xs.length); // β†’ Float32Array

const y = interpolateCSpline(xs, ys, x, xs.length, splines);

csplineMonot

Creates a monotone cubic interpolator for given pivot points:

const f = csplineMonot(xs, ys);
const y = f(x);

Or using more fine-grained approach:

const y = interpolateCSplineMonot(xs, ys, x, xs.length, createCSplinesMonot(xs, ys, xs.length));

The plot below shows that cspline interpolation overshoots pivot points while csplineMonot provides monotonous results.

cspline and csplineMonot comparison

Bitwise

Utilities int byte uint

Bitwise operations left, right, and, or and xor for unsigned integers that exceed 32-bit range:

left(0xAB, 8); // Same as 0xAB << 8
// β†’ 0xAB_00 

left(0xAB_CD_EF_AB_CD, 24)
// β†’ 0xAB_CD_EF_AB_CD_00_00_00

right(0xAB_CD, 8); // Same as 0xAB_CD >> 8
// β†’ 0xAB

algomatic's People

Contributors

smikhalevski avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jgonggrijp

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.