numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
//! Enhanced shape manipulation and multidimensional views
//!
//! This module provides comprehensive shape manipulation capabilities including
//! advanced reshaping, view system, stride calculations, and layout optimization.

use super::advanced_ops::{ArrayView, Shape};
use crate::error::{NumRs2Error, Result};
use crate::traits::NumericElement;
use std::collections::HashMap;

/// Memory layout for arrays
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MemoryLayout {
    /// C-style contiguous (row-major)
    C,
    /// Fortran-style contiguous (column-major)
    Fortran,
    /// Custom stride pattern
    Custom,
    /// Non-contiguous layout
    Strided,
}

/// Advanced shape manipulation engine
pub struct ShapeEngine {
    /// Cache for computed strides
    stride_cache: HashMap<(Vec<usize>, MemoryLayout), Vec<usize>>,
}

impl ShapeEngine {
    /// Create a new shape engine
    pub fn new() -> Self {
        Self {
            stride_cache: HashMap::new(),
        }
    }

    /// Compute optimal strides for a given shape and layout
    pub fn compute_strides(&mut self, shape: &[usize], layout: MemoryLayout) -> Vec<usize> {
        let cache_key = (shape.to_vec(), layout);

        if let Some(cached_strides) = self.stride_cache.get(&cache_key) {
            return cached_strides.clone();
        }

        let strides = match layout {
            MemoryLayout::C => self.compute_c_strides(shape),
            MemoryLayout::Fortran => self.compute_fortran_strides(shape),
            MemoryLayout::Custom => self.compute_optimal_strides(shape),
            MemoryLayout::Strided => self.compute_default_strides(shape),
        };

        self.stride_cache.insert(cache_key, strides.clone());
        strides
    }

    /// Compute C-style (row-major) strides
    fn compute_c_strides(&self, shape: &[usize]) -> Vec<usize> {
        let mut strides = vec![1; shape.len()];

        for i in (0..shape.len().saturating_sub(1)).rev() {
            strides[i] = strides[i + 1] * shape[i + 1];
        }

        strides
    }

    /// Compute Fortran-style (column-major) strides
    fn compute_fortran_strides(&self, shape: &[usize]) -> Vec<usize> {
        let mut strides = vec![1; shape.len()];

        for i in 1..shape.len() {
            strides[i] = strides[i - 1] * shape[i - 1];
        }

        strides
    }

    /// Compute optimal strides for cache efficiency
    fn compute_optimal_strides(&self, shape: &[usize]) -> Vec<usize> {
        // For optimal cache performance, order dimensions by size (smallest stride for largest dimension)
        let mut dim_sizes: Vec<(usize, usize)> = shape
            .iter()
            .enumerate()
            .map(|(i, &size)| (i, size))
            .collect();
        dim_sizes.sort_by_key(|&(_, size)| std::cmp::Reverse(size));

        let mut strides = vec![0; shape.len()];
        let mut current_stride = 1;

        for &(dim_idx, dim_size) in &dim_sizes {
            strides[dim_idx] = current_stride;
            current_stride *= dim_size;
        }

        strides
    }

    /// Compute default strides (same as C-style)
    fn compute_default_strides(&self, shape: &[usize]) -> Vec<usize> {
        self.compute_c_strides(shape)
    }

    /// Check if a reshape operation is valid
    pub fn can_reshape(&self, current_shape: &[usize], new_shape: &[usize]) -> bool {
        let current_size: usize = current_shape.iter().product();
        let new_size: usize = new_shape.iter().product();
        current_size == new_size
    }

