@gkucmierz/utils 3.0.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/main.mjs +9 -1
- package/package.json +11 -2
- package/src/chunks.mjs +64 -0
- package/src/mobius.mjs +31 -0
package/README.md
CHANGED
|
@@ -45,14 +45,25 @@ This library provides a wide range of mathematical functions and data structures
|
|
|
45
45
|
- `squareRoot`: Integer square root using Newton's method.
|
|
46
46
|
- `goldenRatio`, `goldenRatioBI`: The Golden Ratio calculation (with arbitrary precision support).
|
|
47
47
|
|
|
48
|
+
- **Optimization & Machine Learning**:
|
|
49
|
+
- `nelderMead`: Nelder-Mead (simplex) optimization algorithm for multi-dimensional functions.
|
|
50
|
+
- `particleSwarmOptimization`: PSO algorithm for global optimization.
|
|
51
|
+
- `simulatedAnnealing`: Probabilistic technique for approximating the global optimum of a given function.
|
|
52
|
+
|
|
53
|
+
- **Math & 3D Geometry**:
|
|
54
|
+
- `math3d`: Utilities for 3D vector and matrix math (`crossProduct`, `dotProduct`, `normalize`, `multiplyMatrix4`, `projectToTrackball`, `getRotationMatrixFromVectors`).
|
|
55
|
+
- `randNormal`: Normal (Gaussian) distribution random number generator (Box-Muller transform).
|
|
56
|
+
|
|
48
57
|
- **Automata & Simulation**:
|
|
49
58
|
- `createLangtonsAnt`, `createUnlimitedGrid`: Infinite 2D grid and Langton's Ant cellular automaton engine (Project Euler 349 compatible).
|
|
50
59
|
|
|
51
60
|
- **String & Encoding & Arrays**:
|
|
61
|
+
- `chunks`, `chunksIterator`, `chunksAsyncIterator`: Synchronous and asynchronous array/string chunking generators.
|
|
52
62
|
- `base64`: Base64 and Base64Url encoding/decoding.
|
|
53
63
|
- `copyCase`: Match case of a string to another.
|
|
54
64
|
- `arrayHistogram`: Creates Map mappings counting absolute frequencies across any `Iterable`.
|
|
55
65
|
- `bijectiveNumeration`: Bijective base-k numeration system.
|
|
66
|
+
- `formatBigNumber`: Intelligent formatting for massively large numbers with suffixes.
|
|
56
67
|
|
|
57
68
|
- **Developer Utilities**:
|
|
58
69
|
- `measurePerformance`: A micro-benchmarking tool for high-iteration performance testing.
|
|
@@ -60,6 +71,7 @@ This library provides a wide range of mathematical functions and data structures
|
|
|
60
71
|
- `consumeIteratorNonBlocking`: Yield consumption macro-tasks queue resolver for heavy computations.
|
|
61
72
|
- `memoize`: Function memoization based on arguments.
|
|
62
73
|
- `binarySearch`: Various binary search implementations (exact, lower bound, upper bound).
|
|
74
|
+
- `naturalSearch`: Natural sorting/search utility for human-readable string comparisons.
|
|
63
75
|
- `range2array`, `array2range`: Convert between ranges and arrays.
|
|
64
76
|
- `getType`: Precise dynamic type checking.
|
|
65
77
|
|
package/main.mjs
CHANGED
|
@@ -17,6 +17,9 @@ import {
|
|
|
17
17
|
import {
|
|
18
18
|
binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl
|
|
19
19
|
} from './src/binary-search.mjs'
|
|
20
|
+
import {
|
|
21
|
+
chunks, chunksAsyncIterator, chunksIterator
|
|
22
|
+
} from './src/chunks.mjs'
|
|
20
23
|
import {
|
|
21
24
|
combinations, combinationsIterator
|
|
22
25
|
} from './src/combinations.mjs'
|
|
@@ -80,6 +83,9 @@ import {
|
|
|
80
83
|
import {
|
|
81
84
|
memoize
|
|
82
85
|
} from './src/memoize.mjs'
|
|
86
|
+
import {
|
|
87
|
+
mobius, mobiusBI
|
|
88
|
+
} from './src/mobius.mjs'
|
|
83
89
|
import {
|
|
84
90
|
mod, modBI
|
|
85
91
|
} from './src/mod.mjs'
|
|
@@ -129,6 +135,7 @@ export * from './src/array-histogram.mjs';
|
|
|
129
135
|
export * from './src/base64.mjs';
|
|
130
136
|
export * from './src/bijective-numeration.mjs';
|
|
131
137
|
export * from './src/binary-search.mjs';
|
|
138
|
+
export * from './src/chunks.mjs';
|
|
132
139
|
export * from './src/combinations.mjs';
|
|
133
140
|
export * from './src/consume-iterator.mjs';
|
|
134
141
|
export * from './src/copy-case.mjs';
|
|
@@ -150,6 +157,7 @@ export * from './src/math3d.mjs';
|
|
|
150
157
|
export * from './src/matrix.mjs';
|
|
151
158
|
export * from './src/measure-performance.mjs';
|
|
152
159
|
export * from './src/memoize.mjs';
|
|
160
|
+
export * from './src/mobius.mjs';
|
|
153
161
|
export * from './src/mod.mjs';
|
|
154
162
|
export * from './src/n-choose-k.mjs';
|
|
155
163
|
export * from './src/natural-search.mjs';
|
|
@@ -166,5 +174,5 @@ export * from './src/square-root.mjs';
|
|
|
166
174
|
export * from './src/tonelli-shanks.mjs';
|
|
167
175
|
|
|
168
176
|
export default [
|
|
169
|
-
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
177
|
+
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, chunks, chunksAsyncIterator, chunksIterator, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mobius, mobiusBI, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
170
178
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Usefull functions for solving programming tasks",
|
|
6
6
|
"keywords": [
|
|
@@ -44,7 +44,16 @@
|
|
|
44
44
|
"square-root",
|
|
45
45
|
"tonelli-shanks",
|
|
46
46
|
"trie",
|
|
47
|
-
"utils"
|
|
47
|
+
"utils",
|
|
48
|
+
"chunks",
|
|
49
|
+
"math3d",
|
|
50
|
+
"nelder-mead",
|
|
51
|
+
"particle-swarm",
|
|
52
|
+
"simulated-annealing",
|
|
53
|
+
"rand-normal",
|
|
54
|
+
"format-big-number",
|
|
55
|
+
"natural-search",
|
|
56
|
+
"mobius"
|
|
48
57
|
],
|
|
49
58
|
"files": [
|
|
50
59
|
"main.mjs",
|
package/src/chunks.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export const chunks = (input, size = 1) => {
|
|
2
|
+
if (size < 1) size = 1;
|
|
3
|
+
|
|
4
|
+
if (typeof input === 'string' || Array.isArray(input)) {
|
|
5
|
+
const numChunks = Math.ceil(input.length / size);
|
|
6
|
+
const result = new Array(numChunks);
|
|
7
|
+
for (let i = 0, offset = 0; i < numChunks; ++i, offset += size) {
|
|
8
|
+
result[i] = input.slice(offset, offset + size);
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Fallback for other synchronous iterables (Sets, Maps, custom iterators)
|
|
14
|
+
const result = [];
|
|
15
|
+
let currentChunk = [];
|
|
16
|
+
for (const item of input) {
|
|
17
|
+
currentChunk.push(item);
|
|
18
|
+
if (currentChunk.length === size) {
|
|
19
|
+
result.push(currentChunk);
|
|
20
|
+
currentChunk = [];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (currentChunk.length > 0) {
|
|
24
|
+
result.push(currentChunk);
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function* chunksIterator(iterable, size = 1) {
|
|
30
|
+
if (size < 1) size = 1;
|
|
31
|
+
|
|
32
|
+
const isString = typeof iterable === 'string';
|
|
33
|
+
let currentChunk = [];
|
|
34
|
+
|
|
35
|
+
for (const item of iterable) {
|
|
36
|
+
currentChunk.push(item);
|
|
37
|
+
if (currentChunk.length === size) {
|
|
38
|
+
yield isString ? currentChunk.join('') : currentChunk;
|
|
39
|
+
currentChunk = [];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (currentChunk.length > 0) {
|
|
44
|
+
yield isString ? currentChunk.join('') : currentChunk;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function* chunksAsyncIterator(asyncIterable, size = 1) {
|
|
49
|
+
if (size < 1) size = 1;
|
|
50
|
+
|
|
51
|
+
let currentChunk = [];
|
|
52
|
+
|
|
53
|
+
for await (const item of asyncIterable) {
|
|
54
|
+
currentChunk.push(item);
|
|
55
|
+
if (currentChunk.length === size) {
|
|
56
|
+
yield currentChunk;
|
|
57
|
+
currentChunk = [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (currentChunk.length > 0) {
|
|
62
|
+
yield currentChunk;
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/mobius.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { factors, factorsBI } from './factors.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the Mobius function μ(n) of a number.
|
|
5
|
+
* @param {number} n - A positive integer.
|
|
6
|
+
* @returns {number} The Mobius function value (1, -1, or 0).
|
|
7
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LFARiArgZwQXwQDMYowEAiAAQHMBrLAYzBAFMYAvAeizhABscFANzBgaTLlFA|▶ Try it live in Instacode}
|
|
8
|
+
*/
|
|
9
|
+
export const mobius = n => {
|
|
10
|
+
if (n < 1) return 0;
|
|
11
|
+
if (n === 1) return 1;
|
|
12
|
+
const f = factors(n);
|
|
13
|
+
const unique = new Set(f);
|
|
14
|
+
if (unique.size !== f.length) return 0;
|
|
15
|
+
return f.length % 2 === 0 ? 1 : -1;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Calculates the Mobius function μ(n) of a BigInt.
|
|
20
|
+
* @param {bigint} n - A positive BigInt.
|
|
21
|
+
* @returns {number} The Mobius function value (1, -1, or 0).
|
|
22
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LFARiArgZwEICSCAvggGYxRgIBEAAgOYDWWAxmCAKYwBeA9FjggANjhoBuYMDSZchSUA|▶ Try it live in Instacode}
|
|
23
|
+
*/
|
|
24
|
+
export const mobiusBI = n => {
|
|
25
|
+
if (n < 1n) return 0;
|
|
26
|
+
if (n === 1n) return 1;
|
|
27
|
+
const f = factorsBI(n);
|
|
28
|
+
const unique = new Set(f);
|
|
29
|
+
if (unique.size !== f.length) return 0;
|
|
30
|
+
return f.length % 2 === 0 ? 1 : -1;
|
|
31
|
+
};
|