qsym2/sandbox/target/real_space_function/
mod.rs1use std::fmt;
4
5use derive_builder::Builder;
6use itertools::Itertools;
7use nalgebra::Point3;
8use ndarray_linalg::types::Lapack;
9use num_complex::ComplexFloat;
10
11#[cfg(test)]
12mod real_space_function_tests;
13
14pub mod real_space_function_analysis;
15mod real_space_function_transformation;
16
17#[derive(Builder, Clone)]
23#[builder(build_fn(validate = "Self::validate"))]
24pub struct RealSpaceFunction<T, F>
25where
26    T: ComplexFloat + Lapack,
27    F: Fn(&Point3<f64>) -> T,
28{
29    grid_points: Vec<Point3<f64>>,
31
32    function: F,
34
35    #[builder(default = "false")]
37    complex_conjugated: bool,
38}
39
40impl<T, F> RealSpaceFunctionBuilder<T, F>
41where
42    T: ComplexFloat + Lapack,
43    F: Fn(&Point3<f64>) -> T,
44{
45    fn validate(&self) -> Result<(), String> {
46        let _ = self
47            .grid_points
48            .as_ref()
49            .ok_or("No grid points found.".to_string())?;
50        let _ = self
51            .function
52            .as_ref()
53            .ok_or("No real-space function found.".to_string())?;
54        Ok(())
55    }
56}
57
58impl<T, F> RealSpaceFunction<T, F>
59where
60    T: ComplexFloat + Clone + Lapack,
61    F: Clone + Fn(&Point3<f64>) -> T,
62{
63    pub fn builder() -> RealSpaceFunctionBuilder<T, F> {
65        RealSpaceFunctionBuilder::default()
66    }
67
68    pub fn grid_points(&self) -> Vec<&Point3<f64>> {
71        self.grid_points.iter().collect_vec()
72    }
73
74    pub fn function(&self) -> &F {
76        &self.function
77    }
78}
79
80impl<T, F> fmt::Display for RealSpaceFunction<T, F>
88where
89    T: ComplexFloat + Lapack,
90    F: Fn(&Point3<f64>) -> T,
91{
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(
94            f,
95            "RealSpaceFunction{}[grid of {} {}]",
96            if self.complex_conjugated { "*" } else { "" },
97            self.grid_points.len(),
98            if self.grid_points.len() == 1 {
99                "point"
100            } else {
101                "points"
102            }
103        )?;
104        Ok(())
105    }
106}