Merge pull request #149 from tikue/master

Fix transpose_mut for square matrices.
This commit is contained in:
Sébastien Crozet 2015-08-19 21:38:54 +02:00
commit fdfd1bde78
2 changed files with 21 additions and 7 deletions

View File

@ -2,7 +2,7 @@
#![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::ops::{Add, Sub, Mul, Div, Index, IndexMut};
use std::fmt::{Debug, Display, Formatter, Result};
@ -480,16 +480,15 @@ impl<N: Clone + Copy> Transpose for DMat<N> {
#[inline]
fn transpose_mut(&mut self) {
if self.nrows == self.ncols {
for i in 1..self.nrows {
for j in 0..self.ncols - 1 {
let n = self.nrows;
for i in 0..n - 1 {
for j in i + 1..n {
let off_i_j = self.offset(i, j);
let off_j_i = self.offset(j, i);
self.mij[..].swap(off_i_j, off_j_i);
}
}
mem::swap(&mut self.nrows, &mut self.ncols);
}
else {
// FIXME: implement a better algorithm which does that in-place.

View File

@ -3,7 +3,7 @@ extern crate rand;
use rand::random;
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(
($t: ty) => (
@ -689,3 +689,18 @@ fn test_cholesky_mat5() {
fn test_cholesky_mat6() {
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());
}
}