    /// Create a reshaped view if possible
    pub fn reshape_view<'a, T>(
        &self,
        view: &ArrayView<'a, T>,
        new_shape: &[usize],
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        if !self.can_reshape(&view.shape().dims, new_shape) {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Cannot reshape array from {:?} to {:?}: incompatible sizes",
                view.shape().dims,
                new_shape
            )));
        }

        // Check if the view is C-contiguous (required for simple reshape)
        if !view.is_c_contiguous() {
            return Err(NumRs2Error::InvalidOperation(
                "Cannot reshape non-contiguous view. Use copy() first.".to_string(),
            ));
        }

        let new_shape_obj = Shape::new(new_shape.to_vec());
        view.reshape(new_shape_obj)
    }

    /// Transpose an array view with specified axes
    pub fn transpose_view<'a, T>(
        &self,
        view: &ArrayView<'a, T>,
        axes: Option<Vec<usize>>,
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        view.transpose(axes)
    }

    /// Create a view with swapped axes
    pub fn swapaxes_view<'a, T>(
        &self,
        view: &ArrayView<'a, T>,
        axis1: usize,
        axis2: usize,
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        let ndim = view.shape().ndim();

        if axis1 >= ndim || axis2 >= ndim {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Axes {} and {} are out of bounds for array of dimension {}",
                axis1, axis2, ndim
            )));
        }

        let mut axes: Vec<usize> = (0..ndim).collect();
        axes.swap(axis1, axis2);

        self.transpose_view(view, Some(axes))
    }

    /// Move axis to a new position
    pub fn moveaxis_view<'a, T>(
        &self,
        view: &ArrayView<'a, T>,
        source: usize,
        destination: usize,
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        let ndim = view.shape().ndim();

        if source >= ndim || destination >= ndim {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Source axis {} or destination axis {} is out of bounds for array of dimension {}",
                source, destination, ndim
            )));
        }

        let mut axes: Vec<usize> = (0..ndim).collect();
        let removed = axes.remove(source);
        axes.insert(destination, removed);

        self.transpose_view(view, Some(axes))
    }

    /// Roll array elements along a given axis
    pub fn roll_view<T>(
        &self,
        view: &ArrayView<'_, T>,
        shift: isize,
        axis: Option<usize>,
    ) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        match axis {
            Some(ax) => self.roll_along_axis(view, shift, ax),
            None => self.roll_flattened(view, shift),
        }
    }

    /// Roll elements along a specific axis
    fn roll_along_axis<T>(
        &self,
        view: &ArrayView<'_, T>,
        shift: isize,
        axis: usize,
    ) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        if axis >= view.shape().ndim() {
            return Err(NumRs2Error::DimensionMismatch(format!(
                "Axis {} is out of bounds for array of dimension {}",
                axis,
                view.shape().ndim()
            )));
        }

        let axis_size = view.shape().dims[axis];
        let effective_shift =
            ((shift % axis_size as isize) + axis_size as isize) as usize % axis_size;

        let mut result = Vec::with_capacity(view.shape().size());
        let mut indices = vec![0; view.shape().ndim()];

        loop {
            // Calculate rolled index for the specified axis
            let original_axis_idx = indices[axis];
            let rolled_axis_idx = (original_axis_idx + effective_shift) % axis_size;

            // Create rolled indices
            let mut rolled_indices = indices.clone();
            rolled_indices[axis] = rolled_axis_idx;

            // Get element at rolled position
            if let Ok(element) = view.get(&rolled_indices) {
                result.push(*element);
            }

            // Advance indices
            if !self.advance_indices(&mut indices, &view.shape().dims) {
                break;
            }
        }

        Ok(result)
    }

    /// Roll elements in flattened array
    fn roll_flattened<T>(&self, view: &ArrayView<'_, T>, shift: isize) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        let flat_data = view.to_vec();
        let size = flat_data.len();

        if size == 0 {
            return Ok(flat_data);
        }

        let effective_shift = ((shift % size as isize) + size as isize) as usize % size;
        let mut result = Vec::with_capacity(size);

        // Roll the flattened array
        for i in 0..size {
            let src_idx = (i + size - effective_shift) % size;
            result.push(flat_data[src_idx]);
        }

        Ok(result)
    }

    /// Flip array along specified axes
    pub fn flip_view<T>(&self, view: &ArrayView<'_, T>, axes: Option<Vec<usize>>) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        let axes_to_flip = match axes {
            Some(ax) => ax,
            None => (0..view.shape().ndim()).collect(),
        };

        // Validate axes
        for &axis in &axes_to_flip {
            if axis >= view.shape().ndim() {
                return Err(NumRs2Error::DimensionMismatch(format!(
                    "Axis {} is out of bounds for array of dimension {}",
                    axis,
                    view.shape().ndim()
                )));
            }
        }

        let mut result = Vec::with_capacity(view.shape().size());
        let mut indices = vec![0; view.shape().ndim()];

        loop {
            // Create flipped indices
            let mut flipped_indices = indices.clone();
            for &axis in &axes_to_flip {
                flipped_indices[axis] = view.shape().dims[axis] - 1 - indices[axis];
            }

            // Get element at flipped position
            if let Ok(element) = view.get(&flipped_indices) {
                result.push(*element);
            }

            // Advance indices
            if !self.advance_indices(&mut indices, &view.shape().dims) {
                break;
            }
        }

        Ok(result)
    }

    /// Rotate array by 90 degrees
    pub fn rot90_view<T>(
        &self,
        view: &ArrayView<'_, T>,
        k: i32,
        axes: Option<(usize, usize)>,
    ) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        if view.shape().ndim() < 2 {
            return Err(NumRs2Error::DimensionMismatch(
                "rot90 requires at least 2 dimensions".to_string(),
            ));
        }

        let (axis1, axis2) = axes.unwrap_or((0, 1));

        if axis1 >= view.shape().ndim() || axis2 >= view.shape().ndim() || axis1 == axis2 {
            return Err(NumRs2Error::DimensionMismatch(
                "Invalid rotation axes".to_string(),
            ));
        }

        // Normalize k to range [0, 4)
        let k_norm = k.rem_euclid(4);

        match k_norm {
            0 => Ok(view.to_vec()), // No rotation
            1 => self.rotate_90_once(view, axis1, axis2),
            2 => self.rotate_180(view, axis1, axis2),
            3 => self.rotate_270(view, axis1, axis2),
            _ => unreachable!(),
        }
    }

    /// Rotate by 90 degrees once
    fn rotate_90_once<T>(
        &self,
        view: &ArrayView<'_, T>,
        axis1: usize,
        axis2: usize,
    ) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        let mut result = Vec::with_capacity(view.shape().size());
        let mut indices = vec![0; view.shape().ndim()];

        loop {
            // For 90-degree rotation: (i, j) -> (j, -i-1) = (j, rows-1-i)
            let mut rotated_indices = indices.clone();
            let old_i = indices[axis1];
            let old_j = indices[axis2];

            rotated_indices[axis1] = old_j;
            rotated_indices[axis2] = view.shape().dims[axis1] - 1 - old_i;

            if let Ok(element) = view.get(&rotated_indices) {
                result.push(*element);
            }

            if !self.advance_indices(&mut indices, &view.shape().dims) {
                break;
            }
        }

        Ok(result)
    }

    /// Rotate by 180 degrees
    fn rotate_180<T>(&self, view: &ArrayView<'_, T>, axis1: usize, axis2: usize) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        let mut result = Vec::with_capacity(view.shape().size());
        let mut indices = vec![0; view.shape().ndim()];

        loop {
            // For 180-degree rotation: (i, j) -> (-i-1, -j-1) = (rows-1-i, cols-1-j)
            let mut rotated_indices = indices.clone();
            rotated_indices[axis1] = view.shape().dims[axis1] - 1 - indices[axis1];
            rotated_indices[axis2] = view.shape().dims[axis2] - 1 - indices[axis2];

            if let Ok(element) = view.get(&rotated_indices) {
                result.push(*element);
            }

            if !self.advance_indices(&mut indices, &view.shape().dims) {
                break;
            }
        }

        Ok(result)
    }

    /// Rotate by 270 degrees (or -90 degrees)
    fn rotate_270<T>(&self, view: &ArrayView<'_, T>, axis1: usize, axis2: usize) -> Result<Vec<T>>
    where
        T: NumericElement + Copy,
    {
        let mut result = Vec::with_capacity(view.shape().size());
        let mut indices = vec![0; view.shape().ndim()];

        loop {
            // For 270-degree rotation: (i, j) -> (-j-1, i) = (cols-1-j, i)
            let mut rotated_indices = indices.clone();
            let old_i = indices[axis1];
            let old_j = indices[axis2];

            rotated_indices[axis1] = view.shape().dims[axis2] - 1 - old_j;
            rotated_indices[axis2] = old_i;

            if let Ok(element) = view.get(&rotated_indices) {
                result.push(*element);
            }

            if !self.advance_indices(&mut indices, &view.shape().dims) {
                break;
            }
        }

        Ok(result)
    }

    /// Squeeze array dimensions (remove dimensions of size 1)
    pub fn squeeze_view<'a, T>(
        &self,
        view: &ArrayView<'a, T>,
        axes: Option<Vec<usize>>,
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        let axes_to_squeeze = match axes {
            Some(ax) => {
                // Validate specified axes
                for &axis in &ax {
                    if axis >= view.shape().ndim() {
                        return Err(NumRs2Error::DimensionMismatch(format!(
                            "Axis {} is out of bounds",
                            axis
                        )));
                    }
                    if view.shape().dims[axis] != 1 {
                        return Err(NumRs2Error::DimensionMismatch(format!(
                            "Cannot squeeze axis {} with size {}",
                            axis,
                            view.shape().dims[axis]
                        )));
                    }
                }
                ax
            }
            None => {
                // Find all axes with size 1
                view.shape()
                    .dims
                    .iter()
                    .enumerate()
                    .filter_map(|(i, &size)| if size == 1 { Some(i) } else { None })
                    .collect()
            }
        };

        // Create new shape by removing squeezed dimensions
        let new_dims: Vec<usize> = view
            .shape()
            .dims
            .iter()
            .enumerate()
            .filter_map(|(i, &size)| {
                if axes_to_squeeze.contains(&i) {
                    None
                } else {
                    Some(size)
                }
            })
            .collect();

        if new_dims.is_empty() {
            // If all dimensions are squeezed, return scalar (1D array with one element)
            let new_shape = Shape::new(vec![1]);
            view.reshape(new_shape)
        } else {
            let new_shape = Shape::new(new_dims);
            view.reshape(new_shape)
        }
    }

    /// Expand array dimensions (add dimensions of size 1)
    pub fn expand_dims_view<'a, T>(
        &self,
        view: &ArrayView<'a, T>,
        axes: Vec<usize>,
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        let mut new_dims = view.shape().dims.clone();
        let mut sorted_axes = axes.clone();
        sorted_axes.sort_unstable();

        // Validate axes
        for &axis in &sorted_axes {
            if axis > new_dims.len() {
                return Err(NumRs2Error::DimensionMismatch(format!(
                    "Axis {} is out of bounds for expansion",
                    axis
                )));
            }
        }

        // Insert new dimensions in reverse order to maintain correct indices
        for &axis in sorted_axes.iter().rev() {
            new_dims.insert(axis, 1);
        }

        let new_shape = Shape::new(new_dims);
        view.reshape(new_shape)
    }

    /// Check if array is broadcastable to a target shape
    pub fn is_broadcastable(&self, source_shape: &[usize], target_shape: &[usize]) -> bool {
        let max_ndim = std::cmp::max(source_shape.len(), target_shape.len());

        for i in 0..max_ndim {
            let src_dim = if i < source_shape.len() {
                source_shape[source_shape.len() - i - 1]
            } else {
                1
            };
            let tgt_dim = if i < target_shape.len() {
                target_shape[target_shape.len() - i - 1]
            } else {
                1
            };

            if src_dim != tgt_dim && src_dim != 1 && tgt_dim != 1 {
                return false;
            }
        }

        true
    }

    /// Analyze memory layout efficiency
    pub fn analyze_layout_efficiency(&self, shape: &[usize], strides: &[usize]) -> LayoutAnalysis {
        let c_strides = self.compute_c_strides(shape);
        let f_strides = self.compute_fortran_strides(shape);

        let is_c_contiguous = strides == c_strides;
        let is_f_contiguous = strides == f_strides;
        let is_contiguous = is_c_contiguous || is_f_contiguous;

        // Calculate stride efficiency (how close to optimal)
        let total_elements: usize = shape.iter().product();
        let memory_span = self.calculate_memory_span(shape, strides);
        let efficiency = if memory_span > 0 {
            total_elements as f64 / memory_span as f64
        } else {
            0.0
        };

        // Detect common patterns
        let layout_pattern = if is_c_contiguous {
            LayoutPattern::CContiguous
        } else if is_f_contiguous {
            LayoutPattern::FortranContiguous
        } else if self.is_unit_stride_pattern(strides) {
            LayoutPattern::UnitStride
        } else if self.has_regular_pattern(strides) {
            LayoutPattern::Regular
        } else {
            LayoutPattern::Irregular
        };

        LayoutAnalysis {
            is_contiguous,
            is_c_contiguous,
            is_f_contiguous,
            efficiency,
            layout_pattern,
            memory_span,
            recommended_layout: self.recommend_layout(shape, strides),
        }
    }

    /// Calculate memory span (highest address - lowest address + 1)
    fn calculate_memory_span(&self, shape: &[usize], strides: &[usize]) -> usize {
        if shape.is_empty() {
            return 0;
        }

        let mut min_offset = 0;
        let mut max_offset = 0;

        for (&dim_size, &stride) in shape.iter().zip(strides.iter()) {
            if dim_size > 1 {
                let offset = (dim_size - 1) * stride;
                if stride > 0 {
                    max_offset += offset;
                } else {
                    min_offset += offset;
                }
            }
        }

        max_offset - min_offset + 1
    }

    /// Check if strides follow unit stride pattern
    fn is_unit_stride_pattern(&self, strides: &[usize]) -> bool {
        strides.contains(&1)
    }

    /// Check if strides have regular pattern
    fn has_regular_pattern(&self, strides: &[usize]) -> bool {
        if strides.len() < 2 {
            return true;
        }

        // Check if strides are in geometric progression
        for i in 1..strides.len() {
            if strides[i] == 0 || strides[i - 1] == 0 {
                continue;
            }
            // Simple regularity check - more sophisticated analysis could be added
            if strides[i] > strides[i - 1] * 10 || strides[i - 1] > strides[i] * 10 {
                return false;
            }
        }

        true
    }

    /// Recommend optimal layout
    fn recommend_layout(&self, shape: &[usize], current_strides: &[usize]) -> MemoryLayout {
        let c_strides = self.compute_c_strides(shape);
        let f_strides = self.compute_fortran_strides(shape);

        let is_c_contiguous = current_strides == c_strides;
        let is_f_contiguous = current_strides == f_strides;

        // Calculate simple efficiency metric without recursion
        let total_elements: usize = shape.iter().product();
        let memory_span = self.calculate_memory_span(shape, current_strides);
        let efficiency = if memory_span > 0 {
            total_elements as f64 / memory_span as f64
        } else {
            0.0
        };

        if efficiency > 0.9 {
            if is_c_contiguous {
                MemoryLayout::C
            } else if is_f_contiguous {
                MemoryLayout::Fortran
            } else {
                MemoryLayout::Custom
            }
        } else {
            // Recommend based on access patterns (heuristic)
            if shape.len() <= 2 {
                MemoryLayout::C
            } else {
                MemoryLayout::Custom // Use cache-optimized layout
            }
        }
    }

    /// Helper function to advance multi-dimensional indices
    fn advance_indices(&self, indices: &mut [usize], shape: &[usize]) -> bool {
        for i in (0..indices.len()).rev() {
            indices[i] += 1;
            if indices[i] < shape[i] {
                return true;
            }
            indices[i] = 0;
        }
        false
    }
}

