-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathuom.rs
More file actions
29 lines (26 loc) · 981 Bytes
/
uom.rs
File metadata and controls
29 lines (26 loc) · 981 Bytes
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
use ndarray::prelude::*;
use ninterp::prelude::*;
use uom::si::f64::Ratio;
use uom::si::ratio::ratio;
use uom::si::f64::Power;
use uom::si::power::kilowatt;
fn main() {
let x = array![Ratio::new::<ratio>(0.), Ratio::new::<ratio>(1.)];
let f_x = array![Power::new::<kilowatt>(0.25), Power::new::<kilowatt>(0.75)];
// `uom::si::Quantity` is repr(transparent), meaning it has the same memory layout as its contained type.
// This means we can get the contained type via transmuting.
let interp: Interp1DViewed<&f64, _> = unsafe {
Interp1D::new(
std::mem::transmute(x.view()),
std::mem::transmute(f_x.view()),
strategy::Linear,
Extrapolate::Error,
)
.unwrap()
};
let output = interp.interpolate(&[0.5]).unwrap();
// Note the result is not 0.5
assert!(output != 0.5);
// It is instead returned in the base units of f_x `Power`, i.e. 500 W
assert_eq!(output, 500.);
}