Crates.io | ndarray-unit |
lib.rs | ndarray-unit |
version | 0.2.2 |
source | src |
created_at | 2019-12-01 03:03:24.686888 |
updated_at | 2019-12-04 01:39:35.684038 |
description | A package providing a system of units to the ndarray type |
homepage | https://crates.io/crates/ndarray-unit |
repository | https://github.com/politinsa/ndarray-unit/ |
max_upload_size | |
id | 185650 |
size | 1,303,614 |
:warning: This project is under active development and this README is using documentation comments that might be different than those uploaded on crates.io. Always check doc.rs for up-to-date documentation. :warning:
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.
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
// ==========================================================
The program will panic when you try to add or substract two ArrayUnit
s with different Unit
s.
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
Doc of the master branch on github.io
Transpose
Rescale
...