pounce-linalg 0.2.0

Linear algebra primitives for POUNCE (port of Ipopt's src/LinAlg): BLAS-1, Vector and Matrix abstractions, dense vectors/matrices, compound vectors/matrices, expansion and scaling matrices, triplet storage, triplet→CSC conversion.
Documentation
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Diagonal-scaling wrappers.
//!
//! Mirrors `LinAlg/IpScaledMatrix.{hpp,cpp}` and
//! `IpSymScaledMatrix.{hpp,cpp}`. `ScaledMatrix` represents
//! `diag(r) · A · diag(c)` where `r` and `c` are optional row/column
//! scaling vectors. `SymScaledMatrix` is the symmetric refinement,
//! `diag(d) · A · diag(d)`.
//!
//! `compute_row_amax_impl` / `compute_col_amax_impl` panic to mirror
//! upstream `THROW_EXCEPTION(UNIMPLEMENTED_LINALG_METHOD_CALLED, ...)`.

use crate::matrix::{
    sym_default_compute_col_amax_impl, sym_default_trans_mult_vector_impl, Matrix, MatrixCache,
    SymMatrix,
};
use crate::vector::Vector;
use pounce_common::tagged::{Tag, TaggedObject};
use pounce_common::types::{Index, Number};
use std::any::Any;
use std::rc::Rc;

// ---- ScaledMatrix ----

/// Reciprocal flag for [`ScaledMatrixSpace::new`].
#[derive(Copy, Clone, Debug)]
pub enum ScalingReciprocal {
    Direct,
    Reciprocal,
}

/// Scaling-vector ownership for a [`ScaledMatrix`]. Optional because
/// upstream allows the row or column scaling to be NULL — meaning that
/// side is not scaled at all.
#[derive(Debug)]
pub struct ScaledMatrixSpace {
    n_rows: Index,
    n_cols: Index,
    row_scaling: Option<Rc<dyn Vector>>,
    col_scaling: Option<Rc<dyn Vector>>,
}

impl ScaledMatrixSpace {
    /// Build a scaling-space. The reciprocal flags follow upstream's
    /// constructor: when set, the scaling vector is *copied and
    /// element-wise-reciprocated* up front, so matrix-vector products
    /// then just multiply (no per-call reciprocal).
    pub fn new(
        n_rows: Index,
        n_cols: Index,
        row_scaling: Option<Rc<dyn Vector>>,
        row_recip: ScalingReciprocal,
        col_scaling: Option<Rc<dyn Vector>>,
        col_recip: ScalingReciprocal,
    ) -> Rc<Self> {
        let row = row_scaling.map(|r| {
            let mut copy = r.make_new_copy();
            if matches!(row_recip, ScalingReciprocal::Reciprocal) {
                copy.element_wise_reciprocal();
            }
            // Box<dyn Vector> → Rc<dyn Vector>; storage handoff.
            let r: Rc<dyn Vector> = Rc::from(copy);
            r
        });
        let col = col_scaling.map(|c| {
            let mut copy = c.make_new_copy();
            if matches!(col_recip, ScalingReciprocal::Reciprocal) {
                copy.element_wise_reciprocal();
            }
            let c: Rc<dyn Vector> = Rc::from(copy);
            c
        });
        Rc::new(Self {
            n_rows,
            n_cols,
            row_scaling: row,
            col_scaling: col,
        })
    }

    pub fn row_scaling(&self) -> Option<&Rc<dyn Vector>> {
        self.row_scaling.as_ref()
    }
    pub fn col_scaling(&self) -> Option<&Rc<dyn Vector>> {
        self.col_scaling.as_ref()
    }
    pub fn n_rows(&self) -> Index {
        self.n_rows
    }
    pub fn n_cols(&self) -> Index {
        self.n_cols
    }
}

#[derive(Debug)]
pub struct ScaledMatrix {
    space: Rc<ScaledMatrixSpace>,
    matrix: Option<Rc<dyn Matrix>>,
    cache: MatrixCache,
}

impl ScaledMatrix {
    pub fn new(space: Rc<ScaledMatrixSpace>) -> Self {
        Self {
            space,
            matrix: None,
            cache: MatrixCache::new(),
        }
    }

    pub fn set_unscaled(&mut self, m: Rc<dyn Matrix>) {
        debug_assert_eq!(m.n_rows(), self.space.n_rows);
        debug_assert_eq!(m.n_cols(), self.space.n_cols);
        self.matrix = Some(m);
        self.cache.bump();
    }

    pub fn unscaled(&self) -> Option<&Rc<dyn Matrix>> {
        self.matrix.as_ref()
    }

    fn inner(&self) -> &dyn Matrix {
        match self.matrix.as_ref() {
            Some(m) => m.as_ref(),
            None => panic!("ScaledMatrix unscaled matrix unset — call set_unscaled before use"),
        }
    }
}

impl TaggedObject for ScaledMatrix {
    fn get_tag(&self) -> Tag {
        self.cache.tag()
    }
}

impl Matrix for ScaledMatrix {
    fn n_rows(&self) -> Index {
        self.space.n_rows
    }
    fn n_cols(&self) -> Index {
        self.space.n_cols
    }
    fn cache(&self) -> &MatrixCache {
        &self.cache
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
    fn as_tagged(&self) -> &dyn TaggedObject {
        self
    }
    fn as_dyn_matrix(&self) -> &dyn Matrix {
        self
    }

    fn mult_vector_impl(&self, alpha: Number, x: &dyn Vector, beta: Number, y: &mut dyn Vector) {
        // Upstream sequence (IpScaledMatrix.cpp:22-58):
        // 1. y *= beta (or y = 0)
        // 2. tmp_x = x.MakeNewCopy()
        // 3. if col scaling: tmp_x .*= col_scaling
        // 4. tmp_y = y.MakeNew(); A * tmp_x → tmp_y (beta=0)
        // 5. if row scaling: tmp_y .*= row_scaling
        // 6. y.Axpy(alpha, tmp_y)
        if beta != 0.0 {
            y.scal(beta);
        } else {
            y.set(0.0);
        }
        let mut tmp_x = x.make_new_copy();
        let mut tmp_y = y.make_new();
        if let Some(c) = self.space.col_scaling() {
            tmp_x.element_wise_multiply(c.as_ref());
        }
        self.inner()
            .mult_vector(1.0, tmp_x.as_dyn_vector(), 0.0, tmp_y.as_mut());
        if let Some(r) = self.space.row_scaling() {
            tmp_y.element_wise_multiply(r.as_ref());
        }
        y.axpy(alpha, tmp_y.as_dyn_vector());
    }

    fn trans_mult_vector_impl(
        &self,
        alpha: Number,
        x: &dyn Vector,
        beta: Number,
        y: &mut dyn Vector,
    ) {
        if beta != 0.0 {
            y.scal(beta);
        } else {
            y.set(0.0);
        }
        let mut tmp_x = x.make_new_copy();
        let mut tmp_y = y.make_new();
        if let Some(r) = self.space.row_scaling() {
            tmp_x.element_wise_multiply(r.as_ref());
        }
        self.inner()
            .trans_mult_vector(1.0, tmp_x.as_dyn_vector(), 0.0, tmp_y.as_mut());
        if let Some(c) = self.space.col_scaling() {
            tmp_y.element_wise_multiply(c.as_ref());
        }
        y.axpy(alpha, tmp_y.as_dyn_vector());
    }

    fn has_valid_numbers_impl(&self) -> bool {
        self.inner().has_valid_numbers()
    }

    fn compute_row_amax_impl(&self, _rows_norms: &mut dyn Vector, _init: bool) {
        panic!("ScaledMatrix::compute_row_amax not implemented (matches upstream UNIMPLEMENTED_LINALG_METHOD_CALLED)");
    }

    fn compute_col_amax_impl(&self, _cols_norms: &mut dyn Vector, _init: bool) {
        panic!("ScaledMatrix::compute_col_amax not implemented (matches upstream UNIMPLEMENTED_LINALG_METHOD_CALLED)");
    }
}

// ---- SymScaledMatrix ----

#[derive(Debug)]
pub struct SymScaledMatrixSpace {
    dim: Index,
    row_col_scaling: Option<Rc<dyn Vector>>,
}

impl SymScaledMatrixSpace {
    pub fn new(
        dim: Index,
        row_col_scaling: Option<Rc<dyn Vector>>,
        recip: ScalingReciprocal,
    ) -> Rc<Self> {
        let s = row_col_scaling.map(|s| {
            let mut copy = s.make_new_copy();
            if matches!(recip, ScalingReciprocal::Reciprocal) {
                copy.element_wise_reciprocal();
            }
            let s: Rc<dyn Vector> = Rc::from(copy);
            s
        });
        Rc::new(Self {
            dim,
            row_col_scaling: s,
        })
    }

    pub fn row_col_scaling(&self) -> Option<&Rc<dyn Vector>> {
        self.row_col_scaling.as_ref()
    }
    pub fn dim(&self) -> Index {
        self.dim
    }
}

#[derive(Debug)]
pub struct SymScaledMatrix {
    space: Rc<SymScaledMatrixSpace>,
    matrix: Option<Rc<dyn Matrix>>,
    cache: MatrixCache,
}

impl SymScaledMatrix {
    pub fn new(space: Rc<SymScaledMatrixSpace>) -> Self {
        Self {
            space,
            matrix: None,
            cache: MatrixCache::new(),
        }
    }

    pub fn set_unscaled(&mut self, m: Rc<dyn Matrix>) {
        debug_assert_eq!(m.n_rows(), self.space.dim);
        debug_assert_eq!(m.n_cols(), self.space.dim);
        self.matrix = Some(m);
        self.cache.bump();
    }

    fn inner(&self) -> &dyn Matrix {
        match self.matrix.as_ref() {
            Some(m) => m.as_ref(),
            None => panic!("SymScaledMatrix unscaled matrix unset — call set_unscaled before use"),
        }
    }
}

impl TaggedObject for SymScaledMatrix {
    fn get_tag(&self) -> Tag {
        self.cache.tag()
    }
}

impl Matrix for SymScaledMatrix {
    fn n_rows(&self) -> Index {
        self.space.dim
    }
    fn n_cols(&self) -> Index {
        self.space.dim
    }
    fn cache(&self) -> &MatrixCache {
        &self.cache
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
    fn as_tagged(&self) -> &dyn TaggedObject {
        self
    }
    fn as_dyn_matrix(&self) -> &dyn Matrix {
        self
    }

    fn mult_vector_impl(&self, alpha: Number, x: &dyn Vector, beta: Number, y: &mut dyn Vector) {
        if beta != 0.0 {
            y.scal(beta);
        } else {
            y.set(0.0);
        }
        let mut tmp_x = x.make_new_copy();
        let mut tmp_y = y.make_new();
        if let Some(s) = self.space.row_col_scaling() {
            tmp_x.element_wise_multiply(s.as_ref());
        }
        self.inner()
            .mult_vector(1.0, tmp_x.as_dyn_vector(), 0.0, tmp_y.as_mut());
        if let Some(s) = self.space.row_col_scaling() {
            tmp_y.element_wise_multiply(s.as_ref());
        }
        y.axpy(alpha, tmp_y.as_dyn_vector());
    }

    fn trans_mult_vector_impl(
        &self,
        alpha: Number,
        x: &dyn Vector,
        beta: Number,
        y: &mut dyn Vector,
    ) {
        sym_default_trans_mult_vector_impl(self, alpha, x, beta, y);
    }

    fn has_valid_numbers_impl(&self) -> bool {
        self.inner().has_valid_numbers()
    }

    fn compute_row_amax_impl(&self, _rows_norms: &mut dyn Vector, _init: bool) {
        panic!("SymScaledMatrix::compute_row_amax not implemented (matches upstream UNIMPLEMENTED_LINALG_METHOD_CALLED)");
    }

    fn compute_col_amax_impl(&self, cols_norms: &mut dyn Vector, init: bool) {
        sym_default_compute_col_amax_impl(self, cols_norms, init);
    }
}

impl SymMatrix for SymScaledMatrix {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dense_vector::DenseVectorSpace;
    use crate::special_matrix::IdentityMatrix;
    use crate::DenseVector;

    fn dvec_box(values: &[Number]) -> Box<dyn Vector> {
        let space = DenseVectorSpace::new(values.len() as Index);
        let mut v = space.make_new_dense();
        v.set_values(values);
        Box::new(v)
    }

    fn rc_dvec(values: &[Number]) -> Rc<dyn Vector> {
        let space = DenseVectorSpace::new(values.len() as Index);
        let mut v = space.make_new_dense();
        v.set_values(values);
        Rc::new(v)
    }

    #[test]
    fn scaled_matrix_applies_row_and_column_scaling() {
        // R diag(r) * I_factor=1 * diag(c) * x = r .* (c .* x)
        let r = rc_dvec(&[2.0, 3.0]);
        let c = rc_dvec(&[5.0, 7.0]);
        let space = ScaledMatrixSpace::new(
            2,
            2,
            Some(r),
            ScalingReciprocal::Direct,
            Some(c),
            ScalingReciprocal::Direct,
        );
        let mut m = ScaledMatrix::new(space);
        let inner: Rc<dyn Matrix> = Rc::new(IdentityMatrix::new(2));
        m.set_unscaled(inner);
        let x = dvec_box(&[1.0, 1.0]);
        let mut y = dvec_box(&[0.0, 0.0]);
        m.mult_vector(1.0, x.as_dyn_vector(), 0.0, y.as_mut());
        // Expected: r .* (c .* x) = [2,3] .* [5,7] = [10, 21]
        let dv = y.as_any().downcast_ref::<DenseVector>().unwrap();
        assert_eq!(dv.expanded_values().to_vec(), vec![10.0, 21.0]);
    }

    #[test]
    fn scaled_matrix_with_no_scaling_is_identity_wrapped() {
        let space = ScaledMatrixSpace::new(
            2,
            2,
            None,
            ScalingReciprocal::Direct,
            None,
            ScalingReciprocal::Direct,
        );
        let mut m = ScaledMatrix::new(space);
        let inner: Rc<dyn Matrix> = Rc::new(IdentityMatrix::new(2));
        m.set_unscaled(inner);
        let x = dvec_box(&[3.0, 4.0]);
        let mut y = dvec_box(&[10.0, 20.0]);
        m.mult_vector(2.0, x.as_dyn_vector(), 0.5, y.as_mut());
        // y ← 2*I*x + 0.5 y = [6,8] + [5,10] = [11,18]
        let dv = y.as_any().downcast_ref::<DenseVector>().unwrap();
        assert_eq!(dv.expanded_values().to_vec(), vec![11.0, 18.0]);
    }

    #[test]
    fn sym_scaled_matrix_doubles_scaling() {
        // diag(d) I diag(d) x = d .* d .* x
        let d = rc_dvec(&[2.0, 3.0]);
        let space = SymScaledMatrixSpace::new(2, Some(d), ScalingReciprocal::Direct);
        let mut m = SymScaledMatrix::new(space);
        let inner: Rc<dyn Matrix> = Rc::new(IdentityMatrix::new(2));
        m.set_unscaled(inner);
        let x = dvec_box(&[1.0, 1.0]);
        let mut y = dvec_box(&[0.0, 0.0]);
        m.mult_vector(1.0, x.as_dyn_vector(), 0.0, y.as_mut());
        // Expected: [4, 9]
        let dv = y.as_any().downcast_ref::<DenseVector>().unwrap();
        assert_eq!(dv.expanded_values().to_vec(), vec![4.0, 9.0]);
    }
}