Mat2

Struct Mat2 

Source
pub struct Mat2 {
    pub a: f64,
    pub b: f64,
    pub c: f64,
    pub d: f64,
}
Expand description

A 2×2 matrix of f64 values.

The matrix is stored in row-major order:

| a  b |
| c  d |

§Examples

use lars::{Mat2, Vec2};

let m = Mat2::IDENTITY;
let v = Vec2::ONE;

assert_eq!(m * v, v);

Fields§

§a: f64

Top-left element.

§b: f64

Top-right element.

§c: f64

Bottom-left element.

§d: f64

Bottom-right element.

Implementations§

Source§

impl Mat2

Source

pub const fn new(a: f64, b: f64, c: f64, d: f64) -> Mat2

Source§

impl Mat2

Source

pub const IDENTITY: Mat2

The identity matrix:

| 1  0 |
| 0  1 |
Source

pub const ZERO: Mat2

The zero matrix:

| 0  0 |
| 0  0 |
Source

pub fn determinant(&self) -> f64

Returns the determinant of the matrix.

Computed as: [ \det(M) = ad - bc ]

§Examples
use lars::Mat2;
let m = Mat2::new(7.0, 2.0, 6.0, 2.0);
assert_eq!(m.determinant(), 2.0);
Source

pub fn inverse(&self) -> Mat2

Returns the inverse of the matrix, if it exists.

Computed as: [ M^{-1} = \frac{1}{\det(M)} \begin{bmatrix} d & -b \ -c & a \end{bmatrix} ]

§Panics

Panics if the matrix is singular (determinant = 0).

§Examples
use lars::Mat2;
let m = Mat2::new(7.0, 2.0, 6.0, 2.0);
assert_eq!(m.inverse(), Mat2::new(1.0, -1.0, -3.0, 3.5));

Trait Implementations§

Source§

impl Add for Mat2

Source§

type Output = Mat2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Mat2) -> Mat2

Performs the + operation. Read more
Source§

impl Clone for Mat2

Source§

fn clone(&self) -> Mat2

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mat2

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<__RhsT: Copy> Div<__RhsT> for Mat2
where f64: Div<__RhsT, Output = f64>,

Source§

type Output = Mat2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: __RhsT) -> Mat2

Performs the / operation. Read more
Source§

impl Mul<Mat2> for Vec2

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, m: Mat2) -> Vec2

Performs the * operation. Read more
Source§

impl Mul<Mat2> for f64

Implements scalar–matrix multiplication (f64 * Mat2).

§Examples

use lars::Mat2;
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(2.0 * m, Mat2::new(2.0, 4.0, 6.0, 8.0));
Source§

type Output = Mat2

The resulting type after applying the * operator.
Source§

fn mul(self, s: Mat2) -> Mat2

Performs the * operation. Read more
Source§

impl Mul<Vec2> for Mat2

Implements matrix–vector multiplication (Mat2 * Vec2).

Performs the linear transformation of the vector by the matrix.

§[ \begin{bmatrix} a & b \ c & d \end{bmatrix} \begin{bmatrix} x \ y \end{bmatrix}

\begin{bmatrix} ax + by \ cx + dy \end{bmatrix} ]

§Examples

use lars::{Mat2, Vec2};
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
let v = Vec2::new(1.0, 1.0);
assert_eq!(m * v, Vec2::new(3.0, 7.0));
Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vec2) -> Vec2

Performs the * operation. Read more
Source§

impl Mul<f64> for Mat2

Implements matrix–scalar multiplication (Mat2 * f64).

Each element of the matrix is scaled by the scalar.

§Examples

use lars::Mat2;
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(m * 2.0, Mat2::new(2.0, 4.0, 6.0, 8.0));
Source§

type Output = Mat2

The resulting type after applying the * operator.
Source§

fn mul(self, s: f64) -> Mat2

Performs the * operation. Read more
Source§

impl Mul for Mat2

Implements matrix–matrix multiplication (Mat2 * Mat2).

Standard linear algebra multiplication:

[ M_1 \times M_2 = \begin{bmatrix} a_1a_2 + b_1c_2 & a_1b_2 + b_1d_2 \ c_1a_2 + d_1c_2 & c_1b_2 + d_1d_2 \end{bmatrix} ]

§Examples

use lars::Mat2;
let a = Mat2::IDENTITY;
let b = Mat2::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(a * b, b);
Source§

type Output = Mat2

The resulting type after applying the * operator.
Source§

fn mul(self, m: Mat2) -> Mat2

Performs the * operation. Read more
Source§

impl PartialEq for Mat2

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Mat2

Source§

fn partial_cmp(&self, other: &Mat2) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for Mat2

Source§

type Output = Mat2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Mat2) -> Mat2

Performs the - operation. Read more
Source§

impl Copy for Mat2

Auto Trait Implementations§

§

impl Freeze for Mat2

§

impl RefUnwindSafe for Mat2

§

impl Send for Mat2

§

impl Sync for Mat2

§

impl Unpin for Mat2

§

impl UnwindSafe for Mat2

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.