Merge pull request #524 from shivshank/master
Add `push` method to Vector
This commit is contained in:
commit
570611a59b
|
@ -832,14 +832,7 @@ impl<N: Scalar + Zero, D: DimAdd<U1>, S: Storage<N, D>> Vector<N, D, S> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_homogeneous(&self) -> VectorN<N, DimSum<D, U1>>
|
pub fn to_homogeneous(&self) -> VectorN<N, DimSum<D, U1>>
|
||||||
where DefaultAllocator: Allocator<N, DimSum<D, U1>> {
|
where DefaultAllocator: Allocator<N, DimSum<D, U1>> {
|
||||||
let len = self.len();
|
self.push(N::zero())
|
||||||
let hnrows = DimSum::<D, U1>::from_usize(len + 1);
|
|
||||||
let mut res = unsafe { VectorN::<N, _>::new_uninitialized_generic(hnrows, U1) };
|
|
||||||
res.generic_slice_mut((0, 0), self.data.shape())
|
|
||||||
.copy_from(self);
|
|
||||||
res[(len, 0)] = N::zero();
|
|
||||||
|
|
||||||
res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Constructs a vector from coordinates in projective space, i.e., removes a `0` at the end of
|
/// Constructs a vector from coordinates in projective space, i.e., removes a `0` at the end of
|
||||||
|
@ -859,6 +852,22 @@ impl<N: Scalar + Zero, D: DimAdd<U1>, S: Storage<N, D>> Vector<N, D, S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<N: Scalar + Zero, D: DimAdd<U1>, S: Storage<N, D>> Vector<N, D, S> {
|
||||||
|
/// Constructs a new vector of higher dimension by appending `element` to the end of `self`.
|
||||||
|
#[inline]
|
||||||
|
pub fn push(&self, element: N) -> VectorN<N, DimSum<D, U1>>
|
||||||
|
where DefaultAllocator: Allocator<N, DimSum<D, U1>> {
|
||||||
|
let len = self.len();
|
||||||
|
let hnrows = DimSum::<D, U1>::from_usize(len + 1);
|
||||||
|
let mut res = unsafe { VectorN::<N, _>::new_uninitialized_generic(hnrows, U1) };
|
||||||
|
res.generic_slice_mut((0, 0), self.data.shape())
|
||||||
|
.copy_from(self);
|
||||||
|
res[(len, 0)] = element;
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<N, R: Dim, C: Dim, S> AbsDiffEq for Matrix<N, R, C, S>
|
impl<N, R: Dim, C: Dim, S> AbsDiffEq for Matrix<N, R, C, S>
|
||||||
where
|
where
|
||||||
N: Scalar + AbsDiffEq,
|
N: Scalar + AbsDiffEq,
|
||||||
|
|
|
@ -287,6 +287,18 @@ fn to_homogeneous() {
|
||||||
assert_eq!(d.to_homogeneous(), expected_d);
|
assert_eq!(d.to_homogeneous(), expected_d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn push() {
|
||||||
|
let a = Vector3::new(1.0, 2.0, 3.0);
|
||||||
|
let expected_a = Vector4::new(1.0, 2.0, 3.0, 4.0);
|
||||||
|
|
||||||
|
let b = DVector::from_row_slice(3, &[1.0, 2.0, 3.0]);
|
||||||
|
let expected_b = DVector::from_row_slice(4, &[1.0, 2.0, 3.0, 4.0]);
|
||||||
|
|
||||||
|
assert_eq!(a.push(4.0), expected_a);
|
||||||
|
assert_eq!(b.push(4.0), expected_b);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_add() {
|
fn simple_add() {
|
||||||
let a = Matrix2x3::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
|
let a = Matrix2x3::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
|
||||||
|
|
Loading…
Reference in New Issue