impl Default for ShapeEngine {
    fn default() -> Self {
        Self::new()
    }
}

/// Analysis of memory layout efficiency
#[derive(Debug, Clone)]
pub struct LayoutAnalysis {
    /// Whether the layout is contiguous in memory
    pub is_contiguous: bool,
    /// Whether the layout is C-contiguous
    pub is_c_contiguous: bool,
    /// Whether the layout is Fortran-contiguous
    pub is_f_contiguous: bool,
    /// Memory utilization efficiency (0.0 to 1.0)
    pub efficiency: f64,
    /// Pattern classification
    pub layout_pattern: LayoutPattern,
    /// Total memory span used
    pub memory_span: usize,
    /// Recommended layout for optimization
    pub recommended_layout: MemoryLayout,
}

/// Memory layout patterns
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayoutPattern {
    /// C-style contiguous
    CContiguous,
    /// Fortran-style contiguous
    FortranContiguous,
    /// Has unit stride in at least one dimension
    UnitStride,
    /// Regular stride pattern
    Regular,
    /// Irregular stride pattern
    Irregular,
}

/// Advanced view system for efficient array operations
pub struct ViewSystem {
    shape_engine: ShapeEngine,
}

impl ViewSystem {
    /// Create a new view system
    pub fn new() -> Self {
        Self {
            shape_engine: ShapeEngine::new(),
        }
    }

    /// Create an optimized view with the best layout for the given operations
    pub fn create_optimized_view<'a, T>(
        &mut self,
        data: &'a [T],
        shape: &[usize],
        intended_operations: &[ViewOperation],
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement,
    {
        let optimal_layout = self.determine_optimal_layout(shape, intended_operations);
        let strides = self.shape_engine.compute_strides(shape, optimal_layout);

        let shape_obj = Shape::new(shape.to_vec());
        ArrayView::new(data, shape_obj, strides, 0)
    }

    /// Determine optimal layout based on intended operations
    fn determine_optimal_layout(
        &self,
        shape: &[usize],
        operations: &[ViewOperation],
    ) -> MemoryLayout {
        let mut score_c = 0;
        let mut score_fortran = 0;
        let mut score_custom = 0;

        for op in operations {
            match op {
                ViewOperation::RowAccess => score_c += 2,
                ViewOperation::ColumnAccess => score_fortran += 2,
                ViewOperation::RandomAccess => score_custom += 1,
                ViewOperation::SequentialScan => score_c += 1,
                ViewOperation::Transpose => score_fortran += 1,
                ViewOperation::MatrixMultiply => {
                    score_c += 1;
                    score_fortran += 1;
                }
                ViewOperation::Reduction => score_c += 1,
                ViewOperation::Broadcasting => score_custom += 2,
            }
        }

        // Consider shape characteristics
        if shape.len() > 2 {
            score_custom += 1;
        }

        if score_custom > score_c && score_custom > score_fortran {
            MemoryLayout::Custom
        } else if score_fortran > score_c {
            MemoryLayout::Fortran
        } else {
            MemoryLayout::C
        }
    }

    /// Create a view chain for complex operations
    pub fn create_view_chain<'a, T>(
        &mut self,
        initial_view: ArrayView<'a, T>,
        operations: &[ViewChainOperation],
    ) -> Result<ArrayView<'a, T>>
    where
        T: NumericElement + Copy,
    {
        let mut current_view = initial_view;

        for operation in operations {
            current_view = match operation {
                ViewChainOperation::Reshape(new_shape) => {
                    self.shape_engine.reshape_view(&current_view, new_shape)?
                }
                ViewChainOperation::Transpose(axes) => self
                    .shape_engine
                    .transpose_view(&current_view, axes.clone())?,
                ViewChainOperation::SwapAxes(ax1, ax2) => {
                    self.shape_engine.swapaxes_view(&current_view, *ax1, *ax2)?
                }
                ViewChainOperation::MoveAxis(src, dst) => {
                    self.shape_engine.moveaxis_view(&current_view, *src, *dst)?
                }
                ViewChainOperation::Squeeze(axes) => self
                    .shape_engine
                    .squeeze_view(&current_view, axes.clone())?,
                ViewChainOperation::ExpandDims(axes) => self
                    .shape_engine
                    .expand_dims_view(&current_view, axes.clone())?,
            };
        }

        Ok(current_view)
    }
}

impl Default for ViewSystem {
    fn default() -> Self {
        Self::new()
    }
}

/// Operations that can be performed on views
#[derive(Debug, Clone)]
pub enum ViewOperation {
    /// Access rows sequentially
    RowAccess,
    /// Access columns sequentially
    ColumnAccess,
    /// Random element access
    RandomAccess,
    /// Sequential scan through all elements
    SequentialScan,
    /// Transpose operation
    Transpose,
    /// Matrix multiplication
    MatrixMultiply,
    /// Reduction operations (sum, mean, etc.)
    Reduction,
    /// Broadcasting operations
    Broadcasting,
}

/// Operations in a view chain
#[derive(Debug, Clone)]
pub enum ViewChainOperation {
    /// Reshape to new dimensions
    Reshape(Vec<usize>),
    /// Transpose with optional axis specification
    Transpose(Option<Vec<usize>>),
    /// Swap two axes
    SwapAxes(usize, usize),
    /// Move axis from source to destination
    MoveAxis(usize, usize),
    /// Squeeze dimensions
    Squeeze(Option<Vec<usize>>),
    /// Expand dimensions
    ExpandDims(Vec<usize>),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arrays::advanced_ops::{ArrayView, Shape};

    #[test]
    fn test_shape_engine_creation() {
        let engine = ShapeEngine::new();
        assert!(engine.stride_cache.is_empty());
    }

