forked from M-Labs/nalgebra
7d99015473
This should semantically be a no-op, but enables refactorings to use non-Copy scalars on a case-by-case basis. Also, the only instance of a `One + Zero` trait bound was changed into a `Zero + One` bound to match the others. The following sed scripts were used in the refactoring (with each clause added to reduce the error count of `cargo check`): ```bash export RELEVANT_SOURCEFILES="$(find src -name '*.rs') $(find examples -name '*.rs')" for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar,/N: Scalar+Copy,/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + Field/N: Scalar + Copy + Field/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + Zero/N: Scalar + Copy + Zero/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + Closed/N: Scalar + Copy + Closed/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + Eq/N: Scalar + Copy + Eq/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + PartialOrd/N: Scalar + Copy + PartialOrd/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: *Scalar + Zero/N: Scalar + Copy + Zero/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + PartialEq/N: Scalar + Copy + PartialEq/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar>/N: Scalar+Copy>/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: Scalar + $bound/N: Scalar + Copy + $bound/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: *Scalar + $bound/N: Scalar + Copy + $bound/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\): *Scalar,/N\1: Scalar+Copy,/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N: *Scalar + $trait/N: Scalar + Copy + $trait/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\): *Scalar + Superset/N\1: Scalar + Copy + Superset/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\): *Scalar + \([a-zA-Z]*Eq\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \([a-zA-Z]*Eq\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(hash::\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar {/N\1: Scalar + Copy {/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Zero\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Bounded\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Lattice\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Meet\|Join\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(fmt::\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Ring\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Hash\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Send\|Sync\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/One + Zero/Zero + One/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \(Zero\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar + \($marker\)/N\1: Scalar + Copy + \2/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/N\([0-9]\?\): *Scalar>/N\1: Scalar + Copy>/' $f; done for f in $RELEVANT_SOURCEFILES; do sed -i 's/Scalar+Copy/Scalar + Copy/' $f; done ```
122 lines
4.2 KiB
Rust
122 lines
4.2 KiB
Rust
use alga::general::ComplexField;
|
|
use crate::base::allocator::Allocator;
|
|
use crate::base::constraint::{AreMultipliable, DimEq, SameNumberOfRows, ShapeConstraint};
|
|
use crate::base::{DefaultAllocator, Matrix, Scalar, Unit, Vector};
|
|
use crate::dimension::{Dim, DimName, U1};
|
|
use crate::storage::{Storage, StorageMut};
|
|
|
|
use crate::geometry::Point;
|
|
|
|
/// A reflection wrt. a plane.
|
|
pub struct Reflection<N: Scalar + Copy, D: Dim, S: Storage<N, D>> {
|
|
axis: Vector<N, D, S>,
|
|
bias: N,
|
|
}
|
|
|
|
impl<N: ComplexField, D: Dim, S: Storage<N, D>> Reflection<N, D, S> {
|
|
/// Creates a new reflection wrt the plane orthogonal to the given axis and bias.
|
|
///
|
|
/// The bias is the position of the plane on the axis. In particular, a bias equal to zero
|
|
/// represents a plane that passes through the origin.
|
|
pub fn new(axis: Unit<Vector<N, D, S>>, bias: N) -> Self {
|
|
Self {
|
|
axis: axis.into_inner(),
|
|
bias,
|
|
}
|
|
}
|
|
|
|
/// Creates a new reflection wrt. the plane orthogonal to the given axis and that contains the
|
|
/// point `pt`.
|
|
pub fn new_containing_point(
|
|
axis: Unit<Vector<N, D, S>>,
|
|
pt: &Point<N, D>,
|
|
) -> Self
|
|
where
|
|
D: DimName,
|
|
DefaultAllocator: Allocator<N, D>,
|
|
{
|
|
let bias = axis.dotc(&pt.coords);
|
|
Self::new(axis, bias)
|
|
}
|
|
|
|
/// The reflexion axis.
|
|
pub fn axis(&self) -> &Vector<N, D, S> {
|
|
&self.axis
|
|
}
|
|
|
|
// FIXME: naming convention: reflect_to, reflect_assign ?
|
|
/// Applies the reflection to the columns of `rhs`.
|
|
pub fn reflect<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>)
|
|
where
|
|
S2: StorageMut<N, R2, C2>,
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
{
|
|
for i in 0..rhs.ncols() {
|
|
// NOTE: we borrow the column twice here. First it is borrowed immutably for the
|
|
// dot product, and then mutably. Somehow, this allows significantly
|
|
// better optimizations of the dot product from the compiler.
|
|
let m_two: N = crate::convert(-2.0f64);
|
|
let factor = (self.axis.dotc(&rhs.column(i)) - self.bias) * m_two;
|
|
rhs.column_mut(i).axpy(factor, &self.axis, N::one());
|
|
}
|
|
}
|
|
|
|
// FIXME: naming convention: reflect_to, reflect_assign ?
|
|
/// Applies the reflection to the columns of `rhs`.
|
|
pub fn reflect_with_sign<R2: Dim, C2: Dim, S2>(&self, rhs: &mut Matrix<N, R2, C2, S2>, sign: N)
|
|
where
|
|
S2: StorageMut<N, R2, C2>,
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
{
|
|
for i in 0..rhs.ncols() {
|
|
// NOTE: we borrow the column twice here. First it is borrowed immutably for the
|
|
// dot product, and then mutably. Somehow, this allows significantly
|
|
// better optimizations of the dot product from the compiler.
|
|
let m_two = sign.scale(crate::convert(-2.0f64));
|
|
let factor = (self.axis.dotc(&rhs.column(i)) - self.bias) * m_two;
|
|
rhs.column_mut(i).axpy(factor, &self.axis, sign);
|
|
}
|
|
}
|
|
|
|
/// Applies the reflection to the rows of `lhs`.
|
|
pub fn reflect_rows<R2: Dim, C2: Dim, S2, S3>(
|
|
&self,
|
|
lhs: &mut Matrix<N, R2, C2, S2>,
|
|
work: &mut Vector<N, R2, S3>,
|
|
) where
|
|
S2: StorageMut<N, R2, C2>,
|
|
S3: StorageMut<N, R2>,
|
|
ShapeConstraint: DimEq<C2, D> + AreMultipliable<R2, C2, D, U1>,
|
|
{
|
|
lhs.mul_to(&self.axis, work);
|
|
|
|
if !self.bias.is_zero() {
|
|
work.add_scalar_mut(-self.bias);
|
|
}
|
|
|
|
let m_two: N = crate::convert(-2.0f64);
|
|
lhs.gerc(m_two, &work, &self.axis, N::one());
|
|
}
|
|
|
|
/// Applies the reflection to the rows of `lhs`.
|
|
pub fn reflect_rows_with_sign<R2: Dim, C2: Dim, S2, S3>(
|
|
&self,
|
|
lhs: &mut Matrix<N, R2, C2, S2>,
|
|
work: &mut Vector<N, R2, S3>,
|
|
sign: N,
|
|
) where
|
|
S2: StorageMut<N, R2, C2>,
|
|
S3: StorageMut<N, R2>,
|
|
ShapeConstraint: DimEq<C2, D> + AreMultipliable<R2, C2, D, U1>,
|
|
{
|
|
lhs.mul_to(&self.axis, work);
|
|
|
|
if !self.bias.is_zero() {
|
|
work.add_scalar_mut(-self.bias);
|
|
}
|
|
|
|
let m_two = sign.scale(crate::convert(-2.0f64));
|
|
lhs.gerc(m_two, &work, &self.axis, sign);
|
|
}
|
|
}
|