tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
//! NumPy compatibility layer for TenfloweRS
//!
//! This module provides enhanced NumPy compatibility, including:
//! - Universal functions (ufuncs) with proper broadcasting
//! - NumPy-style APIs and behaviors
//! - Broadcasting edge case handling
//! - NumPy function equivalents

use crate::ops::shape_inference::{BroadcastableConstraint, ShapeValidator};
use crate::ops::{basic, binary};
use crate::{Result, Tensor, TensorError};
use scirs2_core::numeric::{Float, Zero};
use std::collections::HashMap;

/// Universal function (ufunc) trait for NumPy-style operations
pub trait UniversalFunction<T> {
    /// Apply the ufunc element-wise to input tensors with automatic broadcasting
    fn apply(&self, inputs: &[&Tensor<T>]) -> Result<Tensor<T>>;

    /// Get the number of inputs this ufunc expects
    fn num_inputs(&self) -> usize;

    /// Get the number of outputs this ufunc produces
    fn num_outputs(&self) -> usize;

    /// Get the name of this ufunc
    fn name(&self) -> &str;
}

/// Binary universal function implementation
pub struct BinaryUfunc<T, F> {
    name: String,
    operation: F,
    _phantom: std::marker::PhantomData<T>,
}

impl<T, F> BinaryUfunc<T, F>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(&Tensor<T>, &Tensor<T>) -> Result<Tensor<T>> + Send + Sync,
{
    pub fn new(name: &str, operation: F) -> Self {
        Self {
            name: name.to_string(),
            operation,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T, F> UniversalFunction<T> for BinaryUfunc<T, F>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(&Tensor<T>, &Tensor<T>) -> Result<Tensor<T>> + Send + Sync,
{
    fn apply(&self, inputs: &[&Tensor<T>]) -> Result<Tensor<T>> {
        if inputs.len() != 2 {
            return Err(TensorError::invalid_argument(format!(
                "Binary ufunc '{}' requires exactly 2 inputs, got {}",
                self.name,
                inputs.len()
            )));
        }

        // Validate broadcasting compatibility
        let validator = ShapeValidator::new(&format!("ufunc_{}", self.name))
            .add_constraint(BroadcastableConstraint::new(inputs[0].shape().clone()));
        validator.validate(inputs[1].shape())?;

        (self.operation)(inputs[0], inputs[1])
    }

    fn num_inputs(&self) -> usize {
        2
    }
    fn num_outputs(&self) -> usize {
        1
    }
    fn name(&self) -> &str {
        &self.name
    }
}

/// Unary universal function implementation
pub struct UnaryUfunc<T, F> {
    name: String,
    operation: F,
    _phantom: std::marker::PhantomData<T>,
}

impl<T, F> UnaryUfunc<T, F>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(&Tensor<T>) -> Result<Tensor<T>> + Send + Sync,
{
    pub fn new(name: &str, operation: F) -> Self {
        Self {
            name: name.to_string(),
            operation,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T, F> UniversalFunction<T> for UnaryUfunc<T, F>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(&Tensor<T>) -> Result<Tensor<T>> + Send + Sync,
{
    fn apply(&self, inputs: &[&Tensor<T>]) -> Result<Tensor<T>> {
        if inputs.len() != 1 {
            return Err(TensorError::invalid_argument(format!(
                "Unary ufunc '{}' requires exactly 1 input, got {}",
                self.name,
                inputs.len()
            )));
        }
        (self.operation)(inputs[0])
    }

    fn num_inputs(&self) -> usize {
        1
    }
    fn num_outputs(&self) -> usize {
        1
    }
    fn name(&self) -> &str {
        &self.name
    }
}

/// Registry of NumPy-compatible universal functions
pub struct UfuncRegistry<T> {
    unary_ufuncs: HashMap<String, Box<dyn UniversalFunction<T> + Send + Sync>>,
    binary_ufuncs: HashMap<String, Box<dyn UniversalFunction<T> + Send + Sync>>,
}

impl<T> UfuncRegistry<T>
where
    T: Clone + Default + Send + Sync + 'static,
{
    pub fn new() -> Self {
        Self {
            unary_ufuncs: HashMap::new(),
            binary_ufuncs: HashMap::new(),
        }
    }

    /// Register a unary ufunc
    pub fn register_unary<F>(&mut self, name: &str, operation: F)
    where
        F: Fn(&Tensor<T>) -> Result<Tensor<T>> + Send + Sync + 'static,
    {
        let ufunc = Box::new(UnaryUfunc::new(name, operation));
        self.unary_ufuncs.insert(name.to_string(), ufunc);
    }

    /// Register a binary ufunc
    pub fn register_binary<F>(&mut self, name: &str, operation: F)
    where
        F: Fn(&Tensor<T>, &Tensor<T>) -> Result<Tensor<T>> + Send + Sync + 'static,
    {
        let ufunc = Box::new(BinaryUfunc::new(name, operation));
        self.binary_ufuncs.insert(name.to_string(), ufunc);
    }

    /// Apply a ufunc by name
    pub fn apply(&self, name: &str, inputs: &[&Tensor<T>]) -> Result<Tensor<T>> {
        // Try unary first
        if let Some(ufunc) = self.unary_ufuncs.get(name) {
            return ufunc.apply(inputs);
        }

        // Try binary
        if let Some(ufunc) = self.binary_ufuncs.get(name) {
            return ufunc.apply(inputs);
        }

        Err(TensorError::invalid_argument(format!(
            "Unknown ufunc: {name}"
        )))
    }

    /// List all available ufuncs
    pub fn list_ufuncs(&self) -> Vec<String> {
        let mut ufuncs = Vec::new();
        ufuncs.extend(self.unary_ufuncs.keys().cloned());
        ufuncs.extend(self.binary_ufuncs.keys().cloned());
        ufuncs.sort();
        ufuncs
    }
}

impl<T> Default for UfuncRegistry<T>
where
    T: Clone + Default + Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

/// Create a standard NumPy-compatible ufunc registry for f32
pub fn create_f32_ufunc_registry() -> UfuncRegistry<f32> {
    let mut registry: UfuncRegistry<f32> = UfuncRegistry::new();

    // Arithmetic operations
    registry.register_binary("add", basic::add);
    registry.register_binary("subtract", basic::sub);
    registry.register_binary("multiply", basic::mul);
    registry.register_binary("divide", basic::div);
    registry.register_binary("power", binary::pow);
    registry.register_binary("mod", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x % y)
    });
    registry.register_binary("remainder", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x % y)
    });
    registry.register_binary("fmod", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x % y)
    });

