#linear-algebra #numerical-methods #vector-math #geometry

linear_isomorphic

A set of traits to astract over linear-like types

8 releases

0.2.4 Aug 20, 2024
0.2.2 Aug 14, 2024
0.2.0 Jul 29, 2024
0.1.3 Jun 23, 2024

#813 in Math

Download history 146/week @ 2024-08-17 9/week @ 2024-08-24 37/week @ 2024-09-07 576/week @ 2024-09-14 359/week @ 2024-09-21 151/week @ 2024-09-28 127/week @ 2024-10-05 66/week @ 2024-10-12 34/week @ 2024-10-19 65/week @ 2024-10-26 137/week @ 2024-11-02 74/week @ 2024-11-09 127/week @ 2024-11-16 259/week @ 2024-11-23 173/week @ 2024-11-30

668 downloads per month

MIT/Apache

8KB
135 lines

Linear Isomorphic

This crate defines a trait to identify when types can be treated as linear objects. Many algorithms can be written generically over many types but often times crates implement their own linear algebra types over and over again to guarantee some kind of functionality.

This crate aims to allow algorithms to be agnostic about the specific representation of vector types. For example, many geoemtric operations can be performed without knowledge of the dimension or vector type:

use linear_isomorphic::*;

pub fn point_segment_distance<Vec>(start: &Vec, end: &Vec, point: &Vec) -> f32
where
    Vec: VectorSpace<Scalar = f32>,
{
    let dir = *end - *start;
    let t = (*point - *start).dot(&dir) / dir.norm_squared();

    let t = t.clamp(0.0, 1.0);

    let closest = *start + dir * t;

    (closest - *point).norm()
}

Or even numerical multivariate calculus:

use std::{
    fmt::Debug,
    ops::{AddAssign, Div, Mul},
};
use linear_isomorphic::*;

pub fn numerical_derivative<T, F>(x: f32, f: &F) -> T
where
    T: LinAlg<f32>,
    f32: Mul<T, Output = T>,
    F: Fn(f32) -> T,
{
    let mul = <f32 as Mul<f32>>::mul;
    let h = mul(1e-3, f32::max(f32::abs(x), 1.0));
    
    let denom = h + h;
    let num = f(x + h) - f(x - h);

    <T as Div<f32>>::div(num, denom)
}

Suggestions to make the traits defined in this crate more robust are always welcomed.

Dependencies

~465KB