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