    #[test]
    fn test_c_strides_computation() {
        let mut engine = ShapeEngine::new();
        let shape = [2, 3, 4];
        let strides = engine.compute_strides(&shape, MemoryLayout::C);
        assert_eq!(strides, vec![12, 4, 1]);
    }

    #[test]
    fn test_fortran_strides_computation() {
        let mut engine = ShapeEngine::new();
        let shape = [2, 3, 4];
        let strides = engine.compute_strides(&shape, MemoryLayout::Fortran);
        assert_eq!(strides, vec![1, 2, 6]);
    }

    #[test]
    fn test_reshape_validation() {
        let engine = ShapeEngine::new();
        assert!(engine.can_reshape(&[2, 3], &[6]));
        assert!(engine.can_reshape(&[2, 3], &[3, 2]));
        assert!(!engine.can_reshape(&[2, 3], &[7]));
    }

    #[test]
    fn test_broadcastability_check() {
        let engine = ShapeEngine::new();
        assert!(engine.is_broadcastable(&[3, 1, 4], &[2, 4]));
        assert!(engine.is_broadcastable(&[1, 4], &[3, 4]));
        assert!(!engine.is_broadcastable(&[3, 4], &[5, 4]));
    }

    #[test]
    fn test_layout_analysis() {
        let mut engine = ShapeEngine::new();
        let shape = [3, 4];
        let c_strides = engine.compute_strides(&shape, MemoryLayout::C);

        let analysis = engine.analyze_layout_efficiency(&shape, &c_strides);
        assert!(analysis.is_c_contiguous);
        assert!(analysis.is_contiguous);
        assert!(analysis.efficiency > 0.9);
    }

    #[test]
    fn test_view_system_creation() {
        let mut view_system = ViewSystem::new();
        let data = vec![1, 2, 3, 4, 5, 6];
        let shape = [2, 3];
        let operations = vec![ViewOperation::RowAccess];

        let view = view_system
            .create_optimized_view(&data, &shape, &operations)
            .expect("test: operation should succeed");
        assert_eq!(view.shape().dims, vec![2, 3]);
    }

    #[test]
    fn test_squeeze_operation() {
        let engine = ShapeEngine::new();
        let data = vec![1, 2, 3, 4];
        let shape = Shape::new(vec![1, 2, 1, 2]);
        let view = ArrayView::from_data(&data, shape).expect("test: operation should succeed");

        let squeezed = engine
            .squeeze_view(&view, None)
            .expect("test: operation should succeed");
        assert_eq!(squeezed.shape().dims, vec![2, 2]);
    }

    #[test]
    fn test_expand_dims_operation() {
        let engine = ShapeEngine::new();
        let data = vec![1, 2, 3, 4];
        let shape = Shape::new(vec![2, 2]);
        let view = ArrayView::from_data(&data, shape).expect("test: operation should succeed");

        let expanded = engine
            .expand_dims_view(&view, vec![1])
            .expect("test: operation should succeed");
        assert_eq!(expanded.shape().dims, vec![2, 1, 2]);
    }
}