@gkucmierz/utils 3.1.0 → 4.0.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.
Files changed (47) hide show
  1. package/README.md +4 -3
  2. package/main.mjs +133 -125
  3. package/package.json +7 -2
  4. package/src/geometry/barycentric.mjs +19 -0
  5. package/src/{math3d.mjs → geometry/math3d.mjs} +1 -0
  6. package/src/number-theory/mobius.mjs +31 -0
  7. /package/src/{langtons-ant.mjs → automata/langtons-ant.mjs} +0 -0
  8. /package/src/{combinations.mjs → combinatorics/combinations.mjs} +0 -0
  9. /package/src/{gray-code.mjs → combinatorics/gray-code.mjs} +0 -0
  10. /package/src/{n-choose-k.mjs → combinatorics/n-choose-k.mjs} +0 -0
  11. /package/src/{permutations.mjs → combinatorics/permutations.mjs} +0 -0
  12. /package/src/{SetCnt.mjs → data-structures/SetCnt.mjs} +0 -0
  13. /package/src/{Trie.mjs → data-structures/Trie.mjs} +0 -0
  14. /package/src/{heap.mjs → data-structures/heap.mjs} +0 -0
  15. /package/src/{list-node.mjs → data-structures/list-node.mjs} +0 -0
  16. /package/src/{binary-search.mjs → developer-utils/binary-search.mjs} +0 -0
  17. /package/src/{consume-iterator.mjs → developer-utils/consume-iterator.mjs} +0 -0
  18. /package/src/{get-type.mjs → developer-utils/get-type.mjs} +0 -0
  19. /package/src/{measure-performance.mjs → developer-utils/measure-performance.mjs} +0 -0
  20. /package/src/{memoize.mjs → developer-utils/memoize.mjs} +0 -0
  21. /package/src/{natural-search.mjs → developer-utils/natural-search.mjs} +0 -0
  22. /package/src/{rand-normal.mjs → developer-utils/rand-normal.mjs} +0 -0
  23. /package/src/{range-array.mjs → developer-utils/range-array.mjs} +0 -0
  24. /package/src/{set-safe-interval.mjs → developer-utils/set-safe-interval.mjs} +0 -0
  25. /package/src/{matrix.mjs → geometry/matrix.mjs} +0 -0
  26. /package/src/{egcd.mjs → number-theory/egcd.mjs} +0 -0
  27. /package/src/{factors.mjs → number-theory/factors.mjs} +0 -0
  28. /package/src/{gcd.mjs → number-theory/gcd.mjs} +0 -0
  29. /package/src/{lcm.mjs → number-theory/lcm.mjs} +0 -0
  30. /package/src/{lucas-lehmer.mjs → number-theory/lucas-lehmer.mjs} +0 -0
  31. /package/src/{mod.mjs → number-theory/mod.mjs} +0 -0
  32. /package/src/{phi.mjs → number-theory/phi.mjs} +0 -0
  33. /package/src/{pow-mod.mjs → number-theory/pow-mod.mjs} +0 -0
  34. /package/src/{tonelli-shanks.mjs → number-theory/tonelli-shanks.mjs} +0 -0
  35. /package/src/{nelder-mead.mjs → optimization/nelder-mead.mjs} +0 -0
  36. /package/src/{particle-swarm.mjs → optimization/particle-swarm.mjs} +0 -0
  37. /package/src/{simulated-annealing.mjs → optimization/simulated-annealing.mjs} +0 -0
  38. /package/src/{golden-ratio.mjs → sequences/golden-ratio.mjs} +0 -0
  39. /package/src/{gpn.mjs → sequences/gpn.mjs} +0 -0
  40. /package/src/{herons-formula.mjs → sequences/herons-formula.mjs} +0 -0
  41. /package/src/{square-root.mjs → sequences/square-root.mjs} +0 -0
  42. /package/src/{array-histogram.mjs → string-arrays/array-histogram.mjs} +0 -0
  43. /package/src/{base64.mjs → string-arrays/base64.mjs} +0 -0
  44. /package/src/{bijective-numeration.mjs → string-arrays/bijective-numeration.mjs} +0 -0
  45. /package/src/{chunks.mjs → string-arrays/chunks.mjs} +0 -0
  46. /package/src/{copy-case.mjs → string-arrays/copy-case.mjs} +0 -0
  47. /package/src/{format-big-number.mjs → string-arrays/format-big-number.mjs} +0 -0
