Skip to main content

math_audio_optimisation/
stack_linear_penalty.rs

1use ndarray::{Array1, Array2};
2
3use crate::LinearPenalty;
4
5pub(crate) fn stack_linear_penalty(dst: &mut LinearPenalty, src: &LinearPenalty) {
6    // Vertically stack A, lb, ub; pick max weight to enforce strongest among merged
7    let a_dst = dst.a.clone();
8    let a_src = src.a.clone();
9    let rows = a_dst.nrows() + a_src.nrows();
10    let cols = a_dst.ncols();
11    assert_eq!(
12        cols,
13        a_src.ncols(),
14        "LinearPenalty A width mismatch while stacking"
15    );
16    let mut a_new = Array2::<f64>::zeros((rows, cols));
17    // copy
18    for i in 0..a_dst.nrows() {
19        for j in 0..cols {
20            a_new[(i, j)] = a_dst[(i, j)];
21        }
22    }
23    for i in 0..a_src.nrows() {
24        for j in 0..cols {
25            a_new[(a_dst.nrows() + i, j)] = a_src[(i, j)];
26        }
27    }
28    let mut lb_new = Array1::<f64>::zeros(rows);
29    let mut ub_new = Array1::<f64>::zeros(rows);
30    for i in 0..a_dst.nrows() {
31        lb_new[i] = dst.lb[i];
32        ub_new[i] = dst.ub[i];
33    }
34    for i in 0..a_src.nrows() {
35        lb_new[a_dst.nrows() + i] = src.lb[i];
36        ub_new[a_dst.nrows() + i] = src.ub[i];
37    }
38    dst.a = a_new;
39    dst.lb = lb_new;
40    dst.ub = ub_new;
41    dst.weight = dst.weight.max(src.weight);
42}