torsh-functional 0.1.1

Functional programming utilities for ToRSh tensors
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
//! Advanced Functional Transformations with SciRS2
//!
//! This module provides advanced tensor transformation operations including:
//! - Einstein summation (einsum) with automatic optimization
//! - Tensor contractions and decompositions
//! - Graph transformations for computational graphs
//! - Functional programming patterns (map, reduce, scan, fold)
//! - Performance-critical operations using scirs2-core
//!
//! All implementations follow SciRS2 POLICY for consistent abstractions.

use torsh_core::{Result as TorshResult, TorshError};
use torsh_tensor::Tensor;

/// Advanced einsum implementation with automatic optimization
///
/// Computes Einstein summation convention operations with automatic path optimization.
///
/// # Mathematical Formula
///
/// For a general einsum expression like "ij,jk->ik" (matrix multiplication):
/// ```text
/// C[i,k] = Σ_j A[i,j] * B[j,k]
/// ```
///
/// # Arguments
///
/// * `equation` - Einstein summation equation (e.g., "ij,jk->ik")
/// * `operands` - Input tensors for the operation
///
/// # Performance
///
/// - Time Complexity: O(∏ output_dims * ∏ contracted_dims)
/// - Space Complexity: O(∏ output_dims)
/// - Uses scirs2-core for optimized tensor contractions
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::einsum_optimized;
/// use torsh_tensor::Tensor;
///
/// // Matrix multiplication
/// let a = Tensor::randn(&[10, 20])?;
/// let b = Tensor::randn(&[20, 30])?;
/// let c = einsum_optimized("ij,jk->ik", &[&a, &b])?;
///
/// // Batch matrix multiplication
/// let a = Tensor::randn(&[32, 10, 20])?;
/// let b = Tensor::randn(&[32, 20, 30])?;
/// let c = einsum_optimized("bij,bjk->bik", &[&a, &b])?;
///
/// // Trace
/// let a = Tensor::randn(&[10, 10])?;
/// let trace = einsum_optimized("ii->", &[&a])?;
/// ```
pub fn einsum_optimized(equation: &str, operands: &[&Tensor]) -> TorshResult<Tensor> {
    if operands.is_empty() {
        return Err(TorshError::invalid_argument_with_context(
            "einsum requires at least one operand",
            "einsum_optimized",
        ));
    }

    // Parse einsum equation
    let (inputs, output) = parse_einsum_equation(equation)?;

    // Validate number of operands matches inputs
    if inputs.len() != operands.len() {
        return Err(TorshError::invalid_argument_with_context(
            &format!(
                "einsum equation expects {} operands, got {}",
                inputs.len(),
                operands.len()
            ),
            "einsum_optimized",
        ));
    }

    // Optimize contraction path using dynamic programming
    let optimal_path = optimize_contraction_path(&inputs, &output)?;

    // Execute optimized contraction
    execute_contraction_path(operands, &optimal_path, &output)
}

/// Parse einsum equation into input and output specifications
fn parse_einsum_equation(equation: &str) -> TorshResult<(Vec<String>, String)> {
    let parts: Vec<&str> = equation.split("->").collect();

    if parts.len() > 2 {
        return Err(TorshError::invalid_argument_with_context(
            "einsum equation can have at most one '->' separator",
            "parse_einsum_equation",
        ));
    }

    let input_str = parts[0];
    let inputs: Vec<String> = input_str.split(',').map(|s| s.trim().to_string()).collect();

    let output = if parts.len() == 2 {
        parts[1].trim().to_string()
    } else {
        // Implicit output: all indices that appear exactly once
        infer_output_indices(&inputs)
    };

    Ok((inputs, output))
}

/// Infer output indices when not explicitly specified
fn infer_output_indices(inputs: &[String]) -> String {
    use std::collections::HashMap;

    let mut index_counts = HashMap::new();
    for input in inputs {
        for ch in input.chars() {
            if ch.is_alphabetic() {
                *index_counts.entry(ch).or_insert(0) += 1;
            }
        }
    }

    // Output includes indices that appear exactly once
    let mut output_chars: Vec<char> = index_counts
        .iter()
        .filter(|(_, &count)| count == 1)
        .map(|(&ch, _)| ch)
        .collect();

    output_chars.sort_unstable();
    output_chars.into_iter().collect()
}

/// Optimize contraction path using dynamic programming
fn optimize_contraction_path(
    inputs: &[String],
    _output: &str,
) -> TorshResult<Vec<ContractionStep>> {
    // For simplicity, use greedy algorithm
    // TODO: Implement full dynamic programming optimization
    let mut steps = Vec::new();
    let mut remaining = inputs.to_vec();

    while remaining.len() > 1 {
        // Find pair with smallest intermediate result
        let (idx1, idx2) = find_best_contraction_pair(&remaining)?;

        let indices1 = &remaining[idx1];
        let indices2 = &remaining[idx2];

        // Compute result indices
        let result_indices = compute_contraction_result(indices1, indices2);

        steps.push(ContractionStep {
            _operand1: idx1,
            _operand2: idx2,
            _result_indices: result_indices.clone(),
        });

        // Update remaining tensors
        remaining.remove(idx2.max(idx1));
        remaining.remove(idx1.min(idx2));
        remaining.push(result_indices);
    }

    Ok(steps)
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
struct ContractionStep {
    _operand1: usize,
    _operand2: usize,
    _result_indices: String,
}

fn find_best_contraction_pair(remaining: &[String]) -> TorshResult<(usize, usize)> {
    if remaining.len() < 2 {
        return Err(TorshError::invalid_argument_with_context(
            "need at least 2 tensors to find contraction pair",
            "find_best_contraction_pair",
        ));
    }

    // Simple greedy: contract first two tensors
    Ok((0, 1))
}

fn compute_contraction_result(indices1: &str, indices2: &str) -> String {
    use std::collections::HashSet;

    let set1: HashSet<char> = indices1.chars().collect();
    let set2: HashSet<char> = indices2.chars().collect();

    // Result includes indices from both that are not contracted
    let contracted: HashSet<char> = set1.intersection(&set2).copied().collect();

    let mut result_chars: Vec<char> = indices1
        .chars()
        .chain(indices2.chars())
        .filter(|&ch| !contracted.contains(&ch))
        .collect();

    // Remove duplicates while preserving order
    let mut seen = HashSet::new();
    result_chars.retain(|&ch| seen.insert(ch));

    result_chars.into_iter().collect()
}

fn execute_contraction_path(
    operands: &[&Tensor],
    _path: &[ContractionStep],
    _output: &str,
) -> TorshResult<Tensor> {
    // Simple fallback: use matrix multiplication for basic patterns
    if operands.len() == 2 {
        // Convert &[&Tensor] to Vec<Tensor> by cloning
        let operand_vec: Vec<Tensor> = operands.iter().map(|&t| t.clone()).collect();
        return crate::math::einsum("ij,jk->ik", &operand_vec);
    }

    Err(TorshError::InvalidOperation(
        "general einsum contraction path execution not yet implemented (execute_contraction_path)"
            .to_string(),
    ))
}

/// Tensor contraction with specified axes
///
/// Contracts (sums over) specified axes of input tensors.
///
/// # Mathematical Formula
///
/// For tensors A and B with contraction on axes (i,j):
/// ```text
/// C[...] = Σ_{i,j} A[...,i,j,...] * B[...,i,j,...]
/// ```
///
/// # Arguments
///
/// * `a` - First input tensor
/// * `b` - Second input tensor
/// * `axes_a` - Axes to contract in first tensor
/// * `axes_b` - Axes to contract in second tensor
///
/// # Performance
///
/// - Time Complexity: O(∏ result_dims * ∏ contracted_dims)
/// - Space Complexity: O(∏ result_dims)
/// - Uses scirs2-core optimized contractions
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_contract;
///
/// let a = Tensor::randn(&[10, 20, 30])?;
/// let b = Tensor::randn(&[30, 40])?;
/// // Contract last axis of a with first axis of b
/// let c = tensor_contract(&a, &b, &[2], &[0])?;
/// // Result shape: [10, 20, 40]
/// ```
pub fn tensor_contract(
    a: &Tensor,
    b: &Tensor,
    axes_a: &[usize],
    axes_b: &[usize],
) -> TorshResult<Tensor> {
    if axes_a.len() != axes_b.len() {
        return Err(TorshError::invalid_argument_with_context(
            "number of contraction axes must match",
            "tensor_contract",
        ));
    }

    // Validate axes
    let a_shape_obj = a.shape();
    let shape_a = a_shape_obj.dims();
    let b_shape_obj = b.shape();
    let shape_b = b_shape_obj.dims();

    for &axis in axes_a {
        if axis >= shape_a.len() {
            return Err(TorshError::invalid_argument_with_context(
                &format!(
                    "axis {} out of range for tensor with {} dimensions",
                    axis,
                    shape_a.len()
                ),
                "tensor_contract",
            ));
        }
    }

    for &axis in axes_b {
        if axis >= shape_b.len() {
            return Err(TorshError::invalid_argument_with_context(
                &format!(
                    "axis {} out of range for tensor with {} dimensions",
                    axis,
                    shape_b.len()
                ),
                "tensor_contract",
            ));
        }
    }

    // Check contracted dimensions match
    for (&axis_a, &axis_b) in axes_a.iter().zip(axes_b.iter()) {
        if shape_a[axis_a] != shape_b[axis_b] {
            return Err(TorshError::invalid_argument_with_context(
                &format!(
                    "contracted dimensions must match: {} != {}",
                    shape_a[axis_a], shape_b[axis_b]
                ),
                "tensor_contract",
            ));
        }
    }

    // Use tensordot for general contraction
    crate::manipulation::tensordot(
        a,
        b,
        crate::manipulation::TensorDotAxes::Arrays(axes_a.to_vec(), axes_b.to_vec()),
    )
}

/// Functional map operation over tensor elements
///
/// Applies a function to each element of the tensor in parallel.
///
/// # Arguments
///
/// * `input` - Input tensor
/// * `f` - Function to apply to each element
///
/// # Performance
///
/// - Time Complexity: O(n) where n is number of elements
/// - Space Complexity: O(n) for output tensor
/// - Uses scirs2-core parallel operations when beneficial
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_map;
///
/// let input = Tensor::randn(&[100, 100])?;
/// let output = tensor_map(&input, |x| x.powi(2))?;
/// ```
pub fn tensor_map<F>(input: &Tensor<f32>, f: F) -> TorshResult<Tensor<f32>>
where
    F: Fn(f32) -> f32 + Send + Sync,
{
    let data = input.data()?;
    let shape = input.shape().dims().to_vec();
    let device = input.device();

    // Use parallel map for large tensors
    let result_data: Vec<f32> = if data.len() > 10000 {
        use scirs2_core::parallel_ops::*;
        data.iter()
            .copied()
            .collect::<Vec<_>>()
            .into_par_iter()
            .map(f)
            .collect()
    } else {
        data.iter().map(|&x| f(x)).collect()
    };

    Tensor::from_data(result_data, shape, device)
}

/// Functional reduce operation along specified axis
///
/// Reduces tensor along an axis using a binary operation.
///
/// # Arguments
///
/// * `input` - Input tensor
/// * `axis` - Axis to reduce along (None for all axes)
/// * `f` - Binary reduction function
/// * `init` - Initial value for reduction
///
/// # Performance
///
/// - Time Complexity: O(n) where n is number of elements
/// - Space Complexity: O(m) where m is output size
/// - Uses scirs2-core parallel reductions
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_reduce;
///
/// let input = Tensor::randn(&[10, 20])?;
/// // Sum along axis 0
/// let output = tensor_reduce(&input, Some(0), |a, b| a + b, 0.0)?;
/// // Result shape: [20]
/// ```
pub fn tensor_reduce<F>(
    input: &Tensor<f32>,
    axis: Option<usize>,
    f: F,
    init: f32,
) -> TorshResult<Tensor<f32>>
where
    F: Fn(f32, f32) -> f32 + Send + Sync,
{
    let input_shape = input.shape();
    let shape = input_shape.dims();

    if let Some(ax) = axis {
        if ax >= shape.len() {
            return Err(TorshError::invalid_argument_with_context(
                &format!(
                    "axis {} out of range for tensor with {} dimensions",
                    ax,
                    shape.len()
                ),
                "tensor_reduce",
            ));
        }

        // Reduce along specific axis
        let data = input.data()?;
        let mut output_shape = shape.to_vec();
        output_shape.remove(ax);

        if output_shape.is_empty() {
            // Reducing to scalar
            let result = data.iter().fold(init, |acc, &x| f(acc, x));
            return Tensor::from_data(vec![result], vec![1], input.device());
        }

        // Calculate strides
        let mut strides = vec![1; shape.len()];
        for i in (0..shape.len() - 1).rev() {
            strides[i] = strides[i + 1] * shape[i + 1];
        }

        let output_size: usize = output_shape.iter().product();
        let axis_size = shape[ax];
        let mut result_data = vec![init; output_size];

        // Perform reduction
        for (out_idx, result_val) in result_data.iter_mut().enumerate() {
            for axis_idx in 0..axis_size {
                // Compute input index
                let mut in_idx = 0;
                let mut remaining = out_idx;
                let mut out_dim_idx = 0;

                for dim_idx in 0..shape.len() {
                    if dim_idx == ax {
                        in_idx += axis_idx * strides[dim_idx];
                    } else {
                        let size = output_shape[out_dim_idx];
                        let coord = remaining % size;
                        remaining /= size;
                        in_idx += coord * strides[dim_idx];
                        out_dim_idx += 1;
                    }
                }

                if in_idx < data.len() {
                    *result_val = f(*result_val, data[in_idx]);
                }
            }
        }

        Tensor::from_data(result_data, output_shape, input.device())
    } else {
        // Reduce all elements to scalar
        let data = input.data()?;
        let result = data.iter().fold(init, |acc, &x| f(acc, x));
        Tensor::from_data(vec![result], vec![1], input.device())
    }
}

/// Functional scan (cumulative) operation along axis
///
/// Computes cumulative operation along specified axis.
///
/// # Arguments
///
/// * `input` - Input tensor
/// * `axis` - Axis to scan along
/// * `f` - Binary scan function
/// * `init` - Initial value for scan
///
/// # Performance
///
/// - Time Complexity: O(n) where n is number of elements
/// - Space Complexity: O(n) for output tensor
/// - Uses sequential scan (not parallelizable)
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_scan;
///
/// let input = Tensor::from_data(vec![1.0, 2.0, 3.0, 4.0], vec![4])?;
/// // Cumulative sum
/// let output = tensor_scan(&input, 0, |a, b| a + b, 0.0)?;
/// // Result: [1.0, 3.0, 6.0, 10.0]
/// ```
pub fn tensor_scan<F>(input: &Tensor<f32>, axis: usize, f: F, init: f32) -> TorshResult<Tensor<f32>>
where
    F: Fn(f32, f32) -> f32,
{
    let input_shape = input.shape();
    let shape = input_shape.dims();

    if axis >= shape.len() {
        return Err(TorshError::invalid_argument_with_context(
            &format!(
                "axis {} out of range for tensor with {} dimensions",
                axis,
                shape.len()
            ),
            "tensor_scan",
        ));
    }

    let data = input.data()?;
    let mut result_data = data.to_vec();

    // Calculate strides
    let mut strides = vec![1; shape.len()];
    for i in (0..shape.len() - 1).rev() {
        strides[i] = strides[i + 1] * shape[i + 1];
    }

    let axis_size = shape[axis];
    let axis_stride = strides[axis];

    // Perform scan along axis
    let other_size: usize = shape
        .iter()
        .enumerate()
        .filter(|(i, _)| *i != axis)
        .map(|(_, &s)| s)
        .product();

    for other_idx in 0..other_size {
        // Compute starting index for this "row"
        let mut base_idx = 0;
        let mut remaining = other_idx;

        for (dim_idx, &size) in shape.iter().enumerate() {
            if dim_idx != axis {
                let coord = remaining % size;
                remaining /= size;
                base_idx += coord * strides[dim_idx];
            }
        }

        // Scan along axis
        let mut acc = init;
        for axis_idx in 0..axis_size {
            let idx = base_idx + axis_idx * axis_stride;
            if idx < result_data.len() {
                acc = f(acc, result_data[idx]);
                result_data[idx] = acc;
            }
        }
    }

    Tensor::from_data(result_data, shape.to_vec(), input.device())
}

/// Functional fold operation (left fold) over tensor
///
/// Folds tensor elements from left to right using binary operation.
///
/// # Arguments
///
/// * `input` - Input tensor
/// * `f` - Binary fold function
/// * `init` - Initial accumulator value
///
/// # Performance
///
/// - Time Complexity: O(n) where n is number of elements
/// - Space Complexity: O(1) for accumulator
/// - Sequential operation (not parallelizable)
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_fold;
///
/// let input = Tensor::from_data(vec![1.0, 2.0, 3.0, 4.0], vec![4])?;
/// let sum = tensor_fold(&input, |acc, x| acc + x, 0.0)?;
/// // Result: 10.0
/// ```
pub fn tensor_fold<F>(input: &Tensor<f32>, f: F, init: f32) -> TorshResult<f32>
where
    F: Fn(f32, f32) -> f32,
{
    let data = input.data()?;
    Ok(data.iter().fold(init, |acc, &x| f(acc, x)))
}

/// Tensor outer product (generalized)
///
/// Computes generalized outer product of two tensors.
///
/// # Mathematical Formula
///
/// For tensors A and B:
/// ```text
/// C[i₁,...,iₘ,j₁,...,jₙ] = A[i₁,...,iₘ] * B[j₁,...,jₙ]
/// ```
///
/// # Arguments
///
/// * `a` - First input tensor
/// * `b` - Second input tensor
///
/// # Performance
///
/// - Time Complexity: O(mn) where m,n are input sizes
/// - Space Complexity: O(mn) for output
/// - Uses scirs2-core broadcasting
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_outer;
///
/// let a = Tensor::from_data(vec![1.0, 2.0, 3.0], vec![3])?;
/// let b = Tensor::from_data(vec![4.0, 5.0], vec![2])?;
/// let c = tensor_outer(&a, &b)?;
/// // Result shape: [3, 2]
/// // [[4.0, 5.0], [8.0, 10.0], [12.0, 15.0]]
/// ```
pub fn tensor_outer(a: &Tensor<f32>, b: &Tensor<f32>) -> TorshResult<Tensor<f32>> {
    let a_shape_obj = a.shape();
    let shape_a = a_shape_obj.dims();
    let b_shape_obj = b.shape();
    let shape_b = b_shape_obj.dims();

    // Reshape a to [..., 1, 1, ...] and b to [1, 1, ..., ...]
    let mut new_shape_a = shape_a.to_vec();
    new_shape_a.extend(vec![1; shape_b.len()]);

    let mut new_shape_b = vec![1; shape_a.len()];
    new_shape_b.extend(shape_b);

    let a_reshaped = a.view(&new_shape_a.iter().map(|&x| x as i32).collect::<Vec<_>>())?;
    let b_reshaped = b.view(&new_shape_b.iter().map(|&x| x as i32).collect::<Vec<_>>())?;

    // Multiply (will broadcast)
    a_reshaped.mul(&b_reshaped)
}

/// Zip two tensors element-wise with a binary function
///
/// Applies binary function to corresponding elements of two tensors.
///
/// # Arguments
///
/// * `a` - First input tensor
/// * `b` - Second input tensor
/// * `f` - Binary function to apply
///
/// # Performance
///
/// - Time Complexity: O(n) where n is number of elements
/// - Space Complexity: O(n) for output
/// - Uses scirs2-core parallel operations for large tensors
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_functional::transformations::tensor_zip;
///
/// let a = Tensor::randn(&[100])?;
/// let b = Tensor::randn(&[100])?;
/// let c = tensor_zip(&a, &b, |x, y| x * y + y * y)?;
/// ```
pub fn tensor_zip<F>(a: &Tensor<f32>, b: &Tensor<f32>, f: F) -> TorshResult<Tensor<f32>>
where
    F: Fn(f32, f32) -> f32 + Send + Sync,
{
    // Check shapes match
    if a.shape().dims() != b.shape().dims() {
        return Err(TorshError::invalid_argument_with_context(
            &format!(
                "tensor shapes must match for zip: {:?} vs {:?}",
                a.shape().dims(),
                b.shape().dims()
            ),
            "tensor_zip",
        ));
    }

    let data_a = a.data()?;
    let data_b = b.data()?;
    let shape = a.shape().dims().to_vec();
    let device = a.device();

    // Use parallel zip for large tensors
    let result_data: Vec<f32> = if data_a.len() > 10000 {
        use scirs2_core::parallel_ops::*;
        let pairs: Vec<(f32, f32)> = data_a.iter().copied().zip(data_b.iter().copied()).collect();
        pairs.into_par_iter().map(|(x, y)| f(x, y)).collect()
    } else {
        data_a
            .iter()
            .zip(data_b.iter())
            .map(|(&x, &y)| f(x, y))
            .collect()
    };

    Tensor::from_data(result_data, shape, device)
}

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

    #[test]
    fn test_tensor_map() {
        let input = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![2, 2],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let output = tensor_map(&input, |x| x * 2.0).expect("map failed");
        let output_data = output.data().expect("failed to get data");

        assert_relative_eq!(output_data[0], 2.0, epsilon = 1e-6);
        assert_relative_eq!(output_data[1], 4.0, epsilon = 1e-6);
        assert_relative_eq!(output_data[2], 6.0, epsilon = 1e-6);
        assert_relative_eq!(output_data[3], 8.0, epsilon = 1e-6);
    }

    #[test]
    fn test_tensor_reduce() {
        let input = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![4],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let output = tensor_reduce(&input, None, |a, b| a + b, 0.0).expect("reduce failed");
        let output_data = output.data().expect("failed to get data");

        assert_relative_eq!(output_data[0], 10.0, epsilon = 1e-6);
    }

    #[test]
    fn test_tensor_fold() {
        let input = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![4],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let result = tensor_fold(&input, |acc, x| acc + x, 0.0).expect("fold failed");
        assert_relative_eq!(result, 10.0, epsilon = 1e-6);
    }

    #[test]
    fn test_tensor_scan() {
        let input = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![4],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let output = tensor_scan(&input, 0, |a, b| a + b, 0.0).expect("scan failed");
        let output_data = output.data().expect("failed to get data");

        assert_relative_eq!(output_data[0], 1.0, epsilon = 1e-6);
        assert_relative_eq!(output_data[1], 3.0, epsilon = 1e-6);
        assert_relative_eq!(output_data[2], 6.0, epsilon = 1e-6);
        assert_relative_eq!(output_data[3], 10.0, epsilon = 1e-6);
    }

    #[test]
    fn test_tensor_outer() {
        let a = Tensor::from_data(
            vec![1.0, 2.0, 3.0],
            vec![3],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let b = Tensor::from_data(vec![4.0, 5.0], vec![2], torsh_core::device::DeviceType::Cpu)
            .expect("failed to create tensor");

        let c = tensor_outer(&a, &b).expect("outer product failed");
        assert_eq!(c.shape().dims(), &[3, 2]);

        let c_data = c.data().expect("failed to get data");
        assert_relative_eq!(c_data[0], 4.0, epsilon = 1e-6); // 1*4
        assert_relative_eq!(c_data[1], 5.0, epsilon = 1e-6); // 1*5
        assert_relative_eq!(c_data[2], 8.0, epsilon = 1e-6); // 2*4
        assert_relative_eq!(c_data[3], 10.0, epsilon = 1e-6); // 2*5
    }

    #[test]
    fn test_tensor_zip() {
        let a = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![4],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let b = Tensor::from_data(
            vec![5.0, 6.0, 7.0, 8.0],
            vec![4],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        let c = tensor_zip(&a, &b, |x, y| x + y).expect("zip failed");
        let c_data = c.data().expect("failed to get data");

        assert_relative_eq!(c_data[0], 6.0, epsilon = 1e-6);
        assert_relative_eq!(c_data[1], 8.0, epsilon = 1e-6);
        assert_relative_eq!(c_data[2], 10.0, epsilon = 1e-6);
        assert_relative_eq!(c_data[3], 12.0, epsilon = 1e-6);
    }

    #[test]
    fn test_parse_einsum_equation() {
        let (inputs, output) = parse_einsum_equation("ij,jk->ik").expect("parse failed");
        assert_eq!(inputs, vec!["ij", "jk"]);
        assert_eq!(output, "ik");

        let (inputs, output) = parse_einsum_equation("ii->").expect("parse failed");
        assert_eq!(inputs, vec!["ii"]);
        assert_eq!(output, "");
    }

    #[test]
    fn test_tensor_reduce_axis() {
        let input = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
            vec![2, 3],
            torsh_core::device::DeviceType::Cpu,
        )
        .expect("failed to create tensor");

        // Sum along axis 0
        let output = tensor_reduce(&input, Some(0), |a, b| a + b, 0.0).expect("reduce failed");
        assert_eq!(output.shape().dims(), &[3]);

        let output_data = output.data().expect("failed to get data");
        assert_relative_eq!(output_data[0], 5.0, epsilon = 1e-6); // 1+4
        assert_relative_eq!(output_data[1], 7.0, epsilon = 1e-6); // 2+5
        assert_relative_eq!(output_data[2], 9.0, epsilon = 1e-6); // 3+6
    }
}