0
# Array Operations
1
2
Basic array operations for finding extremes, sums, and products.
3
4
## Core Imports
5
6
```typescript
7
import {
8
min, max, extent,
9
minSorted, maxSorted, extentSorted,
10
sum, sumSimple, product
11
} from "simple-statistics";
12
```
13
14
## Capabilities
15
16
### Minimum and Maximum
17
18
#### min
19
20
```typescript { .api }
21
/**
22
* Finds the minimum value in an array
23
* @param values - Array of numeric values
24
* @returns The minimum value
25
*/
26
function min(values: number[]): number;
27
```
28
29
**Usage Example:**
30
31
```typescript
32
import { min } from "simple-statistics";
33
34
const data = [3, 1, 4, 1, 5, 9, 2, 6];
35
const minimum = min(data); // 1
36
```
37
38
#### max
39
40
```typescript { .api }
41
/**
42
* Finds the maximum value in an array
43
* @param values - Array of numeric values
44
* @returns The maximum value
45
*/
46
function max(values: number[]): number;
47
```
48
49
#### extent
50
51
```typescript { .api }
52
/**
53
* Finds both minimum and maximum values in one pass
54
* @param values - Array of numeric values
55
* @returns Tuple containing [min, max]
56
*/
57
function extent(values: number[]): [number, number];
58
```
59
60
**Usage Example:**
61
62
```typescript
63
import { extent } from "simple-statistics";
64
65
const data = [3, 1, 4, 1, 5, 9, 2, 6];
66
const [minimum, maximum] = extent(data); // [1, 9]
67
```
68
69
### Optimized Functions for Sorted Arrays
70
71
#### minSorted
72
73
```typescript { .api }
74
/**
75
* Finds minimum value in a pre-sorted array (optimized)
76
* @param values - Pre-sorted array of numeric values
77
* @returns The minimum value (first element)
78
*/
79
function minSorted(values: number[]): number;
80
```
81
82
#### maxSorted
83
84
```typescript { .api }
85
/**
86
* Finds maximum value in a pre-sorted array (optimized)
87
* @param values - Pre-sorted array of numeric values
88
* @returns The maximum value (last element)
89
*/
90
function maxSorted(values: number[]): number;
91
```
92
93
#### extentSorted
94
95
```typescript { .api }
96
/**
97
* Finds extent in a pre-sorted array (optimized)
98
* @param values - Pre-sorted array of numeric values
99
* @returns Tuple containing [min, max]
100
*/
101
function extentSorted(values: number[]): [number, number];
102
```
103
104
### Aggregation Functions
105
106
#### sum
107
108
```typescript { .api }
109
/**
110
* Calculates the sum of array values using Kahan summation for numerical stability
111
* @param values - Array of numeric values
112
* @returns The sum of all values
113
*/
114
function sum(values: number[]): number;
115
```
116
117
**Usage Example:**
118
119
```typescript
120
import { sum } from "simple-statistics";
121
122
const data = [1, 2, 3, 4, 5];
123
const total = sum(data); // 15
124
```
125
126
#### sumSimple
127
128
```typescript { .api }
129
/**
130
* Calculates the sum using simple addition (faster but less numerically stable)
131
* @param values - Array of numeric values
132
* @returns The sum of all values
133
*/
134
function sumSimple(values: number[]): number;
135
```
136
137
#### product
138
139
```typescript { .api }
140
/**
141
* Calculates the product of all values in an array
142
* @param values - Array of numeric values
143
* @returns The product of all values
144
*/
145
function product(values: number[]): number;
146
```
147
148
**Usage Example:**
149
150
```typescript
151
import { product } from "simple-statistics";
152
153
const data = [2, 3, 4];
154
const result = product(data); // 24
155
```