0
# Mathematical Operations
1
2
Mathematical computations and operations on iterables.
3
4
## Capabilities
5
6
### Basic Mathematical Operations
7
8
Core mathematical functions for iterables.
9
10
```python { .api }
11
def dotproduct(vec1, vec2):
12
"""
13
Compute dot product of two vectors.
14
15
Args:
16
vec1: First vector (iterable of numbers)
17
vec2: Second vector (iterable of numbers)
18
19
Returns:
20
Dot product as a number
21
"""
22
23
def sum_of_squares(iterable):
24
"""
25
Compute sum of squares of all items.
26
27
Args:
28
iterable: Iterable of numbers
29
30
Returns:
31
Sum of squares
32
"""
33
```
34
35
**Usage Examples:**
36
37
```python
38
from more_itertools import dotproduct, sum_of_squares
39
40
# Dot product
41
dp = dotproduct([1, 2, 3], [4, 5, 6])
42
# Result: 32 (1*4 + 2*5 + 3*6)
43
44
# Sum of squares
45
sos = sum_of_squares([1, 2, 3, 4])
46
# Result: 30 (1² + 2² + 3² + 4²)
47
```
48
49
### Polynomial Operations
50
51
Functions for polynomial mathematics.
52
53
```python { .api }
54
def polynomial_eval(coefficients, x):
55
"""
56
Evaluate polynomial at given value.
57
58
Args:
59
coefficients: Sequence of polynomial coefficients (constant first)
60
x: Value at which to evaluate polynomial
61
62
Returns:
63
Value of polynomial at x
64
"""
65
66
def polynomial_from_roots(roots):
67
"""
68
Generate polynomial coefficients from roots.
69
70
Args:
71
roots: Sequence of polynomial roots
72
73
Returns:
74
List of coefficients for polynomial with given roots
75
"""
76
77
def polynomial_derivative(coefficients):
78
"""
79
Compute derivative coefficients of polynomial.
80
81
Args:
82
coefficients: Sequence of polynomial coefficients
83
84
Returns:
85
List of derivative coefficients
86
"""
87
```
88
89
**Usage Examples:**
90
91
```python
92
from more_itertools import polynomial_eval, polynomial_from_roots
93
94
# Evaluate polynomial 2x² + 3x + 1 at x = 2
95
result = polynomial_eval([1, 3, 2], 2)
96
# Result: 15 (2*4 + 3*2 + 1)
97
98
# Polynomial from roots [1, 2] gives (x-1)(x-2) = x² - 3x + 2
99
coeffs = polynomial_from_roots([1, 2])
100
# Result: [2, -3, 1]
101
```
102
103
### Signal Processing
104
105
Functions for signal processing and convolution.
106
107
```python { .api }
108
def convolve(signal, kernel):
109
"""
110
Compute convolution of signal with kernel.
111
112
Args:
113
signal: Input signal (iterable of numbers)
114
kernel: Convolution kernel (iterable of numbers)
115
116
Returns:
117
Iterator of convolved values
118
"""
119
120
def dft(xlist):
121
"""
122
Compute Discrete Fourier Transform.
123
124
Args:
125
xlist: Input sequence (iterable of numbers)
126
127
Returns:
128
Iterator of complex DFT coefficients
129
"""
130
131
def idft(xlist):
132
"""
133
Compute Inverse Discrete Fourier Transform.
134
135
Args:
136
xlist: DFT coefficients (iterable of complex numbers)
137
138
Returns:
139
Iterator of real inverse DFT values
140
"""
141
```
142
143
### Matrix Operations
144
145
Functions for matrix mathematics.
146
147
```python { .api }
148
def matmul(m1, m2):
149
"""
150
Matrix multiplication of two matrices.
151
152
Args:
153
m1: First matrix (sequence of sequences)
154
m2: Second matrix (sequence of sequences)
155
156
Returns:
157
Iterator of tuples representing result matrix rows
158
"""
159
160
def reshape(matrix, shape):
161
"""
162
Reshape matrix to new dimensions.
163
164
Args:
165
matrix: Input matrix or flat iterable
166
shape: New shape (int for 1D, iterable for multi-D)
167
168
Returns:
169
Iterator with reshaped data
170
"""
171
```
172
173
### Number Theory
174
175
Advanced number theory functions.
176
177
```python { .api }
178
def factor(n):
179
"""
180
Generate prime factors of integer n.
181
182
Args:
183
n: Integer to factor
184
185
Returns:
186
Iterator of prime factors
187
"""
188
189
def is_prime(n):
190
"""
191
Test if n is prime using Miller-Rabin test.
192
193
Args:
194
n: Integer to test
195
196
Returns:
197
True if n is prime, False otherwise
198
"""
199
200
def sieve(n):
201
"""
202
Generate prime numbers up to n using Sieve of Eratosthenes.
203
204
Args:
205
n: Upper limit for prime generation
206
207
Returns:
208
Iterator of prime numbers ≤ n
209
"""
210
211
def totient(n):
212
"""
213
Compute Euler's totient function φ(n).
214
215
Args:
216
n: Positive integer
217
218
Returns:
219
Number of integers ≤ n that are coprime to n
220
"""
221
222
def multinomial(*counts):
223
"""
224
Compute multinomial coefficient.
225
226
Args:
227
*counts: Sequence of non-negative integers
228
229
Returns:
230
Multinomial coefficient
231
"""
232
233
def nth_prime(n, *, approximate=False):
234
"""
235
Return the nth prime number (0-indexed).
236
237
Args:
238
n: Index of prime to return (0-based)
239
approximate: If True, return approximate result for faster computation
240
241
Returns:
242
The nth prime number
243
"""
244
```
245
246
**Usage Examples:**
247
248
```python
249
from more_itertools import factor, is_prime, sieve, nth_prime
250
251
# Prime factorization
252
factors = list(factor(60))
253
# Result: [2, 2, 3, 5]
254
255
# Prime testing
256
print(is_prime(17)) # True
257
print(is_prime(18)) # False
258
259
# Generate primes up to 20
260
primes = list(sieve(20))
261
# Result: [2, 3, 5, 7, 11, 13, 17, 19]
262
263
# Get nth prime
264
first_prime = nth_prime(0) # Result: 2
265
tenth_prime = nth_prime(10) # Result: 31
266
267
# Fast approximation for large indices
268
approx_prime = nth_prime(1000000, approximate=True)
269
```
270
271
### Statistical Operations
272
273
Functions for statistical calculations.
274
275
```python { .api }
276
def running_median(iterable, *, maxlen=None):
277
"""
278
Compute running median of values.
279
280
Args:
281
iterable: Input sequence of numbers
282
maxlen: Maximum window size for median calculation
283
284
Returns:
285
Iterator of running median values
286
"""
287
```
288
289
**Usage Examples:**
290
291
```python
292
from more_itertools import running_median
293
294
# Running median
295
medians = list(running_median([1, 2, 3, 4, 5]))
296
# Result: [1, 1.5, 2, 2.5, 3]
297
```