Skip to content

Commit 0a0dfb7

Browse files
committed
add helper transpose
1 parent ac6f16f commit 0a0dfb7

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

src/numerical_derivative/hessian.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ use num_complex::ComplexFloat;
77
/// Returns the hessian matrix for a given function
88
/// Can handle multivariable functions of any order or complexity
99
///
10+
/// The 2-D matrix returned has the structure [[d2f/d2var1, d2f/dvar1*dvar2, ... , d2f/dvar1*dvarN],
11+
/// [ ... ],
12+
/// [d2f/dvar1*dvarN, d2f/dvar2*dvarN, ... , dfM/d2varN]]
13+
/// where 'N' is the total number of variables
14+
///
1015
/// assume our function is y*sin(x) + 2*x*e^y. First define the function
1116
/// ```
1217
/// use multicalc::numerical_derivative::hessian;

src/numerical_derivative/jacobian.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ use num_complex::ComplexFloat;
66
/// Returns the jacobian matrix for a given vector of functions
77
/// Can handle multivariable functions of any order or complexity
88
///
9+
/// The 2-D matrix returned has the structure [[df1/dvar1, df1/dvar2, ... , df1/dvarN],
10+
/// [ ... ],
11+
/// [dfM/dvar1, dfM/dvar2, ... , dfM/dvarN]]
12+
///
13+
/// where 'N' is the total number of variables, and 'M' is the total number of functions
14+
///
15+
/// consult the helper utility [`multicalc::utils::helper::transpose`] to transpose the matrix shape if required
16+
///
917
/// NOTE: Returns a Result<T, ErrorCode>
1018
/// Possible ErrorCode are:
1119
/// VectorOfFunctionsCannotBeEmpty -> if function_matrix argument is an empty array

src/utils/helper.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use num_complex::ComplexFloat;
2+
3+
///utility to convert the transpose the matrix
4+
///takes an input of a matrix of shape MxN, and returns the matrix as NxM
5+
pub fn transpose<T: ComplexFloat, const NUM_ROWS: usize, const NUM_COLUMNS: usize>(matrix: &[[T; NUM_COLUMNS]; NUM_ROWS]) -> [[T; NUM_ROWS]; NUM_COLUMNS]
6+
{
7+
let mut result = [[T::zero(); NUM_ROWS]; NUM_COLUMNS];
8+
9+
for row_index in 0..NUM_COLUMNS
10+
{
11+
for col_index in 0..NUM_ROWS
12+
{
13+
result[row_index][col_index] = matrix[col_index][row_index];
14+
}
15+
}
16+
17+
return result;
18+
}

src/utils/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
pub mod gl_table;
2-
pub mod error_codes;
2+
pub mod error_codes;
3+
pub mod helper;
4+
5+
#[cfg(test)]
6+
mod test;

src/utils/test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::utils::helper;
2+
3+
#[test]
4+
fn test_transpose_1()
5+
{
6+
let original = [[1.0, 2.0, 3.0, 4.0], [1.0, 4.0, 6.0, 8.0], [1.0, 6.0, 9.0, 12.0]];
7+
8+
let trans = helper::transpose(&original);
9+
10+
for i in 0..original.len()
11+
{
12+
for j in 0..original[0].len()
13+
{
14+
assert!(original[i][j] == trans[j][i]);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)