Crate ndarray_unit

Source
Expand description

This crate provides a struct representing a multidimensionnal array together with a Unit.
It allows to do computations taking into account the unit of your n-dimensional array.

§Examples

use ndarray_unit::*;

extern crate ndarray;
use ndarray::Array;

fn main() {
    println!("meter / second = {}", &get_meter() / &get_second());

    let arr1 = Array::linspace(30.0, 40.0, 11);
    let arr_u1 = ArrayUnit::new(arr1, get_joule());

    let arr2 = Array::linspace(10.0, 60.0, 11);
    let arr_u2 = ArrayUnit::new(arr2, get_second());

    let arr3 = ndarray::array![
        [1.0, 0.0, 2.0, 6.0],
        [1.0, 2.0, 3.0, 5.0],
        [1.0, 2.0, 3.0, 6.0]
    ];
    let arr_u3 = ArrayUnit::new(arr3, get_meter());

    println!("arr_u3 = {}", arr_u3);
    println!("==========================================================");
    println!("{}\n*{}\n={}", &arr_u1, &arr_u2, &arr_u1 * &arr_u2);
    println!("==========================================================");
    println!("{}\n/{}\n={}", &arr_u1, &arr_u2, &arr_u1 / &arr_u2);
    println!("==========================================================");
    println!("{}\n+{}\n={}", &arr_u1, &arr_u1, &arr_u1 + &arr_u1);
    println!("==========================================================");
    println!("{}\n-{}\n={}", &arr_u2, &arr_u2, &arr_u2 - &arr_u2);
    println!("==========================================================");
}

Output

// meter / second = m·s⁻¹
// arr_u3 = [[1, 0, 2, 6],
//  [1, 2, 3, 5],
//  [1, 2, 3, 6]] m
// ==========================================================
// [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] m²·kg·s⁻²
// *[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60] s
// =[300, 465, 640, 825, 1020, 1225, 1440, 1665, 1900, 2145, 2400] m²·kg·s⁻¹
// ==========================================================
// [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] m²·kg·s⁻²
// /[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60] s
// =[3, 2.0666, 1.6, 1.32, 1.1333, 1, 0.9, 0.8222, 0.76, 0.7090, 0.6666] m²·kg·s⁻³
// ==========================================================
// [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] m²·kg·s⁻²
// +[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] m²·kg·s⁻²
// =[60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80] m²·kg·s⁻²
// ==========================================================
// [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60] s
// -[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60] s
// =[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] s
// ==========================================================

§Panics

The program will panic when you try to add or substract two ArrayUnits with different Units.

extern crate ndarray;
use ndarray::Array;
use ndarray_unit::*;

let arr1 = Array::linspace(30.0, 40.0, 11);
let arr_u1 = ArrayUnit::new(arr1, get_joule());

let arr2 = Array::linspace(10.0, 60.0, 11);
let arr_u2 = ArrayUnit::new(arr2, get_second());

// let result = &arr_u1 + &arr_u2; // ==> panicking

Structs§

ArrayUnit
Unit

Enums§

BaseUnit
An enum representing the seven units of the International System of Units

Functions§

get_ampere
Utility method to get a Unit from a BaseUnit (BaseUnit::AMPERE)
get_becquerel
Utility method to get the becquerel Unit (composed)
get_birth
get_candela
Utility method to get a Unit from a BaseUnit (BaseUnit::CANDELA)
get_coulomb
Utility method to get the Coulomb Unit (composed)
get_currency
get_death
get_farad
Utility method to get the Coulomb Unit (composed)
get_henry
Utility method to get the Henry Unit (composed)
get_hertz
Utility method to get the Hertz Unit (composed)
get_inhabitant
get_joule
Utility method to get the Joule Unit (composed)
get_kelvin
Utility method to get a Unit from a BaseUnit (BaseUnit::KELVIN)
get_meter
Utility method to get a Unit from a BaseUnit (BaseUnit::METER)
get_mole
Utility method to get a Unit from a BaseUnit (BaseUnit::MOLE)
get_newton
Utility method to get the Joule Unit (composed)
get_ohm
Utility method to get the Ohm Unit (composed)
get_pascal
Utility metgod to get the Pascal Unit (composed)
get_radian
Utility method to get a Unit from a BaseUnit (BaseUnit::RADIAN)
get_second
Utility method to get a Unit from a BaseUnit (BaseUnit::SECOND)
get_siemens
Utility method to get the Siemens Unit (composed)
get_steradian
Utility method to get a Unit from a BaseUnit (BaseUnit::STERADIAN)
get_tesla
Utility method to get the Tesla Unit (composed)
get_volt
Utility method to get the Volt Unit (composed)
get_watt
Utility method to get the Watt Unit (composed)
get_weber
Utility method to get the Weber Unit (composed)