{"id":19592309,"url":"https://github.com/smikhalevski/algomatic","last_synced_at":"2025-04-27T14:33:39.878Z","repository":{"id":43877182,"uuid":"441748377","full_name":"smikhalevski/algomatic","owner":"smikhalevski","description":"🔢 Various algorithms and math utilities.","archived":false,"fork":false,"pushed_at":"2025-03-29T15:20:58.000Z","size":972,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T15:27:33.713Z","etag":null,"topics":["binary","binary-search","bitwise","byte","cdf","cspline","gauss","int","interpolation","lerp","math","number","parallel-sort","qsort","spline","uint"],"latest_commit_sha":null,"homepage":"https://smikhalevski.github.io/algomatic/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smikhalevski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-25T19:51:34.000Z","updated_at":"2025-03-29T15:21:35.000Z","dependencies_parsed_at":"2024-11-11T08:49:45.496Z","dependency_job_id":null,"html_url":"https://github.com/smikhalevski/algomatic","commit_stats":{"total_commits":16,"total_committers":1,"mean_commits":16.0,"dds":0.0,"last_synced_commit":"c355e4eb619a48c721e34d7845b7442ba823bcc4"},"previous_names":["smikhalevski/numeric-wrench"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Falgomatic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Falgomatic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Falgomatic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Falgomatic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smikhalevski","download_url":"https://codeload.github.com/smikhalevski/algomatic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251154707,"owners_count":21544546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary","binary-search","bitwise","byte","cdf","cspline","gauss","int","interpolation","lerp","math","number","parallel-sort","qsort","spline","uint"],"created_at":"2024-11-11T08:34:35.445Z","updated_at":"2025-04-27T14:33:39.848Z","avatar_url":"https://github.com/smikhalevski.png","language":"TypeScript","readme":"# Algomatic\n\nVarious algorithms and utilities.\n\n- High performance and low memory consumption (no heap allocations);\n- Lightweight and tree-shakable;\n- Thoroughly tested.\n\n🔎 [API documentation is available here.](https://smikhalevski.github.io/algomatic/)\n\n```shell\nnpm install --save-prod algomatic\n```\n\n- [Algebra](#algebra)\n- [Bitwise operations](#bitwise-operations)\n- [Distributions](#distributions)\n- [Easing](#easing)\n- [Interpolation](#interpolation)\n- [Search](#search)\n- [Sort](#sort)\n\n# Algebra\n\nClamp a value to range:\n\n```ts\nimport { clamp } from 'algomatic';\n\nconst f = clamp(5, 10);\n\nf(33);\n// ⮕ 10\n```\n\nScale a value from one range to another:\n\n```ts\nconst f = scale(0, 1, 50, 100);\n\nf(75);\n// ⮕ 0.5\n```\n\nRound a value to the closest value that is divided by divisor without any remainder:\n\n```ts\nconst f = snap(10);\n\nf(17);\n// ⮕ 20\n```\n\nBring a value to the range by adding or subtracting the range size:\n\n```ts\nconst f = cycle(0, 10);\n\nf(12);\n// ⮕ 2\n```\n\n# Bitwise operations\n\nBitwise operators that can manipulate integers in\n[[`MIN_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER), [`MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)]\nrange:\n\n```ts\nimport { left, right, and, or } from 'algomatic';\n\nleft(0x11, 8); // Same as 0x11 \u003c\u003c 8\n// ⮕ 0x11_00 \n\nleft(0x11_22_33_44, 24)\n// ⮕ 0x11_22_33_44_00_00_00\n\nright(0x11_22, 8); // Same as 0x11_22 \u003e\u003e 8\n// ⮕ 0x11\n\nright(0x11_22_22_44_55_66_77, 24);\n// ⮕ 0x11_22_22_44\n\nand(0x10_aa_bb_00_00_00_ff, 0x10_00_bb_cc_00_00_ff);\n// ⮕ 0x10_00_bb_00_00_00_ff\n\nor(0x10_aa_bb_00_00_00_ff, 0x10_00_bb_cc_00_00_ff);\n// ⮕ 0x10_aa_bb_cc_00_00_ff\n\nxor(0x10_aa_bb_00_00_00_ff, 0x10_00_bb_cc_00_00_ff);\n// ⮕ 0xaa_00_cc_00_00_00\n```\n\n# Distributions\n\nCreate an array and fill it with numbers that are evenly distributed in a range:\n\n```ts\nimport { seq } from 'algomatic';\n\nseq(3);\n// ⮕ [0, 0.5, 1]\n\nseq(4, -10, 2);\n// ⮕ [-10, -6, -2, 2]\n```\n\nCreate gaussian distribution ([normal distribution](https://en.wikipedia.org/wiki/Normal_distribution)):\n\n```ts\nseq(3).map(gauss(0.5, 0.3));\n// ⮕ [0.33, 1.32, 0.33]\n```\n\nCreate [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) for Gaussian\n(normal) distribution:\n\n```ts\nseq(3).map(cdfGauss(0.5, 0.3));\n// ⮕ [ 0.04, 0.5, 0.95 ]\n```\n\n[Inverse distribution function (quantile function)](https://en.wikipedia.org/wiki/Cumulative_distribution_function#Inverse_distribution_function_(quantile_function))\nfor Gaussian (normal) distribution:\n\n```ts\nseq(3).map(cdfGaussInv);\n```\n\n# Easing\n\nVarious easing functions:\n\n```ts\nimport { easeExp, easeInCubic } from 'algomatic';\n\nconst f = easeExp();\n\nf(0.5)\n// ⮕ 0.3775\n\neaseInCubic(0.5);\n// ⮕ 0.125\n```\n\n# Interpolation\n\nLinear interpolator:\n\n```ts\nimport { lerp } from 'algomatic';\n\nconst f = lerp(xs, ys);\n\nconst y = f(x);\n```\n\nHere `xs` is the array of X coordinates of pivot points in ascending order, and `ys` is the array of corresponding Y\ncoordinates of pivot points.\n\n[A cubic spline interpolator](https://en.wikipedia.org/wiki/Spline_(mathematics)#Algorithm_for_computing_natural_cubic_splines)\nfor given pivot points:\n\n```ts\nconst f = cspline(xs, ys);\nconst y = f(x);\n```\n\nMore control over cubic spline caching and computation:\n\n```ts\nimport { interpolateCSpline, populateCSplines } from 'algomatic';\n\n// Pre-allocate an array of spline components that can be later reused\n// to avoid excessive memory allocations\nconst splines = new Float32Array(xs.length * 3);\n\npopulateCSplines(xs, ys, xs.length, splines);\n// or\n// const splines = createCSplines(xs, ys, xs.length);\n\nconst y = interpolateCSpline(xs, ys, x, xs.length, splines);\n```\n\n[A monotone cubic interpolator](https://en.wikipedia.org/wiki/Monotone_cubic_interpolation) for given pivot points:\n\n```ts\nconst f = csplineMonot(xs, ys);\nconst y = f(x);\n```\n\nOr using more fine-grained approach:\n\n```ts\nimport { interpolateCSplineMonot, populateCSplinesMonot } from 'algomatic';\n\nconst y = interpolateCSplineMonot(xs, ys, x, xs.length, populateCSplinesMonot(xs, ys, xs.length));\n```\n\n# Search\n\nBinary search searches the specified array for the specified value using the binary search algorithm. The array must be\nsorted into ascending order according to the natural ordering of its elements prior to making this call. If it is not\nsorted, the results are undefined.\n\nReturns the index of the searched value, if it is contained in the array; otherwise, -(insertion point) - 1. The\ninsertion point is defined as the point at which the searched value would be inserted into the array: the index of the\nfirst element greater than the searched value, or array length if all elements in the array are less than the specified\nkey. Note that this guarantees that the return value will be ≥ 0 if and only if the searched value is found.\n\n```ts\nimport { binarySearch } from 'algomatic';\n\nbinarySearch([10, 20, 30, 40], 20);\n// ⮕ 1\n\nbinarySearch([10, 20, 30, 40], 25);\n// ⮕ -3\n```\n\nBinary search with a comparator:\n\n```ts\nbinarySearchComp(\n  [{x: 10}, {x: 20}, {x: 30}],\n  {x: 20},\n  (a, b) =\u003e a.x - b.x\n);\n```\n\n# Sort\n\nSort the array in-place:\n\n```ts\nimport { qsort } from 'algomatic';\n\nconst arr = [3, 2, 0, 1];\n\nqsort(arr);\n```\n\nSort the array in-place and invoke a callback after a pair of elements was swapped:\n\n```ts\nqsort(arr, (i, j) =\u003e {\n  // Called when i and j elements of arr were swapped.\n  // Use this to sort multiple arrays in parallel.\n});\n```\n\nSort using a comparator:\n\n```ts\nqsort(arr, undefined, (a, b) =\u003e {\n  // Comparator works the same way as in Array.sort\n  return 0;\n});\n```\n\n`qsort` uses a non-recursive [Quicksort](https://en.wikipedia.org/wiki/Quicksort) algorithm. In contrast to\n[`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort), `qsort`\ndoesn't convert array elements to strings before comparison and uses comparison operators directly. So numeric arrays\nare sorted in natural order with `qsort(arr)`. You can provide an element comparator to change the sorting order.\n\n`qsort` is _10x_ faster than `Array.sort` on both small and big arrays. The plot below uses a log scale and\nshows the dependency of number of operations per second from the input array length.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmikhalevski%2Falgomatic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmikhalevski%2Falgomatic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmikhalevski%2Falgomatic/lists"}