From b20fdcd0d3d9f44d7ed660ce98944e2f12dd4152 Mon Sep 17 00:00:00 2001 From: Dzuchun Date: Sun, 24 Nov 2024 17:15:57 +0200 Subject: [PATCH] Renamed `Entity` to `Model` in a bunch of places (leftovers from earlier development stages) --- nacfahi/doc/fit_macro.md | 2 +- nacfahi/src/const_problem.rs | 54 ++++++++++++------------- nacfahi/src/dyn_problem.rs | 54 ++++++++++++------------- nacfahi/src/lib.rs | 78 ++++++++++++++++++------------------ 4 files changed, 94 insertions(+), 94 deletions(-) diff --git a/nacfahi/doc/fit_macro.md b/nacfahi/doc/fit_macro.md index 5bcd59f..c01af2a 100644 --- a/nacfahi/doc/fit_macro.md +++ b/nacfahi/doc/fit_macro.md @@ -3,7 +3,7 @@ A convenience macro for [`function@fit`] function. You are free to call the func [`function@fit`] function signature: ```rust,no_run,ignore -pub fn fit(Entity, X, Y, &LevenbergMarquardt, impl Fn(Scalar, Scalar) -> Scalar) -> MinimizationReport +pub fn fit(Model, X, Y, &LevenbergMarquardt, impl Fn(Scalar, Scalar) -> Scalar) -> MinimizationReport ``` Here you can see two optional arguments: diff --git a/nacfahi/src/const_problem.rs b/nacfahi/src/const_problem.rs index 5e56890..2a874b7 100644 --- a/nacfahi/src/const_problem.rs +++ b/nacfahi/src/const_problem.rs @@ -5,47 +5,47 @@ use nalgebra::{allocator::Allocator, ComplexField, DefaultAllocator, OMatrix}; use crate::models::FitModel; -pub(crate) struct ConstOptimizationProblem<'data, Points: Conv, Entity: FitModel, Weights> { - pub entity: Entity, - pub x: nalgebra::VectorView<'data, Entity::Scalar, Points::Nalg>, - pub y: nalgebra::VectorView<'data, Entity::Scalar, Points::Nalg>, +pub(crate) struct ConstOptimizationProblem<'data, Points: Conv, Model: FitModel, Weights> { + pub entity: Model, + pub x: nalgebra::VectorView<'data, Model::Scalar, Points::Nalg>, + pub y: nalgebra::VectorView<'data, Model::Scalar, Points::Nalg>, pub weights: Weights, } -impl - LeastSquaresProblem::Nalg> - for ConstOptimizationProblem<'_, Points, Entity, Weights> +impl + LeastSquaresProblem::Nalg> + for ConstOptimizationProblem<'_, Points, Model, Weights> where DefaultAllocator: Allocator, - DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, - DefaultAllocator: Allocator::Nalg>, + DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, + DefaultAllocator: Allocator::Nalg>, - Entity::Scalar: nalgebra::Scalar + ComplexField + Copy, - Entity: FitModel, - Weights: Fn(Entity::Scalar, Entity::Scalar) -> Entity::Scalar, + Model::Scalar: nalgebra::Scalar + ComplexField + Copy, + Model: FitModel, + Weights: Fn(Model::Scalar, Model::Scalar) -> Model::Scalar, { - type ResidualStorage = GenericArrayStorage; + type ResidualStorage = GenericArrayStorage; - type ParameterStorage = GenericArrayStorage; + type ParameterStorage = GenericArrayStorage; - type JacobianStorage = GenericArrayStorage; + type JacobianStorage = GenericArrayStorage; - fn set_params(&mut self, x: &GenericMatrix) { - let slice: &[Entity::Scalar] = x.data.as_ref(); + fn set_params(&mut self, x: &GenericMatrix) { + let slice: &[Model::Scalar] = x.data.as_ref(); let arr = - GenericArray::::TNum>::from_slice(slice) + GenericArray::::TNum>::from_slice(slice) .clone(); self.entity.set_params(arr); } - fn params(&self) -> GenericMatrix { - let pars: GenericArray::TNum> = + fn params(&self) -> GenericMatrix { + let pars: GenericArray::TNum> = self.entity.get_params().into(); GenericMatrix::from_data(GenericArrayStorage(GenericArray::from_array([pars]))) } - fn residuals(&self) -> Option> { - let mat: GenericMatrix = self + fn residuals(&self) -> Option> { + let mat: GenericMatrix = self .x .zip_map(&self.y, |x, y| { (self.weights)(x, y) * (self.entity.evaluate(&x) - y) @@ -54,18 +54,18 @@ where Some(mat) } - fn jacobian(&self) -> Option> { + fn jacobian(&self) -> Option> { let mut res = - OMatrix::::Nalg>::zeros_generic( + OMatrix::::Nalg>::zeros_generic( Points::new_nalg(), - Entity::ParamCount::new_nalg(), + Model::ParamCount::new_nalg(), ); for i_x in 0..self.x.len() { - let jacobian_x: GenericArray<_, ::TNum> = + let jacobian_x: GenericArray<_, ::TNum> = self.entity.jacobian(&self.x[i_x]).into(); let arr = jacobian_x.map(|v| GenericArray::<_, typenum::U1>::from_array([v])); - let mat = GenericMatrix::::from_data( + let mat = GenericMatrix::::from_data( GenericArrayStorage(arr), ); res.set_row(i_x, &mat.row(0)); diff --git a/nacfahi/src/dyn_problem.rs b/nacfahi/src/dyn_problem.rs index 164ebab..40c3954 100644 --- a/nacfahi/src/dyn_problem.rs +++ b/nacfahi/src/dyn_problem.rs @@ -5,67 +5,67 @@ use nalgebra::{allocator::Allocator, ComplexField, DefaultAllocator, Dyn, OMatri use crate::models::FitModel; -pub(crate) struct DynOptimizationProblem<'data, Entity: FitModel, Weights> { - pub entity: Entity, - pub x: nalgebra::VectorView<'data, Entity::Scalar, Dyn>, - pub y: nalgebra::VectorView<'data, Entity::Scalar, Dyn>, +pub(crate) struct DynOptimizationProblem<'data, Model: FitModel, Weights> { + pub entity: Model, + pub x: nalgebra::VectorView<'data, Model::Scalar, Dyn>, + pub y: nalgebra::VectorView<'data, Model::Scalar, Dyn>, pub weights: Weights, } -impl - LeastSquaresProblem::Nalg> - for DynOptimizationProblem<'_, Entity, Weights> +impl + LeastSquaresProblem::Nalg> + for DynOptimizationProblem<'_, Model, Weights> where - DefaultAllocator: Allocator<::Nalg, nalgebra::U1> + DefaultAllocator: Allocator<::Nalg, nalgebra::U1> + Allocator - + Allocator::Nalg>, + + Allocator::Nalg>, - Entity::Scalar: nalgebra::Scalar + ComplexField + Copy, - Weights: Fn(Entity::Scalar, Entity::Scalar) -> Entity::Scalar, + Model::Scalar: nalgebra::Scalar + ComplexField + Copy, + Weights: Fn(Model::Scalar, Model::Scalar) -> Model::Scalar, { type ResidualStorage = - >::Buffer; + >::Buffer; - type ParameterStorage = GenericArrayStorage; + type ParameterStorage = GenericArrayStorage; type JacobianStorage = ::Nalg, - >>::Buffer; + ::Nalg, + >>::Buffer; - fn set_params(&mut self, x: &GenericMatrix) { - let slice: &[Entity::Scalar] = x.data.as_ref(); + fn set_params(&mut self, x: &GenericMatrix) { + let slice: &[Model::Scalar] = x.data.as_ref(); let arr = - GenericArray::::TNum>::from_slice(slice) + GenericArray::::TNum>::from_slice(slice) .clone(); self.entity.set_params(arr); } - fn params(&self) -> GenericMatrix { - let pars: GenericArray::TNum> = + fn params(&self) -> GenericMatrix { + let pars: GenericArray::TNum> = self.entity.get_params().into(); GenericMatrix::from_data(GenericArrayStorage(GenericArray::from_array([pars]))) } - fn residuals(&self) -> Option> { - let mat: OMatrix = self.x.zip_map(&self.y, |x, y| { + fn residuals(&self) -> Option> { + let mat: OMatrix = self.x.zip_map(&self.y, |x, y| { (self.weights)(x, y) * (self.entity.evaluate(&x) - y) }); Some(mat) } - fn jacobian(&self) -> Option::Nalg>> { + fn jacobian(&self) -> Option::Nalg>> { let mut res = - OMatrix::::Nalg>::zeros_generic( + OMatrix::::Nalg>::zeros_generic( Dyn(self.x.len()), - Entity::ParamCount::new_nalg(), + Model::ParamCount::new_nalg(), ); for i_x in 0..self.x.len() { - let jacobian_x: GenericArray<_, ::TNum> = + let jacobian_x: GenericArray<_, ::TNum> = self.entity.jacobian(&self.x[i_x]).into(); let arr = jacobian_x.map(|v| GenericArray::<_, typenum::U1>::from_array([v])); - let mat = GenericMatrix::::from_data( + let mat = GenericMatrix::::from_data( GenericArrayStorage(arr), ); res.set_row(i_x, &mat.row(0)); diff --git a/nacfahi/src/lib.rs b/nacfahi/src/lib.rs index 0d42503..9f2dbd3 100644 --- a/nacfahi/src/lib.rs +++ b/nacfahi/src/lib.rs @@ -118,17 +118,17 @@ pub trait CreateProblem { type Nalg: Dim; /// Creates a problem from data views and arbitrary model. - fn create<'d, Entity: FitModel + 'd>( - x: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - y: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - entity: Entity, - weights: impl Fn(Entity::Scalar, Entity::Scalar) -> Entity::Scalar + 'd, - ) -> impl LeastSquaresProblem::Nalg> + 'd + fn create<'d, Model: FitModel + 'd>( + x: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + y: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + entity: Model, + weights: impl Fn(Model::Scalar, Model::Scalar) -> Model::Scalar + 'd, + ) -> impl LeastSquaresProblem::Nalg> + 'd where - Entity::Scalar: ComplexField + Copy, - DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, + Model::Scalar: ComplexField + Copy, + DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, DefaultAllocator: Allocator, - DefaultAllocator: Allocator::Nalg>; + DefaultAllocator: Allocator::Nalg>; } impl CreateProblem for typenum::Const @@ -137,19 +137,19 @@ where { type Nalg = nalgebra::Const; - fn create<'d, Entity: FitModel + 'd>( - x: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - y: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - entity: Entity, - weights: impl Fn(Entity::Scalar, Entity::Scalar) -> Entity::Scalar + 'd, - ) -> impl LeastSquaresProblem::Nalg> + 'd + fn create<'d, Model: FitModel + 'd>( + x: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + y: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + entity: Model, + weights: impl Fn(Model::Scalar, Model::Scalar) -> Model::Scalar + 'd, + ) -> impl LeastSquaresProblem::Nalg> + 'd where - Entity::Scalar: ComplexField + Copy, - DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, + Model::Scalar: ComplexField + Copy, + DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, DefaultAllocator: Allocator, - DefaultAllocator: Allocator::Nalg>, + DefaultAllocator: Allocator::Nalg>, { - ConstOptimizationProblem::<'d, Self, Entity, _> { + ConstOptimizationProblem::<'d, Self, Model, _> { entity, x, y, @@ -164,19 +164,19 @@ where { type Nalg = Self; - fn create<'d, Entity: FitModel + 'd>( - x: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - y: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - entity: Entity, - weights: impl Fn(Entity::Scalar, Entity::Scalar) -> Entity::Scalar + 'd, - ) -> impl LeastSquaresProblem::Nalg> + 'd + fn create<'d, Model: FitModel + 'd>( + x: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + y: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + entity: Model, + weights: impl Fn(Model::Scalar, Model::Scalar) -> Model::Scalar + 'd, + ) -> impl LeastSquaresProblem::Nalg> + 'd where - Entity::Scalar: ComplexField + Copy, - DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, + Model::Scalar: ComplexField + Copy, + DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, DefaultAllocator: Allocator, - DefaultAllocator: Allocator::Nalg>, + DefaultAllocator: Allocator::Nalg>, { - ConstOptimizationProblem::<'d, Self, Entity, _> { + ConstOptimizationProblem::<'d, Self, Model, _> { entity, x, y, @@ -188,19 +188,19 @@ where impl CreateProblem for Dyn { type Nalg = Self; - fn create<'d, Entity: FitModel + 'd>( - x: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - y: MatrixView<'d, Entity::Scalar, Self::Nalg, nalgebra::U1>, - entity: Entity, - weights: impl Fn(Entity::Scalar, Entity::Scalar) -> Entity::Scalar + 'd, - ) -> impl LeastSquaresProblem::Nalg> + 'd + fn create<'d, Model: FitModel + 'd>( + x: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + y: MatrixView<'d, Model::Scalar, Self::Nalg, nalgebra::U1>, + entity: Model, + weights: impl Fn(Model::Scalar, Model::Scalar) -> Model::Scalar + 'd, + ) -> impl LeastSquaresProblem::Nalg> + 'd where - Entity::Scalar: ComplexField + Copy, - DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, + Model::Scalar: ComplexField + Copy, + DefaultAllocator: Allocator<::Nalg, nalgebra::U1>, DefaultAllocator: Allocator, - DefaultAllocator: Allocator::Nalg>, + DefaultAllocator: Allocator::Nalg>, { - DynOptimizationProblem::<'d, Entity, _> { + DynOptimizationProblem::<'d, Model, _> { entity, x, y,