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
//! Array creation methods
//!
//! This module contains constructors and factory methods for creating arrays:
//! - zeros, ones, full, empty
//! - identity, eye
//! - tri, tril, triu
//! - diagonal matrices
//! - from_vec
use super::Array;
use num_traits::{One, Zero};
use scirs2_core::ndarray::{Array as NdArray, IxDyn};
impl<T: Clone> Array<T> {
/// Create a new array with the same shape as another array, filled with zeros
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
/// let zeros: Array<i32> = Array::zeros_like(&a);
/// assert_eq!(zeros.shape(), vec![2, 2]);
/// assert_eq!(zeros.to_vec(), vec![0, 0, 0, 0]);
/// ```
pub fn zeros_like<U>(other: &Array<U>) -> Self
where
T: Zero + Clone,
U: Clone,
{
Self::zeros(&other.shape())
}
/// Create a new array with the specified shape, data type, and order, filled with zeros
///
/// # Parameters
///
/// * `other` - The array whose shape to copy
/// * `shape` - Optional shape for the new array. If None, the shape is the same as `other`
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
///
/// // Same shape as a
/// let zeros = Array::<f64>::zeros_like_with(&a, None);
/// assert_eq!(zeros.shape(), vec![2, 2]);
/// assert_eq!(zeros.to_vec(), vec![0.0, 0.0, 0.0, 0.0]);
///
/// // Different shape
/// let zeros_3d = Array::<i32>::zeros_like_with(&a, Some(&[2, 2, 2]));
/// assert_eq!(zeros_3d.shape(), vec![2, 2, 2]);
/// assert_eq!(zeros_3d.size(), 8);
/// ```
pub fn zeros_like_with<U>(other: &Array<U>, shape: Option<&[usize]>) -> Self
where
T: Zero + Clone,
U: Clone,
{
match shape {
Some(s) => Self::zeros(s),
None => Self::zeros(&other.shape()),
}
}
/// Create a new array with the same shape as another array, filled with ones
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
/// let ones: Array<i32> = Array::ones_like(&a);
/// assert_eq!(ones.shape(), vec![2, 2]);
/// assert_eq!(ones.to_vec(), vec![1, 1, 1, 1]);
/// ```
pub fn ones_like<U>(other: &Array<U>) -> Self
where
T: One + Clone,
U: Clone,
{
Self::ones(&other.shape())
}
/// Create a new array with the specified shape, data type, and order, filled with ones
///
/// # Parameters
///
/// * `other` - The array whose shape to copy
/// * `shape` - Optional shape for the new array. If None, the shape is the same as `other`
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
///
/// // Same shape as a
/// let ones = Array::<f64>::ones_like_with(&a, None);
/// assert_eq!(ones.shape(), vec![2, 2]);
/// assert_eq!(ones.to_vec(), vec![1.0, 1.0, 1.0, 1.0]);
///
/// // Different shape
/// let ones_3d = Array::<i32>::ones_like_with(&a, Some(&[2, 2, 2]));
/// assert_eq!(ones_3d.shape(), vec![2, 2, 2]);
/// assert_eq!(ones_3d.size(), 8);
/// ```
pub fn ones_like_with<U>(other: &Array<U>, shape: Option<&[usize]>) -> Self
where
T: One + Clone,
U: Clone,
{
match shape {
Some(s) => Self::ones(s),
None => Self::ones(&other.shape()),
}
}
/// Create a new array with the same shape as another array with uninitialized values
/// Note: This is similar to NumPy's empty_like but with safe Rust semantics
/// The array will be initialized with a default value instead of random memory
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
/// let empty = Array::<i32>::empty_like(&a);
/// assert_eq!(empty.shape(), vec![2, 2]);
/// // Values are default-initialized
/// assert_eq!(empty.to_vec(), vec![0, 0, 0, 0]);
/// ```
pub fn empty_like<U>(other: &Array<U>) -> Self
where
T: Default + Clone,
U: Clone,
{
let shape = other.shape();
let size: usize = shape.iter().product();
let vec = vec![T::default(); size];
Self::from_vec(vec).reshape(&shape)
}
/// Create a new array with the specified shape, data type, and order, uninitialized
/// Note: This is similar to NumPy's empty_like_with but with safe Rust semantics
///
/// # Parameters
///
/// * `other` - The array whose shape to copy
/// * `shape` - Optional shape for the new array. If None, the shape is the same as `other`
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
///
/// // Same shape as a
/// let empty = Array::<f64>::empty_like_with(&a, None);
/// assert_eq!(empty.shape(), vec![2, 2]);
///
/// // Different shape
/// let empty_3d = Array::<i32>::empty_like_with(&a, Some(&[2, 2, 2]));
/// assert_eq!(empty_3d.shape(), vec![2, 2, 2]);
/// assert_eq!(empty_3d.size(), 8);
/// ```
pub fn empty_like_with<U>(other: &Array<U>, shape: Option<&[usize]>) -> Self
where
T: Default + Clone,
U: Clone,
{
let shape_to_use = match shape {
Some(s) => s,
None => &other.shape(),
};
let size: usize = shape_to_use.iter().product();
let vec = vec![T::default(); size];
Self::from_vec(vec).reshape(shape_to_use)
}
/// Create a new array from a vector and reshape it
pub fn from_vec(vec: Vec<T>) -> Self {
let data = NdArray::from_shape_vec(IxDyn(&[vec.len()]), vec).unwrap_or_else(|e| {
// This should never happen with a properly sized vector
// Log the error and create an empty array as last resort
eprintln!(
"Critical: Array creation failed: {}. This indicates a serious bug.",
e
);
// Create a minimal array that won't cause undefined behavior
NdArray::from_shape_vec(IxDyn(&[0]), Vec::new())
.expect("empty array creation should succeed")
});
Self { data }
}
/// Create a new array with a specific shape, filled with zeros
pub fn zeros(shape: &[usize]) -> Self
where
T: Zero + Clone,
{
let data = NdArray::zeros(IxDyn(shape));
Self { data }
}
/// Create a triangular matrix with ones below the given diagonal and zeros elsewhere
///
/// # Parameters
///
/// * `n` - Number of rows
/// * `m` - Number of columns (defaults to `n` if None)
/// * `k` - The diagonal below which to fill with ones (0 for main, positive for above, negative for below)
/// * `value` - The value to fill with (defaults to 1)
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// // Lower triangular matrix
/// let a = Array::<i32>::tri(3, None, 0, None);
/// assert_eq!(a.shape(), vec![3, 3]);
/// assert_eq!(a.to_vec(), vec![1, 0, 0, 1, 1, 0, 1, 1, 1]);
///
/// // Upper triangular matrix (k=1)
/// let b = Array::<i32>::tri(3, None, 1, None);
/// assert_eq!(b.shape(), vec![3, 3]);
/// assert_eq!(b.to_vec(), vec![1, 1, 0, 1, 1, 1, 1, 1, 1]);
/// ```
pub fn tri(n: usize, m: Option<usize>, k: isize, value: Option<T>) -> Self
where
T: Zero + One + Clone,
{
let m = m.unwrap_or(n);
let value = value.unwrap_or_else(T::one);
let zero = T::zero();
let mut result = Self::zeros(&[n, m]);
for i in 0..n {
for j in 0..m {
// NumPy's tri returns 1s on or below the diagonal (i-j <= k)
if (j as isize) <= (i as isize) + k {
result.set(&[i, j], value.clone()).unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in tri function",
i, j
)
});
} else {
result.set(&[i, j], zero.clone()).unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in tri function",
i, j
)
});
}
}
}
result
}
/// Create a lower triangular matrix or extract the lower triangle from an existing matrix
///
/// # Parameters
///
/// * `k` - The diagonal below which to extract/fill (0 for main, positive for above, negative for below)
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// // Create a 3x3 matrix
/// let a = Array::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(&[3, 3]);
///
/// // Get the lower triangle including the main diagonal
/// let lower = a.tril(0);
/// assert_eq!(lower.shape(), vec![3, 3]);
/// assert_eq!(lower.to_vec(), vec![1, 0, 0, 4, 5, 0, 7, 8, 9]);
///
/// // Get the lower triangle excluding the main diagonal
/// let strictly_lower = a.tril(-1);
/// assert_eq!(strictly_lower.shape(), vec![3, 3]);
/// assert_eq!(strictly_lower.to_vec(), vec![0, 0, 0, 4, 0, 0, 7, 8, 0]);
/// ```
pub fn tril(&self, k: isize) -> Self
where
T: Zero + Clone,
{
if self.ndim() != 2 {
panic!("tril requires a 2D array");
}
let shape = self.shape();
let n = shape[0];
let m = shape[1];
let zero = T::zero();
let mut result = self.clone();
for i in 0..n {
for j in 0..m {
// Zero out elements above the k-th diagonal
// In NumPy, the condition is j > i + k
if (j as isize) > (i as isize) + k {
result.set(&[i, j], zero.clone()).unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in tril function",
i, j
)
});
}
}
}
result
}
/// Create an upper triangular matrix or extract the upper triangle from an existing matrix
///
/// # Parameters
///
/// * `k` - The diagonal above which to extract/fill (0 for main, positive for above, negative for below)
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// // Create a 3x3 matrix
/// let a = Array::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(&[3, 3]);
///
/// // Get the upper triangle including the main diagonal
/// let upper = a.triu(0);
/// assert_eq!(upper.shape(), vec![3, 3]);
/// assert_eq!(upper.to_vec(), vec![1, 2, 3, 0, 5, 6, 0, 0, 9]);
///
/// // Get the upper triangle excluding the main diagonal
/// let strictly_upper = a.triu(1);
/// assert_eq!(strictly_upper.shape(), vec![3, 3]);
/// assert_eq!(strictly_upper.to_vec(), vec![0, 2, 3, 0, 0, 6, 0, 0, 0]);
/// ```
pub fn triu(&self, k: isize) -> Self
where
T: Zero + Clone,
{
if self.ndim() != 2 {
panic!("triu requires a 2D array");
}
let shape = self.shape();
let n = shape[0];
let m = shape[1];
let zero = T::zero();
let mut result = self.clone();
for i in 0..n {
for j in 0..m {
// Zero out elements below the k-th diagonal
// In NumPy, the condition is j < i + k
if (j as isize) < (i as isize) + k {
result.set(&[i, j], zero.clone()).unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in triu function",
i, j
)
});
}
}
}
result
}
/// Create a new array with a specific shape, filled with ones
pub fn ones(shape: &[usize]) -> Self
where
T: One + Clone,
{
let data = NdArray::ones(IxDyn(shape));
Self { data }
}
/// Create a new array with a specific shape, filled with a specific value
pub fn full(shape: &[usize], value: T) -> Self
where
T: Clone,
{
let size: usize = shape.iter().product();
let vec = vec![value; size];
Self::from_vec(vec).reshape(shape)
}
/// Create a new array with a specific shape, with uninitialized values
///
/// Note: This is similar to NumPy's empty but with safe Rust semantics.
/// The array will be initialized with default values instead of random memory.
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let empty: Array<i32> = Array::empty(&[2, 3]);
/// assert_eq!(empty.shape(), vec![2, 3]);
/// assert_eq!(empty.size(), 6);
/// // Values are default-initialized (zeros for numeric types)
/// assert_eq!(empty.to_vec(), vec![0, 0, 0, 0, 0, 0]);
///
/// let empty_f64: Array<f64> = Array::empty(&[2, 2]);
/// assert_eq!(empty_f64.to_vec(), vec![0.0, 0.0, 0.0, 0.0]);
/// ```
pub fn empty(shape: &[usize]) -> Self
where
T: Default + Clone,
{
let size: usize = shape.iter().product();
let vec = vec![T::default(); size];
Self::from_vec(vec).reshape(shape)
}
/// Create a 2D identity matrix of the specified size
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let eye = Array::<i32>::identity(3);
/// assert_eq!(eye.shape(), vec![3, 3]);
/// assert_eq!(eye.to_vec(), vec![1, 0, 0, 0, 1, 0, 0, 0, 1]);
/// ```
pub fn identity(n: usize) -> Self
where
T: Zero + One + Clone,
{
Self::eye(n, n, 0)
}
/// Create a 2D identity matrix of the specified size (compatibility function)
pub fn eye_square(n: usize) -> Self
where
T: Zero + One + Clone,
{
Self::eye(n, n, 0)
}
/// Create a 2D array with ones on the diagonal and zeros elsewhere
///
/// Parameters:
/// - `n_rows`: Number of rows
/// - `n_cols`: Number of columns
/// - `k`: Index of the diagonal (0 for main diagonal, positive for above, negative for below)
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// // 3x3 identity matrix
/// let eye = Array::<i32>::eye(3, 3, 0);
/// assert_eq!(eye.shape(), vec![3, 3]);
/// assert_eq!(eye.to_vec(), vec![1, 0, 0, 0, 1, 0, 0, 0, 1]);
///
/// // 3x3 matrix with diagonal above the main
/// let eye_above = Array::<i32>::eye(3, 3, 1);
/// assert_eq!(eye_above.shape(), vec![3, 3]);
/// assert_eq!(eye_above.to_vec(), vec![0, 1, 0, 0, 0, 1, 0, 0, 0]);
///
/// // 3x3 matrix with diagonal below the main
/// let eye_below = Array::<i32>::eye(3, 3, -1);
/// assert_eq!(eye_below.shape(), vec![3, 3]);
/// assert_eq!(eye_below.to_vec(), vec![0, 0, 0, 1, 0, 0, 0, 1, 0]);
///
/// // Rectangular matrix
/// let rect_eye = Array::<f64>::eye(2, 4, 0);
/// assert_eq!(rect_eye.shape(), vec![2, 4]);
/// assert_eq!(rect_eye.to_vec(), vec![1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
/// ```
pub fn eye(n_rows: usize, n_cols: usize, k: isize) -> Self
where
T: Zero + One + Clone,
{
let mut result = Self::zeros(&[n_rows, n_cols]);
// Optimized diagonal setting with bounds checking
let diagonal_start = if k >= 0 { 0 } else { (-k) as usize };
let diagonal_col_start = if k >= 0 { k as usize } else { 0 };
let max_diagonal_length = n_rows
.saturating_sub(diagonal_start)
.min(n_cols.saturating_sub(diagonal_col_start));
// Set ones on the specified diagonal efficiently
for i in 0..max_diagonal_length {
let row = diagonal_start + i;
let col = diagonal_col_start + i;
if row < n_rows && col < n_cols {
result.set(&[row, col], T::one()).unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in eye function",
row, col
)
});
}
}
result
}
/// Create a 2D array with the given values as a diagonal
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1, 2, 3]);
/// let diag: Array<i32> = Array::create_diagonal_matrix(&a, 0);
/// assert_eq!(diag.shape(), vec![3, 3]);
/// assert_eq!(diag.to_vec(), vec![1, 0, 0, 0, 2, 0, 0, 0, 3]);
///
/// // Diagonal above the main
/// let diag_above: Array<i32> = Array::create_diagonal_matrix(&a, 1);
/// assert_eq!(diag_above.shape(), vec![4, 4]);
/// assert_eq!(diag_above.to_vec(), vec![0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0]);
/// ```
pub fn create_diagonal_matrix_helper(v: &Array<T>, k: isize) -> Self
where
T: Zero + Clone,
{
if v.ndim() != 1 {
// In a real implementation, we should return a Result, but for simplicity,
// we'll panic with a clear message
panic!("diag requires a 1D array");
}
let diag_len = v.size();
let size = diag_len + k.unsigned_abs();
let mut result = Self::zeros(&[size, size]);
// Set values along the specified diagonal
for i in 0..diag_len {
if k >= 0 {
let j = i + k as usize;
if j < size {
result
.set(
&[i, j],
v.array()
.get([i])
.expect("element access should succeed within bounds")
.clone(),
)
.unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in diag function",
i, j
)
});
}
} else {
let i_offset = (-k) as usize;
if i + i_offset < size {
result
.set(
&[i + i_offset, i],
v.array()
.get([i])
.expect("element access should succeed within bounds")
.clone(),
)
.unwrap_or_else(|_| {
panic!(
"Internal error: failed to set element at [{}, {}] in diag function",
i + i_offset, i
)
});
}
}
}
result
}
/// Extract a diagonal from a 2D array or create a diagonal matrix from a 1D array
///
/// # Parameters
///
/// * `v` - Input array. If v is 2D, return its kth diagonal. If v is 1D, return a 2D array with v as its kth diagonal.
/// * `k` - Diagonal offset. k=0 refers to the main diagonal, k>0 to the kth diagonal above the main, and k<0 to the kth diagonal below the main.
///
/// # Returns
///
/// The diagonal array or a matrix with the diagonal
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// // Create a diagonal matrix from a 1D array
/// let a = Array::from_vec(vec![1, 2, 3]);
/// let diag: Array<i32> = Array::create_diagonal_matrix(&a, 0);
/// assert_eq!(diag.shape(), vec![3, 3]);
/// assert_eq!(diag.to_vec(), vec![1, 0, 0, 0, 2, 0, 0, 0, 3]);
///
/// // Extract the main diagonal from a 2D array
/// let b = Array::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(&[3, 3]);
/// let main_diag: Array<i32> = Array::create_diagonal_matrix(&b, 0);
/// assert_eq!(main_diag.shape(), vec![3]);
/// assert_eq!(main_diag.to_vec(), vec![1, 5, 9]);
///
/// // Extract diagonal above the main
/// let above_diag: Array<i32> = Array::create_diagonal_matrix(&b, 1);
/// assert_eq!(above_diag.shape(), vec![2]);
/// assert_eq!(above_diag.to_vec(), vec![2, 6]);
/// ```
// This needs a different name to avoid conflict with the instance method in indexing.rs
pub fn create_diagonal_matrix(v: &Array<T>, k: isize) -> Self
where
T: Zero + Clone,
{
if v.ndim() == 1 {
// Create a diagonal matrix
Self::create_diagonal_matrix_helper(v, k)
} else if v.ndim() == 2 {
// Extract the diagonal
let shape = v.shape();
let n_rows = shape[0];
let n_cols = shape[1];
let mut diag_elements = Vec::new();
// Calculate the length of the diagonal we're extracting
let diag_len = if k >= 0 {
std::cmp::min(n_rows, n_cols.saturating_sub(k as usize))
} else {
std::cmp::min(n_cols, n_rows.saturating_sub((-k) as usize))
};
// Extract the diagonal elements
for i in 0..diag_len {
if k >= 0 {
let j = i + k as usize;
if j < n_cols {
diag_elements.push(
v.array()
.get([i, j])
.expect("element access should succeed within bounds")
.clone(),
);
}
} else {
let i_offset = (-k) as usize;
if i + i_offset < n_rows {
diag_elements.push(
v.array()
.get([i + i_offset, i])
.expect("element access should succeed within bounds")
.clone(),
);
}
}
}
// Return as a 1D array
Self::from_vec(diag_elements)
} else {
panic!("diag requires a 1D or 2D array");
}
}
/// Extract the diagonal of an array or construct a diagonal array from a 1D array
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
///
/// // Extract diagonal from a 2D array
/// let a = Array::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(&[3, 3]);
/// let diag: Array<i32> = Array::diagflat(&a, 0);
/// assert_eq!(diag.shape(), vec![9, 9]);
///
/// // Create diagonal array from a 1D array
/// let b = Array::from_vec(vec![1, 2, 3]);
/// let diag_b: Array<i32> = Array::diagflat(&b, 0);
/// assert_eq!(diag_b.shape(), vec![3, 3]);
/// assert_eq!(diag_b.to_vec(), vec![1, 0, 0, 0, 2, 0, 0, 0, 3]);
/// ```
pub fn diagflat(v: &Array<T>, k: isize) -> Self
where
T: Zero + Clone,
{
// If already 1D, create a diagonal matrix
if v.ndim() == 1 {
return Self::create_diagonal_matrix(v, k);
}
// Otherwise, flatten the array and create a diagonal matrix
let flat = v.reshape(&[v.size()]);
Self::create_diagonal_matrix(&flat, k)
}
}