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
//! Matrix operation functions including determinant and matrix power calculations.
//!
//! This module provides essential matrix operations that are commonly used
//! in linear algebra computations.
// Used conditionally based on features
use crateArray;
// Used conditionally based on features
use crate;
// Used conditionally based on features
use Float;
// Used conditionally based on features
use Debug;
/// Compute the determinant of a matrix
///
/// # Arguments
/// * `a` - Input square matrix for which to compute the determinant
///
/// # Returns
/// * `Result<T>` - The determinant value if successful, error otherwise
///
/// # Errors
/// * `NumRs2Error::DimensionMismatch` - If the input is not a square matrix
///
/// # Examples
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
/// let det_val = det(&a).expect("determinant computation should succeed for square matrix");
/// assert_eq!(det_val, -2.0);
/// ```
/// Compute the matrix power (A raised to power n)
///
/// # Arguments
/// * `a` - Input square matrix to raise to power n
/// * `n` - The power to raise the matrix to (can be positive, negative, or zero)
///
/// # Returns
/// * `Result<Array<T>>` - The matrix raised to power n if successful, error otherwise
///
/// # Errors
/// * `NumRs2Error::DimensionMismatch` - If the input is not a square matrix
/// * Matrix inversion errors if n is negative and matrix is singular
///
/// # Examples
/// ```
/// use numrs2::prelude::*;
///
/// let a = Array::from_vec(vec![2.0, 0.0, 0.0, 2.0]).reshape(&[2, 2]);
/// let a_squared = matrix_power(&a, 2).expect("matrix power should succeed for square matrix");
/// let expected = Array::from_vec(vec![4.0, 0.0, 0.0, 4.0]).reshape(&[2, 2]);
/// // Result should be [[4, 0], [0, 4]]
/// ```
///
/// # Special cases
/// * `n = 0`: Returns the identity matrix of the same size
/// * `n = 1`: Returns a copy of the original matrix
/// * `n = -1`: Returns the matrix inverse
/// * `n > 1`: Computes A * A * ... * A (n times)
/// * `n < -1`: Computes (A^-1)^|n|