Rename identity to one

Zero is the additive identity, so this disambiguates it
This commit is contained in:
Brendan Zabarauskas 2015-10-01 18:52:44 +10:00
parent af38e8a8be
commit b168c03174
7 changed files with 43 additions and 43 deletions

View file

@ -271,7 +271,7 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
fn zero() -> Self { Self::from_value(S::zero()) }
/// Create a matrix where the each element of the diagonal is equal to one.
#[inline]
fn identity() -> Self { Self::from_value(S::one()) }
fn one() -> Self { Self::from_value(S::one()) }
/// Multiply this matrix by a scalar, returning the new matrix.
#[must_use]
@ -348,7 +348,7 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
/// Test if this matrix is the identity matrix. That is, it is diagonal
/// and every element in the diagonal is one.
#[inline]
fn is_identity(&self) -> bool { self.approx_eq(&Self::identity()) }
fn is_one(&self) -> bool { self.approx_eq(&Self::one()) }
/// Test if this is a diagonal matrix. That is, every element outside of
/// the diagonal is 0.

View file

@ -64,7 +64,7 @@ impl<S: BaseFloat> Quaternion<S> {
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
#[inline]
pub fn identity() -> Quaternion<S> {
pub fn one() -> Quaternion<S> {
Quaternion::from_sv(S::one(), Vector3::zero())
}
@ -341,7 +341,7 @@ impl<S: BaseFloat> From<Quaternion<S>> for Basis3<S> {
impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Quaternion<S> {
#[inline]
fn identity() -> Quaternion<S> { Quaternion::identity() }
fn one() -> Quaternion<S> { Quaternion::one() }
#[inline]
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Quaternion<S> {

View file

@ -28,7 +28,7 @@ use vector::{Vector, Vector2, Vector3};
/// creates a circular motion, and preserves at least one point in the space.
pub trait Rotation<S: BaseFloat, V: Vector<S>, P: Point<S, V>>: PartialEq + ApproxEq<S> + Sized {
/// Create the identity transform (causes no transformation).
fn identity() -> Self;
fn one() -> Self;
/// Create a rotation to a given direction with an 'up' vector
fn look_at(dir: &V, up: &V) -> Self;
@ -181,7 +181,7 @@ impl<S: BaseFloat> From<Basis2<S>> for Matrix2<S> {
impl<S: BaseFloat + 'static> Rotation<S, Vector2<S>, Point2<S>> for Basis2<S> {
#[inline]
fn identity() -> Basis2<S> { Basis2{ mat: Matrix2::identity() } }
fn one() -> Basis2<S> { Basis2 { mat: Matrix2::one() } }
#[inline]
fn look_at(dir: &Vector2<S>, up: &Vector2<S>) -> Basis2<S> {
@ -262,7 +262,7 @@ impl<S: BaseFloat + 'static> From<Basis3<S>> for Quaternion<S> {
impl<S: BaseFloat + 'static> Rotation<S, Vector3<S>, Point3<S>> for Basis3<S> {
#[inline]
fn identity() -> Basis3<S> { Basis3{ mat: Matrix3::identity() } }
fn one() -> Basis3<S> { Basis3 { mat: Matrix3::one() } }
#[inline]
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Basis3<S> {

View file

@ -31,7 +31,7 @@ use vector::*;
pub trait Transform<S: BaseNum, V: Vector<S>, P: Point<S, V>>: Sized {
/// Create an identity transformation. That is, a transformation which
/// does nothing.
fn identity() -> Self;
fn one() -> Self;
/// Create a transformation that rotates a vector to look at `center` from
/// `eye`, using `up` for orientation.
@ -92,10 +92,10 @@ impl<
R: Rotation<S, V, P>,
> Transform<S, V, P> for Decomposed<S, V, R> {
#[inline]
fn identity() -> Decomposed<S, V, R> {
fn one() -> Decomposed<S, V, R> {
Decomposed {
scale: S::one(),
rot: R::identity(),
rot: R::one(),
disp: V::zero(),
}
}
@ -200,8 +200,8 @@ pub struct AffineMatrix3<S> {
impl<S: BaseFloat + 'static> Transform<S, Vector3<S>, Point3<S>> for AffineMatrix3<S> {
#[inline]
fn identity() -> AffineMatrix3<S> {
AffineMatrix3 { mat: Matrix4::identity() }
fn one() -> AffineMatrix3<S> {
AffineMatrix3 { mat: Matrix4::one() }
}
#[inline]
@ -262,7 +262,7 @@ impl<
R: Rotation<S, V, P> + Clone,
> ToComponents<S, V, P, R> for Decomposed<S, V, R> {
fn decompose(&self) -> (V, R, V) {
(V::identity().mul_s(self.scale), self.rot.clone(), self.disp.clone())
(V::one().mul_s(self.scale), self.rot.clone(), self.disp.clone())
}
}

View file

@ -18,7 +18,7 @@
//! ## Working with Vectors
//!
//! Vectors can be created in several different ways. There is, of course, the
//! traditional `new()` method, but unit vectors, zero vectors, and an identity
//! traditional `new()` method, but unit vectors, zero vectors, and an one
//! vector are also provided:
//!
//! ```rust
@ -41,7 +41,7 @@
//!
//! assert_eq!(&a + &b, Vector2::zero());
//! assert_eq!(-(&a * &b), Vector2::new(9.0f64, 16.0f64));
//! assert_eq!(&a / &Vector2::identity(), a);
//! assert_eq!(&a / &Vector2::one(), a);
//!
//! // As with Rust's `int` and `f32` types, Vectors of different types cannot
//! // be added and so on with impunity. The following will fail to compile:
@ -135,7 +135,7 @@ pub trait Vector<S: BaseNum>: Array1<S> + Clone // where
fn zero() -> Self { Self::from_value(S::zero()) }
/// The identity vector (with all components set to one)
#[inline]
fn identity() -> Self { Self::from_value(S::one()) }
fn one() -> Self { Self::from_value(S::one()) }
/// Add a scalar to this vector, returning a new vector.
#[must_use]

View file

@ -265,7 +265,7 @@ fn test_transpose() {
#[test]
fn test_invert() {
// Matrix2
assert!(Matrix2::<f64>::identity().invert().unwrap().is_identity());
assert!(Matrix2::<f64>::one().invert().unwrap().is_one());
assert_eq!(matrix2::A.invert().unwrap(),
Matrix2::new(-2.0f64, 1.5f64,
@ -277,7 +277,7 @@ fn test_invert() {
assert_eq!(mut_a, matrix2::A.invert().unwrap());
// Matrix3
assert!(Matrix3::<f64>::identity().invert().unwrap().is_identity());
assert!(Matrix3::<f64>::one().invert().unwrap().is_one());
assert_eq!(matrix3::A.invert(), None);
@ -290,7 +290,7 @@ fn test_invert() {
assert_eq!(mut_c, matrix3::C.invert().unwrap());
// Matrix4
assert!(Matrix4::<f64>::identity().invert().unwrap().is_identity());
assert!(Matrix4::<f64>::one().invert().unwrap().is_one());
assert!(matrix4::C.invert().unwrap().approx_eq(&
Matrix4::new( 5.0f64, -4.0f64, 1.0f64, 0.0f64,
@ -305,25 +305,25 @@ fn test_invert() {
-0., 0.631364f64, 0.775487f64, 0.0f64,
-0.991261f64, 0.1023f64, -0.083287f64, 0.0f64,
0., -1.262728f64, -1.550973f64, 1.0f64);
assert!(mat_c.invert().unwrap().mul_m(&mat_c).is_identity());
assert!(mat_c.invert().unwrap().mul_m(&mat_c).is_one());
let mat_d = Matrix4::new( 0.065455f64, -0.720002f64, 0.690879f64, 0.0f64,
-0., 0.692364f64, 0.721549f64, 0.0f64,
-0.997856f64, -0.047229f64, 0.045318f64, 0.0f64,
0., -1.384727f64, -1.443098f64, 1.0f64);
assert!(mat_d.invert().unwrap().mul_m(&mat_d).is_identity());
assert!(mat_d.invert().unwrap().mul_m(&mat_d).is_one());
let mat_e = Matrix4::new( 0.409936f64, 0.683812f64, -0.603617f64, 0.0f64,
0., 0.661778f64, 0.7497f64, 0.0f64,
0.912114f64, -0.307329f64, 0.271286f64, 0.0f64,
-0., -1.323555f64, -1.499401f64, 1.0f64);
assert!(mat_e.invert().unwrap().mul_m(&mat_e).is_identity());
assert!(mat_e.invert().unwrap().mul_m(&mat_e).is_one());
let mat_f = Matrix4::new(-0.160691f64, -0.772608f64, 0.614211f64, 0.0f64,
-0., 0.622298f64, 0.78278f64, 0.0f64,
-0.987005f64, 0.125786f64, -0.099998f64, 0.0f64,
0., -1.244597f64, -1.565561f64, 1.0f64);
assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_identity());
assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_one());
}
#[test]
@ -338,17 +338,17 @@ fn test_from_translation() {
fn test_predicates() {
// Matrix2
assert!(Matrix2::<f64>::identity().is_identity());
assert!(Matrix2::<f64>::identity().is_symmetric());
assert!(Matrix2::<f64>::identity().is_diagonal());
assert!(Matrix2::<f64>::identity().is_invertible());
assert!(Matrix2::<f64>::one().is_one());
assert!(Matrix2::<f64>::one().is_symmetric());
assert!(Matrix2::<f64>::one().is_diagonal());
assert!(Matrix2::<f64>::one().is_invertible());
assert!(!matrix2::A.is_identity());
assert!(!matrix2::A.is_one());
assert!(!matrix2::A.is_symmetric());
assert!(!matrix2::A.is_diagonal());
assert!(matrix2::A.is_invertible());
assert!(!matrix2::C.is_identity());
assert!(!matrix2::C.is_one());
assert!(matrix2::C.is_symmetric());
assert!(!matrix2::C.is_diagonal());
assert!(matrix2::C.is_invertible());
@ -357,17 +357,17 @@ fn test_predicates() {
// Matrix3
assert!(Matrix3::<f64>::identity().is_identity());
assert!(Matrix3::<f64>::identity().is_symmetric());
assert!(Matrix3::<f64>::identity().is_diagonal());
assert!(Matrix3::<f64>::identity().is_invertible());
assert!(Matrix3::<f64>::one().is_one());
assert!(Matrix3::<f64>::one().is_symmetric());
assert!(Matrix3::<f64>::one().is_diagonal());
assert!(Matrix3::<f64>::one().is_invertible());
assert!(!matrix3::A.is_identity());
assert!(!matrix3::A.is_one());
assert!(!matrix3::A.is_symmetric());
assert!(!matrix3::A.is_diagonal());
assert!(!matrix3::A.is_invertible());
assert!(!matrix3::D.is_identity());
assert!(!matrix3::D.is_one());
assert!(matrix3::D.is_symmetric());
assert!(!matrix3::D.is_diagonal());
assert!(matrix3::D.is_invertible());
@ -376,17 +376,17 @@ fn test_predicates() {
// Matrix4
assert!(Matrix4::<f64>::identity().is_identity());
assert!(Matrix4::<f64>::identity().is_symmetric());
assert!(Matrix4::<f64>::identity().is_diagonal());
assert!(Matrix4::<f64>::identity().is_invertible());
assert!(Matrix4::<f64>::one().is_one());
assert!(Matrix4::<f64>::one().is_symmetric());
assert!(Matrix4::<f64>::one().is_diagonal());
assert!(Matrix4::<f64>::one().is_invertible());
assert!(!matrix4::A.is_identity());
assert!(!matrix4::A.is_one());
assert!(!matrix4::A.is_symmetric());
assert!(!matrix4::A.is_diagonal());
assert!(!matrix4::A.is_invertible());
assert!(!matrix4::D.is_identity());
assert!(!matrix4::D.is_one());
assert!(matrix4::D.is_symmetric());
assert!(!matrix4::D.is_diagonal());
assert!(matrix4::D.is_invertible());

View file

@ -35,7 +35,7 @@ fn test_invert_basis2() {
let a: Basis2<_> = rotation::a2();
let a = a.concat(&a.invert());
let a: &Matrix2<_> = a.as_ref();
assert!(a.is_identity());
assert!(a.is_one());
}
#[test]
@ -43,5 +43,5 @@ fn test_invert_basis3() {
let a: Basis3<_> = rotation::a3();
let a = a.concat(&a.invert());
let a: &Matrix3<_> = a.as_ref();
assert!(a.is_identity());
assert!(a.is_one());
}