Fix the implementation of `Row` for fixed-size matrices.

Matrices are column-major.
This commit is contained in:
Sébastien Crozet 2014-09-19 23:51:27 +02:00
parent 1945cb09f2
commit 086088a0ed
2 changed files with 27 additions and 15 deletions

View File

@ -259,7 +259,7 @@ macro_rules! col_slice_impl(
macro_rules! row_impl( macro_rules! row_impl(
($t: ident, $tv: ident, $dim: expr) => ( ($t: ident, $tv: ident, $dim: expr) => (
impl<N: Clone> Row<$tv<N>> for $t<N> { impl<N: Clone + Zero> Row<$tv<N>> for $t<N> {
#[inline] #[inline]
fn nrows(&self) -> uint { fn nrows(&self) -> uint {
Dim::dim(None::<$t<N>>) Dim::dim(None::<$t<N>>)
@ -267,12 +267,20 @@ macro_rules! row_impl(
#[inline] #[inline]
fn set_row(&mut self, row: uint, v: $tv<N>) { fn set_row(&mut self, row: uint, v: $tv<N>) {
self[row] = v; for (i, e) in v.iter().enumerate() {
self.set((row, i), e.clone());
}
} }
#[inline] #[inline]
fn row(&self, row: uint) -> $tv<N> { fn row(&self, row: uint) -> $tv<N> {
self[row].clone() let mut res: $tv<N> = Zero::zero();
for (i, e) in res.mut_iter().enumerate() {
*e = self.at((row, i));
}
res
} }
} }
) )
@ -292,7 +300,7 @@ macro_rules! row_slice_impl(
macro_rules! col_impl( macro_rules! col_impl(
($t: ident, $tv: ident, $dim: expr) => ( ($t: ident, $tv: ident, $dim: expr) => (
impl<N: Clone + Zero> Col<$tv<N>> for $t<N> { impl<N: Clone> Col<$tv<N>> for $t<N> {
#[inline] #[inline]
fn ncols(&self) -> uint { fn ncols(&self) -> uint {
Dim::dim(None::<$t<N>>) Dim::dim(None::<$t<N>>)
@ -300,20 +308,12 @@ macro_rules! col_impl(
#[inline] #[inline]
fn set_col(&mut self, col: uint, v: $tv<N>) { fn set_col(&mut self, col: uint, v: $tv<N>) {
for (i, e) in v.iter().enumerate() { self[col] = v;
self.set((i, col), e.clone());
}
} }
#[inline] #[inline]
fn col(&self, col: uint) -> $tv<N> { fn col(&self, col: uint) -> $tv<N> {
let mut res: $tv<N> = Zero::zero(); self[col].clone()
for (i, e) in res.mut_iter().enumerate() {
*e = self.at((i, col));
}
res
} }
} }
) )

View File

@ -1,6 +1,6 @@
use std::num::{Float, abs}; use std::num::{Float, abs};
use std::rand::random; use std::rand::random;
use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot3, DMat, DVec, Indexable}; use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot3, DMat, DVec, Indexable, Row, Col};
use na; use na;
use std::cmp::{min, max}; use std::cmp::{min, max};
@ -330,3 +330,15 @@ fn test_from_fn() {
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test]
fn test_row_3() {
let mat = Mat3::new(0.0f32, 1.0, 2.0,
3.0, 4.0, 5.0,
6.0, 7.0, 8.0);
let second_row = mat.row(1);
let second_col = mat.col(1);
assert!(second_row == Vec3::new(3.0, 4.0, 5.0));
assert!(second_col == Vec3::new(1.0, 4.0, 7.0));
}