numr 0.5.2

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! CPU implementation of complex number operations.

use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::ComplexOps;
use crate::ops::common::{validate_complex_real_inputs, validate_make_complex_inputs};
use crate::runtime::cpu::{CpuClient, CpuRuntime, helpers::ensure_contiguous, kernels};
use crate::tensor::Tensor;

/// ComplexOps implementation for CPU runtime.
impl ComplexOps<CpuRuntime> for CpuClient {
    fn conj(&self, a: &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>> {
        let dtype = a.dtype();

        // For real types, conjugate is identity
        if !dtype.is_complex() {
            return Ok(a.clone());
        }

        let shape = a.shape();
        let numel = a.numel();
        let a_contig = ensure_contiguous(a);
        let out = Tensor::<CpuRuntime>::empty(shape, dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let a_ptr = a_contig.ptr();
        let out_ptr = out.ptr();
        let chunk_size = self.chunk_size_hint();

        match dtype {
            DType::Complex64 => {
                self.install_parallelism(|| unsafe {
                    kernels::conj_complex64(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::Complex128 => {
                self.install_parallelism(|| unsafe {
                    kernels::conj_complex128(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("conj called on non-complex dtype"),
        }

        Ok(out)
    }

    fn real(&self, a: &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>> {
        let dtype = a.dtype();

        // For real types, return copy
        if !dtype.is_complex() {
            return Ok(a.clone());
        }

        let shape = a.shape();
        let numel = a.numel();
        let a_contig = ensure_contiguous(a);

        // Determine output dtype (F32 for Complex64, F64 for Complex128)
        let out_dtype = dtype
            .complex_component_dtype()
            .ok_or_else(|| Error::Internal("Expected complex dtype".to_string()))?;

        let out = Tensor::<CpuRuntime>::empty(shape, out_dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let a_ptr = a_contig.ptr();
        let out_ptr = out.ptr();
        let chunk_size = self.chunk_size_hint();

        match dtype {
            DType::Complex64 => {
                self.install_parallelism(|| unsafe {
                    kernels::real_complex64(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::Complex128 => {
                self.install_parallelism(|| unsafe {
                    kernels::real_complex128(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("real called on non-complex dtype"),
        }

        Ok(out)
    }

    fn imag(&self, a: &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>> {
        let dtype = a.dtype();
        let shape = a.shape();
        let numel = a.numel();

        // For real types, return zeros
        if !dtype.is_complex() {
            return Ok(Tensor::<CpuRuntime>::zeros(shape, dtype, &self.device));
        }

        let a_contig = ensure_contiguous(a);

        // Determine output dtype (F32 for Complex64, F64 for Complex128)
        let out_dtype = dtype
            .complex_component_dtype()
            .ok_or_else(|| Error::Internal("Expected complex dtype".to_string()))?;

        let out = Tensor::<CpuRuntime>::empty(shape, out_dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let a_ptr = a_contig.ptr();
        let out_ptr = out.ptr();
        let chunk_size = self.chunk_size_hint();

        match dtype {
            DType::Complex64 => {
                self.install_parallelism(|| unsafe {
                    kernels::imag_complex64(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::Complex128 => {
                self.install_parallelism(|| unsafe {
                    kernels::imag_complex128(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("imag called on non-complex dtype"),
        }

        Ok(out)
    }

    fn angle(&self, a: &Tensor<CpuRuntime>) -> Result<Tensor<CpuRuntime>> {
        let dtype = a.dtype();
        let shape = a.shape();
        let numel = a.numel();
        let chunk_size = self.chunk_size_hint();

        let a_contig = ensure_contiguous(a);

        // For real types: angle(x) = 0 if x >= 0, π if x < 0
        if !dtype.is_complex() {
            let out = Tensor::<CpuRuntime>::empty(shape, dtype, &self.device);

            // Handle empty tensors
            if numel == 0 {
                return Ok(out);
            }

            let a_ptr = a_contig.ptr();
            let out_ptr = out.ptr();

            match dtype {
                DType::F32 => {
                    self.install_parallelism(|| unsafe {
                        kernels::angle_real_f32(
                            a_ptr as *const _,
                            out_ptr as *mut _,
                            numel,
                            chunk_size,
                        );
                    });
                }
                DType::F64 => {
                    self.install_parallelism(|| unsafe {
                        kernels::angle_real_f64(
                            a_ptr as *const _,
                            out_ptr as *mut _,
                            numel,
                            chunk_size,
                        );
                    });
                }
                _ => {
                    // For integer types, angle doesn't make mathematical sense
                    // Return zeros
                    return Ok(Tensor::<CpuRuntime>::zeros(shape, dtype, &self.device));
                }
            }
            return Ok(out);
        }

        // Determine output dtype (F32 for Complex64, F64 for Complex128)
        let out_dtype = dtype
            .complex_component_dtype()
            .ok_or_else(|| Error::Internal("Expected complex dtype".to_string()))?;

        let out = Tensor::<CpuRuntime>::empty(shape, out_dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let a_ptr = a_contig.ptr();
        let out_ptr = out.ptr();

        match dtype {
            DType::Complex64 => {
                self.install_parallelism(|| unsafe {
                    kernels::angle_complex64(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::Complex128 => {
                self.install_parallelism(|| unsafe {
                    kernels::angle_complex128(
                        a_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("angle called on non-complex dtype"),
        }

        Ok(out)
    }

    fn make_complex(
        &self,
        real: &Tensor<CpuRuntime>,
        imag: &Tensor<CpuRuntime>,
    ) -> Result<Tensor<CpuRuntime>> {
        validate_make_complex_inputs(real, imag)?;

        let input_dtype = real.dtype();
        let shape = real.shape();
        let numel = real.numel();

        // Determine output dtype
        let out_dtype = match input_dtype {
            DType::F32 => DType::Complex64,
            DType::F64 => DType::Complex128,
            _ => unreachable!("validated above"),
        };

        let real_contig = ensure_contiguous(real);
        let imag_contig = ensure_contiguous(imag);
        let out = Tensor::<CpuRuntime>::empty(shape, out_dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let real_ptr = real_contig.ptr();
        let imag_ptr = imag_contig.ptr();
        let out_ptr = out.ptr();
        let chunk_size = self.chunk_size_hint();

        match input_dtype {
            DType::F32 => {
                self.install_parallelism(|| unsafe {
                    kernels::from_real_imag_f32(
                        real_ptr as *const _,
                        imag_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::F64 => {
                self.install_parallelism(|| unsafe {
                    kernels::from_real_imag_f64(
                        real_ptr as *const _,
                        imag_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("validated above"),
        }

        Ok(out)
    }

    fn complex_mul_real(
        &self,
        complex: &Tensor<CpuRuntime>,
        real: &Tensor<CpuRuntime>,
    ) -> Result<Tensor<CpuRuntime>> {
        validate_complex_real_inputs(complex, real, "complex_mul_real")?;

        let dtype = complex.dtype();
        let shape = complex.shape();
        let numel = complex.numel();

        let complex_contig = ensure_contiguous(complex);
        let real_contig = ensure_contiguous(real);
        let out = Tensor::<CpuRuntime>::empty(shape, dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let complex_ptr = complex_contig.ptr();
        let real_ptr = real_contig.ptr();
        let out_ptr = out.ptr();
        let chunk_size = self.chunk_size_hint();

        match dtype {
            DType::Complex64 => {
                self.install_parallelism(|| unsafe {
                    kernels::complex64_mul_real(
                        complex_ptr as *const _,
                        real_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::Complex128 => {
                self.install_parallelism(|| unsafe {
                    kernels::complex128_mul_real(
                        complex_ptr as *const _,
                        real_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("validated above"),
        }

        Ok(out)
    }

    fn complex_div_real(
        &self,
        complex: &Tensor<CpuRuntime>,
        real: &Tensor<CpuRuntime>,
    ) -> Result<Tensor<CpuRuntime>> {
        validate_complex_real_inputs(complex, real, "complex_div_real")?;

        let dtype = complex.dtype();
        let shape = complex.shape();
        let numel = complex.numel();

        let complex_contig = ensure_contiguous(complex);
        let real_contig = ensure_contiguous(real);
        let out = Tensor::<CpuRuntime>::empty(shape, dtype, &self.device);

        // Handle empty tensors
        if numel == 0 {
            return Ok(out);
        }

        let complex_ptr = complex_contig.ptr();
        let real_ptr = real_contig.ptr();
        let out_ptr = out.ptr();
        let chunk_size = self.chunk_size_hint();

        match dtype {
            DType::Complex64 => {
                self.install_parallelism(|| unsafe {
                    kernels::complex64_div_real(
                        complex_ptr as *const _,
                        real_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            DType::Complex128 => {
                self.install_parallelism(|| unsafe {
                    kernels::complex128_div_real(
                        complex_ptr as *const _,
                        real_ptr as *const _,
                        out_ptr as *mut _,
                        numel,
                        chunk_size,
                    );
                });
            }
            _ => unreachable!("validated above"),
        }

        Ok(out)
    }
}