@stacksjs/arrays 0.65.0 → 0.68.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/src/math.ts DELETED
@@ -1,245 +0,0 @@
1
- /**
2
- * Returns the average of an array of numbers
3
- * @param arr
4
- * @category Array
5
- * @example
6
- * ```ts
7
- * average([1, 2, 3, 4]) // 2.5
8
- * ```
9
- */
10
- export function average(arr: number[]): number {
11
- return sum(arr) / arr.length
12
- }
13
-
14
- /**
15
- * Returns the average of an array of numbers
16
- * @param arr
17
- * @category Array
18
- * @example
19
- * ```ts
20
- * avg([1, 2, 3, 4]) // 2.5
21
- * ```
22
- */
23
- export function avg(arr: number[]): number {
24
- return average(arr)
25
- }
26
-
27
- /**
28
- * Returns the median of an array of numbers
29
- * @param arr
30
- * @category Array
31
- * @example
32
- * ```ts
33
- * median([1, 2, 3, 4]) // 2.5
34
- * ```
35
- */
36
- export function median(arr: number[]): number {
37
- if (arr.length === 0)
38
- throw new Error('Cannot compute median of an empty array')
39
-
40
- const sorted = [...arr].sort((a, b) => a - b)
41
-
42
- const mid = Math.floor(sorted.length / 2)
43
-
44
- let medianValue: number
45
-
46
- if (sorted.length % 2 !== 0)
47
- medianValue = sorted[mid] as number
48
- else medianValue = ((sorted[mid] as number) + (sorted[mid - 1] as number)) / 2 // or (sorted[mid - 1] + sorted[mid]) / 2
49
-
50
- return medianValue
51
- }
52
-
53
- /**
54
- * Returns the mode of an array of numbers
55
- * @param arr
56
- * @category Array
57
- * @example
58
- * ```ts
59
- * mode([1, 2, 3, 4]) // 1
60
- * mode([1, 2, 2, 3, 4]) // 2
61
- * mode([1, 2, 2, 3, 3, 4]) // 2
62
- * mode([1, 2, 2, 3, 3, 4, 4]) // 2
63
- * mode([1, 2, 2, 3, 3, 4, 4, 4]) // 4
64
- * ```
65
- */
66
- export function mode(arr: number[]): number {
67
- return arr.sort((a, b) => arr.filter(v => v === a).length - arr.filter(v => v === b).length).pop() as number
68
- }
69
-
70
- /**
71
- * Returns the sum of an array of numbers
72
- * @param array
73
- * @category Array
74
- * @example
75
- * ```ts
76
- * sum([1, 2, 3, 4]) // 10
77
- * ```
78
- */
79
- export function sum(array: readonly number[]): number {
80
- return array.reduce((acc, cur) => acc + cur, 0)
81
- }
82
-
83
- /**
84
- * Returns the product of an array of numbers
85
- * @param array
86
- * @category Array
87
- * @example
88
- * ```ts
89
- * product([1, 2, 3, 4]) // 24
90
- * ```
91
- */
92
- export function product(array: readonly number[]): number {
93
- return array.reduce((acc, cur) => acc * cur, 1)
94
- }
95
-
96
- /**
97
- * Returns the minimum value of an array of numbers
98
- * @param array
99
- * @category Array
100
- * @example
101
- * ```ts
102
- * min([1, 2, 3, 4]) // 1
103
- * min([1, 2, 3, 4, -1]) // -1
104
- * ```
105
- */
106
- export function min(array: readonly number[]): number {
107
- return Math.min(...array)
108
- }
109
-
110
- /**
111
- * Returns the maximum value of an array of numbers
112
- * @param array
113
- * @category Array
114
- * @example
115
- * ```ts
116
- * max([1, 2, 3, 4]) // 4
117
- * max([1, 2, 3, 4, -1]) // 4
118
- * ```
119
- */
120
- export function max(array: readonly number[]): number {
121
- return Math.max(...array)
122
- }
123
-
124
- /**
125
- * Returns the range of an array of numbers
126
- * @param array
127
- * @category Array
128
- * @example
129
- * ```ts
130
- * range([1, 2, 3, 4]) // 3
131
- * range([1, 2, 3, 4, -1]) // 5
132
- * range([1, 2, 3, 4, -1, 10]) // 11
133
- * ```
134
- */
135
- export function range(array: readonly number[]): number {
136
- return max(array) - min(array)
137
- }
138
-
139
- /**
140
- * Returns the variance of an array of numbers
141
- * @param array
142
- * @category Array
143
- * @example
144
- * ```ts
145
- * variance([1, 2, 3, 4]) // 1.25
146
- * ```
147
- * @see https://en.wikipedia.org/wiki/Variance
148
- */
149
- export function variance(array: number[]): number {
150
- const mean = average(array)
151
- return average(array.map(num => (num - mean) ** 2))
152
- }
153
-
154
- /**
155
- * Returns the standard deviation of an array of numbers
156
- * @param array
157
- * @category Array
158
- * @example
159
- * ```ts
160
- * standardDeviation([1, 2, 3, 4]) // 1.118033988749895
161
- * ```
162
- * @see https://en.wikipedia.org/wiki/Standard_deviation
163
- * @see https://stackoverflow.com/questions/7343890/standard-deviation-javascript
164
- */
165
- export function standardDeviation(array: number[]): number {
166
- return Math.sqrt(variance(array))
167
- }
168
-
169
- /**
170
- * Returns the z-score of a number in an array of numbers
171
- * @param array
172
- * @param num
173
- * @category Array
174
- * @example
175
- * ```ts
176
- * zScore([1, 2, 3, 4], 2) // 0
177
- * zScore([1, 2, 3, 4], 3) // 0.7071067811865475
178
- * ```
179
- * @see https://en.wikipedia.org/wiki/Standard_score
180
- */
181
- export function zScore(array: number[], num: number): number {
182
- return (num - average(array)) / standardDeviation(array)
183
- }
184
-
185
- /**
186
- * Returns the percentile of a number in an array of numbers
187
- * @param array
188
- * @param num
189
- * @category Array
190
- * @example
191
- * ```ts
192
- * percentile([1, 2, 3, 4], 2) // 0.25
193
- * percentile([1, 2, 3, 4], 3) // 0.75
194
- * ```
195
- * @see https://en.wikipedia.org/wiki/Percentile
196
- */
197
- export function percentile(array: number[], num: number): number {
198
- const sorted = [...array].sort((a, b) => a - b)
199
- const index = (num / 100) * (sorted.length - 1)
200
- const lower = Math.floor(index)
201
- const upper = Math.ceil(index)
202
- const weight = index - lower
203
-
204
- if (upper === lower)
205
- return sorted[index] ?? 0
206
- return (1 - weight) * (sorted[lower] ?? 0) + weight * (sorted[upper] ?? 0)
207
- }
208
-
209
- /**
210
- * Returns the interquartile range of an array of numbers
211
- * @param array
212
- * @category Array
213
- * @example
214
- * ```ts
215
- * interquartileRange([1, 2, 3, 4]) // 1.5
216
- * ```
217
- * @see https://en.wikipedia.org/wiki/Interquartile_range
218
- * @see https://stackoverflow.com/questions/48719873/how-to-calculate-interquartile-range-in-javascript
219
- */
220
- export function interquartileRange(array: number[]): number {
221
- const q1 = median(array.slice(0, Math.floor(array.length / 2)))
222
- const q3 = median(array.slice(Math.ceil(array.length / 2)))
223
- return q3 - q1
224
- }
225
-
226
- /**
227
- * Returns the covariance of two arrays of numbers
228
- * @param array1
229
- * @param array2
230
- * @category Array
231
- * @example
232
- * ```ts
233
- * covariance([1, 2, 3, 4], [1, 2, 3, 4]) // 1.25
234
- * covariance([1, 2, 3, 4], [4, 3, 2, 1]) // -1.25
235
- * ```
236
- */
237
- export function covariance(array1: number[], array2: number[]): number {
238
- if (array1.length !== array2.length)
239
- throw new Error('Arrays must have the same length')
240
-
241
- const mean1 = average(array1)
242
- const mean2 = average(array2)
243
-
244
- return average(array1.map((num1, i) => (num1 - mean1) * ((array2[i] as number) - mean2)))
245
- }