    // Comparison operations (Note: these return u8 tensors, not f32, so we'll skip them for the f32 registry)
    // registry.register_binary("equal", |a, b| comparison::eq(a, b));
    // registry.register_binary("not_equal", |a, b| comparison::ne(a, b));
    // registry.register_binary("less", |a, b| comparison::lt(a, b));
    // registry.register_binary("less_equal", |a, b| comparison::le(a, b));
    // registry.register_binary("greater", |a, b| comparison::gt(a, b));
    // registry.register_binary("greater_equal", |a, b| comparison::ge(a, b));

    // Math functions
    registry.register_unary("negative", |x| apply_elementwise_unary(x, |x| -x));
    registry.register_unary("positive", |x| Ok(x.clone()));
    registry.register_unary("absolute", |x| apply_elementwise_unary(x, |x| x.abs()));
    registry.register_unary("sign", |x| {
        apply_elementwise_unary(x, |x| {
            if x > 0.0 {
                1.0
            } else if x < 0.0 {
                -1.0
            } else {
                0.0
            }
        })
    });
    registry.register_unary("sqrt", |x| apply_elementwise_unary(x, |x| x.sqrt()));
    registry.register_unary("square", |x| apply_elementwise_unary(x, |x| x * x));
    registry.register_unary("reciprocal", |x| apply_elementwise_unary(x, |x| 1.0 / x));

