#vector #multi-dimensional #tuple #slice #iterator #type #dot

mdmath_core

Multidimensional math, fundamental functionality and types

2 unstable releases

0.2.0 Oct 28, 2024
0.1.0 Sep 17, 2024

#2871 in Rust patterns

Download history 155/week @ 2024-09-16 12/week @ 2024-09-23 7/week @ 2024-09-30 144/week @ 2024-10-28

144 downloads per month
Used in ndarray_cg

MIT license

78KB
2K SLoC

mdmath_core

Fundamental functionality and types, representation slices and tuples as vectors.

Implemented Features

  • Vector:
    • Operations:
      • Dot product of two vectors.
      • Magnitude of the vector.
      • Normalizing the vector.
      • Projection on another vector.
      • Angle beetween two vectors.
      • Orthogonal checking between two vectors.
      • Dimension offset of the vector.
    • Mut/Unmut ref from slice and tuple.
    • Mut/Unmut iterators.

Installation

Add to your example [dependencies] in Cargo.toml configuration file:

mdmath_core = { workspace = true }

Examples

Dot product of two vectors

  let vec_a = [ 1.0, 2.0, 3.0 ];
  let vec_b = [ 4.0, 5.0, 6.0 ];
  let result = mdmath_core::vector::dot( &vec_a, &vec_b );
  assert_eq!( result, 32.0 );

Magnitude of the vector

  let vec_a = [ 1.0, 2.0, 3.0 ];
  let result = mdmath_core::vector::mag2( &vec_a );
  assert_ulps_eq!( result, 14.0 );

Normalizing the vector

  let vec_a = [ 3.0, 4.0 ];
  let mut result = vec_a.clone();
  mdmath_core::vector::normalize( &mut result, &vec_a );
  let expected = [ 0.6, 0.8 ];
  assert_eq!( result, expected );

Projection on another vector

  let mut vec_a = [ 1.0, 2.0, 3.0 ];
  let vec_b = [ 4.0, 5.0, 6.0 ];
  mdmath_core::vector::project_on( &mut vec_a, &vec_b );
  let expected = [ 1.6623376623376624, 2.077922077922078, 2.4935064935064934 ];
  assert_eq!( vec_a, expected );

Angle beetween two vectors

  let vec_a = [ 1.0, 0.0 ];
  let vec_b = [ 0.0, 1.0 ];
  let result = mdmath_core::vector::angle( &vec_a, &vec_b );
  assert_ulps_eq!( result, std::f32::consts::FRAC_PI_2 );

Orthogonal checking between two vectors

  let vec_a = [ 1.0, 0.0 ];
  let vec_b = [ 0.0, 1.0 ];
  assert!( mdmath_core::vector::is_orthogonal( &vec_a, &vec_b ), "Orthogonal test failed for orthogonal vectors" );

Dependencies

~0–0.9MB
~16K SLoC