#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

#883 in Math

Download history 135/week @ 2024-07-29 1/week @ 2024-08-05 223/week @ 2024-08-12 132/week @ 2024-08-19 79/week @ 2024-09-09 587/week @ 2024-09-16 308/week @ 2024-09-23 158/week @ 2024-09-30 125/week @ 2024-10-07 63/week @ 2024-10-14 36/week @ 2024-10-21 64/week @ 2024-10-28 139/week @ 2024-11-04 79/week @ 2024-11-11

321 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