AtheMathmo / rulinalg

A linear algebra library written in Rust

Home Page:https://crates.io/crates/rulinalg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Right-multiplication by permutation matrix is inconsistent with representation

gvissers opened this issue · comments

Right multiplication of a matrix by a permutation permutes the columns of said matrix. The permutation that is performed however, corresponds to the transpose (inverse) of the permutation matrix, which leads to unexpected results. Consider the simple sample program:

extern crate rulinalg;

fn main()
{
    let p = rulinalg::matrix::PermutationMatrix::from_array(vec![1, 2, 0]).unwrap();
    let i = rulinalg::matrix::Matrix::<u32>::identity(3);

    println!("p.as_matrix() =\n{}", p.as_matrix());
    println!("p*i =\n{}", &p * &i);
    println!("i*p =\n{}", &i * &p);
}

The output of this program is

p.as_matrix() =
⎡0 0 1⎤
⎢1 0 0⎥
⎣0 1 0⎦
p*i =
⎡0 0 1⎤
⎢1 0 0⎥
⎣0 1 0⎦
i*p =
⎡0 1 0⎤
⎢0 0 1⎥
⎣1 0 0⎦

So left multiplying the identity matrix by a permutation gives its matrix representation (as per as_matrix()), but right multiplication results in the transpose matrix.

I can see how this happens (the same permutation is simply applied to the columns when right-multiplying), but for matrix multiplication this is not what I would expect.