1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! This module provides a struct being a simple representation of the international measure system and an Enum containing all of the seven base unit of the Internation SI.    
//! It allows to multiply and divise unit between them and to print them.  
//! Several utility functions are provided to construct your own derived Unit from the base ones
//!
//! # Exemples
//!
//! ```
//! use ndarray_unit;
//! use ndarray_unit::{Unit, BaseUnit};
//!
//! let meter_per_sec = Unit::from_vec(vec![(BaseUnit::METER, 1), (BaseUnit::SECOND, -1)]);
//! println!("meter_per_sec = {}", meter_per_sec);
//!
//! let acceleration = &meter_per_sec / &ndarray_unit::get_second();
//! println!("acceleration = {}", acceleration);
//! ```
//! **Output**
//! ```
//! // meter_per_sec = m·s⁻¹
//! // acceleration = m·s⁻²
//! ```

#![crate_name = "ndarray_unit"]

mod unit;
pub use unit::BaseUnit;
pub use unit::Unit;

// Getters for base units

/// Utility method to get a Unit from a BaseUnit (BaseUnit::METER)
pub fn get_meter() -> Unit {
    Unit::from_vec(vec![(BaseUnit::METER, 1)])
}

/// Utility method to get a Unit from a BaseUnit (BaseUnit::SECOND)
pub fn get_second() -> Unit {
    Unit::from_vec(vec![(BaseUnit::SECOND, 1)])
}

/// Utility method to get a Unit from a BaseUnit (BaseUnit::CANDELA)
pub fn get_candela() -> Unit {
    Unit::from_vec(vec![(BaseUnit::CANDELA, 1)])
}

/// Utility method to get a Unit from a BaseUnit (BaseUnit::MOLE)
pub fn get_mole() -> Unit {
    Unit::from_vec(vec![(BaseUnit::MOLE, 1)])
}

/// Utility method to get a Unit from a BaseUnit (BaseUnit::KELVIN)
pub fn get_kelvin() -> Unit {
    Unit::from_vec(vec![(BaseUnit::KELVIN, 1)])
}

/// Utility method to get a Unit from a BaseUnit (BaseUnit::AMPERE)
pub fn get_ampere() -> Unit {
    Unit::from_vec(vec![(BaseUnit::AMPERE, 1)])
}

// Getters for composed units

/// Utility method to get the Joule Unit (composed)
pub fn get_newton() -> Unit {
    Unit::from_vec(vec![
        (BaseUnit::KILOGRAM, 1),
        (BaseUnit::METER, 1),
        (BaseUnit::SECOND, -2),
    ])
}

/// Utility method to get the Joule Unit (composed)
pub fn get_joule() -> Unit {
    &get_newton() * &get_meter()
}

/// Utility method to get the Watt Unit (composed)
pub fn get_watt() -> Unit {
    Unit::from_vec(vec![
        (BaseUnit::KILOGRAM, 1),
        (BaseUnit::METER, 2),
        (BaseUnit::SECOND, -3),
    ])
}

/// Utility method to get the Volt Unit (composed)
pub fn get_volt() -> Unit {
    &get_watt() * &get_ampere().get_inverse()
}

/// Utility method to get the Ohm Unit (composed)
pub fn get_ohm() -> Unit {
    &get_volt() / &get_ampere()
}

/// Utility method to get the Siemens Unit (composed)
pub fn get_siemens() -> Unit {
    get_ohm().get_inverse()
}

/// Utility metgod to get the Pascal Unit (composed)
pub fn get_pascal() -> Unit {
    Unit::from_vec(vec![
        (BaseUnit::KILOGRAM, 1),
        (BaseUnit::METER, -1),
        (BaseUnit::SECOND, -2),
    ])
}

/// Utility method to get the Coulomb Unit (composed)
pub fn get_coulomb() -> Unit {
    &get_ampere() * &get_second()
}

/// Utility method to get the Coulomb Unit (composed)
pub fn get_farad() -> Unit {
    &get_coulomb() / &get_volt()
}

/// Utility method to get the Henry Unit (composed)
pub fn get_henry() -> Unit {
    Unit::from_vec(vec![
        (BaseUnit::KILOGRAM, 1),
        (BaseUnit::METER, 2),
        (BaseUnit::SECOND, -2),
        (BaseUnit::AMPERE, -2),
    ])
}

/// Utility method to get the Weber Unit (composed)
pub fn get_weber() -> Unit {
    &get_volt() * &get_second()
}

/// Utility method to get the becquerel Unit (composed)
pub fn get_becquerel() -> Unit {
    get_second().get_inverse()
}

/// Utility method to get the Hertz Unit (composed)
pub fn get_hertz() -> Unit {
    get_second().get_inverse()
}

/// Utility method to get the Tesla Unit (composed)
pub fn get_tesla() -> Unit {
    Unit::from_vec(vec![
        (BaseUnit::KILOGRAM, 1),
        (BaseUnit::SECOND, -2),
        (BaseUnit::AMPERE, -1),
    ])
}


#[cfg(test)]
mod test;