| 9 use alg_tools::mapping::{ClosedSpace, DifferentiableImpl, Instance, Mapping, Space}; |
9 use alg_tools::mapping::{ClosedSpace, DifferentiableImpl, Instance, Mapping, Space}; |
| 10 use anyhow::anyhow; |
10 use anyhow::anyhow; |
| 11 use ndarray::Dimension; |
11 use ndarray::Dimension; |
| 12 use numpy::{Ix1, Ix2}; |
12 use numpy::{Ix1, Ix2}; |
| 13 use pointsource_algs::forward_model::{BoundedCurvature, BoundedCurvatureGuess}; |
13 use pointsource_algs::forward_model::{BoundedCurvature, BoundedCurvatureGuess}; |
| 14 use pointsource_algs::prox_penalty::{RadonSquared, StepLengthBound, StepLengthBoundPair}; |
14 use pointsource_algs::prox_penalty::{ |
| |
15 RadonSquared, StepLengthBound, StepLengthBoundPair, StepLengthBoundValue, |
| |
16 }; |
| 15 use pyo3::conversion::FromPyObject; |
17 use pyo3::conversion::FromPyObject; |
| 16 use pyo3::intern; |
18 use pyo3::intern; |
| 17 use pyo3::prelude::*; |
19 use pyo3::prelude::*; |
| 18 use pyo3::PyClass; |
20 use pyo3::PyClass; |
| 19 use std::marker::PhantomData; |
21 use std::marker::PhantomData; |
| 121 impl<'py, Domain> DifferentiableImpl<Domain> |
123 impl<'py, Domain> DifferentiableImpl<Domain> |
| 122 for PythonMapping<'py, Domain, f64, Differentiable<$marker>> |
124 for PythonMapping<'py, Domain, f64, Differentiable<$marker>> |
| 123 where |
125 where |
| 124 Domain: Space, |
126 Domain: Space, |
| 125 Domain::Principal: IntoPyObject<'py>, |
127 Domain::Principal: IntoPyObject<'py>, |
| |
128 Self: Mapping<Domain>, |
| |
129 <Self as Mapping<Domain>>::Codomain: |
| |
130 ClosedSpace + for<'a> FromPyObject<'a, 'py, Error = PyErr>, |
| 126 //$derivative: Space + for<'py> FromPyObject<'py>, |
131 //$derivative: Space + for<'py> FromPyObject<'py>, |
| 127 //f64 : for<'py> FromPyObject<'py>, |
132 //f64 : for<'py> FromPyObject<'py>, |
| 128 { |
133 { |
| 129 type Derivative = $derivative; |
134 type Derivative = $derivative; |
| 130 |
135 |
| 131 /// Compute the value of `self` at `x`. |
|
| 132 fn differential_impl<I: Instance<Domain>>(&self, x: I) -> $derivative { |
136 fn differential_impl<I: Instance<Domain>>(&self, x: I) -> $derivative { |
| 133 // TODO: use references and internal mutability? |
137 // TODO: use references and internal mutability? |
| 134 //x_py = x.own().to_python(py).unwrap(); |
138 //x_py = x.own().to_python(py).unwrap(); |
| 135 let diff = intern!(self.obj.py(), "diff"); |
139 let diff = intern!(self.obj.py(), "diff"); |
| 136 process_error( |
140 process_error( |
| 196 Domain: Space, |
200 Domain: Space, |
| 197 { |
201 { |
| 198 fn step_length_bound( |
202 fn step_length_bound( |
| 199 &self, |
203 &self, |
| 200 f: &PythonMapping<'py, Domain, f64, Differentiable<Marker>>, |
204 f: &PythonMapping<'py, Domain, f64, Differentiable<Marker>>, |
| 201 ) -> DynResult<f64> { |
205 ) -> StepLengthBoundValue<f64> { |
| 202 let m = intern!(f.obj.py(), "diff_lipschitz_factor"); |
206 let m = intern!(f.obj.py(), "diff_lipschitz_factor"); |
| 203 process_error( |
207 match process_error( |
| 204 "PythonMapping::diff_lipschitz_factor", |
208 "PythonMapping::diff_lipschitz_factor", |
| 205 f.obj.py(), |
209 f.obj.py(), |
| 206 f.obj.call_method0(m).and_then(|r| r.extract()), |
210 f.obj.call_method0(m).and_then(|r| r.extract()), |
| 207 ) |
211 ) { |
| |
212 Ok(v) => StepLengthBoundValue::UnreliableLipschitzFactor(v), |
| |
213 Err(_) => StepLengthBoundValue::Failure, |
| |
214 } |
| 208 } |
215 } |
| 209 } |
216 } |
| 210 |
217 |
| 211 impl<'py, 'a, Domain, Marker, Z> |
218 impl<'py, 'a, Domain, Marker, Z> |
| 212 StepLengthBoundPair<f64, PythonMapping<'py, Domain, f64, Differentiable<Marker>>> |
219 StepLengthBoundPair<f64, PythonMapping<'py, Domain, f64, Differentiable<Marker>>> |