1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Fast Fourier Transform (FFT) and its inverse (IFFT) for one, two, and `N` dimensions.
//!
//! Unsuffixed functions are the canonical named `Result` paths and use the current thread's scoped
//! stream or runtime default. Select an explicit stream or device with [`crate::with_stream`] or
//! [`crate::with_device`]. The `_device` functions and generated operation macros are deprecated
//! forwarding shims.
//!
//! N-dimensional transforms accept [`FftnOptions`]. Its default selects every axis at the input
//! lengths, and correlated lengths and axes are validated before calling MLX.
//!
//! # Examples
//!
//! ## One dimension
//!
//! ```rust
//! use mlx_rs::{
//! Dtype, Array, complex64, fft::*,
//! test_utils::{assert_array_eq, tolerances},
//! };
//!
//! let src = [1.0f32, 2.0, 3.0, 4.0];
//! let mut array = Array::from_slice(&src[..], &[4]);
//!
//! let mut fft_result = fft(&array, 4, 0).unwrap();
//! assert_eq!(fft_result.dtype(), Dtype::Complex64);
//!
//! let expected = Array::from_slice(&[
//! complex64::new(10.0, 0.0),
//! complex64::new(-2.0, 2.0),
//! complex64::new(-2.0, 0.0),
//! complex64::new(-2.0, -2.0),
//! ], &[4]);
//! assert_array_eq(&fft_result, expected, tolerances::EXACT.rtol, tolerances::EXACT.atol);
//!
//! let mut ifft_result = ifft(&fft_result, 4, 0).unwrap();
//! assert_eq!(ifft_result.dtype(), Dtype::Complex64);
//!
//! let expected = Array::from_slice(&[
//! complex64::new(1.0, 0.0),
//! complex64::new(2.0, 0.0),
//! complex64::new(3.0, 0.0),
//! complex64::new(4.0, 0.0),
//! ], &[4]);
//! assert_array_eq(ifft_result, expected, tolerances::EXACT.rtol, tolerances::EXACT.atol);
//!
//! let mut rfft_result = rfft(&array, 4, 0).unwrap();
//! assert_eq!(rfft_result.dtype(), Dtype::Complex64);
//!
//! let expected = Array::from_slice(&[
//! complex64::new(10.0, 0.0),
//! complex64::new(-2.0, 2.0),
//! complex64::new(-2.0, 0.0),
//! ], &[3]);
//! assert_array_eq(&rfft_result, expected, tolerances::EXACT.rtol, tolerances::EXACT.atol);
//!
//! let mut irfft_result = irfft(&rfft_result, 4, 0).unwrap();
//! assert_eq!(irfft_result.dtype(), Dtype::Float32);
//! assert_array_eq(
//! irfft_result,
//! Array::from_slice(&src, &[4]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//!
//! // The original array is not modified
//! assert_array_eq(
//! array,
//! Array::from_slice(&src, &[4]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//! ```
//!
//! ## Two dimensions
//!
//! ```rust
//! use mlx_rs::{
//! Dtype, Array, complex64, fft::*,
//! test_utils::{assert_array_eq, tolerances},
//! };
//!
//! let src = [1.0f32, 1.0, 1.0, 1.0];
//! let mut array = Array::from_slice(&src[..], &[2, 2]);
//!
//! let mut fft2_result = fft2(&array, None, None).unwrap();
//! assert_eq!(fft2_result.dtype(), Dtype::Complex64);
//! let expected = Array::from_slice(&[
//! complex64::new(4.0, 0.0),
//! complex64::new(0.0, 0.0),
//! complex64::new(0.0, 0.0),
//! complex64::new(0.0, 0.0),
//! ], &[2, 2]);
//! assert_array_eq(&fft2_result, expected, tolerances::EXACT.rtol, tolerances::EXACT.atol);
//!
//! let mut ifft2_result = ifft2(&fft2_result, None, None).unwrap();
//! assert_eq!(ifft2_result.dtype(), Dtype::Complex64);
//!
//! let expected = Array::from_slice(&[
//! complex64::new(1.0, 0.0),
//! complex64::new(1.0, 0.0),
//! complex64::new(1.0, 0.0),
//! complex64::new(1.0, 0.0),
//! ], &[2, 2]);
//! assert_array_eq(ifft2_result, expected, tolerances::EXACT.rtol, tolerances::EXACT.atol);
//!
//! let mut rfft2_result = rfft2(&array, None, None).unwrap();
//! assert_eq!(rfft2_result.dtype(), Dtype::Complex64);
//!
//! let expected = Array::from_slice(&[
//! complex64::new(4.0, 0.0),
//! complex64::new(0.0, 0.0),
//! complex64::new(0.0, 0.0),
//! complex64::new(0.0, 0.0),
//! ], &[2, 2]);
//! assert_array_eq(&rfft2_result, expected, tolerances::EXACT.rtol, tolerances::EXACT.atol);
//!
//! let mut irfft2_result = irfft2(&rfft2_result, None, None).unwrap();
//! assert_eq!(irfft2_result.dtype(), Dtype::Float32);
//! assert_array_eq(
//! irfft2_result,
//! Array::from_slice(&src, &[2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//!
//! // The original array is not modified
//! assert_array_eq(
//! array,
//! Array::from_slice(&src, &[2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//! ```
//!
//! ## `N` dimensions
//!
//! ```rust
//! use mlx_rs::{
//! Dtype, Array, complex64, fft::*,
//! test_utils::{assert_array_eq, tolerances},
//! };
//!
//! let mut array = Array::ones::<f32>(&[2, 2, 2]).unwrap();
//! let mut fftn_result = fftn(&array, FftnOptions::default()).unwrap();
//! assert_eq!(fftn_result.dtype(), Dtype::Complex64);
//!
//! let mut expected = [complex64::new(0.0, 0.0); 8];
//! expected[0] = complex64::new(8.0, 0.0);
//! assert_array_eq(
//! &fftn_result,
//! Array::from_slice(&expected, &[2, 2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//!
//! let mut ifftn_result = ifftn(&fftn_result, FftnOptions::default()).unwrap();
//! assert_eq!(ifftn_result.dtype(), Dtype::Complex64);
//!
//! let expected = [complex64::new(1.0, 0.0); 8];
//! assert_array_eq(
//! ifftn_result,
//! Array::from_slice(&expected, &[2, 2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//!
//! let mut rfftn_result = rfftn(&array, FftnOptions::default()).unwrap();
//! assert_eq!(rfftn_result.dtype(), Dtype::Complex64);
//!
//! let mut expected = [complex64::new(0.0, 0.0); 8];
//! expected[0] = complex64::new(8.0, 0.0);
//! assert_array_eq(
//! &rfftn_result,
//! Array::from_slice(&expected, &[2, 2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//!
//! let mut irfftn_result = irfftn(&rfftn_result, FftnOptions::default()).unwrap();
//! assert_eq!(irfftn_result.dtype(), Dtype::Float32);
//!
//! let expected = [1.0; 8];
//! assert_array_eq(
//! irfftn_result,
//! Array::from_slice(&expected, &[2, 2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//!
//! // The original array is not modified
//! assert_array_eq(
//! array,
//! Array::from_slice(&[1.0; 8], &[2, 2, 2]),
//! tolerances::EXACT.rtol,
//! tolerances::EXACT.atol,
//! );
//! ```
pub use ;
/* -------------------------------------------------------------------------- */
/* Helper functions */
/* -------------------------------------------------------------------------- */