package/README.md CHANGED
@@ -28,7 +28,6 @@ This library provides a wide range of mathematical functions and data structures
28
28
  - `Trie`: Efficient prefix tree implementation (`class`).
29
29
  - `Heap`: Min-heap priority queue (`class`).
30
30
  - `ListNode`: Linked list node implementation (`class`).
31
- - `matrixAsArray`: 2D matrix representation as a flat array.
32
31
 
33
32
  - **Number Theory**:
34
33
  - `gcd`, `lcm`: Greatest Common Divisor and Least Common Multiple (supports BigInt).
@@ -50,9 +49,10 @@ This library provides a wide range of mathematical functions and data structures
50
49
  - `particleSwarmOptimization`: PSO algorithm for global optimization.
51
50
  - `simulatedAnnealing`: Probabilistic technique for approximating the global optimum of a given function.
52
51
 
53
- - **Math & 3D Geometry**:
52
+ - **Geometry & Math**:
53
+ - `barycentricCoordinates`: Triangle barycentric coordinates computation (supports coordinate arrays).
54
+ - `matrixAsArray`: 2D matrix representation as a flat array.
54
55
  - `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
56
 
57
57
  - **Automata & Simulation**:
58
58
  - `createLangtonsAnt`, `createUnlimitedGrid`: Infinite 2D grid and Langton's Ant cellular automaton engine (Project Euler 349 compatible).
@@ -66,6 +66,7 @@ This library provides a wide range of mathematical functions and data structures
66
66
  - `formatBigNumber`: Intelligent formatting for massively large numbers with suffixes.
67
67
 
68
68
  - **Developer Utilities**:
69
+ - `randNormal`: Normal (Gaussian) distribution random number generator (Box-Muller transform).
69
70
  - `measurePerformance`: A micro-benchmarking tool for high-iteration performance testing.
70
71
  - `setSafeInterval`: A safe, asynchronous alternative to `setInterval` that prevents overlapping executions and supports immediate yielding.
71
72
  - `consumeIteratorNonBlocking`: Yield consumption macro-tasks queue resolver for heavy computations.
package/main.mjs CHANGED
@@ -1,174 +1,182 @@
1
1
 
2
2
  import {
3
- SetCnt
4
- } from './src/SetCnt.mjs'
5
- import {
6
- Trie
7
- } from './src/Trie.mjs'
3
+ createLangtonsAnt, createUnlimitedGrid
4
+ } from './src/automata/langtons-ant.mjs'
8
5
  import {
9
- arrayHistogram
10
- } from './src/array-histogram.mjs'
6
+ combinations, combinationsIterator
7
+ } from './src/combinatorics/combinations.mjs'
11
8
  import {
12
- fromBase64, fromBase64Url, toBase64, toBase64Url
13
- } from './src/base64.mjs'
9
+ bin2gray, gray2bin
10
+ } from './src/combinatorics/gray-code.mjs'
14
11
  import {
15
- bijective2num, bijective2numBI, num2bijective, num2bijectiveBI
16
- } from './src/bijective-numeration.mjs'
12
+ nChooseK
13
+ } from './src/combinatorics/n-choose-k.mjs'
17
14
  import {
18
- binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl
19
- } from './src/binary-search.mjs'
15
+ permutations, permutationsIterator
16
+ } from './src/combinatorics/permutations.mjs'
20
17
  import {
21
- chunks, chunksAsyncIterator, chunksIterator
22
- } from './src/chunks.mjs'
18
+ SetCnt
19
+ } from './src/data-structures/SetCnt.mjs'
23
20
  import {
24
- combinations, combinationsIterator
25
- } from './src/combinations.mjs'
21
+ Trie
22
+ } from './src/data-structures/Trie.mjs'
26
23
  import {
27
- consumeIteratorNonBlocking
28
- } from './src/consume-iterator.mjs'
24
+ Heap
25
+ } from './src/data-structures/heap.mjs'
29
26
  import {
30
- copyCase
31
- } from './src/copy-case.mjs'
27
+ ListNode
28
+ } from './src/data-structures/list-node.mjs'
32
29
  import {
33
- egcd
34
- } from './src/egcd.mjs'
30
+ binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl
31
+ } from './src/developer-utils/binary-search.mjs'
35
32
  import {
36
- factors, factorsBI
37
- } from './src/factors.mjs'
33
+ consumeIteratorNonBlocking
34
+ } from './src/developer-utils/consume-iterator.mjs'
38
35
  import {
39
- formatBigNumber, formatBigNumberBI, wrapFn
40
- } from './src/format-big-number.mjs'
36
+ getType
37
+ } from './src/developer-utils/get-type.mjs'
41
38
  import {
42
- gcd, gcdBI
43
- } from './src/gcd.mjs'
39
+ measurePerformance
40
+ } from './src/developer-utils/measure-performance.mjs'
44
41
  import {
45
- getType
46
- } from './src/get-type.mjs'
42
+ memoize
43
+ } from './src/developer-utils/memoize.mjs'
47
44
  import {
48
- goldenRatio, goldenRatioBI, goldenRatioStr
49
- } from './src/golden-ratio.mjs'
45
+ naturalSearch
46
+ } from './src/developer-utils/natural-search.mjs'
50
47
  import {
51
- gpn, gpnBI
52
- } from './src/gpn.mjs'
48
+ randNormal
49
+ } from './src/developer-utils/rand-normal.mjs'
53
50
  import {
54
- bin2gray, gray2bin
55
- } from './src/gray-code.mjs'
51
+ array2range, range2array
52
+ } from './src/developer-utils/range-array.mjs'
56
53
  import {
57
- Heap
58
- } from './src/heap.mjs'
54
+ setSafeInterval
55
+ } from './src/developer-utils/set-safe-interval.mjs'
59
56
  import {
60
- heronsFormula, heronsFormulaBI
61
- } from './src/herons-formula.mjs'
57
+ barycentricCoordinates
58
+ } from './src/geometry/barycentric.mjs'
62
59
  import {
63
- createLangtonsAnt, createUnlimitedGrid
64
- } from './src/langtons-ant.mjs'
60
+ axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball
61
+ } from './src/geometry/math3d.mjs'
65
62
  import {
66
- lcm, lcmBI
67
- } from './src/lcm.mjs'
63
+ matrixAsArray
64
+ } from './src/geometry/matrix.mjs'
68
65
  import {
69
- ListNode
70
- } from './src/list-node.mjs'
66
+ egcd
67
+ } from './src/number-theory/egcd.mjs'
71
68
  import {
72
- lucasLehmerBI
73
- } from './src/lucas-lehmer.mjs'
69
+ factors, factorsBI
70
+ } from './src/number-theory/factors.mjs'
74
71
  import {
75
- axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball
76
- } from './src/math3d.mjs'
72
+ gcd, gcdBI
73
+ } from './src/number-theory/gcd.mjs'
77
74
  import {
78
- matrixAsArray
79
- } from './src/matrix.mjs'
75
+ lcm, lcmBI
76
+ } from './src/number-theory/lcm.mjs'
80
77
  import {
81
- measurePerformance
82
- } from './src/measure-performance.mjs'
78
+ lucasLehmerBI
79
+ } from './src/number-theory/lucas-lehmer.mjs'
83
80
  import {
84
- memoize
85
- } from './src/memoize.mjs'
81
+ mobius, mobiusBI
82
+ } from './src/number-theory/mobius.mjs'
86
83
  import {
87
84
  mod, modBI
88
- } from './src/mod.mjs'
85
+ } from './src/number-theory/mod.mjs'
89
86
  import {
90
- nChooseK
91
- } from './src/n-choose-k.mjs'
87
+ phi, phiBI
88
+ } from './src/number-theory/phi.mjs'
92
89
  import {
93
- naturalSearch
94
- } from './src/natural-search.mjs'
90
+ powMod, powModBI
91
+ } from './src/number-theory/pow-mod.mjs'
92
+ import {
93
+ tonelliShanksBI
94
+ } from './src/number-theory/tonelli-shanks.mjs'
95
95
  import {
96
96
  nelderMead
97
- } from './src/nelder-mead.mjs'
97
+ } from './src/optimization/nelder-mead.mjs'
98
98
  import {
99
99
  particleSwarmOptimization
100
- } from './src/particle-swarm.mjs'
100
+ } from './src/optimization/particle-swarm.mjs'
101
101
  import {
102
- permutations, permutationsIterator
103
- } from './src/permutations.mjs'
102
+ simulatedAnnealing
103
+ } from './src/optimization/simulated-annealing.mjs'
104
104
  import {
105
- phi, phiBI
106
- } from './src/phi.mjs'
105
+ goldenRatio, goldenRatioBI, goldenRatioStr
106
+ } from './src/sequences/golden-ratio.mjs'
107
107
  import {
108
- powMod, powModBI
109
- } from './src/pow-mod.mjs'
108
+ gpn, gpnBI
109
+ } from './src/sequences/gpn.mjs'
110
110
  import {
111
- randNormal
112
- } from './src/rand-normal.mjs'
111
+ heronsFormula, heronsFormulaBI
112
+ } from './src/sequences/herons-formula.mjs'
113
113
  import {
114
- array2range, range2array
115
- } from './src/range-array.mjs'
114
+ squareRoot, squareRootBI
115
+ } from './src/sequences/square-root.mjs'
116
116
  import {
117
- setSafeInterval
118
- } from './src/set-safe-interval.mjs'
117
+ arrayHistogram
118
+ } from './src/string-arrays/array-histogram.mjs'
119
119
  import {
120
- simulatedAnnealing
121
- } from './src/simulated-annealing.mjs'
120
+ fromBase64, fromBase64Url, toBase64, toBase64Url
121
+ } from './src/string-arrays/base64.mjs'
122
122
  import {
123
- squareRoot, squareRootBI
124
- } from './src/square-root.mjs'
123
+ bijective2num, bijective2numBI, num2bijective, num2bijectiveBI
124
+ } from './src/string-arrays/bijective-numeration.mjs'
125
125
  import {
126
- tonelliShanksBI
127
- } from './src/tonelli-shanks.mjs'
126
+ chunks, chunksAsyncIterator, chunksIterator
127
+ } from './src/string-arrays/chunks.mjs'
128
+ import {
129
+ copyCase
130
+ } from './src/string-arrays/copy-case.mjs'
131
+ import {
132
+ formatBigNumber, formatBigNumberBI, wrapFn
133
+ } from './src/string-arrays/format-big-number.mjs'
128
134
 
129
- export * from './src/SetCnt.mjs';
130
- export * from './src/Trie.mjs';
131
- export * from './src/array-histogram.mjs';
132
- export * from './src/base64.mjs';
133
- export * from './src/bijective-numeration.mjs';
134
- export * from './src/binary-search.mjs';
135
- export * from './src/chunks.mjs';
136
- export * from './src/combinations.mjs';
137
- export * from './src/consume-iterator.mjs';
138
- export * from './src/copy-case.mjs';
139
- export * from './src/egcd.mjs';
140
- export * from './src/factors.mjs';
141
- export * from './src/format-big-number.mjs';
142
- export * from './src/gcd.mjs';
143
- export * from './src/get-type.mjs';
144
- export * from './src/golden-ratio.mjs';
145
- export * from './src/gpn.mjs';
146
- export * from './src/gray-code.mjs';
147
- export * from './src/heap.mjs';
148
- export * from './src/herons-formula.mjs';
149
- export * from './src/langtons-ant.mjs';
150
- export * from './src/lcm.mjs';
151
- export * from './src/list-node.mjs';
152
- export * from './src/lucas-lehmer.mjs';
153
- export * from './src/math3d.mjs';
154
- export * from './src/matrix.mjs';
155
- export * from './src/measure-performance.mjs';
156
- export * from './src/memoize.mjs';
157
- export * from './src/mod.mjs';
158
- export * from './src/n-choose-k.mjs';
159
- export * from './src/natural-search.mjs';
160
- export * from './src/nelder-mead.mjs';
161
- export * from './src/particle-swarm.mjs';
162
- export * from './src/permutations.mjs';
163
- export * from './src/phi.mjs';
164
- export * from './src/pow-mod.mjs';
165
- export * from './src/rand-normal.mjs';
166
- export * from './src/range-array.mjs';
167
- export * from './src/set-safe-interval.mjs';
168
- export * from './src/simulated-annealing.mjs';
169
- export * from './src/square-root.mjs';
170
- export * from './src/tonelli-shanks.mjs';
135
+ export * from './src/automata/langtons-ant.mjs';
136
+ export * from './src/combinatorics/combinations.mjs';
137
+ export * from './src/combinatorics/gray-code.mjs';
138
+ export * from './src/combinatorics/n-choose-k.mjs';
139
+ export * from './src/combinatorics/permutations.mjs';
140
+ export * from './src/data-structures/SetCnt.mjs';
141
+ export * from './src/data-structures/Trie.mjs';
142
+ export * from './src/data-structures/heap.mjs';
143
+ export * from './src/data-structures/list-node.mjs';
144
+ export * from './src/developer-utils/binary-search.mjs';
145
+ export * from './src/developer-utils/consume-iterator.mjs';
146
+ export * from './src/developer-utils/get-type.mjs';
147
+ export * from './src/developer-utils/measure-performance.mjs';
148
+ export * from './src/developer-utils/memoize.mjs';
149
+ export * from './src/developer-utils/natural-search.mjs';
150
+ export * from './src/developer-utils/rand-normal.mjs';
151
+ export * from './src/developer-utils/range-array.mjs';
152
+ export * from './src/developer-utils/set-safe-interval.mjs';
153
+ export * from './src/geometry/barycentric.mjs';
154
+ export * from './src/geometry/math3d.mjs';
155
+ export * from './src/geometry/matrix.mjs';
156
+ export * from './src/number-theory/egcd.mjs';
157
+ export * from './src/number-theory/factors.mjs';
158
+ export * from './src/number-theory/gcd.mjs';
159
+ export * from './src/number-theory/lcm.mjs';
160
+ export * from './src/number-theory/lucas-lehmer.mjs';
161
+ export * from './src/number-theory/mobius.mjs';
162
+ export * from './src/number-theory/mod.mjs';
163
+ export * from './src/number-theory/phi.mjs';
164
+ export * from './src/number-theory/pow-mod.mjs';
165
+ export * from './src/number-theory/tonelli-shanks.mjs';
166
+ export * from './src/optimization/nelder-mead.mjs';
167
+ export * from './src/optimization/particle-swarm.mjs';
168
+ export * from './src/optimization/simulated-annealing.mjs';
169
+ export * from './src/sequences/golden-ratio.mjs';
170
+ export * from './src/sequences/gpn.mjs';
171
+ export * from './src/sequences/herons-formula.mjs';
172
+ export * from './src/sequences/square-root.mjs';
173
+ export * from './src/string-arrays/array-histogram.mjs';
174
+ export * from './src/string-arrays/base64.mjs';
175
+ export * from './src/string-arrays/bijective-numeration.mjs';
176
+ export * from './src/string-arrays/chunks.mjs';
177
+ export * from './src/string-arrays/copy-case.mjs';
178
+ export * from './src/string-arrays/format-big-number.mjs';
171
179
 
172
180
  export default [
173
- 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, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
181
+ createLangtonsAnt, createUnlimitedGrid, combinations, combinationsIterator, bin2gray, gray2bin, nChooseK, permutations, permutationsIterator, SetCnt, Trie, Heap, ListNode, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, consumeIteratorNonBlocking, getType, measurePerformance, memoize, naturalSearch, randNormal, array2range, range2array, setSafeInterval, barycentricCoordinates, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, egcd, factors, factorsBI, gcd, gcdBI, lcm, lcmBI, lucasLehmerBI, mobius, mobiusBI, mod, modBI, phi, phiBI, powMod, powModBI, tonelliShanksBI, nelderMead, particleSwarmOptimization, simulatedAnnealing, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, heronsFormula, heronsFormulaBI, squareRoot, squareRootBI, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, chunks, chunksAsyncIterator, chunksIterator, copyCase, formatBigNumber, formatBigNumberBI, wrapFn
174
182
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "3.1.0",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "description": "Usefull functions for solving programming tasks",
6
6
  "keywords": [
@@ -52,7 +52,12 @@
52
52
  "simulated-annealing",
53
53
  "rand-normal",
54
54
  "format-big-number",
55
- "natural-search"
55
+ "natural-search",
56
+ "mobius",
57
+ "barycentric",
58
+ "barycentric-coordinates",
59
+ "geometry",
60
+ "interpolation"
56
61
  ],
57
62
  "files": [
58
63
  "main.mjs",
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Calculates the barycentric coordinates of a 2D point P relative to a triangle ABC.
3
+ * @param {Array<number>} p - Point [x, y]
4
+ * @param {Array<number>} a - Vertex A [x, y]
5
+ * @param {Array<number>} b - Vertex B [x, y]
6
+ * @param {Array<number>} c - Vertex C [x, y]
7
+ * @returns {Array<number>} [l1, l2, l3] barycentric coordinates summing to 1
8
+ * @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IEYEMYE8DGBTAOzhhBwGEpYATEA9OPAZwQF8EAzGKMBAIgAEA5gGsArjjAg8MAF4B6UXBAAbRrwDcwYBmz4iJcpRg06DRpqA|▶ Try it live in Instacode}
9
+ */
10
+ export const barycentricCoordinates = (p, a, b, c) => {
11
+ const det = (b[1] - c[1]) * (a[0] - c[0]) + (c[0] - b[0]) * (a[1] - c[1]);
12
+ if (Math.abs(det) < 1e-9) {
13
+ return [1/3, 1/3, 1/3]; // Fallback for collinear vertices
14
+ }
15
+ const l1 = ((b[1] - c[1]) * (p[0] - c[0]) + (c[0] - b[0]) * (p[1] - c[1])) / det;
16
+ const l2 = ((c[1] - a[1]) * (p[0] - c[0]) + (a[0] - c[0]) * (p[1] - c[1])) / det;
17
+ const l3 = 1 - l1 - l2;
18
+ return [l1, l2, l3];
19
+ };
@@ -65,3 +65,4 @@ export const getRotationMatrixFromVectors = (u, v) => {
65
65
  const angle = Math.acos(Math.max(-1, Math.min(1, dot)));
66
66
  return axisAngleToMatrix4(axis, angle);
67
67
  };
68
+
@@ -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
+ };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes