scirs2-transform 0.4.1

Data transformation module for SciRS2 (scirs2-transform)
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
//! SIMD-accelerated normalization operations
//!
//! This module provides SIMD-optimized implementations of normalization operations
//! using the unified SIMD operations from scirs2-core.
//!
//! Features:
//! - Adaptive block sizing based on data dimensions and cache sizes
//! - Memory-aligned processing for maximum SIMD efficiency
//! - Cache-optimal memory access patterns
//! - Advanced prefetching strategies for large datasets

use scirs2_core::ndarray::{Array1, Array2, ArrayBase, Data, Ix2};
use scirs2_core::numeric::{Float, NumCast};
use scirs2_core::simd_ops::SimdUnifiedOps;

use crate::error::{Result, TransformError};
use crate::normalize::{NormalizationMethod, EPSILON};

/// Cache line size for optimal memory access
const CACHE_LINE_SIZE: usize = 64;
/// L1 cache size estimate for block sizing
const L1_CACHE_SIZE: usize = 32_768;
/// L2 cache size estimate for adaptive processing
const L2_CACHE_SIZE: usize = 262_144;

/// Adaptive block sizing strategy for cache-optimal processing
#[derive(Debug, Clone)]
pub struct AdaptiveBlockSizer {
    /// Optimal block size for current data dimensions
    pub optimal_block_size: usize,
    /// Whether to use cache-aligned processing
    pub use_cache_alignment: bool,
    /// Prefetch distance for memory optimization
    pub prefetch_distance: usize,
}

impl AdaptiveBlockSizer {
    /// Create adaptive block sizer based on data characteristics
    pub fn new<F>(datashape: &[usize]) -> Self
    where
        F: Float + NumCast,
    {
        let element_size = std::mem::size_of::<F>();
        let data_size = datashape.iter().product::<usize>() * element_size;

        // Adaptive block sizing based on data size and cache characteristics
        let optimal_block_size = if data_size <= L1_CACHE_SIZE / 4 {
            // Small data: use larger blocks for better vectorization
            256
        } else if data_size <= L2_CACHE_SIZE / 2 {
            // Medium data: balance cache efficiency and parallelism
            128
        } else {
            // Large data: smaller blocks to maintain cache locality
            64
        };

        let use_cache_alignment = data_size > L1_CACHE_SIZE;
        let prefetch_distance = if data_size > L2_CACHE_SIZE { 8 } else { 4 };

        AdaptiveBlockSizer {
            optimal_block_size,
            use_cache_alignment,
            prefetch_distance,
        }
    }

    /// Get cache-aligned block size
    pub fn get_aligned_block_size(&self, dimensionsize: usize) -> usize {
        if self.use_cache_alignment {
            // Align to cache line boundaries
            let cache_aligned =
                (self.optimal_block_size + CACHE_LINE_SIZE - 1) & !(CACHE_LINE_SIZE - 1);
            cache_aligned.min(dimensionsize)
        } else {
            self.optimal_block_size.min(dimensionsize)
        }
    }
}

/// SIMD-accelerated min-max normalization for 1D arrays
#[allow(dead_code)]
pub fn simd_minmax_normalize_1d<F>(array: &Array1<F>) -> Result<Array1<F>>
where
    F: Float + NumCast + SimdUnifiedOps,
{
    if array.is_empty() {
        return Err(TransformError::InvalidInput(
            "Input array is empty".to_string(),
        ));
    }

    let min = F::simd_min_element(&array.view());
    let max = F::simd_max_element(&array.view());
    let range = max - min;

    if range.abs() <= F::from(EPSILON).expect("Failed to convert to float") {
        // Constant feature, return array of 0.5
        return Ok(Array1::from_elem(
            array.len(),
            F::from(0.5).expect("Failed to convert constant to float"),
        ));
    }

    // Normalize: (x - min) / range
    let minarray = Array1::from_elem(array.len(), min);
    let normalized = F::simd_sub(&array.view(), &minarray.view());
    let rangearray = Array1::from_elem(array.len(), range);
    let result = F::simd_div(&normalized.view(), &rangearray.view());

    Ok(result)
}

/// SIMD-accelerated Z-score normalization for 1D arrays
#[allow(dead_code)]
pub fn simd_zscore_normalize_1d<F>(array: &Array1<F>) -> Result<Array1<F>>
where
    F: Float + NumCast + SimdUnifiedOps,
{
    if array.is_empty() {
        return Err(TransformError::InvalidInput(
            "Input array is empty".to_string(),
        ));
    }

    let mean = F::simd_mean(&array.view());
    let n = F::from(array.len()).expect("Operation failed");

    // Compute variance
    let meanarray = Array1::from_elem(array.len(), mean);
    let centered = F::simd_sub(&array.view(), &meanarray.view());
    let squared = F::simd_mul(&centered.view(), &centered.view());
    let variance = F::simd_sum(&squared.view()) / n;
    let std_dev = variance.sqrt();

    if std_dev <= F::from(EPSILON).expect("Failed to convert to float") {
        // Constant feature, return zeros
        return Ok(Array1::zeros(array.len()));
    }

    // Normalize: (x - mean) / std_dev
    let stdarray = Array1::from_elem(array.len(), std_dev);
    let result = F::simd_div(&centered.view(), &stdarray.view());

    Ok(result)
}

/// SIMD-accelerated L2 normalization for 1D arrays
#[allow(dead_code)]
pub fn simd_l2_normalize_1d<F>(array: &Array1<F>) -> Result<Array1<F>>
where
    F: Float + NumCast + SimdUnifiedOps,
{
    if array.is_empty() {
        return Err(TransformError::InvalidInput(
            "Input array is empty".to_string(),
        ));
    }

    let l2_norm = F::simd_norm(&array.view());

    if l2_norm <= F::from(EPSILON).expect("Failed to convert to float") {
        // Zero vector, return zeros
        return Ok(Array1::zeros(array.len()));
    }

    // Normalize: x / l2_norm
    let normarray = Array1::from_elem(array.len(), l2_norm);
    let result = F::simd_div(&array.view(), &normarray.view());

    Ok(result)
}

/// SIMD-accelerated max absolute scaling for 1D arrays
#[allow(dead_code)]
pub fn simd_maxabs_normalize_1d<F>(array: &Array1<F>) -> Result<Array1<F>>
where
    F: Float + NumCast + SimdUnifiedOps,
{
    if array.is_empty() {
        return Err(TransformError::InvalidInput(
            "Input array is empty".to_string(),
        ));
    }

    let absarray = F::simd_abs(&array.view());
    let max_abs = F::simd_max_element(&absarray.view());

    if max_abs <= F::from(EPSILON).expect("Failed to convert to float") {
        // All zeros, return zeros
        return Ok(Array1::zeros(array.len()));
    }

    // Normalize: x / max_abs
    let max_absarray = Array1::from_elem(array.len(), max_abs);
    let result = F::simd_div(&array.view(), &max_absarray.view());

    Ok(result)
}

/// Advanced SIMD-accelerated normalization for 2D arrays with optimized memory access patterns
#[allow(dead_code)]
pub fn simd_normalizearray<S, F>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
) -> Result<Array2<F>>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    if !array.is_standard_layout() {
        return Err(TransformError::InvalidInput(
            "Input array must be in standard memory layout".to_string(),
        ));
    }

    if array.ndim() != 2 {
        return Err(TransformError::InvalidInput(
            "Only 2D arrays are supported".to_string(),
        ));
    }

    if axis >= array.ndim() {
        return Err(TransformError::InvalidInput(format!(
            "Invalid axis {} for array with {} dimensions",
            axis,
            array.ndim()
        )));
    }

    let shape = array.shape();
    let mut normalized = Array2::zeros((shape[0], shape[1]));

    match method {
        NormalizationMethod::MinMax => simd_normalize_block_minmax(array, &mut normalized, axis)?,
        NormalizationMethod::ZScore => simd_normalize_block_zscore(array, &mut normalized, axis)?,
        NormalizationMethod::L2 => simd_normalize_block_l2(array, &mut normalized, axis)?,
        NormalizationMethod::MaxAbs => simd_normalize_block_maxabs(array, &mut normalized, axis)?,
        _ => {
            // Fall back to non-SIMD implementation for other methods
            return Err(TransformError::InvalidInput(
                "SIMD implementation not available for this normalization method".to_string(),
            ));
        }
    }

    Ok(normalized)
}

/// Block-wise SIMD min-max normalization with optimized memory access
#[allow(dead_code)]
fn simd_normalize_block_minmax<S, F>(
    array: &ArrayBase<S, Ix2>,
    normalized: &mut Array2<F>,
    axis: usize,
) -> Result<()>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let block_sizer = AdaptiveBlockSizer::new::<F>(&[shape[0], shape[1]]);
    let block_size =
        block_sizer.get_aligned_block_size(if axis == 0 { shape[1] } else { shape[0] });

    if axis == 0 {
        // Column-wise normalization with block processing
        let mut global_mins = Array1::zeros(shape[1]);
        let mut global_maxs = Array1::zeros(shape[1]);

        // First pass: compute global min/max for each column in blocks
        for block_start in (0..shape[1]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();
                global_mins[j] = F::simd_min_element(&colarray.view());
                global_maxs[j] = F::simd_max_element(&colarray.view());
            }
        }

        // Second pass: normalize using pre-computed min/max
        for block_start in (0..shape[1]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();
                let range = global_maxs[j] - global_mins[j];

                if range.abs() <= F::from(EPSILON).expect("Failed to convert to float") {
                    // Constant feature
                    for i in 0..shape[0] {
                        normalized[[i, j]] =
                            F::from(0.5).expect("Failed to convert constant to float");
                    }
                } else {
                    // Vectorized normalization
                    let minarray = Array1::from_elem(shape[0], global_mins[j]);
                    let rangearray = Array1::from_elem(shape[0], range);
                    let centered = F::simd_sub(&colarray.view(), &minarray.view());
                    let norm_col = F::simd_div(&centered.view(), &rangearray.view());

                    for i in 0..shape[0] {
                        normalized[[i, j]] = norm_col[i];
                    }
                }
            }
        }
    } else {
        // Row-wise normalization with block processing
        for block_start in (0..shape[0]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[0]);

            for i in block_start..block_end {
                let row = array.row(i);
                let rowarray = row.to_owned();
                let norm_row = simd_minmax_normalize_1d(&rowarray)?;

                for j in 0..shape[1] {
                    normalized[[i, j]] = norm_row[j];
                }
            }
        }
    }
    Ok(())
}

/// Block-wise SIMD Z-score normalization with optimized memory access
#[allow(dead_code)]
fn simd_normalize_block_zscore<S, F>(
    array: &ArrayBase<S, Ix2>,
    normalized: &mut Array2<F>,
    axis: usize,
) -> Result<()>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let block_sizer = AdaptiveBlockSizer::new::<F>(&[shape[0], shape[1]]);
    let block_size =
        block_sizer.get_aligned_block_size(if axis == 0 { shape[1] } else { shape[0] });

    if axis == 0 {
        // Column-wise normalization
        let mut global_means = Array1::zeros(shape[1]);
        let mut global_stds = Array1::zeros(shape[1]);
        let n_samples_f = F::from(shape[0]).expect("Failed to convert to float");

        // First pass: compute means and standard deviations
        for block_start in (0..shape[1]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();

                // Compute mean
                global_means[j] = F::simd_sum(&colarray.view()) / n_samples_f;

                // Compute standard deviation
                let meanarray = Array1::from_elem(shape[0], global_means[j]);
                let centered = F::simd_sub(&colarray.view(), &meanarray.view());
                let squared = F::simd_mul(&centered.view(), &centered.view());
                let variance = F::simd_sum(&squared.view()) / n_samples_f;
                global_stds[j] = variance.sqrt();

                // Avoid division by zero
                if global_stds[j] <= F::from(EPSILON).expect("Failed to convert to float") {
                    global_stds[j] = F::one();
                }
            }
        }

        // Second pass: normalize using pre-computed statistics
        for block_start in (0..shape[1]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();

                if global_stds[j] <= F::from(EPSILON).expect("Failed to convert to float") {
                    // Constant feature
                    for i in 0..shape[0] {
                        normalized[[i, j]] = F::zero();
                    }
                } else {
                    // Vectorized normalization
                    let meanarray = Array1::from_elem(shape[0], global_means[j]);
                    let stdarray = Array1::from_elem(shape[0], global_stds[j]);
                    let centered = F::simd_sub(&colarray.view(), &meanarray.view());
                    let norm_col = F::simd_div(&centered.view(), &stdarray.view());

                    for i in 0..shape[0] {
                        normalized[[i, j]] = norm_col[i];
                    }
                }
            }
        }
    } else {
        // Row-wise normalization with block processing
        for block_start in (0..shape[0]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[0]);

            for i in block_start..block_end {
                let row = array.row(i);
                let rowarray = row.to_owned();
                let norm_row = simd_zscore_normalize_1d(&rowarray)?;

                for j in 0..shape[1] {
                    normalized[[i, j]] = norm_row[j];
                }
            }
        }
    }
    Ok(())
}

/// Block-wise SIMD L2 normalization with optimized memory access
#[allow(dead_code)]
fn simd_normalize_block_l2<S, F>(
    array: &ArrayBase<S, Ix2>,
    normalized: &mut Array2<F>,
    axis: usize,
) -> Result<()>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let block_sizer = AdaptiveBlockSizer::new::<F>(&[shape[0], shape[1]]);
    let block_size =
        block_sizer.get_aligned_block_size(if axis == 0 { shape[1] } else { shape[0] });

    if axis == 0 {
        // Column-wise L2 normalization
        for block_start in (0..shape[1]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();
                let norm_col = simd_l2_normalize_1d(&colarray)?;

                for i in 0..shape[0] {
                    normalized[[i, j]] = norm_col[i];
                }
            }
        }
    } else {
        // Row-wise L2 normalization with SIMD optimization
        for block_start in (0..shape[0]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[0]);

            for i in block_start..block_end {
                let row = array.row(i);
                let rowarray = row.to_owned();
                let l2_norm = F::simd_norm(&rowarray.view());

                if l2_norm <= F::from(EPSILON).expect("Failed to convert to float") {
                    // Zero vector
                    for j in 0..shape[1] {
                        normalized[[i, j]] = F::zero();
                    }
                } else {
                    // Vectorized division
                    let normarray = Array1::from_elem(shape[1], l2_norm);
                    let norm_row = F::simd_div(&rowarray.view(), &normarray.view());

                    for j in 0..shape[1] {
                        normalized[[i, j]] = norm_row[j];
                    }
                }
            }
        }
    }
    Ok(())
}

/// Block-wise SIMD max-absolute normalization with optimized memory access
#[allow(dead_code)]
fn simd_normalize_block_maxabs<S, F>(
    array: &ArrayBase<S, Ix2>,
    normalized: &mut Array2<F>,
    axis: usize,
) -> Result<()>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let block_sizer = AdaptiveBlockSizer::new::<F>(&[shape[0], shape[1]]);
    let block_size =
        block_sizer.get_aligned_block_size(if axis == 0 { shape[1] } else { shape[0] });

    if axis == 0 {
        // Column-wise max-abs normalization
        for block_start in (0..shape[1]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();
                let norm_col = simd_maxabs_normalize_1d(&colarray)?;

                for i in 0..shape[0] {
                    normalized[[i, j]] = norm_col[i];
                }
            }
        }
    } else {
        // Row-wise max-abs normalization with SIMD optimization
        for block_start in (0..shape[0]).step_by(block_size) {
            let block_end = (block_start + block_size).min(shape[0]);

            for i in block_start..block_end {
                let row = array.row(i);
                let rowarray = row.to_owned();
                let absarray = F::simd_abs(&rowarray.view());
                let max_abs = F::simd_max_element(&absarray.view());

                if max_abs <= F::from(EPSILON).expect("Failed to convert to float") {
                    // All zeros
                    for j in 0..shape[1] {
                        normalized[[i, j]] = F::zero();
                    }
                } else {
                    // Vectorized division
                    let max_absarray = Array1::from_elem(shape[1], max_abs);
                    let norm_row = F::simd_div(&rowarray.view(), &max_absarray.view());

                    for j in 0..shape[1] {
                        normalized[[i, j]] = norm_row[j];
                    }
                }
            }
        }
    }
    Ok(())
}

/// Advanced SIMD normalization with automatic optimization selection
#[allow(dead_code)]
pub fn simd_normalize_adaptive<S, F>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
) -> Result<Array2<F>>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let data_size = shape[0] * shape[1] * std::mem::size_of::<F>();

    // Choose optimal strategy based on data characteristics
    if data_size > L2_CACHE_SIZE {
        // Large data: use chunked processing with memory optimization
        simd_normalize_chunked(array, method, axis)
    } else if shape[0] > shape[1] * 4 {
        // Tall matrix: optimize for column-wise operations
        simd_normalize_optimized_tall(array, method, axis)
    } else if shape[1] > shape[0] * 4 {
        // Wide matrix: optimize for row-wise operations
        simd_normalize_optimized_wide(array, method, axis)
    } else {
        // Standard processing
        simd_normalizearray(array, method, axis)
    }
}

/// Memory-efficient batch processing for advanced-large datasets
#[allow(dead_code)]
pub fn simd_normalize_batch<S, F>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
    batch_size_mb: usize,
) -> Result<Array2<F>>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let element_size = std::mem::size_of::<F>();
    let max_elements_per_batch = (batch_size_mb * 1024 * 1024) / element_size;

    if shape[0] * shape[1] <= max_elements_per_batch {
        // Small enough for single batch
        return simd_normalize_adaptive(array, method, axis);
    }

    let mut normalized = Array2::zeros((shape[0], shape[1]));

    if axis == 0 {
        // Column-wise: batch by columns
        let cols_per_batch = max_elements_per_batch / shape[0];
        for col_start in (0..shape[1]).step_by(cols_per_batch) {
            let col_end = (col_start + cols_per_batch).min(shape[1]);
            let batch_view = array.slice(scirs2_core::ndarray::s![.., col_start..col_end]);
            let batch_normalized = simd_normalize_adaptive(&batch_view, method, axis)?;

            for (j_local, j_global) in (col_start..col_end).enumerate() {
                for i in 0..shape[0] {
                    normalized[[i, j_global]] = batch_normalized[[i, j_local]];
                }
            }
        }
    } else {
        // Row-wise: batch by rows
        let rows_per_batch = max_elements_per_batch / shape[1];
        for row_start in (0..shape[0]).step_by(rows_per_batch) {
            let row_end = (row_start + rows_per_batch).min(shape[0]);
            let batch_view = array.slice(scirs2_core::ndarray::s![row_start..row_end, ..]);
            let batch_normalized = simd_normalize_adaptive(&batch_view, method, axis)?;

            for (i_local, i_global) in (row_start..row_end).enumerate() {
                for j in 0..shape[1] {
                    normalized[[i_global, j]] = batch_normalized[[i_local, j]];
                }
            }
        }
    }

    Ok(normalized)
}

/// Chunked SIMD normalization for large datasets
#[allow(dead_code)]
fn simd_normalize_chunked<S, F>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
) -> Result<Array2<F>>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    let shape = array.shape();
    let mut normalized = Array2::zeros((shape[0], shape[1]));

    let block_sizer = AdaptiveBlockSizer::new::<F>(&[shape[0], shape[1]]);
    let chunk_size = block_sizer.optimal_block_size * 4; // Larger chunks for big data

    if axis == 0 {
        // Process in column chunks
        for chunk_start in (0..shape[1]).step_by(chunk_size) {
            let chunk_end = (chunk_start + chunk_size).min(shape[1]);
            let chunk_view = array.slice(scirs2_core::ndarray::s![.., chunk_start..chunk_end]);
            let chunk_normalized = simd_normalizearray(&chunk_view, method, axis)?;

            for (j_local, j_global) in (chunk_start..chunk_end).enumerate() {
                for i in 0..shape[0] {
                    normalized[[i, j_global]] = chunk_normalized[[i, j_local]];
                }
            }
        }
    } else {
        // Process in row chunks
        for chunk_start in (0..shape[0]).step_by(chunk_size) {
            let chunk_end = (chunk_start + chunk_size).min(shape[0]);
            let chunk_view = array.slice(scirs2_core::ndarray::s![chunk_start..chunk_end, ..]);
            let chunk_normalized = simd_normalizearray(&chunk_view, method, axis)?;

            for (i_local, i_global) in (chunk_start..chunk_end).enumerate() {
                for j in 0..shape[1] {
                    normalized[[i_global, j]] = chunk_normalized[[i_local, j]];
                }
            }
        }
    }

    Ok(normalized)
}

/// Optimized SIMD normalization for tall matrices (many rows, few columns)
#[allow(dead_code)]
fn simd_normalize_optimized_tall<S, F>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
) -> Result<Array2<F>>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    // For tall matrices, optimize memory access patterns
    if axis == 0 {
        // Column-wise normalization: pre-compute all statistics
        simd_normalizearray(array, method, axis)
    } else {
        // Row-wise normalization: use smaller blocks for better cache usage
        let shape = array.shape();
        let mut normalized = Array2::zeros((shape[0], shape[1]));
        let small_block_size = 32; // Smaller blocks for tall matrices

        for block_start in (0..shape[0]).step_by(small_block_size) {
            let block_end = (block_start + small_block_size).min(shape[0]);

            for i in block_start..block_end {
                let row = array.row(i);
                let rowarray = row.to_owned();
                let norm_row = match method {
                    NormalizationMethod::MinMax => simd_minmax_normalize_1d(&rowarray)?,
                    NormalizationMethod::ZScore => simd_zscore_normalize_1d(&rowarray)?,
                    NormalizationMethod::L2 => simd_l2_normalize_1d(&rowarray)?,
                    NormalizationMethod::MaxAbs => simd_maxabs_normalize_1d(&rowarray)?,
                    _ => {
                        return Err(TransformError::InvalidInput(
                            "Unsupported normalization method for tall matrix optimization"
                                .to_string(),
                        ))
                    }
                };

                for j in 0..shape[1] {
                    normalized[[i, j]] = norm_row[j];
                }
            }
        }

        Ok(normalized)
    }
}

/// Optimized SIMD normalization for wide matrices (few rows, many columns)
#[allow(dead_code)]
fn simd_normalize_optimized_wide<S, F>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
) -> Result<Array2<F>>
where
    S: Data<Elem = F>,
    F: Float + NumCast + SimdUnifiedOps,
{
    // For wide matrices, optimize for column-wise operations
    if axis == 1 {
        // Row-wise normalization: straightforward processing
        simd_normalizearray(array, method, axis)
    } else {
        // Column-wise normalization: use vectorized column processing
        let shape = array.shape();
        let mut normalized = Array2::zeros((shape[0], shape[1]));
        let wide_block_size = 128; // Larger blocks for wide matrices

        for block_start in (0..shape[1]).step_by(wide_block_size) {
            let block_end = (block_start + wide_block_size).min(shape[1]);

            for j in block_start..block_end {
                let col = array.column(j);
                let colarray = col.to_owned();
                let norm_col = match method {
                    NormalizationMethod::MinMax => simd_minmax_normalize_1d(&colarray)?,
                    NormalizationMethod::ZScore => simd_zscore_normalize_1d(&colarray)?,
                    NormalizationMethod::L2 => simd_l2_normalize_1d(&colarray)?,
                    NormalizationMethod::MaxAbs => simd_maxabs_normalize_1d(&colarray)?,
                    _ => {
                        return Err(TransformError::InvalidInput(
                            "Unsupported normalization method for wide matrix optimization"
                                .to_string(),
                        ))
                    }
                };

                for i in 0..shape[0] {
                    normalized[[i, j]] = norm_col[i];
                }
            }
        }

        Ok(normalized)
    }
}