Skip to content

Commit

Permalink
refactor: replace &'a [T] with Vec<T> in explicit domain descript…
Browse files Browse the repository at this point in the history
…ors (#39)

* replace `&'a [T]` with `Vec<T>` for explicit domain descriptors

* fix tests
  • Loading branch information
imrn99 authored Oct 22, 2024
1 parent 640e872 commit a96eef4
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 132 deletions.
9 changes: 4 additions & 5 deletions integraal/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use crate::Scalar;
/// is automatially implemented for types satisfying its requirements. In the future, adding support for
/// higher dimension can be considered.
#[derive(Debug, Clone)]
pub enum DomainDescriptor<'a, T: Scalar> {
// FIXME: change to a vector
pub enum DomainDescriptor<X: Scalar> {
/// List of values taken by the variable on which we integrate.
Explicit(&'a [T]),
Explicit(Vec<X>),
/// Description of a uniform discretization over a certain range of values.
Uniform {
/// First value of the range
start: T,
start: X,
/// Step between each value of the range
step: T,
step: X,
/// Total number of values
n_step: usize,
},
Expand Down
4 changes: 2 additions & 2 deletions integraal/src/structure/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ pub enum IntegraalError {
/// # }
/// ```
#[derive(Default)]
pub struct Integraal<'a, X: Scalar> {
pub struct Integraal<X: Scalar> {
/// Domain over which the function is integrated.
pub(crate) domain: Option<DomainDescriptor<'a, X>>,
pub(crate) domain: Option<DomainDescriptor<X>>,
/// Function to integrate.
pub(crate) function: Option<FunctionDescriptor<X>>,
/// Numerical integration method used for value approximation.
Expand Down
4 changes: 2 additions & 2 deletions integraal/src/structure/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use rand::Rng;

// ------ CONTENT

impl<'a, X: Scalar> Integraal<'a, X> {
impl<X: Scalar> Integraal<X> {
/// Set the domain descriptor.
#[must_use = "unused builder struct - please remove this call"]
pub fn domain(mut self, domain_descriptor: DomainDescriptor<'a, X>) -> Self {
pub fn domain(mut self, domain_descriptor: DomainDescriptor<X>) -> Self {
self.domain = Some(domain_descriptor);
self
}
Expand Down
18 changes: 10 additions & 8 deletions integraal/src/tests/function_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ mod double {

all_tests!(
f64,
let domain: Vec<f64> = (0..(std::f64::consts::PI * 1000.) as usize)
.map(|step_id| step_id as f64 * STEP)
.collect(),
FunctionDescriptor::Closure(Box::new(f64::sin)),
FunctionDescriptor::Values(
(0..(1000. * std::f64::consts::PI) as usize)
.map(|step_id| (step_id as f64 * STEP).sin())
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit(
(0..(std::f64::consts::PI * 1000.) as usize)
.map(|step_id| step_id as f64 * STEP)
.collect()
),
DomainDescriptor::Uniform {
start: 0.,
step: STEP,
Expand All @@ -55,16 +56,17 @@ mod simple {

all_tests!(
f32,
let domain: Vec<f32> = (0..(std::f32::consts::PI * 1000.) as usize)
.map(|step_id| step_id as f32 * STEP)
.collect(),
FunctionDescriptor::Closure(Box::new(f32::sin)),
FunctionDescriptor::Values(
(0..(1000. * std::f32::consts::PI) as usize)
.map(|step_id| (step_id as f32 * STEP).sin())
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit(
(0..(std::f32::consts::PI * 1000.) as usize)
.map(|step_id| step_id as f32 * STEP)
.collect()
),
DomainDescriptor::Uniform {
start: 0.,
step: STEP,
Expand Down
68 changes: 38 additions & 30 deletions integraal/src/tests/function_b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,32 @@ mod double {

all_tests!(
f64,
let domain: Vec<f64> = (0..3000)
.map(|step_id| f64::from(step_id) * STEP)
.collect(),
FunctionDescriptor::Closure(
Box::new(|x|
if (0.0..1.0).contains(&x) { x }
else if (1.0..2.0).contains(&x) { 1.0 }
else if (2.0..3.0).contains(&x) { 3.0 - x }
else { 0.0 })
),
FunctionDescriptor::Closure(Box::new(|x| if (0.0..1.0).contains(&x) {
x
} else if (1.0..2.0).contains(&x) {
1.0
} else if (2.0..3.0).contains(&x) {
3.0 - x
} else {
0.0
})),
FunctionDescriptor::Values(
(0..3000)
.map(|step_id| {
let x = f64::from(step_id) * STEP;
if (0.0..1.0).contains(&x) { x }
else if (1.0..2.0).contains(&x) { 1.0 }
else if (2.0..3.0).contains(&x) { 3.0 - x }
else { 0.0 }
if (0.0..1.0).contains(&x) {
x
} else if (1.0..2.0).contains(&x) {
1.0
} else if (2.0..3.0).contains(&x) {
3.0 - x
} else {
0.0
}
})
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit((0..3000).map(|step_id| f64::from(step_id) * STEP).collect()),
DomainDescriptor::Uniform {
start: 0.,
step: STEP,
Expand All @@ -69,28 +73,32 @@ mod simple {

all_tests!(
f32,
let domain: Vec<f32> = (0..3000)
.map(|step_id| step_id as f32 * STEP)
.collect(),
FunctionDescriptor::Closure(
Box::new(|x: f32|
if x < 1.0 { x }
else if x < 2.0 { 1.0 }
else if x < 3.0 { 3.0 - x }
else { 0.0 })
),
FunctionDescriptor::Closure(Box::new(|x: f32| if x < 1.0 {
x
} else if x < 2.0 {
1.0
} else if x < 3.0 {
3.0 - x
} else {
0.0
})),
FunctionDescriptor::Values(
(0..3000)
.map(|step_id| {
let x = step_id as f32 * STEP;
if x < 1.0 { x }
else if x < 2.0 { 1.0 }
else if x < 3.0 { 3.0 - x }
else { 0.0 }
if x < 1.0 {
x
} else if x < 2.0 {
1.0
} else if x < 3.0 {
3.0 - x
} else {
0.0
}
})
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit((0..3000).map(|step_id| step_id as f32 * STEP).collect()),
DomainDescriptor::Uniform {
start: 0.,
step: STEP,
Expand Down
56 changes: 30 additions & 26 deletions integraal/src/tests/function_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ mod double {

all_tests!(
f64,
let domain: Vec<f64> = (0..2000)
.map(|step_id| f64::from(step_id) * STEP)
.collect(),
FunctionDescriptor::Closure(
Box::new(|x|
if x < 1.0 { 0.0 }
else if x < 1.5 { 1.0 }
else { 0.0 })
),
FunctionDescriptor::Closure(Box::new(|x| if x < 1.0 {
0.0
} else if x < 1.5 {
1.0
} else {
0.0
})),
FunctionDescriptor::Values(
(0..2000)
.map(|step_id| {
let x = f64::from(step_id) * STEP;
if x < 1.0 { 0.0 }
else if x < 1.5 { 1.0 }
else { 0.0 }
if x < 1.0 {
0.0
} else if x < 1.5 {
1.0
} else {
0.0
}
})
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit((0..2000).map(|step_id| f64::from(step_id) * STEP).collect()),
DomainDescriptor::Uniform {
start: 0.,
step: STEP,
Expand All @@ -67,26 +69,28 @@ mod simple {

all_tests!(
f32,
let domain: Vec<f32> = (0..2000)
.map(|step_id| step_id as f32 * STEP)
.collect(),
FunctionDescriptor::Closure(
Box::new(|x|
if x < 1.0 { 0.0 }
else if x < 1.5 { 1.0 }
else { 0.0 })
),
FunctionDescriptor::Closure(Box::new(|x| if x < 1.0 {
0.0
} else if x < 1.5 {
1.0
} else {
0.0
})),
FunctionDescriptor::Values(
(0..2000)
.map(|step_id| {
let x = step_id as f32 * STEP;
if x < 1.0 { 0.0 }
else if x < 1.5 { 1.0 }
else { 0.0 }
if x < 1.0 {
0.0
} else if x < 1.5 {
1.0
} else {
0.0
}
})
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit((0..2000).map(|step_id| step_id as f32 * STEP).collect()),
DomainDescriptor::Uniform {
start: 0.,
step: STEP,
Expand Down
14 changes: 6 additions & 8 deletions integraal/src/tests/function_d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ mod double {

all_tests!(
f64,
let domain: Vec<f64> = (-4000..4000)
.map(|step_id| f64::from(step_id) * STEP)
.collect(),
FunctionDescriptor::Closure(Box::new(|x: f64| x.powi(2))),
FunctionDescriptor::Values(
(-4000..4000)
.map(|step_id| (f64::from(step_id) * STEP).powi(2))
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit(
(-4000..4000)
.map(|step_id| f64::from(step_id) * STEP)
.collect()
),
DomainDescriptor::Uniform {
start: -4.,
step: STEP,
Expand All @@ -56,16 +57,13 @@ mod simple {

all_tests!(
f32,
let domain: Vec<f32> = (-4000..4000)
.map(|step_id| step_id as f32 * STEP)
.collect(),
FunctionDescriptor::Closure(Box::new(|x: f32| x.powi(2))),
FunctionDescriptor::Values(
(-4000..4000)
.map(|step_id| (step_id as f32 * STEP).powi(2))
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit((-4000..4000).map(|step_id| step_id as f32 * STEP).collect()),
DomainDescriptor::Uniform {
start: -4.,
step: STEP,
Expand Down
14 changes: 6 additions & 8 deletions integraal/src/tests/function_e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ mod double {

all_tests!(
f64,
let domain: Vec<f64> = (-4000..4000)
.map(|step_id| f64::from(step_id) * STEP)
.collect(),
FunctionDescriptor::Closure(Box::new(|x: f64| x.powi(2))),
FunctionDescriptor::Values(
(-4000..4000)
.map(|step_id| (f64::from(step_id) * STEP).powi(2))
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit(
(-4000..4000)
.map(|step_id| f64::from(step_id) * STEP)
.collect()
),
DomainDescriptor::Uniform {
start: -4.,
step: STEP,
Expand All @@ -56,16 +57,13 @@ mod simple {

all_tests!(
f32,
let domain: Vec<f32> = (-4000..4000)
.map(|step_id| step_id as f32 * STEP)
.collect(),
FunctionDescriptor::Closure(Box::new(|x: f32| x.powi(2))),
FunctionDescriptor::Values(
(-4000..4000)
.map(|step_id| (step_id as f32 * STEP).powi(2))
.collect()
),
DomainDescriptor::Explicit(&domain),
DomainDescriptor::Explicit((-4000..4000).map(|step_id| step_id as f32 * STEP).collect()),
DomainDescriptor::Uniform {
start: -4.,
step: STEP,
Expand Down
7 changes: 3 additions & 4 deletions integraal/src/tests/incorrect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn missing_parameters() {
generate_missing!(function, domain);

// missing all but one
let mut integral: Integraal<'_, f64> = Integraal::default().method(method);
let mut integral: Integraal<f64> = Integraal::default().method(method);
assert_eq!(
integral.compute(),
Err(IntegraalError::MissingParameters(
Expand All @@ -33,8 +33,7 @@ fn missing_parameters() {
fn inconsistent_parameters() {
let method = ComputeMethod::RectangleLeft;
let function = FunctionDescriptor::Values(vec![1., 1., 1., 1., 1., 1.]);
let domain = vec![0.0, 0.1, 0.2, 0.3, 0.4]; // missing the last x value
let domain = DomainDescriptor::Explicit(&domain);
let domain = DomainDescriptor::Explicit(vec![0.0, 0.1, 0.2, 0.3, 0.4]);

let mut integral = Integraal::default()
.method(method)
Expand All @@ -47,7 +46,7 @@ fn inconsistent_parameters() {
))
);

// this is equivalent to the first domain
// equivalent to the first domain
let domain = DomainDescriptor::Uniform {
start: 0.,
step: 0.1,
Expand Down
Loading

0 comments on commit a96eef4

Please sign in to comment.