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
//! Bias (intercept) column insertion (RFC-028 §4.3).
use crateMattenMlprepError;
use cratematrix_dims;
use Tensor;
/// Prepends a constant `1.0` bias column: `[n, m] -> [n, m+1]`.
///
/// Column `0` of the result is all ones; the original features shift to columns
/// `1..=m`. Prepending (intercept at index 0) matches the common `w · [1, x]`
/// convention, so the first weight is the intercept.
///
/// # Errors
///
/// - [`MattenMlprepError::ExpectedMatrix`] if `x` is not rank-2.
/// - [`MattenMlprepError::DynamicTensor`] (with the `dynamic` feature) if `x` is dynamic.
///
/// ```
/// use matten::Tensor;
/// use matten_mlprep::add_bias_column;
///
/// let x = Tensor::new(vec![2.0, 3.0, 4.0, 5.0], &[2, 2]);
/// let b = add_bias_column(&x).unwrap();
/// assert_eq!(b.shape(), &[2, 3]);
/// // row 0 -> [1, 2, 3], row 1 -> [1, 4, 5]
/// assert_eq!(b.as_slice(), &[1.0, 2.0, 3.0, 1.0, 4.0, 5.0]);
/// ```