pub fn vander<T>(
x: ArrayView<'_, T, Ix1>,
n: Option<usize>,
increasing: Option<bool>,
) -> Result<Array<T, Ix2>, &'static str>
Expand description
Create a matrix vander from a 1D array
§Arguments
x
- Input 1D arrayn
- Optional number of columns in the output (defaults to x.len())increasing
- Optional boolean to determine order (defaults to false)
§Returns
A Vandermonde matrix where each column is a power of the input array
§Examples
ⓘ
use ndarray::array;
use scirs2_core::ndarray_ext::matrix::vander;
let x = array![1.0, 2.0, 3.0];
// Default behavior: decreasing powers from n-1 to 0
let v1 = vander(x.view(), None, None).unwrap();
assert_eq!(v1.shape(), &[3, 3]);
// Powers: x^2, x^1, x^0
assert_eq!(v1, array![
[1.0, 1.0, 1.0],
[4.0, 2.0, 1.0],
[9.0, 3.0, 1.0]
]);
// Increasing powers: 0 to n-1
let v2 = vander(x.view(), None, Some(true)).unwrap();
assert_eq!(v2.shape(), &[3, 3]);
// Powers: x^0, x^1, x^2
assert_eq!(v2, array![
[1.0, 1.0, 1.0],
[1.0, 2.0, 4.0],
[1.0, 3.0, 9.0]
]);
// Specify 4 columns
let v3 = vander(x.view(), Some(4), None).unwrap();
assert_eq!(v3.shape(), &[3, 4]);
// Powers: x^3, x^2, x^1, x^0
assert_eq!(v3, array![
[1.0, 1.0, 1.0, 1.0],
[8.0, 4.0, 2.0, 1.0],
[27.0, 9.0, 3.0, 1.0]
]);