-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from NREL/f3/ninterp-methods
Convenience methods for ninterp
- Loading branch information
Showing
3 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::imports::*; | ||
|
||
/// Methods for proportionally scaling interpolator function data | ||
pub trait InterpolatorMethods { | ||
fn set_min(&mut self, min: f64) -> anyhow::Result<()>; | ||
fn set_max(&mut self, max: f64) -> anyhow::Result<()>; | ||
fn set_range(&mut self, range: f64) -> anyhow::Result<()>; | ||
} | ||
|
||
impl InterpolatorMethods for Interpolator { | ||
fn set_min(&mut self, min: f64) -> anyhow::Result<()> { | ||
let old_min = self.min()?; | ||
match self { | ||
Interpolator::Interp0D(value) => Ok(*value = min), | ||
Interpolator::Interp1D(interp) => { | ||
todo!() | ||
} | ||
Interpolator::Interp2D(interp) => { | ||
todo!() | ||
} | ||
Interpolator::Interp3D(interp) => { | ||
todo!() | ||
} | ||
Interpolator::InterpND(interp) => { | ||
todo!() | ||
} | ||
} | ||
} | ||
|
||
fn set_max(&mut self, max: f64) -> anyhow::Result<()> { | ||
let old_max = self.max()?; | ||
match self { | ||
Interpolator::Interp0D(value) => Ok(*value = max), | ||
Interpolator::Interp1D(interp) => { | ||
Ok(interp.set_f_x(interp.f_x().iter().map(|x| x * max / old_max).collect())?) | ||
} | ||
Interpolator::Interp2D(interp) => Ok(interp.set_f_xy( | ||
interp | ||
.f_xy() | ||
.iter() | ||
.map(|v| v.iter().map(|x| x * max / old_max).collect()) | ||
.collect(), | ||
)?), | ||
Interpolator::Interp3D(interp) => Ok(interp.set_f_xyz( | ||
interp | ||
.f_xyz() | ||
.iter() | ||
.map(|v0| { | ||
v0.iter() | ||
.map(|v1| v1.iter().map(|x| x * max / old_max).collect()) | ||
.collect() | ||
}) | ||
.collect(), | ||
)?), | ||
Interpolator::InterpND(interp) => { | ||
Ok(interp.set_values(interp.values().map(|x| x * max / old_max))?) | ||
} | ||
} | ||
} | ||
|
||
fn set_range(&mut self, range: f64) -> anyhow::Result<()> { | ||
let old_max = self.max()?; | ||
let old_range = old_max - self.min()?; | ||
ensure!(old_range != 0., "Cannot modify range when min == max"); | ||
match self { | ||
Interpolator::Interp0D(_value) => unreachable!("The above `ensure` should trigger"), | ||
Interpolator::Interp1D(interp) => Ok(interp.set_f_x( | ||
interp | ||
.f_x() | ||
.iter() | ||
.map(|x| old_max + (x - old_max) * range / old_range) | ||
.collect(), | ||
)?), | ||
Interpolator::Interp2D(interp) => Ok(interp.set_f_xy( | ||
interp | ||
.f_xy() | ||
.iter() | ||
.map(|v| { | ||
v.iter() | ||
.map(|x| old_max + (x - old_max) * range / old_range) | ||
.collect() | ||
}) | ||
.collect(), | ||
)?), | ||
Interpolator::Interp3D(interp) => Ok(interp.set_f_xyz( | ||
interp | ||
.f_xyz() | ||
.iter() | ||
.map(|v0| { | ||
v0.iter() | ||
.map(|v1| { | ||
v1.iter() | ||
.map(|x| old_max + (x - old_max) * range / old_range) | ||
.collect() | ||
}) | ||
.collect() | ||
}) | ||
.collect(), | ||
)?), | ||
Interpolator::InterpND(interp) => Ok(interp.set_values( | ||
interp | ||
.values() | ||
.map(|x| old_max + (x - old_max) * range / old_range), | ||
)?), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters