Fix transpose_mut for square matrices.
And add a test for it.
This commit is contained in:
parent
a862444c07
commit
e2c21c4ae2
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#![allow(missing_docs)] // we hide doc to not have to document the $trhs double dispatch trait.
|
#![allow(missing_docs)] // we hide doc to not have to document the $trhs double dispatch trait.
|
||||||
|
|
||||||
use std::{cmp, mem};
|
use std::cmp;
|
||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
use std::ops::{Add, Sub, Mul, Div, Index, IndexMut};
|
use std::ops::{Add, Sub, Mul, Div, Index, IndexMut};
|
||||||
use std::fmt::{Debug, Display, Formatter, Result};
|
use std::fmt::{Debug, Display, Formatter, Result};
|
||||||
|
@ -480,16 +480,15 @@ impl<N: Clone + Copy> Transpose for DMat<N> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn transpose_mut(&mut self) {
|
fn transpose_mut(&mut self) {
|
||||||
if self.nrows == self.ncols {
|
if self.nrows == self.ncols {
|
||||||
for i in 1..self.nrows {
|
let n = self.nrows;
|
||||||
for j in 0..self.ncols - 1 {
|
for i in 0..n - 1 {
|
||||||
|
for j in i + 1..n {
|
||||||
let off_i_j = self.offset(i, j);
|
let off_i_j = self.offset(i, j);
|
||||||
let off_j_i = self.offset(j, i);
|
let off_j_i = self.offset(j, i);
|
||||||
|
|
||||||
self.mij[..].swap(off_i_j, off_j_i);
|
self.mij[..].swap(off_i_j, off_j_i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mem::swap(&mut self.nrows, &mut self.ncols);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// FIXME: implement a better algorithm which does that in-place.
|
// FIXME: implement a better algorithm which does that in-place.
|
||||||
|
|
17
tests/mat.rs
17
tests/mat.rs
|
@ -3,7 +3,7 @@ extern crate rand;
|
||||||
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, PerspMat3, Ortho3,
|
use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, PerspMat3, Ortho3,
|
||||||
OrthoMat3, DMat, DVec, Row, Col, BaseFloat, Diag};
|
OrthoMat3, DMat, DVec, Row, Col, BaseFloat, Diag, Transpose, RowSlice};
|
||||||
|
|
||||||
macro_rules! test_inv_mat_impl(
|
macro_rules! test_inv_mat_impl(
|
||||||
($t: ty) => (
|
($t: ty) => (
|
||||||
|
@ -689,3 +689,18 @@ fn test_cholesky_mat5() {
|
||||||
fn test_cholesky_mat6() {
|
fn test_cholesky_mat6() {
|
||||||
test_cholesky_impl!(Mat6<f64>);
|
test_cholesky_impl!(Mat6<f64>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transpose_square_mat() {
|
||||||
|
let col_major_mat = &[0, 1, 2, 3,
|
||||||
|
0, 1, 2, 3,
|
||||||
|
0, 1, 2, 3,
|
||||||
|
0, 1, 2, 3];
|
||||||
|
let num_rows = 4;
|
||||||
|
let num_cols = 4;
|
||||||
|
let mut mat = DMat::from_col_vec(num_rows, num_cols, col_major_mat);
|
||||||
|
mat.transpose_mut();
|
||||||
|
for i in 0..num_rows {
|
||||||
|
assert_eq!(&[0, 1, 2, 3], mat.row_slice(i, 0, num_cols).as_slice());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue