0
# Arrays
1
2
NumPy-compatible distributed arrays for parallel and out-of-core computation. Dask arrays break large arrays into chunks and operate on them in parallel, providing the NumPy interface while scaling beyond memory limits.
3
4
## Import Statements
5
6
```python
7
import dask.array as da
8
from dask.array import (
9
# Core array creation and manipulation
10
array, asarray, asanyarray, from_array,
11
zeros, ones, empty, full, zeros_like, ones_like, empty_like, full_like,
12
arange, linspace, logspace, geomspace, eye, diag, diagonal, indices, meshgrid,
13
14
# Mathematical operations
15
add, subtract, multiply, divide, power, sqrt, sin, cos, tan, exp, log,
16
17
# Linear algebra (also available as da.linalg)
18
dot, matmul, tensordot, einsum, outer, inner, vdot,
19
20
# Reductions and aggregations
21
sum, mean, std, var, min, max, argmin, argmax, all, any, prod,
22
nansum, nanmean, nanstd, nanvar, nanmin, nanmax, nanprod,
23
24
# Array manipulation
25
reshape, transpose, concatenate, stack, split, flip, roll,
26
27
# Advanced operations
28
map_blocks, blockwise, apply_along_axis, apply_gufunc,
29
30
# I/O operations
31
store, to_zarr, from_zarr, to_hdf5, from_npy_stack, to_npy_stack
32
)
33
34
# Submodules
35
import dask.array.linalg as da_linalg
36
import dask.array.fft as da_fft
37
import dask.array.random as da_random
38
```
39
40
## Capabilities
41
42
### Array Creation
43
44
Create Dask arrays from various sources including existing arrays, functions, and file formats.
45
46
```python { .api }
47
def array(object, dtype=None, chunks=None, name=None, meta=None):
48
"""
49
Create dask array from array-like object.
50
51
Parameters:
52
- object: Array-like object to convert
53
- dtype: Data type for the array
54
- chunks: Chunk size specification
55
- name: Custom name for array in task graph
56
- meta: Metadata array for type inference
57
58
Returns:
59
dask.array.Array: Dask array
60
"""
61
62
def from_array(x, chunks=None, name=None, lock=False, asarray=None,
63
fancy=True, getitem=None, meta=None):
64
"""
65
Create dask array from existing array with specified chunking.
66
67
Parameters:
68
- x: Existing array-like object
69
- chunks: Chunk size specification (int, tuple, or 'auto')
70
- name: Custom name for array
71
- lock: Thread lock for thread-unsafe arrays
72
- asarray: Function to convert chunks to arrays
73
- fancy: Support advanced indexing
74
- getitem: Custom getitem function
75
- meta: Metadata array
76
77
Returns:
78
dask.array.Array: Chunked dask array
79
"""
80
81
def asarray(a, chunks=None, name=None, dtype=None, **kwargs):
82
"""
83
Convert input to dask array.
84
85
Parameters:
86
- a: Input array-like object
87
- chunks: Chunk specification
88
- name: Array name
89
- dtype: Data type
90
- **kwargs: Additional arguments
91
92
Returns:
93
dask.array.Array: Dask array
94
"""
95
96
def asanyarray(a, chunks=None, name=None, dtype=None, **kwargs):
97
"""
98
Convert input to dask array, preserving subclasses.
99
100
Parameters:
101
- a: Input array-like object
102
- chunks: Chunk specification
103
- name: Array name
104
- dtype: Data type
105
- **kwargs: Additional arguments
106
107
Returns:
108
dask.array.Array: Dask array
109
"""
110
```
111
112
### Basic Array Construction
113
114
Standard array creation functions similar to NumPy.
115
116
```python { .api }
117
def zeros(shape, dtype=float, chunks=None, **kwargs):
118
"""Create array filled with zeros."""
119
120
def ones(shape, dtype=float, chunks=None, **kwargs):
121
"""Create array filled with ones."""
122
123
def full(shape, fill_value, dtype=None, chunks=None, **kwargs):
124
"""Create array filled with specified value."""
125
126
def empty(shape, dtype=float, chunks=None, **kwargs):
127
"""Create uninitialized array."""
128
129
def zeros_like(a, dtype=None, chunks=None, **kwargs):
130
"""Create zeros array with same shape as input."""
131
132
def ones_like(a, dtype=None, chunks=None, **kwargs):
133
"""Create ones array with same shape as input."""
134
135
def full_like(a, fill_value, dtype=None, chunks=None, **kwargs):
136
"""Create filled array with same shape as input."""
137
138
def empty_like(a, dtype=None, chunks=None, **kwargs):
139
"""Create empty array with same shape as input."""
140
```
141
142
### Numeric Arrays
143
144
Create arrays with numeric sequences and patterns.
145
146
```python { .api }
147
def arange(start, stop=None, step=None, chunks=None, dtype=None, **kwargs):
148
"""
149
Create array with evenly spaced values.
150
151
Parameters:
152
- start: Start value or stop if only one argument
153
- stop: End value (exclusive)
154
- step: Step between values
155
- chunks: Chunk specification
156
- dtype: Data type
157
158
Returns:
159
dask.array.Array: Array with range values
160
"""
161
162
def linspace(start, stop, num=50, endpoint=True, retstep=False,
163
chunks=None, dtype=None):
164
"""
165
Create array with linearly spaced values.
166
167
Parameters:
168
- start: Start value
169
- stop: End value
170
- num: Number of samples
171
- endpoint: Include stop value
172
- retstep: Return step size
173
- chunks: Chunk specification
174
- dtype: Data type
175
176
Returns:
177
dask.array.Array or tuple: Array and optionally step size
178
"""
179
180
def logspace(start, stop, num=50, endpoint=True, base=10.0,
181
chunks=None, dtype=None):
182
"""
183
Create array with logarithmically spaced values.
184
185
Parameters:
186
- start: Start value (base**start)
187
- stop: End value (base**stop)
188
- num: Number of samples
189
- endpoint: Include stop value
190
- base: Base of logarithm
191
- chunks: Chunk specification
192
- dtype: Data type
193
194
Returns:
195
dask.array.Array: Array with log-spaced values
196
"""
197
198
def geomspace(start, stop, num=50, endpoint=True, chunks=None, dtype=None):
199
"""
200
Create array with geometrically spaced values.
201
202
Parameters:
203
- start: Start value
204
- stop: End value
205
- num: Number of samples
206
- endpoint: Include stop value
207
- chunks: Chunk specification
208
- dtype: Data type
209
210
Returns:
211
dask.array.Array: Array with geometrically spaced values
212
"""
213
214
def eye(N, M=None, k=0, dtype=float, chunks=None, **kwargs):
215
"""
216
Create identity matrix.
217
218
Parameters:
219
- N: Number of rows
220
- M: Number of columns (defaults to N)
221
- k: Diagonal offset (0=main diagonal)
222
- dtype: Data type
223
- chunks: Chunk specification
224
225
Returns:
226
dask.array.Array: Identity matrix
227
"""
228
229
def diag(v, k=0, chunks=None, **kwargs):
230
"""
231
Create diagonal array or extract diagonal.
232
233
Parameters:
234
- v: Input array or diagonal values
235
- k: Diagonal offset
236
- chunks: Chunk specification
237
238
Returns:
239
dask.array.Array: Diagonal array or extracted diagonal
240
"""
241
242
def diagonal(a, offset=0, axis1=0, axis2=1):
243
"""
244
Extract diagonal from array.
245
246
Parameters:
247
- a: Input array
248
- offset: Diagonal offset
249
- axis1: First axis
250
- axis2: Second axis
251
252
Returns:
253
dask.array.Array: Diagonal elements
254
"""
255
256
def indices(dimensions, dtype=int, chunks=None):
257
"""
258
Return coordinate arrays for given dimensions.
259
260
Parameters:
261
- dimensions: Shape of output arrays
262
- dtype: Data type for coordinates
263
- chunks: Chunk specification
264
265
Returns:
266
dask.array.Array: Grid coordinate arrays
267
"""
268
269
def meshgrid(*xi, copy=True, sparse=False, indexing='xy', chunks=None):
270
"""
271
Return coordinate matrices from coordinate vectors.
272
273
Parameters:
274
- *xi: Input coordinate vectors
275
- copy: Copy input arrays
276
- sparse: Return sparse grid
277
- indexing: Cartesian ('xy') or matrix ('ij') indexing
278
- chunks: Chunk specification
279
280
Returns:
281
tuple of dask.array.Array: Coordinate matrices
282
"""
283
284
def tri(N, M=None, k=0, dtype=float, chunks=None):
285
"""
286
Create array with ones on and below diagonal.
287
288
Parameters:
289
- N: Number of rows
290
- M: Number of columns (defaults to N)
291
- k: Diagonal offset
292
- dtype: Data type
293
- chunks: Chunk specification
294
295
Returns:
296
dask.array.Array: Lower triangular array
297
"""
298
299
def tril(m, k=0):
300
"""
301
Lower triangle of array.
302
303
Parameters:
304
- m: Input array
305
- k: Diagonal offset
306
307
Returns:
308
dask.array.Array: Lower triangular array
309
"""
310
311
def triu(m, k=0):
312
"""
313
Upper triangle of array.
314
315
Parameters:
316
- m: Input array
317
- k: Diagonal offset
318
319
Returns:
320
dask.array.Array: Upper triangular array
321
"""
322
```
323
324
### Array Properties and Methods
325
326
Core Array class with essential properties and methods.
327
328
```python { .api }
329
class Array:
330
"""
331
N-dimensional distributed array with NumPy interface.
332
333
Properties:
334
- shape: tuple - Array dimensions
335
- dtype: numpy.dtype - Data type
336
- ndim: int - Number of dimensions
337
- size: int - Total number of elements
338
- chunks: tuple - Chunk sizes per dimension
339
- npartitions: int - Total number of chunks
340
- nbytes: int - Total bytes of array
341
"""
342
343
def compute(self, scheduler=None, **kwargs):
344
"""
345
Compute array and return NumPy result.
346
347
Parameters:
348
- scheduler: Scheduler to use
349
- **kwargs: Additional compute arguments
350
351
Returns:
352
numpy.ndarray: Computed array
353
"""
354
355
def persist(self, scheduler=None, **kwargs):
356
"""
357
Persist array in memory for reuse.
358
359
Parameters:
360
- scheduler: Scheduler to use
361
- **kwargs: Additional persist arguments
362
363
Returns:
364
dask.array.Array: Persisted array
365
"""
366
367
def rechunk(self, chunks=None, threshold=None, block_size_limit=None,
368
balance=False):
369
"""
370
Change array chunking.
371
372
Parameters:
373
- chunks: New chunk specification
374
- threshold: Rechunk threshold
375
- block_size_limit: Maximum chunk size
376
- balance: Balance chunk sizes
377
378
Returns:
379
dask.array.Array: Rechunked array
380
"""
381
382
def __getitem__(self, key):
383
"""Array indexing and slicing."""
384
385
def __array_ufunc__(self, numpy_ufunc, method, *inputs, **kwargs):
386
"""NumPy universal function interface."""
387
```
388
389
### Mathematical Operations
390
391
Mathematical functions and operations following NumPy conventions.
392
393
```python { .api }
394
# Arithmetic operations
395
def add(x1, x2, **kwargs):
396
"""
397
Add arrays element-wise.
398
399
Parameters:
400
- x1, x2: Input arrays
401
- **kwargs: Additional ufunc parameters
402
403
Returns:
404
dask.array.Array: Element-wise sum
405
"""
406
407
def subtract(x1, x2, **kwargs):
408
"""
409
Subtract arrays element-wise.
410
411
Parameters:
412
- x1, x2: Input arrays
413
- **kwargs: Additional ufunc parameters
414
415
Returns:
416
dask.array.Array: Element-wise difference
417
"""
418
419
def multiply(x1, x2, **kwargs):
420
"""
421
Multiply arrays element-wise.
422
423
Parameters:
424
- x1, x2: Input arrays
425
- **kwargs: Additional ufunc parameters
426
427
Returns:
428
dask.array.Array: Element-wise product
429
"""
430
431
def divide(x1, x2, **kwargs):
432
"""
433
Divide arrays element-wise.
434
435
Parameters:
436
- x1, x2: Input arrays
437
- **kwargs: Additional ufunc parameters
438
439
Returns:
440
dask.array.Array: Element-wise division
441
"""
442
443
def true_divide(x1, x2, **kwargs):
444
"""
445
True division element-wise (always returns float).
446
447
Parameters:
448
- x1, x2: Input arrays
449
- **kwargs: Additional ufunc parameters
450
451
Returns:
452
dask.array.Array: Element-wise true division
453
"""
454
455
def floor_divide(x1, x2, **kwargs):
456
"""
457
Floor division element-wise.
458
459
Parameters:
460
- x1, x2: Input arrays
461
- **kwargs: Additional ufunc parameters
462
463
Returns:
464
dask.array.Array: Element-wise floor division
465
"""
466
467
def power(x1, x2, **kwargs):
468
"""
469
Raise first array to powers from second array.
470
471
Parameters:
472
- x1: Base array
473
- x2: Exponent array
474
- **kwargs: Additional ufunc parameters
475
476
Returns:
477
dask.array.Array: Element-wise powers
478
"""
479
480
def float_power(x1, x2, **kwargs):
481
"""
482
Raise first array to powers (always returns float).
483
484
Parameters:
485
- x1: Base array
486
- x2: Exponent array
487
- **kwargs: Additional ufunc parameters
488
489
Returns:
490
dask.array.Array: Element-wise powers as float
491
"""
492
493
def mod(x1, x2, **kwargs):
494
"""
495
Modulo operation element-wise.
496
497
Parameters:
498
- x1: Dividend array
499
- x2: Divisor array
500
- **kwargs: Additional ufunc parameters
501
502
Returns:
503
dask.array.Array: Element-wise remainder
504
"""
505
506
def remainder(x1, x2, **kwargs):
507
"""
508
Remainder operation element-wise (alias for mod).
509
510
Parameters:
511
- x1: Dividend array
512
- x2: Divisor array
513
- **kwargs: Additional ufunc parameters
514
515
Returns:
516
dask.array.Array: Element-wise remainder
517
"""
518
519
def divmod(x1, x2, **kwargs):
520
"""
521
Simultaneous floor division and modulo.
522
523
Parameters:
524
- x1: Dividend array
525
- x2: Divisor array
526
- **kwargs: Additional ufunc parameters
527
528
Returns:
529
tuple: (quotient, remainder) arrays
530
"""
531
532
def negative(x, **kwargs):
533
"""
534
Numerical negative element-wise.
535
536
Parameters:
537
- x: Input array
538
- **kwargs: Additional ufunc parameters
539
540
Returns:
541
dask.array.Array: Element-wise negative
542
"""
543
544
def positive(x, **kwargs):
545
"""
546
Numerical positive element-wise.
547
548
Parameters:
549
- x: Input array
550
- **kwargs: Additional ufunc parameters
551
552
Returns:
553
dask.array.Array: Element-wise positive
554
"""
555
556
def absolute(x, **kwargs):
557
"""
558
Absolute value element-wise.
559
560
Parameters:
561
- x: Input array
562
- **kwargs: Additional ufunc parameters
563
564
Returns:
565
dask.array.Array: Element-wise absolute value
566
"""
567
568
def abs(x, **kwargs):
569
"""Alias for absolute."""
570
571
# Comparison operations
572
def equal(x1, x2, **kwargs):
573
"""
574
Element-wise equality comparison.
575
576
Parameters:
577
- x1, x2: Input arrays
578
- **kwargs: Additional ufunc parameters
579
580
Returns:
581
dask.array.Array: Boolean array of equality
582
"""
583
584
def not_equal(x1, x2, **kwargs):
585
"""
586
Element-wise inequality comparison.
587
588
Parameters:
589
- x1, x2: Input arrays
590
- **kwargs: Additional ufunc parameters
591
592
Returns:
593
dask.array.Array: Boolean array of inequality
594
"""
595
596
def less(x1, x2, **kwargs):
597
"""
598
Element-wise less-than comparison.
599
600
Parameters:
601
- x1, x2: Input arrays
602
- **kwargs: Additional ufunc parameters
603
604
Returns:
605
dask.array.Array: Boolean array of x1 < x2
606
"""
607
608
def less_equal(x1, x2, **kwargs):
609
"""
610
Element-wise less-than-or-equal comparison.
611
612
Parameters:
613
- x1, x2: Input arrays
614
- **kwargs: Additional ufunc parameters
615
616
Returns:
617
dask.array.Array: Boolean array of x1 <= x2
618
"""
619
620
def greater(x1, x2, **kwargs):
621
"""
622
Element-wise greater-than comparison.
623
624
Parameters:
625
- x1, x2: Input arrays
626
- **kwargs: Additional ufunc parameters
627
628
Returns:
629
dask.array.Array: Boolean array of x1 > x2
630
"""
631
632
def greater_equal(x1, x2, **kwargs):
633
"""
634
Element-wise greater-than-or-equal comparison.
635
636
Parameters:
637
- x1, x2: Input arrays
638
- **kwargs: Additional ufunc parameters
639
640
Returns:
641
dask.array.Array: Boolean array of x1 >= x2
642
"""
643
644
# Trigonometric functions
645
def sin(x, **kwargs):
646
"""Trigonometric sine element-wise."""
647
648
def cos(x, **kwargs):
649
"""Trigonometric cosine element-wise."""
650
651
def tan(x, **kwargs):
652
"""Trigonometric tangent element-wise."""
653
654
def arcsin(x, **kwargs):
655
"""Inverse sine element-wise."""
656
657
def arccos(x, **kwargs):
658
"""Inverse cosine element-wise."""
659
660
def arctan(x, **kwargs):
661
"""Inverse tangent element-wise."""
662
663
def arctan2(y, x, **kwargs):
664
"""Element-wise arc tangent of y/x in correct quadrant."""
665
666
def degrees(x, **kwargs):
667
"""Convert angles from radians to degrees."""
668
669
def radians(x, **kwargs):
670
"""Convert angles from degrees to radians."""
671
672
def deg2rad(x, **kwargs):
673
"""Convert degrees to radians (alias for radians)."""
674
675
def rad2deg(x, **kwargs):
676
"""Convert radians to degrees (alias for degrees)."""
677
678
# Hyperbolic functions
679
def sinh(x, **kwargs):
680
"""Hyperbolic sine element-wise."""
681
682
def cosh(x, **kwargs):
683
"""Hyperbolic cosine element-wise."""
684
685
def tanh(x, **kwargs):
686
"""Hyperbolic tangent element-wise."""
687
688
def arcsinh(x, **kwargs):
689
"""Inverse hyperbolic sine element-wise."""
690
691
def arccosh(x, **kwargs):
692
"""Inverse hyperbolic cosine element-wise."""
693
694
def arctanh(x, **kwargs):
695
"""Inverse hyperbolic tangent element-wise."""
696
697
# Exponential and logarithmic functions
698
def exp(x, **kwargs):
699
"""Calculate the exponential of all elements."""
700
701
def exp2(x, **kwargs):
702
"""Calculate 2**x for all elements."""
703
704
def expm1(x, **kwargs):
705
"""Calculate exp(x) - 1 for all elements."""
706
707
def log(x, **kwargs):
708
"""Natural logarithm element-wise."""
709
710
def log2(x, **kwargs):
711
"""Base-2 logarithm of x."""
712
713
def log10(x, **kwargs):
714
"""Base-10 logarithm of x."""
715
716
def log1p(x, **kwargs):
717
"""Natural logarithm of 1 + x element-wise."""
718
719
def logaddexp(x1, x2, **kwargs):
720
"""Logarithm of sum of exponentials element-wise."""
721
722
def logaddexp2(x1, x2, **kwargs):
723
"""Logarithm base 2 of sum of exponentials."""
724
725
# Power and root functions
726
def sqrt(x, **kwargs):
727
"""Square root element-wise."""
728
729
def cbrt(x, **kwargs):
730
"""Cube root element-wise."""
731
732
def square(x, **kwargs):
733
"""Square of input element-wise."""
734
735
def reciprocal(x, **kwargs):
736
"""Reciprocal (1/x) element-wise."""
737
738
# Rounding functions
739
def around(x, decimals=0, **kwargs):
740
"""Round to given number of decimals."""
741
742
def round(x, decimals=0, **kwargs):
743
"""Round to given number of decimals (alias for around)."""
744
745
def rint(x, **kwargs):
746
"""Round to nearest integer."""
747
748
def fix(x, **kwargs):
749
"""Round to nearest integer towards zero."""
750
751
def floor(x, **kwargs):
752
"""Floor of input element-wise."""
753
754
def ceil(x, **kwargs):
755
"""Ceiling of input element-wise."""
756
757
def trunc(x, **kwargs):
758
"""Truncated value of input element-wise."""
759
760
# Floating point functions
761
def isfinite(x, **kwargs):
762
"""Test finiteness element-wise (not infinity or NaN)."""
763
764
def isinf(x, **kwargs):
765
"""Test for positive or negative infinity."""
766
767
def isnan(x, **kwargs):
768
"""Test for NaN element-wise."""
769
770
def isneginf(x, **kwargs):
771
"""Test for negative infinity element-wise."""
772
773
def isposinf(x, **kwargs):
774
"""Test for positive infinity element-wise."""
775
776
def signbit(x, **kwargs):
777
"""Test whether sign bit is set element-wise."""
778
779
def copysign(x1, x2, **kwargs):
780
"""Change sign of x1 to match sign of x2."""
781
782
def frexp(x, **kwargs):
783
"""Decompose into mantissa and exponent."""
784
785
def ldexp(x1, x2, **kwargs):
786
"""Compute x1 * 2**x2 element-wise."""
787
788
def nextafter(x1, x2, **kwargs):
789
"""Return next floating-point value after x1 towards x2."""
790
791
def spacing(x, **kwargs):
792
"""Return spacing between x and nearest adjacent number."""
793
794
# Complex number functions
795
def real(val):
796
"""Return real part of complex array."""
797
798
def imag(val):
799
"""Return imaginary part of complex array."""
800
801
def conj(x, **kwargs):
802
"""Return complex conjugate element-wise."""
803
804
def conjugate(x, **kwargs):
805
"""Return complex conjugate element-wise (alias for conj)."""
806
807
def angle(z, deg=False):
808
"""Return argument of complex number."""
809
810
# Bit manipulation (integer arrays)
811
def bitwise_and(x1, x2, **kwargs):
812
"""Compute bitwise AND element-wise."""
813
814
def bitwise_or(x1, x2, **kwargs):
815
"""Compute bitwise OR element-wise."""
816
817
def bitwise_xor(x1, x2, **kwargs):
818
"""Compute bitwise XOR element-wise."""
819
820
def bitwise_not(x, **kwargs):
821
"""Compute bitwise NOT element-wise."""
822
823
def invert(x, **kwargs):
824
"""Compute bitwise NOT element-wise (alias for bitwise_not)."""
825
826
def left_shift(x1, x2, **kwargs):
827
"""Shift bits to the left."""
828
829
def right_shift(x1, x2, **kwargs):
830
"""Shift bits to the right."""
831
832
# Logical operations
833
def logical_and(x1, x2, **kwargs):
834
"""Compute logical AND element-wise."""
835
836
def logical_or(x1, x2, **kwargs):
837
"""Compute logical OR element-wise."""
838
839
def logical_xor(x1, x2, **kwargs):
840
"""Compute logical XOR element-wise."""
841
842
def logical_not(x, **kwargs):
843
"""Compute logical NOT element-wise."""
844
845
# Basic linear algebra
846
def dot(a, b):
847
"""Dot product of two arrays."""
848
849
def matmul(x1, x2):
850
"""Matrix product of two arrays."""
851
852
def tensordot(a, b, axes=2):
853
"""Compute tensor dot product along specified axes."""
854
855
def outer(a, b):
856
"""Compute outer product of two vectors."""
857
858
def inner(a, b):
859
"""Inner product of two arrays."""
860
861
def vdot(a, b):
862
"""Return dot product of two vectors."""
863
864
def einsum(subscripts, *operands, **kwargs):
865
"""Evaluate Einstein summation convention."""
866
867
def kron(a, b):
868
"""Kronecker product of two arrays."""
869
870
# Miscellaneous operations
871
def clip(a, a_min, a_max, **kwargs):
872
"""Clip values to specified range."""
873
874
def fabs(x, **kwargs):
875
"""Compute absolute values element-wise."""
876
877
def sign(x, **kwargs):
878
"""Return element-wise indication of sign."""
879
880
def heaviside(x1, x2, **kwargs):
881
"""Compute Heaviside step function."""
882
883
def maximum(x1, x2, **kwargs):
884
"""Element-wise maximum of arrays."""
885
886
def minimum(x1, x2, **kwargs):
887
"""Element-wise minimum of arrays."""
888
889
def fmax(x1, x2, **kwargs):
890
"""Element-wise maximum, ignoring NaNs."""
891
892
def fmin(x1, x2, **kwargs):
893
"""Element-wise minimum, ignoring NaNs."""
894
895
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None):
896
"""Replace NaN with zero and infinity with large numbers."""
897
898
def interp(x, xp, fp, left=None, right=None, period=None):
899
"""One-dimensional linear interpolation."""
900
```
901
902
### Reductions
903
904
Aggregate operations that reduce array dimensions.
905
906
```python { .api }
907
# Statistical reductions
908
def sum(a, axis=None, dtype=None, keepdims=False, split_every=None):
909
"""
910
Sum of array elements over given axes.
911
912
Parameters:
913
- a: Input array
914
- axis: Axis or axes along which to sum
915
- dtype: Data type for result
916
- keepdims: Keep reduced dimensions as size-one dimensions
917
- split_every: Branching factor for reduction tree
918
919
Returns:
920
dask.array.Array: Sum along specified axes
921
"""
922
923
def mean(a, axis=None, dtype=None, keepdims=False, split_every=None):
924
"""
925
Arithmetic mean along specified axes.
926
927
Parameters:
928
- a: Input array
929
- axis: Axis or axes along which to compute mean
930
- dtype: Data type for result
931
- keepdims: Keep reduced dimensions
932
- split_every: Branching factor for reduction tree
933
934
Returns:
935
dask.array.Array: Mean along specified axes
936
"""
937
938
def std(a, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None):
939
"""
940
Standard deviation along specified axes.
941
942
Parameters:
943
- a: Input array
944
- axis: Axis or axes along which to compute standard deviation
945
- dtype: Data type for result
946
- keepdims: Keep reduced dimensions
947
- ddof: Delta degrees of freedom
948
- split_every: Branching factor for reduction tree
949
950
Returns:
951
dask.array.Array: Standard deviation
952
"""
953
954
def var(a, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None):
955
"""
956
Variance along specified axes.
957
958
Parameters:
959
- a: Input array
960
- axis: Axis or axes along which to compute variance
961
- dtype: Data type for result
962
- keepdims: Keep reduced dimensions
963
- ddof: Delta degrees of freedom
964
- split_every: Branching factor for reduction tree
965
966
Returns:
967
dask.array.Array: Variance
968
"""
969
970
# Extrema
971
def min(a, axis=None, keepdims=False, split_every=None):
972
"""Minimum values along specified axes."""
973
974
def max(a, axis=None, keepdims=False, split_every=None):
975
"""Maximum values along specified axes."""
976
977
def argmin(a, axis=None, split_every=None):
978
"""Indices of minimum values along specified axis."""
979
980
def argmax(a, axis=None, split_every=None):
981
"""Indices of maximum values along specified axis."""
982
983
def amin(a, axis=None, keepdims=False, split_every=None):
984
"""Minimum values along specified axes (alias for min)."""
985
986
def amax(a, axis=None, keepdims=False, split_every=None):
987
"""Maximum values along specified axes (alias for max)."""
988
989
# Logical reductions
990
def all(a, axis=None, keepdims=False, split_every=None):
991
"""Test whether all array elements evaluate to True."""
992
993
def any(a, axis=None, keepdims=False, split_every=None):
994
"""Test whether any array element evaluates to True."""
995
996
# Other reductions
997
def prod(a, axis=None, dtype=None, keepdims=False, split_every=None):
998
"""Product of array elements over given axes."""
999
1000
def product(a, axis=None, dtype=None, keepdims=False, split_every=None):
1001
"""Product of array elements (alias for prod)."""
1002
1003
def cumsum(a, axis=None, dtype=None):
1004
"""Cumulative sum along specified axis."""
1005
1006
def cumprod(a, axis=None, dtype=None):
1007
"""Cumulative product along specified axis."""
1008
1009
def cumulative_sum(a, axis=None, dtype=None):
1010
"""Cumulative sum (alias for cumsum)."""
1011
1012
def cumulative_prod(a, axis=None, dtype=None):
1013
"""Cumulative product (alias for cumprod)."""
1014
1015
# NaN-aware reductions
1016
def nansum(a, axis=None, dtype=None, keepdims=False, split_every=None):
1017
"""Sum of array elements, treating NaNs as zero."""
1018
1019
def nanmean(a, axis=None, dtype=None, keepdims=False, split_every=None):
1020
"""Mean of array elements, ignoring NaNs."""
1021
1022
def nanstd(a, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None):
1023
"""Standard deviation ignoring NaNs."""
1024
1025
def nanvar(a, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None):
1026
"""Variance ignoring NaNs."""
1027
1028
def nanmin(a, axis=None, keepdims=False, split_every=None):
1029
"""Minimum values ignoring NaNs."""
1030
1031
def nanmax(a, axis=None, keepdims=False, split_every=None):
1032
"""Maximum values ignoring NaNs."""
1033
1034
def nanprod(a, axis=None, dtype=None, keepdims=False, split_every=None):
1035
"""Product of array elements, treating NaNs as one."""
1036
1037
def nanargmin(a, axis=None, split_every=None):
1038
"""Indices of minimum values ignoring NaNs."""
1039
1040
def nanargmax(a, axis=None, split_every=None):
1041
"""Indices of maximum values ignoring NaNs."""
1042
1043
def nancumsum(a, axis=None, dtype=None):
1044
"""Cumulative sum ignoring NaNs."""
1045
1046
def nancumprod(a, axis=None, dtype=None):
1047
"""Cumulative product ignoring NaNs."""
1048
1049
def nanmedian(a, axis=None, keepdims=False):
1050
"""Median ignoring NaNs."""
1051
1052
def nanquantile(a, q, axis=None, keepdims=False, interpolation='linear'):
1053
"""Quantiles ignoring NaNs."""
1054
1055
def nanpercentile(a, q, axis=None, keepdims=False, interpolation='linear'):
1056
"""Percentiles ignoring NaNs."""
1057
1058
# Count and validation reductions
1059
def count_nonzero(a, axis=None, keepdims=False, split_every=None):
1060
"""Count non-zero elements along specified axes."""
1061
1062
def ptp(a, axis=None, keepdims=False):
1063
"""Peak-to-peak (maximum - minimum) value."""
1064
1065
# Additional statistical functions
1066
def median(a, axis=None, keepdims=False):
1067
"""Median of array elements along specified axes."""
1068
1069
def quantile(a, q, axis=None, keepdims=False, interpolation='linear'):
1070
"""Compute quantiles along specified axes."""
1071
1072
def percentile(a, q, axis=None, keepdims=False, interpolation='linear'):
1073
"""Compute percentiles along specified axes."""
1074
1075
# Top-k operations
1076
def topk(a, k, axis=None, split_every=None):
1077
"""Find top k values along specified axis."""
1078
1079
def argtopk(a, k, axis=None, split_every=None):
1080
"""Find indices of top k values along specified axis."""
1081
1082
# Moment and correlation functions
1083
def moment(a, moment, axis=None, keepdims=False):
1084
"""Calculate nth moment about the mean."""
1085
1086
def skew(a, axis=None, keepdims=False, bias=True):
1087
"""Compute sample skewness of data."""
1088
1089
def kurtosis(a, axis=None, keepdims=False, fisher=True, bias=True):
1090
"""Compute kurtosis (Fisher or Pearson) of dataset."""
1091
```
1092
1093
### Array Manipulation
1094
1095
Functions for changing array shape and structure.
1096
1097
```python { .api }
1098
def reshape(a, shape, merge_chunks=True): ...
1099
def transpose(a, axes=None): ...
1100
def swapaxes(a, axis1, axis2): ...
1101
def moveaxis(a, source, destination): ...
1102
def squeeze(a, axis=None): ...
1103
def expand_dims(a, axis): ...
1104
1105
def concatenate(seq, axis=0, allow_unknown_chunksizes=False): ...
1106
def stack(arrays, axis=0, allow_unknown_chunksizes=False): ...
1107
def block(arrays): ...
1108
1109
def split(ary, indices_or_sections, axis=0): ...
1110
def hsplit(ary, indices_or_sections): ...
1111
def vsplit(ary, indices_or_sections): ...
1112
1113
def flip(m, axis=None): ...
1114
def fliplr(m): ...
1115
def flipud(m): ...
1116
def roll(a, shift, axis=None): ...
1117
def rot90(m, k=1, axes=(0, 1)): ...
1118
1119
# Advanced array operations
1120
def take(a, indices, axis=None): ...
1121
def choose(a, choices, mode='raise'): ...
1122
def compress(condition, a, axis=None): ...
1123
def extract(condition, arr): ...
1124
def place(arr, mask, vals): ...
1125
def put(a, ind, v, mode='raise'): ...
1126
def putmask(a, mask, values): ...
1127
def select(condlist, choicelist, default=0): ...
1128
def where(condition, x=None, y=None): ...
1129
1130
# Indexing and searching
1131
def argwhere(a): ...
1132
def nonzero(a): ...
1133
def flatnonzero(a): ...
1134
def searchsorted(a, v, side='left', sorter=None): ...
1135
1136
# Array testing
1137
def allclose(a, b, rtol=1e-5, atol=1e-8, equal_nan=False): ...
1138
def isclose(a, b, rtol=1e-5, atol=1e-8, equal_nan=False): ...
1139
def isin(element, test_elements, assume_unique=False, invert=False): ...
1140
1141
# Histograms
1142
def histogram(a, bins=10, range=None, weights=None, density=None): ...
1143
def histogram2d(x, y, bins=10, range=None, weights=None, density=None): ...
1144
def histogramdd(sample, bins=10, range=None, weights=None, density=None): ...
1145
def bincount(x, weights=None, minlength=0): ...
1146
def digitize(x, bins, right=False): ...
1147
1148
# Sorting and ordering
1149
def sort(a, axis=-1, kind=None, order=None): ...
1150
def argsort(a, axis=-1, kind=None, order=None): ...
1151
def partition(a, kth, axis=-1, kind='introselect', order=None): ...
1152
def argpartition(a, kth, axis=-1, kind='introselect', order=None): ...
1153
1154
# Set operations
1155
def unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None): ...
1156
def union1d(ar1, ar2): ...
1157
def intersect1d(ar1, ar2, assume_unique=False, return_indices=False): ...
1158
def setdiff1d(ar1, ar2, assume_unique=False): ...
1159
def setxor1d(ar1, ar2, assume_unique=False): ...
1160
def in1d(ar1, ar2, assume_unique=False, invert=False): ...
1161
1162
# Array padding and tiling
1163
def pad(array, pad_width, mode='constant', **kwargs): ...
1164
def tile(A, reps): ...
1165
def repeat(a, repeats, axis=None): ...
1166
1167
# Matrix operations
1168
def diag(v, k=0): ...
1169
def diagonal(a, offset=0, axis1=0, axis2=1): ...
1170
def trace(a, offset=0, axis1=0, axis2=1, dtype=None): ...
1171
def tri(N, M=None, k=0, dtype=float): ...
1172
def tril(m, k=0): ...
1173
def triu(m, k=0): ...
1174
def tril_indices(n, k=0, m=None): ...
1175
def triu_indices(n, k=0, m=None): ...
1176
def tril_indices_from(arr, k=0): ...
1177
def triu_indices_from(arr, k=0): ...
1178
1179
# Multi-dimensional array indexing
1180
def unravel_index(indices, shape, order='C'): ...
1181
def ravel_multi_index(multi_index, dims, mode='raise', order='C'): ...
1182
def ix_(*args): ...
1183
def meshgrid(*xi, copy=True, sparse=False, indexing='xy'): ...
1184
def mgrid(): ...
1185
def ogrid(): ...
1186
1187
# Statistical functions
1188
def average(a, axis=None, weights=None, returned=False): ...
1189
def corrcoef(x, y=None, rowvar=True, bias=None, ddof=None): ...
1190
def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None): ...
1191
def percentile(a, q, axis=None, interpolation='linear', keepdims=False): ...
1192
def quantile(a, q, axis=None, interpolation='linear', keepdims=False): ...
1193
def nanpercentile(a, q, axis=None, interpolation='linear', keepdims=False): ...
1194
def nanquantile(a, q, axis=None, interpolation='linear', keepdims=False): ...
1195
1196
# Array shape utilities
1197
def atleast_1d(*arys): ...
1198
def atleast_2d(*arys): ...
1199
def atleast_3d(*arys): ...
1200
def broadcast_arrays(*args, **kwargs): ...
1201
def broadcast_to(array, shape, chunks=None): ...
1202
def squeeze(a, axis=None): ...
1203
def expand_dims(a, axis): ...
1204
```
1205
1206
### I/O Operations
1207
1208
Save and load arrays from various storage formats.
1209
1210
```python { .api }
1211
def store(sources, targets, lock=True, regions=None, compute=True,
1212
return_stored=False, **kwargs):
1213
"""
1214
Store arrays to various backends.
1215
1216
Parameters:
1217
- sources: Arrays to store
1218
- targets: Storage targets
1219
- lock: Use locks for thread safety
1220
- regions: Storage regions
1221
- compute: Whether to compute immediately
1222
- return_stored: Return stored arrays
1223
1224
Returns:
1225
None or stored arrays if return_stored=True
1226
"""
1227
1228
def to_zarr(arr, url, component=None, storage_options=None,
1229
overwrite=False, region=None, compute=True, return_stored=False,
1230
**kwargs):
1231
"""Save array to Zarr format."""
1232
1233
def from_zarr(url, component=None, storage_options=None, chunks=None,
1234
**kwargs):
1235
"""Load array from Zarr format."""
1236
1237
def to_hdf5(filename, datapath, array, **kwargs):
1238
"""Save array to HDF5 format."""
1239
1240
def to_npy_stack(dirname, array, axis=0):
1241
"""Save array as stack of .npy files."""
1242
1243
def from_npy_stack(dirname, mmap_mode='r'):
1244
"""Load array from stack of .npy files."""
1245
```
1246
1247
### Advanced Operations
1248
1249
Specialized array operations for complex computations.
1250
1251
```python { .api }
1252
def map_blocks(func, *arrays, dtype=None, chunks=None, drop_axis=None,
1253
new_axis=None, **kwargs):
1254
"""
1255
Apply function to blocks of arrays.
1256
1257
Parameters:
1258
- func: Function to apply to each block
1259
- *arrays: Input arrays
1260
- dtype: Output data type
1261
- chunks: Output chunk specification
1262
- drop_axis: Axes to remove from output
1263
- new_axis: Axes to add to output
1264
- **kwargs: Additional arguments to func
1265
1266
Returns:
1267
dask.array.Array: Result of blockwise operation
1268
"""
1269
1270
def blockwise(func, output_indices, *arrays_and_indices, **kwargs):
1271
"""
1272
General blockwise operation with Einstein notation.
1273
1274
Parameters:
1275
- func: Function to apply
1276
- output_indices: Output dimension labels
1277
- *arrays_and_indices: Arrays and their dimension labels
1278
- **kwargs: Additional parameters
1279
1280
Returns:
1281
dask.array.Array: Result array
1282
"""
1283
1284
def atop(func, output_indices, *arrays_and_indices, **kwargs):
1285
"""Alias for blockwise."""
1286
1287
def apply_along_axis(func1d, axis, arr, *args, dtype=None, shape=None, **kwargs):
1288
"""
1289
Apply function to 1-D slices along given axis.
1290
1291
Parameters:
1292
- func1d: Function to apply to 1-D slices
1293
- axis: Axis along which to apply function
1294
- arr: Input array
1295
- *args: Additional arguments to func1d
1296
- dtype: Output data type
1297
- shape: Output shape per slice
1298
- **kwargs: Additional keyword arguments
1299
1300
Returns:
1301
dask.array.Array: Result of applying function along axis
1302
"""
1303
1304
def apply_gufunc(func, signature, *args, axes=None, axis=None, keepdims=False,
1305
output_dtypes=None, output_sizes=None, vectorize=None, **kwargs):
1306
"""
1307
Apply generalized universal function over trailing axes.
1308
1309
Parameters:
1310
- func: Function to apply
1311
- signature: Generalized ufunc signature
1312
- *args: Input arrays
1313
- axes: Axes over which to apply function
1314
- axis: Single axis specification
1315
- keepdims: Keep reduced dimensions
1316
- output_dtypes: Output data types
1317
- output_sizes: Output dimension sizes
1318
- vectorize: Vectorize the function
1319
- **kwargs: Additional arguments
1320
1321
Returns:
1322
dask.array.Array: Result of gufunc application
1323
"""
1324
```
1325
1326
### Linear Algebra
1327
1328
Advanced linear algebra operations from `dask.array.linalg`.
1329
1330
```python { .api }
1331
# Basic linear algebra operations
1332
def norm(x, ord=None, axis=None, keepdims=False):
1333
"""
1334
Compute vector or matrix norm.
1335
1336
Parameters:
1337
- x: Input array
1338
- ord: Order of the norm (None, 'fro', 'nuc', inf, -inf, int)
1339
- axis: Axis along which to compute norm
1340
- keepdims: Keep reduced dimensions
1341
1342
Returns:
1343
dask.array.Array: Norm of the input
1344
"""
1345
1346
def solve(a, b):
1347
"""
1348
Solve linear system ax = b.
1349
1350
Parameters:
1351
- a: Coefficient matrix (square)
1352
- b: Dependent variable values
1353
1354
Returns:
1355
dask.array.Array: Solution to the system
1356
"""
1357
1358
def inv(a):
1359
"""
1360
Compute matrix inverse.
1361
1362
Parameters:
1363
- a: Input matrix
1364
1365
Returns:
1366
dask.array.Array: Inverse matrix
1367
"""
1368
1369
def pinv(a, rcond=1e-15):
1370
"""
1371
Compute Moore-Penrose pseudoinverse.
1372
1373
Parameters:
1374
- a: Input matrix
1375
- rcond: Cutoff for small singular values
1376
1377
Returns:
1378
dask.array.Array: Pseudoinverse matrix
1379
"""
1380
1381
def lstsq(a, b, rcond=None):
1382
"""
1383
Solve least-squares problem.
1384
1385
Parameters:
1386
- a: Coefficient matrix
1387
- b: Dependent variable values
1388
- rcond: Cutoff ratio for small singular values
1389
1390
Returns:
1391
tuple: (solution, residuals, rank, singular_values)
1392
"""
1393
1394
# Matrix decompositions
1395
def svd(a, full_matrices=True, compute_uv=True):
1396
"""
1397
Singular Value Decomposition.
1398
1399
Parameters:
1400
- a: Input matrix
1401
- full_matrices: Return full-sized U and Vh matrices
1402
- compute_uv: Compute U and Vh in addition to s
1403
1404
Returns:
1405
tuple: (U, s, Vh) if compute_uv else s
1406
"""
1407
1408
def qr(a, mode='reduced'):
1409
"""
1410
QR decomposition.
1411
1412
Parameters:
1413
- a: Input matrix
1414
- mode: Decomposition mode ('reduced', 'complete', 'r', 'raw')
1415
1416
Returns:
1417
tuple: (Q, R) or R depending on mode
1418
"""
1419
1420
def lu(a):
1421
"""
1422
LU decomposition with partial pivoting.
1423
1424
Parameters:
1425
- a: Input matrix
1426
1427
Returns:
1428
tuple: (P, L, U) where P is permutation, L lower, U upper triangular
1429
"""
1430
1431
def cholesky(a):
1432
"""
1433
Cholesky decomposition for positive definite matrices.
1434
1435
Parameters:
1436
- a: Positive definite input matrix
1437
1438
Returns:
1439
dask.array.Array: Lower triangular Cholesky factor
1440
"""
1441
1442
def eig(a):
1443
"""
1444
Compute eigenvalues and eigenvectors.
1445
1446
Parameters:
1447
- a: Input matrix
1448
1449
Returns:
1450
tuple: (eigenvalues, eigenvectors)
1451
"""
1452
1453
def eigh(a, UPLO='L'):
1454
"""
1455
Compute eigenvalues and eigenvectors of Hermitian matrix.
1456
1457
Parameters:
1458
- a: Hermitian input matrix
1459
- UPLO: Use upper ('U') or lower ('L') triangle
1460
1461
Returns:
1462
tuple: (eigenvalues, eigenvectors)
1463
"""
1464
1465
def eigvals(a):
1466
"""
1467
Compute eigenvalues only.
1468
1469
Parameters:
1470
- a: Input matrix
1471
1472
Returns:
1473
dask.array.Array: Eigenvalues
1474
"""
1475
1476
def eigvalsh(a, UPLO='L'):
1477
"""
1478
Compute eigenvalues of Hermitian matrix.
1479
1480
Parameters:
1481
- a: Hermitian input matrix
1482
- UPLO: Use upper ('U') or lower ('L') triangle
1483
1484
Returns:
1485
dask.array.Array: Eigenvalues
1486
"""
1487
1488
# Matrix properties and operations
1489
def det(a):
1490
"""
1491
Compute matrix determinant.
1492
1493
Parameters:
1494
- a: Input matrix
1495
1496
Returns:
1497
dask.array.Array: Determinant
1498
"""
1499
1500
def slogdet(a):
1501
"""
1502
Compute sign and log of determinant.
1503
1504
Parameters:
1505
- a: Input matrix
1506
1507
Returns:
1508
tuple: (sign, logdet)
1509
"""
1510
1511
def matrix_rank(M, tol=None, hermitian=False):
1512
"""
1513
Compute matrix rank using SVD.
1514
1515
Parameters:
1516
- M: Input matrix
1517
- tol: Threshold below which SVD values are zero
1518
- hermitian: Whether M is Hermitian
1519
1520
Returns:
1521
dask.array.Array: Matrix rank
1522
"""
1523
1524
def cond(x, p=None):
1525
"""
1526
Compute condition number.
1527
1528
Parameters:
1529
- x: Input matrix
1530
- p: Order of the norm used in computation
1531
1532
Returns:
1533
dask.array.Array: Condition number
1534
"""
1535
1536
# Tensor operations
1537
def multi_dot(arrays):
1538
"""
1539
Compute dot product of multiple arrays efficiently.
1540
1541
Parameters:
1542
- arrays: Sequence of arrays to multiply
1543
1544
Returns:
1545
dask.array.Array: Matrix product result
1546
"""
1547
1548
def matrix_power(a, n):
1549
"""
1550
Raise matrix to integer power.
1551
1552
Parameters:
1553
- a: Input matrix
1554
- n: Integer exponent
1555
1556
Returns:
1557
dask.array.Array: Matrix raised to power n
1558
"""
1559
```
1560
1561
### FFT Functions
1562
1563
Fast Fourier Transform operations from `dask.array.fft`.
1564
1565
```python { .api }
1566
# 1-D FFT functions
1567
def fft(a, n=None, axis=-1, norm=None):
1568
"""
1569
Compute 1-D discrete Fourier Transform.
1570
1571
Parameters:
1572
- a: Input array
1573
- n: Length of transformed axis (zero-padding/truncation)
1574
- axis: Axis over which to compute FFT
1575
- norm: Normalization mode ('backward', 'ortho', 'forward')
1576
1577
Returns:
1578
dask.array.Array: Complex FFT result
1579
"""
1580
1581
def ifft(a, n=None, axis=-1, norm=None):
1582
"""
1583
Compute 1-D inverse discrete Fourier Transform.
1584
1585
Parameters:
1586
- a: Input array
1587
- n: Length of transformed axis
1588
- axis: Axis over which to compute inverse FFT
1589
- norm: Normalization mode
1590
1591
Returns:
1592
dask.array.Array: Complex inverse FFT result
1593
"""
1594
1595
def rfft(a, n=None, axis=-1, norm=None):
1596
"""
1597
Compute 1-D FFT for real input.
1598
1599
Parameters:
1600
- a: Input real array
1601
- n: Length of transformed axis
1602
- axis: Axis over which to compute FFT
1603
- norm: Normalization mode
1604
1605
Returns:
1606
dask.array.Array: Complex FFT result (half spectrum)
1607
"""
1608
1609
def irfft(a, n=None, axis=-1, norm=None):
1610
"""
1611
Compute inverse of rfft.
1612
1613
Parameters:
1614
- a: Input complex array
1615
- n: Length of output axis
1616
- axis: Axis over which to compute inverse FFT
1617
- norm: Normalization mode
1618
1619
Returns:
1620
dask.array.Array: Real inverse FFT result
1621
"""
1622
1623
def hfft(a, n=None, axis=-1, norm=None):
1624
"""
1625
Compute FFT of signal with Hermitian symmetry.
1626
1627
Parameters:
1628
- a: Input array with Hermitian symmetry
1629
- n: Length of output axis
1630
- axis: Axis over which to compute FFT
1631
- norm: Normalization mode
1632
1633
Returns:
1634
dask.array.Array: Real FFT result
1635
"""
1636
1637
def ihfft(a, n=None, axis=-1, norm=None):
1638
"""
1639
Compute inverse FFT of real signal.
1640
1641
Parameters:
1642
- a: Input real array
1643
- n: Length of output axis
1644
- axis: Axis over which to compute inverse FFT
1645
- norm: Normalization mode
1646
1647
Returns:
1648
dask.array.Array: Complex result with Hermitian symmetry
1649
"""
1650
1651
# 2-D FFT functions
1652
def fft2(a, s=None, axes=(-2, -1), norm=None):
1653
"""
1654
Compute 2-D discrete Fourier Transform.
1655
1656
Parameters:
1657
- a: Input array
1658
- s: Shape of output (zero-padding/truncation)
1659
- axes: Axes over which to compute 2-D FFT
1660
- norm: Normalization mode
1661
1662
Returns:
1663
dask.array.Array: Complex 2-D FFT result
1664
"""
1665
1666
def ifft2(a, s=None, axes=(-2, -1), norm=None):
1667
"""
1668
Compute 2-D inverse discrete Fourier Transform.
1669
1670
Parameters:
1671
- a: Input array
1672
- s: Shape of output
1673
- axes: Axes over which to compute 2-D inverse FFT
1674
- norm: Normalization mode
1675
1676
Returns:
1677
dask.array.Array: Complex 2-D inverse FFT result
1678
"""
1679
1680
def rfft2(a, s=None, axes=(-2, -1), norm=None):
1681
"""
1682
Compute 2-D FFT for real input.
1683
1684
Parameters:
1685
- a: Input real array
1686
- s: Shape of output
1687
- axes: Axes over which to compute 2-D FFT
1688
- norm: Normalization mode
1689
1690
Returns:
1691
dask.array.Array: Complex 2-D FFT result
1692
"""
1693
1694
def irfft2(a, s=None, axes=(-2, -1), norm=None):
1695
"""
1696
Compute 2-D inverse of rfft2.
1697
1698
Parameters:
1699
- a: Input complex array
1700
- s: Shape of output
1701
- axes: Axes over which to compute 2-D inverse FFT
1702
- norm: Normalization mode
1703
1704
Returns:
1705
dask.array.Array: Real 2-D inverse FFT result
1706
"""
1707
1708
# N-D FFT functions
1709
def fftn(a, s=None, axes=None, norm=None):
1710
"""
1711
Compute N-D discrete Fourier Transform.
1712
1713
Parameters:
1714
- a: Input array
1715
- s: Shape of output along transformed axes
1716
- axes: Axes over which to compute N-D FFT
1717
- norm: Normalization mode
1718
1719
Returns:
1720
dask.array.Array: Complex N-D FFT result
1721
"""
1722
1723
def ifftn(a, s=None, axes=None, norm=None):
1724
"""
1725
Compute N-D inverse discrete Fourier Transform.
1726
1727
Parameters:
1728
- a: Input array
1729
- s: Shape of output along transformed axes
1730
- axes: Axes over which to compute N-D inverse FFT
1731
- norm: Normalization mode
1732
1733
Returns:
1734
dask.array.Array: Complex N-D inverse FFT result
1735
"""
1736
1737
def rfftn(a, s=None, axes=None, norm=None):
1738
"""
1739
Compute N-D FFT for real input.
1740
1741
Parameters:
1742
- a: Input real array
1743
- s: Shape of output along transformed axes
1744
- axes: Axes over which to compute N-D FFT
1745
- norm: Normalization mode
1746
1747
Returns:
1748
dask.array.Array: Complex N-D FFT result
1749
"""
1750
1751
def irfftn(a, s=None, axes=None, norm=None):
1752
"""
1753
Compute N-D inverse of rfftn.
1754
1755
Parameters:
1756
- a: Input complex array
1757
- s: Shape of output along transformed axes
1758
- axes: Axes over which to compute N-D inverse FFT
1759
- norm: Normalization mode
1760
1761
Returns:
1762
dask.array.Array: Real N-D inverse FFT result
1763
"""
1764
1765
# Helper functions
1766
def fftfreq(n, d=1.0, chunks=None):
1767
"""
1768
Return discrete Fourier Transform sample frequencies.
1769
1770
Parameters:
1771
- n: Window length
1772
- d: Sample spacing
1773
- chunks: Chunk specification for output
1774
1775
Returns:
1776
dask.array.Array: Sample frequencies
1777
"""
1778
1779
def rfftfreq(n, d=1.0, chunks=None):
1780
"""
1781
Return sample frequencies for rfft.
1782
1783
Parameters:
1784
- n: Window length
1785
- d: Sample spacing
1786
- chunks: Chunk specification for output
1787
1788
Returns:
1789
dask.array.Array: Sample frequencies for real FFT
1790
"""
1791
1792
def fftshift(x, axes=None):
1793
"""
1794
Shift zero-frequency component to center.
1795
1796
Parameters:
1797
- x: Input array
1798
- axes: Axes over which to shift (None for all)
1799
1800
Returns:
1801
dask.array.Array: Shifted array
1802
"""
1803
1804
def ifftshift(x, axes=None):
1805
"""
1806
Inverse of fftshift.
1807
1808
Parameters:
1809
- x: Input array
1810
- axes: Axes over which to shift (None for all)
1811
1812
Returns:
1813
dask.array.Array: Inverse shifted array
1814
"""
1815
```
1816
1817
### Random Functions
1818
1819
Random number generation from `dask.array.random`.
1820
1821
```python { .api }
1822
# Random state management
1823
class Generator:
1824
"""
1825
Random number generator using the new numpy.random.Generator interface.
1826
1827
Parameters:
1828
- bit_generator: Bit generator instance (PCG64, MT19937, etc.)
1829
"""
1830
1831
def __init__(self, bit_generator):
1832
"""Initialize Generator with bit generator."""
1833
1834
def random(self, size=None, chunks=None, **kwargs):
1835
"""Generate random floats in [0, 1)."""
1836
1837
def integers(self, low, high=None, size=None, dtype=int,
1838
endpoint=False, chunks=None, **kwargs):
1839
"""Generate random integers."""
1840
1841
def normal(self, loc=0.0, scale=1.0, size=None, chunks=None, **kwargs):
1842
"""Generate normally distributed random numbers."""
1843
1844
def uniform(self, low=0.0, high=1.0, size=None, chunks=None, **kwargs):
1845
"""Generate uniformly distributed random numbers."""
1846
1847
class RandomState:
1848
"""
1849
Legacy random state interface compatible with numpy.random.RandomState.
1850
1851
Parameters:
1852
- seed: Random seed for reproducibility
1853
"""
1854
1855
def __init__(self, seed=None):
1856
"""Initialize RandomState with optional seed."""
1857
1858
def random_sample(self, size=None, chunks=None, **kwargs):
1859
"""Generate random floats in [0, 1)."""
1860
1861
def randint(self, low, high=None, size=None, dtype=int, chunks=None, **kwargs):
1862
"""Generate random integers."""
1863
1864
def normal(self, loc=0.0, scale=1.0, size=None, chunks=None, **kwargs):
1865
"""Generate normally distributed random numbers."""
1866
1867
def uniform(self, low=0.0, high=1.0, size=None, chunks=None, **kwargs):
1868
"""Generate uniformly distributed random numbers."""
1869
1870
# Default random functions using global state
1871
def random(size=None, chunks=None, **kwargs):
1872
"""
1873
Generate random floats in [0, 1).
1874
1875
Parameters:
1876
- size: Output shape
1877
- chunks: Chunk specification
1878
- **kwargs: Additional arguments
1879
1880
Returns:
1881
dask.array.Array: Random float array
1882
"""
1883
1884
def randint(low, high=None, size=None, dtype=int, chunks=None, **kwargs):
1885
"""
1886
Generate random integers.
1887
1888
Parameters:
1889
- low: Lowest integer (inclusive) or highest if high is None
1890
- high: Highest integer (exclusive)
1891
- size: Output shape
1892
- dtype: Integer type
1893
- chunks: Chunk specification
1894
1895
Returns:
1896
dask.array.Array: Random integer array
1897
"""
1898
1899
# Continuous distributions
1900
def beta(a, b, size=None, chunks=None, **kwargs):
1901
"""Generate beta distributed random numbers."""
1902
1903
def binomial(n, p, size=None, chunks=None, **kwargs):
1904
"""Generate binomial distributed random numbers."""
1905
1906
def chisquare(df, size=None, chunks=None, **kwargs):
1907
"""Generate chi-square distributed random numbers."""
1908
1909
def dirichlet(alpha, size=None, chunks=None, **kwargs):
1910
"""Generate Dirichlet distributed random numbers."""
1911
1912
def exponential(scale=1.0, size=None, chunks=None, **kwargs):
1913
"""
1914
Generate exponentially distributed random numbers.
1915
1916
Parameters:
1917
- scale: Scale parameter (1/rate)
1918
- size: Output shape
1919
- chunks: Chunk specification
1920
1921
Returns:
1922
dask.array.Array: Exponentially distributed random numbers
1923
"""
1924
1925
def f(dfnum, dfden, size=None, chunks=None, **kwargs):
1926
"""Generate F-distributed random numbers."""
1927
1928
def gamma(shape, scale=1.0, size=None, chunks=None, **kwargs):
1929
"""
1930
Generate gamma distributed random numbers.
1931
1932
Parameters:
1933
- shape: Shape parameter
1934
- scale: Scale parameter
1935
- size: Output shape
1936
- chunks: Chunk specification
1937
1938
Returns:
1939
dask.array.Array: Gamma distributed random numbers
1940
"""
1941
1942
def geometric(p, size=None, chunks=None, **kwargs):
1943
"""Generate geometric distributed random numbers."""
1944
1945
def gumbel(loc=0.0, scale=1.0, size=None, chunks=None, **kwargs):
1946
"""Generate Gumbel distributed random numbers."""
1947
1948
def hypergeometric(ngood, nbad, nsample, size=None, chunks=None, **kwargs):
1949
"""Generate hypergeometric distributed random numbers."""
1950
1951
def laplace(loc=0.0, scale=1.0, size=None, chunks=None, **kwargs):
1952
"""Generate Laplace distributed random numbers."""
1953
1954
def logistic(loc=0.0, scale=1.0, size=None, chunks=None, **kwargs):
1955
"""Generate logistic distributed random numbers."""
1956
1957
def lognormal(mean=0.0, sigma=1.0, size=None, chunks=None, **kwargs):
1958
"""
1959
Generate log-normal distributed random numbers.
1960
1961
Parameters:
1962
- mean: Mean of underlying normal distribution
1963
- sigma: Standard deviation of underlying normal distribution
1964
- size: Output shape
1965
- chunks: Chunk specification
1966
1967
Returns:
1968
dask.array.Array: Log-normally distributed random numbers
1969
"""
1970
1971
def logseries(p, size=None, chunks=None, **kwargs):
1972
"""Generate log-series distributed random numbers."""
1973
1974
def multinomial(n, pvals, size=None, chunks=None, **kwargs):
1975
"""Generate multinomial distributed random numbers."""
1976
1977
def multivariate_normal(mean, cov, size=None, chunks=None, **kwargs):
1978
"""
1979
Generate multivariate normal distributed random numbers.
1980
1981
Parameters:
1982
- mean: Mean vector
1983
- cov: Covariance matrix
1984
- size: Number of samples
1985
- chunks: Chunk specification
1986
1987
Returns:
1988
dask.array.Array: Multivariate normal distributed samples
1989
"""
1990
1991
def negative_binomial(n, p, size=None, chunks=None, **kwargs):
1992
"""Generate negative binomial distributed random numbers."""
1993
1994
def noncentral_chisquare(df, nonc, size=None, chunks=None, **kwargs):
1995
"""Generate non-central chi-square distributed random numbers."""
1996
1997
def noncentral_f(dfnum, dfden, nonc, size=None, chunks=None, **kwargs):
1998
"""Generate non-central F distributed random numbers."""
1999
2000
def normal(loc=0.0, scale=1.0, size=None, chunks=None, **kwargs):
2001
"""
2002
Generate normal distributed random numbers.
2003
2004
Parameters:
2005
- loc: Mean (location parameter)
2006
- scale: Standard deviation (scale parameter)
2007
- size: Output shape
2008
- chunks: Chunk specification
2009
2010
Returns:
2011
dask.array.Array: Normally distributed random numbers
2012
"""
2013
2014
def pareto(a, size=None, chunks=None, **kwargs):
2015
"""Generate Pareto distributed random numbers."""
2016
2017
def poisson(lam=1.0, size=None, chunks=None, **kwargs):
2018
"""
2019
Generate Poisson distributed random numbers.
2020
2021
Parameters:
2022
- lam: Expected number of events (rate parameter)
2023
- size: Output shape
2024
- chunks: Chunk specification
2025
2026
Returns:
2027
dask.array.Array: Poisson distributed random numbers
2028
"""
2029
2030
def power(a, size=None, chunks=None, **kwargs):
2031
"""Generate power distributed random numbers."""
2032
2033
def rayleigh(scale=1.0, size=None, chunks=None, **kwargs):
2034
"""Generate Rayleigh distributed random numbers."""
2035
2036
def standard_cauchy(size=None, chunks=None, **kwargs):
2037
"""Generate standard Cauchy distributed random numbers."""
2038
2039
def standard_exponential(size=None, chunks=None, **kwargs):
2040
"""Generate standard exponential distributed random numbers."""
2041
2042
def standard_gamma(shape, size=None, chunks=None, **kwargs):
2043
"""Generate standard gamma distributed random numbers."""
2044
2045
def standard_normal(size=None, chunks=None, **kwargs):
2046
"""
2047
Generate standard normal distributed random numbers.
2048
2049
Parameters:
2050
- size: Output shape
2051
- chunks: Chunk specification
2052
2053
Returns:
2054
dask.array.Array: Standard normal distributed random numbers
2055
"""
2056
2057
def standard_t(df, size=None, chunks=None, **kwargs):
2058
"""Generate Student's t distributed random numbers."""
2059
2060
def triangular(left, mode, right, size=None, chunks=None, **kwargs):
2061
"""Generate triangular distributed random numbers."""
2062
2063
def uniform(low=0.0, high=1.0, size=None, chunks=None, **kwargs):
2064
"""
2065
Generate uniform distributed random numbers.
2066
2067
Parameters:
2068
- low: Lower bound (inclusive)
2069
- high: Upper bound (exclusive)
2070
- size: Output shape
2071
- chunks: Chunk specification
2072
2073
Returns:
2074
dask.array.Array: Uniformly distributed random numbers
2075
"""
2076
2077
def vonmises(mu, kappa, size=None, chunks=None, **kwargs):
2078
"""Generate von Mises distributed random numbers."""
2079
2080
def wald(mean, scale, size=None, chunks=None, **kwargs):
2081
"""Generate Wald (inverse Gaussian) distributed random numbers."""
2082
2083
def weibull(a, size=None, chunks=None, **kwargs):
2084
"""Generate Weibull distributed random numbers."""
2085
2086
def zipf(a, size=None, chunks=None, **kwargs):
2087
"""Generate Zipf distributed random numbers."""
2088
2089
# Utility functions
2090
def choice(a, size=None, replace=True, p=None, chunks=None, **kwargs):
2091
"""
2092
Generate random samples from array elements.
2093
2094
Parameters:
2095
- a: Array to sample from or integer for arange(a)
2096
- size: Output shape
2097
- replace: Sample with replacement
2098
- p: Probabilities for each element
2099
- chunks: Chunk specification
2100
2101
Returns:
2102
dask.array.Array: Random samples
2103
"""
2104
2105
def permutation(x, chunks=None, **kwargs):
2106
"""
2107
Randomly permute sequence or array.
2108
2109
Parameters:
2110
- x: Sequence to permute or integer for arange(x)
2111
- chunks: Chunk specification
2112
2113
Returns:
2114
dask.array.Array: Permuted array
2115
"""
2116
2117
def shuffle(x, chunks=None, **kwargs):
2118
"""
2119
Shuffle array in-place along first axis.
2120
2121
Parameters:
2122
- x: Array to shuffle
2123
- chunks: Chunk specification for output
2124
2125
Returns:
2126
dask.array.Array: Shuffled array
2127
"""
2128
2129
def seed(seed=None):
2130
"""
2131
Seed the global random state.
2132
2133
Parameters:
2134
- seed: Random seed for reproducibility
2135
"""
2136
2137
def get_state():
2138
"""Get current random state."""
2139
2140
def set_state(state):
2141
"""Set random state."""
2142
```
2143
2144
## Usage Examples
2145
2146
### Basic Array Creation
2147
2148
```python
2149
import dask.array as da
2150
import numpy as np
2151
2152
# Create from NumPy array
2153
x_np = np.random.random((10000, 10000))
2154
x_da = da.from_array(x_np, chunks=(1000, 1000))
2155
2156
# Create directly
2157
y = da.random.random((10000, 10000), chunks=(1000, 1000))
2158
z = da.zeros((5000, 5000), chunks=(1000, 1000))
2159
2160
# Basic operations maintain chunking
2161
result = (x_da + y).sum(axis=0)
2162
```
2163
2164
### Chunking Strategies
2165
2166
```python
2167
import dask.array as da
2168
2169
# Different chunking approaches
2170
x = da.random.random((10000, 10000), chunks='100MB') # Size-based
2171
y = da.random.random((10000, 10000), chunks=(1000, 1000)) # Shape-based
2172
z = da.random.random((10000, 10000), chunks='auto') # Automatic
2173
2174
# Rechunking for different access patterns
2175
x_rechunked = x.rechunk((5000, 2000)) # Better for column operations
2176
```
2177
2178
### Complex Mathematical Operations
2179
2180
```python
2181
import dask.array as da
2182
2183
# Linear algebra
2184
A = da.random.random((5000, 3000), chunks=(1000, 1000))
2185
B = da.random.random((3000, 2000), chunks=(1000, 1000))
2186
C = da.dot(A, B) # Matrix multiplication
2187
2188
# Element-wise operations
2189
x = da.linspace(0, 10, 100000, chunks=10000)
2190
y = da.sin(x) * da.exp(-x/5)
2191
2192
# Reductions with custom chunking
2193
large_sum = da.sum(y, split_every=8) # Control reduction tree
2194
```
2195
2196
### Custom Block Operations
2197
2198
```python
2199
import dask.array as da
2200
import numpy as np
2201
2202
def custom_filter(block):
2203
"""Apply custom filtering to each block."""
2204
return np.where(block > 0.5, block, 0)
2205
2206
# Apply to all blocks
2207
x = da.random.random((10000, 10000), chunks=(1000, 1000))
2208
filtered = da.map_blocks(custom_filter, x, dtype=x.dtype)
2209
2210
# More complex block operation
2211
def normalize_block(block, axis=None):
2212
"""Normalize each block independently."""
2213
return (block - block.mean(axis=axis, keepdims=True)) / block.std(axis=axis, keepdims=True)
2214
2215
normalized = da.map_blocks(normalize_block, x, axis=1, dtype=x.dtype)
2216
```