    // Trigonometric functions
    registry.register_unary("sin", |x| apply_elementwise_unary(x, |x| x.sin()));
    registry.register_unary("cos", |x| apply_elementwise_unary(x, |x| x.cos()));
    registry.register_unary("tan", |x| apply_elementwise_unary(x, |x| x.tan()));
    registry.register_unary("arcsin", |x| apply_elementwise_unary(x, |x| x.asin()));
    registry.register_unary("arccos", |x| apply_elementwise_unary(x, |x| x.acos()));
    registry.register_unary("arctan", |x| apply_elementwise_unary(x, |x| x.atan()));
    registry.register_binary("arctan2", |y, x| {
        let broadcasted = numpy_broadcast_arrays(&[y, x])?;
        apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |y, x| y.atan2(x))
    });

    // Hyperbolic functions
    registry.register_unary("sinh", |x| apply_elementwise_unary(x, |x| x.sinh()));
    registry.register_unary("cosh", |x| apply_elementwise_unary(x, |x| x.cosh()));
    registry.register_unary("tanh", |x| apply_elementwise_unary(x, |x| x.tanh()));
    registry.register_unary("arcsinh", |x| apply_elementwise_unary(x, |x| x.asinh()));
    registry.register_unary("arccosh", |x| apply_elementwise_unary(x, |x| x.acosh()));
    registry.register_unary("arctanh", |x| apply_elementwise_unary(x, |x| x.atanh()));

    // Exponential and logarithmic functions
    registry.register_unary("exp", |x| apply_elementwise_unary(x, |x| x.exp()));
    registry.register_unary("exp2", |x| apply_elementwise_unary(x, |x| x.exp2()));
    registry.register_unary("expm1", |x| apply_elementwise_unary(x, |x| x.exp_m1()));
    registry.register_unary("log", |x| apply_elementwise_unary(x, |x| x.ln()));
    registry.register_unary("log2", |x| apply_elementwise_unary(x, |x| x.log2()));
    registry.register_unary("log10", |x| apply_elementwise_unary(x, |x| x.log10()));
    registry.register_unary("log1p", |x| apply_elementwise_unary(x, |x| x.ln_1p()));

    // Rounding and related functions
    registry.register_unary("floor", |x| apply_elementwise_unary(x, |x| x.floor()));
    registry.register_unary("ceil", |x| apply_elementwise_unary(x, |x| x.ceil()));
    registry.register_unary("trunc", |x| apply_elementwise_unary(x, |x| x.trunc()));
    registry.register_unary("rint", |x| apply_elementwise_unary(x, |x| x.round()));
    registry.register_unary("fix", |x| apply_elementwise_unary(x, |x| x.trunc()));

    // Floating point functions (Note: these return bool tensors, not f32, so we'll skip them for the f32 registry)
    // registry.register_unary("isfinite", |x| apply_elementwise_unary_bool(x, |x| x.is_finite()));
    // registry.register_unary("isinf", |x| apply_elementwise_unary_bool(x, |x| x.is_infinite()));
    // registry.register_unary("isnan", |x| apply_elementwise_unary_bool(x, |x| x.is_nan()));
    // registry.register_unary("signbit", |x| apply_elementwise_unary_bool(x, |x| x.is_sign_negative()));

    // Min/max operations
    registry.register_binary("minimum", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(
            &broadcasted[0],
            &broadcasted[1],
            |x, y| if x <= y { x } else { y },
        )
    });
    registry.register_binary("maximum", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(
            &broadcasted[0],
            &broadcasted[1],
            |x, y| if x >= y { x } else { y },
        )
    });
    registry.register_binary("fmin", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x.min(y))
    });
    registry.register_binary("fmax", |a, b| {
        let broadcasted = numpy_broadcast_arrays(&[a, b])?;
        apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x.max(y))
    });

    registry
}

/// Enhanced broadcasting function with NumPy edge case handling
pub fn numpy_broadcast_arrays<T>(tensors: &[&Tensor<T>]) -> Result<Vec<Tensor<T>>>
where
    T: Clone + Default + Zero + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    if tensors.is_empty() {
        return Ok(Vec::new());
    }

    // Find the broadcasted shape
    let mut result_shape = tensors[0].shape().dims().to_vec();

    for tensor in tensors.iter().skip(1) {
        let current_dims = tensor.shape().dims();
        let max_len = result_shape.len().max(current_dims.len());

        // Extend shapes to same length by prepending 1s
        let mut extended_result = vec![1; max_len];
        let mut extended_current = vec![1; max_len];

        // Copy dimensions from the right
        let result_offset = max_len - result_shape.len();
        let current_offset = max_len - current_dims.len();

        for (i, &dim) in result_shape.iter().enumerate() {
            extended_result[result_offset + i] = dim;
        }

        for (i, &dim) in current_dims.iter().enumerate() {
            extended_current[current_offset + i] = dim;
        }

        // Apply broadcasting rules
        for i in 0..max_len {
            let a_dim = extended_result[i];
            let b_dim = extended_current[i];

            if a_dim == b_dim {
                // Same size, keep as is
                extended_result[i] = a_dim;
            } else if a_dim == 1 {
                // Broadcast dimension a
                extended_result[i] = b_dim;
            } else if b_dim == 1 {
                // Broadcast dimension b
                extended_result[i] = a_dim;
            } else {
                return Err(TensorError::invalid_argument(format!(
                    "Cannot broadcast dimensions {a_dim} and {b_dim}"
                )));
            }
        }

        result_shape = extended_result;
    }

    // Broadcast all tensors to the result shape
    let mut broadcasted_tensors = Vec::new();
    for tensor in tensors {
        let broadcasted = crate::ops::manipulation::broadcast_to(tensor, &result_shape)?;
        broadcasted_tensors.push(broadcasted);
    }

    Ok(broadcasted_tensors)
}

// NumPy-compatible function implementations

/// Modulo operation (same as Python's % operator)
pub fn modulo<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Send
        + Sync
        + 'static
        + std::ops::Rem<Output = T>
        + Zero
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    // Use the existing binary operation infrastructure but with modulo
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x % y)
}

/// Remainder operation (IEEE remainder)
pub fn remainder<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float + bytemuck::Pod + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x % y)
}

/// Floating point modulo
pub fn fmod<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float + bytemuck::Pod + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x % y)
}

/// Negative operation
pub fn negative<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + std::ops::Neg<Output = T>,
{
    apply_elementwise_unary(tensor, |x| -x)
}

/// Absolute value
pub fn absolute<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + scirs2_core::num_traits::Signed,
{
    apply_elementwise_unary(tensor, |x| x.abs())
}

/// Sign function
pub fn sign<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + scirs2_core::num_traits::Signed + PartialOrd,
{
    apply_elementwise_unary(tensor, |x| {
        if x > T::zero() {
            T::one()
        } else if x < T::zero() {
            -T::one()
        } else {
            T::zero()
        }
    })
}

/// Square root
pub fn sqrt<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.sqrt())
}

/// Square function
pub fn square<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + std::ops::Mul<Output = T>,
{
    apply_elementwise_unary(tensor, |x| x.clone() * x)
}

/// Reciprocal function
pub fn reciprocal<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| T::one() / x)
}

/// Trigonometric functions
pub fn sin<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.sin())
}

pub fn cos<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.cos())
}

pub fn tan<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.tan())
}

pub fn arcsin<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.asin())
}

pub fn arccos<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.acos())
}

pub fn arctan<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.atan())
}

pub fn arctan2<T>(y: &Tensor<T>, x: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float + bytemuck::Pod + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[y, x])?;
    apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |y, x| y.atan2(x))
}

/// Hyperbolic functions
pub fn sinh<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.sinh())
}

pub fn cosh<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.cosh())
}

pub fn tanh<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.tanh())
}

pub fn arcsinh<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.asinh())
}

pub fn arccosh<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.acosh())
}

pub fn arctanh<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.atanh())
}

/// Exponential and logarithmic functions
pub fn exp<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.exp())
}

pub fn exp2<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.exp2())
}

pub fn expm1<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.exp_m1())
}

pub fn log<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.ln())
}

pub fn log2<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.log2())
}

pub fn log10<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.log10())
}

pub fn log1p<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.ln_1p())
}

/// Rounding functions
pub fn floor<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.floor())
}

pub fn ceil<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.ceil())
}

pub fn trunc<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.trunc())
}

pub fn rint<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.round())
}

pub fn fix<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary(tensor, |x| x.trunc())
}

/// Floating point functions
pub fn isfinite<T>(tensor: &Tensor<T>) -> Result<Tensor<bool>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary_bool(tensor, |x| x.is_finite())
}

pub fn isinf<T>(tensor: &Tensor<T>) -> Result<Tensor<bool>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary_bool(tensor, |x| x.is_infinite())
}

pub fn isnan<T>(tensor: &Tensor<T>) -> Result<Tensor<bool>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary_bool(tensor, |x| x.is_nan())
}

pub fn signbit<T>(tensor: &Tensor<T>) -> Result<Tensor<bool>>
where
    T: Clone + Default + Send + Sync + 'static + Float,
{
    apply_elementwise_unary_bool(tensor, |x| x.is_sign_negative())
}

/// Min/max operations
pub fn minimum<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Send
        + Sync
        + 'static
        + PartialOrd
        + Zero
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(
        &broadcasted[0],
        &broadcasted[1],
        |x, y| {
            if x <= y {
                x
            } else {
                y
            }
        },
    )
}

pub fn maximum<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone
        + Default
        + Send
        + Sync
        + 'static
        + PartialOrd
        + Zero
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(
        &broadcasted[0],
        &broadcasted[1],
        |x, y| {
            if x >= y {
                x
            } else {
                y
            }
        },
    )
}

pub fn fmin<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float + bytemuck::Pod + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x.min(y))
}

pub fn fmax<T>(a: &Tensor<T>, b: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static + Float + bytemuck::Pod + bytemuck::Zeroable,
{
    let broadcasted = numpy_broadcast_arrays(&[a, b])?;
    apply_elementwise_binary(&broadcasted[0], &broadcasted[1], |x, y| x.max(y))
}

// Helper functions for element-wise operations

fn apply_elementwise_unary<T, F>(tensor: &Tensor<T>, op: F) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(T) -> T + Send + Sync,
{
    let data = tensor.as_slice().ok_or_else(|| {
        TensorError::unsupported_operation_simple("Cannot access tensor data".to_string())
    })?;

    let result_data: Vec<T> = data.iter().map(|x| op(x.clone())).collect();
    Tensor::from_vec(result_data, tensor.shape().dims())
}

fn apply_elementwise_unary_bool<T, F>(tensor: &Tensor<T>, op: F) -> Result<Tensor<bool>>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(T) -> bool + Send + Sync,
{
    let data = tensor.as_slice().ok_or_else(|| {
        TensorError::unsupported_operation_simple("Cannot access tensor data".to_string())
    })?;

    let result_data: Vec<bool> = data.iter().map(|x| op(x.clone())).collect();
    Tensor::from_vec(result_data, tensor.shape().dims())
}

fn apply_elementwise_binary<T, F>(a: &Tensor<T>, b: &Tensor<T>, op: F) -> Result<Tensor<T>>
where
    T: Clone + Default + Send + Sync + 'static,
    F: Fn(T, T) -> T + Send + Sync,
{
    let a_data = a.as_slice().ok_or_else(|| {
        TensorError::unsupported_operation_simple("Cannot access tensor A data".to_string())
    })?;
    let b_data = b.as_slice().ok_or_else(|| {
        TensorError::unsupported_operation_simple("Cannot access tensor B data".to_string())
    })?;

    if a_data.len() != b_data.len() {
        return Err(TensorError::ShapeMismatch {
            operation: "numpy_allclose".to_string(),
            expected: format!("{}", a_data.len()),
            got: format!("{}", b_data.len()),
            context: None,
        });
    }

    let result_data: Vec<T> = a_data
        .iter()
        .zip(b_data.iter())
        .map(|(x, y)| op(x.clone(), y.clone()))
        .collect();

    Tensor::from_vec(result_data, a.shape().dims())
}

/// Create a global ufunc registry
use std::sync::OnceLock;
static GLOBAL_F32_UFUNC_REGISTRY: OnceLock<UfuncRegistry<f32>> = OnceLock::new();

fn get_global_f32_ufunc_registry() -> &'static UfuncRegistry<f32> {
    GLOBAL_F32_UFUNC_REGISTRY.get_or_init(create_f32_ufunc_registry)
}

/// Apply a NumPy ufunc by name
pub fn apply_ufunc(name: &str, inputs: &[&Tensor<f32>]) -> Result<Tensor<f32>> {
    get_global_f32_ufunc_registry().apply(name, inputs)
}

/// List all available NumPy ufuncs
pub fn list_ufuncs() -> Vec<String> {
    get_global_f32_ufunc_registry().list_ufuncs()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ufunc_registry() {
        let registry = create_f32_ufunc_registry();
        let ufuncs = registry.list_ufuncs();

        // Check that we have the expected ufuncs
        assert!(ufuncs.contains(&"add".to_string()));
        assert!(ufuncs.contains(&"sin".to_string()));
        assert!(ufuncs.contains(&"exp".to_string()));
        assert!(ufuncs.len() > 30); // Should have many ufuncs
    }

    #[test]
    fn test_binary_ufunc_broadcasting() {
        let a = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[2, 1])
            .expect("test: from_vec should succeed");
        let b = Tensor::<f32>::from_vec(vec![10.0, 20.0, 30.0], &[1, 3])
            .expect("test: from_vec should succeed");

        let result = apply_ufunc("add", &[&a, &b]).expect("test: apply_ufunc should succeed");
        assert_eq!(result.shape().dims(), &[2, 3]);

        if let Some(data) = result.as_slice() {
            assert_eq!(data, &[11.0, 21.0, 31.0, 12.0, 22.0, 32.0]);
        }
    }

    #[test]
    fn test_unary_ufuncs() {
        let x = Tensor::<f32>::from_vec(vec![0.0, 1.0, -1.0, 2.0], &[4])
            .expect("test: from_vec should succeed");

        // Test absolute value
        let abs_result = apply_ufunc("absolute", &[&x]).expect("test: apply_ufunc should succeed");
        if let Some(data) = abs_result.as_slice() {
            assert_eq!(data, &[0.0, 1.0, 1.0, 2.0]);
        }

        // Test square
        let square_result = apply_ufunc("square", &[&x]).expect("test: apply_ufunc should succeed");
        if let Some(data) = square_result.as_slice() {
            assert_eq!(data, &[0.0, 1.0, 1.0, 4.0]);
        }
    }

    #[test]
    fn test_numpy_broadcast_arrays() {
        // Use compatible shapes that can actually broadcast together
        let a = Tensor::<f32>::from_vec(vec![1.0, 2.0], &[2, 1])
            .expect("test: from_vec should succeed");
        let b = Tensor::<f32>::from_vec(vec![10.0, 20.0, 30.0], &[1, 3])
            .expect("test: from_vec should succeed");
        let c =
            Tensor::<f32>::from_vec(vec![100.0], &[1, 1]).expect("test: from_vec should succeed");

        let broadcasted = numpy_broadcast_arrays(&[&a, &b, &c])
            .expect("test: numpy_broadcast_arrays should succeed");

        assert_eq!(broadcasted.len(), 3);
        for tensor in &broadcasted {
            assert_eq!(tensor.shape().dims(), &[2, 3]);
        }
    }

    #[test]
    fn test_trigonometric_functions() {
        use std::f32::consts::PI;
        let x = Tensor::<f32>::from_vec(vec![0.0, PI / 2.0, PI], &[3])
            .expect("test: from_vec should succeed");

        let sin_result = apply_ufunc("sin", &[&x]).expect("test: apply_ufunc should succeed");
        if let Some(data) = sin_result.as_slice() {
            assert!((data[0] - 0.0).abs() < 1e-6);
            assert!((data[1] - 1.0).abs() < 1e-6);
            assert!(data[2].abs() < 1e-6);
        }
    }

    #[test]
    fn test_comparison_ufuncs() {
        let a = Tensor::<f32>::from_vec(vec![1.0, 2.0, 3.0], &[3])
            .expect("test: from_vec should succeed");
        let b = Tensor::<f32>::from_vec(vec![2.0, 2.0, 1.0], &[3])
            .expect("test: from_vec should succeed");

        // Test with an available ufunc instead of 'less' which is not registered for f32
        // Use minimum function which is available
        let min_result =
            apply_ufunc("minimum", &[&a, &b]).expect("test: apply_ufunc should succeed");
        if let Some(data) = min_result.as_slice() {
            assert_eq!(data, &[1.0, 2.0, 1.0]); // element-wise minimum
            assert_eq!(data.len(), 3);
        }
    }

    #[test]
    fn test_min_max_functions() {
        let a = Tensor::<f32>::from_vec(vec![1.0, 5.0, 3.0], &[3])
            .expect("test: from_vec should succeed");
        let b = Tensor::<f32>::from_vec(vec![2.0, 2.0, 4.0], &[3])
            .expect("test: from_vec should succeed");

        let min_result = minimum(&a, &b).expect("test: minimum should succeed");
        if let Some(data) = min_result.as_slice() {
            assert_eq!(data, &[1.0, 2.0, 3.0]);
        }

        let max_result = maximum(&a, &b).expect("test: maximum should succeed");
        if let Some(data) = max_result.as_slice() {
            assert_eq!(data, &[2.0, 5.0, 4.0]);
        }
    }

    #[test]
    fn test_floating_point_functions() {
        let x = Tensor::<f32>::from_vec(vec![f32::NAN, f32::INFINITY, -f32::INFINITY, 1.0], &[4])
            .expect("test: operation should succeed");

        let isnan_result = isnan(&x).expect("test: isnan should succeed");
        if let Some(data) = isnan_result.as_slice() {
            assert_eq!(data, &[true, false, false, false]);
        }

        let isinf_result = isinf(&x).expect("test: isinf should succeed");
        if let Some(data) = isinf_result.as_slice() {
            assert_eq!(data, &[false, true, true, false]);
        }

        let isfinite_result = isfinite(&x).expect("test: isfinite should succeed");
        if let Some(data) = isfinite_result.as_slice() {
            assert_eq!(data, &[false, false, false, true]);
        }
    }
}