1

I am using nalgebra and I want to modify one matrix by setting it as the columns of another with compatible dimensions, like this:

    let zero: T = convert(0.0);
    let mut basis = DMatrix::from_element(rows, rank, zero);
    // matrix is an input with dimensions rows x cols
    for i in 0..location.len()
    {
        basis.column_mut(i) = matrix.column(location[i]);
    }

I have also tried dereferencing both sides of the assignment and looked for an "assign" method of some kind, no luck.

set_column doesn't work because DMatrix does not implement DimName

My current work around is this, but I don;t like it one bit:

 // Construct basis vectors initialized to 0.
    let zero: T = convert(0.0);
    let mut basis = DMatrix::from_element(rows, rank, zero);

    for i in 0..location.len()
    {
        // Copy the pivot column from the matrix.
        let col = location[i];
        for r in 0..matrix.nrows()
        {
            basis[(r, i)] = matrix[(r, col)];
        }
    }
Makogan
  • 8,208
  • 7
  • 44
  • 112
  • I can't imagine `T` is anything but a generic type parameter. That means this isn't a [mre]. Please provide one. – cafce25 Aug 18 '23 at 04:44
  • 1
    @cafce25 I can't imagine that it matters whether this is an MRE or not. They're clearly asking about syntax, and they've clearly demonstrated the semantics. – mcmuffin6o Aug 18 '23 at 06:10
  • @mcmuffin6o Well I'm fairly certain I know a way but I won't bother creating a MRE to check it. And I won't post a unverified answer either. Is it strictly necessary? - No. Would it improve the question? - Most definitely. So why not add it? – cafce25 Aug 18 '23 at 07:11
  • @cafce25 If you're not going to bother meeting a student where they're at, someone else will. If it's the quality of the question you're concerned about, go ahead and edit it based off of my answer. – mcmuffin6o Aug 18 '23 at 07:19
  • @cafce25 Strictly speaking, yes, you're correct -- they were so close to an MRE that they might as well have just properly made one. On the other hand, I'm so sick of everyone on this site being insufferable when questions aren't asked with perfect etiquette. In this case, it wasn't much work to tweak their code to be an MRE, especially when they provided an easily understood example of what they wanted, along with an easily understood workaround. They simply wanted to remove the row-by-row tedium of their workaround, and `set_column` does that (if you call it correctly). – mcmuffin6o Aug 18 '23 at 07:39
  • @mcmuffin6o I think you've misunderstood the purpose of this site. Please take the [tour] and read [ask] where it's explained. It's expressly not the purpose to help OP with a question they have. – cafce25 Aug 18 '23 at 07:44
  • @cafce25 "It's expressly not the purpose to help OP with a question they have." LMAO excuse me for trying to help OP with their question on a Q&A site. – mcmuffin6o Aug 18 '23 at 07:45

1 Answers1

0

I don't know what column_mut means, what it does, or how it's supposed to work, either. The documentation for it is fairly sparse (nonexistent). I think set_column does what you're trying to do (perhaps you called it incorrectly?):

use nalgebra::DMatrix;
let rows = 2;
let cols = 3;
let zero = 0.0;
let mut basis = DMatrix::from_element(rows, cols, zero);
let matrix = DMatrix::from_row_slice(rows, cols, &[
  1.0, 3.0, 5.0,
  2.0, 4.0, 6.0
]);
let location = [1, 0, 2];
for i in 0..location.len() {
  basis.set_column(i, &matrix.column(location[i]));
}

(I tweaked your code to be an MWE)

mcmuffin6o
  • 348
  • 1
  • 9
  • From the question "`set_column` doesn't work because `DMatrix` does not implement `DimName`" I think they've tried that. Unfortunately the question lacks the context to be certain. Which is why I asked for a MRE in the first place. – cafce25 Aug 18 '23 at 07:23
  • Have you tried running my answer? It gives the same result that he obtains in his workaround. – mcmuffin6o Aug 18 '23 at 07:27
  • How do you know the workaround doesn't compile so at best your statement is based on assumptions that you cannot prove. – cafce25 Aug 18 '23 at 07:31
  • @cafce25 Sometimes you have to put aside your religious adherence to procedures and actually get shit done. I did more for OP than you did, and it's gonna be funny to see a negative answer get accepted. – mcmuffin6o Aug 18 '23 at 07:49
  • @cafce25 And boom goes the dynamite, lmao – mcmuffin6o Aug 18 '23 at 08:05