Skip to content

Commit

Permalink
Renamed Entity to Model in a bunch of places
Browse files Browse the repository at this point in the history
(leftovers from earlier development stages)
  • Loading branch information
Dzuchun committed Nov 24, 2024
1 parent 9d4c06a commit b20fdcd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 94 deletions.
2 changes: 1 addition & 1 deletion nacfahi/doc/fit_macro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scalar>(Entity, X, Y, &LevenbergMarquardt<Scalar>, impl Fn(Scalar, Scalar) -> Scalar) -> MinimizationReport<Scalar>
pub fn fit<Scalar>(Model, X, Y, &LevenbergMarquardt<Scalar>, impl Fn(Scalar, Scalar) -> Scalar) -> MinimizationReport<Scalar>
```

Here you can see two optional arguments:
Expand Down
54 changes: 27 additions & 27 deletions nacfahi/src/const_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Points: Conv, Entity: FitModel, Weights>
LeastSquaresProblem<Entity::Scalar, Points::Nalg, <Entity::ParamCount as Conv>::Nalg>
for ConstOptimizationProblem<'_, Points, Entity, Weights>
impl<Points: Conv, Model: FitModel, Weights>
LeastSquaresProblem<Model::Scalar, Points::Nalg, <Model::ParamCount as Conv>::Nalg>
for ConstOptimizationProblem<'_, Points, Model, Weights>
where
DefaultAllocator: Allocator<Points::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<<Entity::ParamCount as Conv>::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Points::Nalg, <Entity::ParamCount as Conv>::Nalg>,
DefaultAllocator: Allocator<<Model::ParamCount as Conv>::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Points::Nalg, <Model::ParamCount as Conv>::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<Entity::Scalar, Points, typenum::U1>;
type ResidualStorage = GenericArrayStorage<Model::Scalar, Points, typenum::U1>;

type ParameterStorage = GenericArrayStorage<Entity::Scalar, Entity::ParamCount, typenum::U1>;
type ParameterStorage = GenericArrayStorage<Model::Scalar, Model::ParamCount, typenum::U1>;

type JacobianStorage = GenericArrayStorage<Entity::Scalar, Points, Entity::ParamCount>;
type JacobianStorage = GenericArrayStorage<Model::Scalar, Points, Model::ParamCount>;

fn set_params(&mut self, x: &GenericMatrix<Entity::Scalar, Entity::ParamCount, typenum::U1>) {
let slice: &[Entity::Scalar] = x.data.as_ref();
fn set_params(&mut self, x: &GenericMatrix<Model::Scalar, Model::ParamCount, typenum::U1>) {
let slice: &[Model::Scalar] = x.data.as_ref();
let arr =
GenericArray::<Entity::Scalar, <Entity::ParamCount as Conv>::TNum>::from_slice(slice)
GenericArray::<Model::Scalar, <Model::ParamCount as Conv>::TNum>::from_slice(slice)
.clone();
self.entity.set_params(arr);
}

fn params(&self) -> GenericMatrix<Entity::Scalar, Entity::ParamCount, typenum::U1> {
let pars: GenericArray<Entity::Scalar, <Entity::ParamCount as Conv>::TNum> =
fn params(&self) -> GenericMatrix<Model::Scalar, Model::ParamCount, typenum::U1> {
let pars: GenericArray<Model::Scalar, <Model::ParamCount as Conv>::TNum> =
self.entity.get_params().into();
GenericMatrix::from_data(GenericArrayStorage(GenericArray::from_array([pars])))
}

fn residuals(&self) -> Option<GenericMatrix<Entity::Scalar, Points, typenum::U1>> {
let mat: GenericMatrix<Entity::Scalar, Points, typenum::U1> = self
fn residuals(&self) -> Option<GenericMatrix<Model::Scalar, Points, typenum::U1>> {
let mat: GenericMatrix<Model::Scalar, Points, typenum::U1> = self
.x
.zip_map(&self.y, |x, y| {
(self.weights)(x, y) * (self.entity.evaluate(&x) - y)
Expand All @@ -54,18 +54,18 @@ where
Some(mat)
}

fn jacobian(&self) -> Option<GenericMatrix<Entity::Scalar, Points, Entity::ParamCount>> {
fn jacobian(&self) -> Option<GenericMatrix<Model::Scalar, Points, Model::ParamCount>> {
let mut res =
OMatrix::<Entity::Scalar, Points::Nalg, <Entity::ParamCount as Conv>::Nalg>::zeros_generic(
OMatrix::<Model::Scalar, Points::Nalg, <Model::ParamCount as Conv>::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<_, <Entity::ParamCount as Conv>::TNum> =
let jacobian_x: GenericArray<_, <Model::ParamCount as Conv>::TNum> =
self.entity.jacobian(&self.x[i_x]).into();
let arr = jacobian_x.map(|v| GenericArray::<_, typenum::U1>::from_array([v]));
let mat = GenericMatrix::<Entity::Scalar, nalgebra::U1, Entity::ParamCount>::from_data(
let mat = GenericMatrix::<Model::Scalar, nalgebra::U1, Model::ParamCount>::from_data(
GenericArrayStorage(arr),
);
res.set_row(i_x, &mat.row(0));
Expand Down
54 changes: 27 additions & 27 deletions nacfahi/src/dyn_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity: FitModel, Weights>
LeastSquaresProblem<Entity::Scalar, Dyn, <Entity::ParamCount as Conv>::Nalg>
for DynOptimizationProblem<'_, Entity, Weights>
impl<Model: FitModel, Weights>
LeastSquaresProblem<Model::Scalar, Dyn, <Model::ParamCount as Conv>::Nalg>
for DynOptimizationProblem<'_, Model, Weights>
where
DefaultAllocator: Allocator<<Entity::ParamCount as Conv>::Nalg, nalgebra::U1>
DefaultAllocator: Allocator<<Model::ParamCount as Conv>::Nalg, nalgebra::U1>
+ Allocator<Dyn>
+ Allocator<Dyn, <Entity::ParamCount as Conv>::Nalg>,
+ Allocator<Dyn, <Model::ParamCount as Conv>::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 =
<DefaultAllocator as Allocator<Dyn, nalgebra::U1>>::Buffer<Entity::Scalar>;
<DefaultAllocator as Allocator<Dyn, nalgebra::U1>>::Buffer<Model::Scalar>;

type ParameterStorage = GenericArrayStorage<Entity::Scalar, Entity::ParamCount, typenum::U1>;
type ParameterStorage = GenericArrayStorage<Model::Scalar, Model::ParamCount, typenum::U1>;

type JacobianStorage = <DefaultAllocator as Allocator<
Dyn,
<Entity::ParamCount as Conv>::Nalg,
>>::Buffer<Entity::Scalar>;
<Model::ParamCount as Conv>::Nalg,
>>::Buffer<Model::Scalar>;

fn set_params(&mut self, x: &GenericMatrix<Entity::Scalar, Entity::ParamCount, typenum::U1>) {
let slice: &[Entity::Scalar] = x.data.as_ref();
fn set_params(&mut self, x: &GenericMatrix<Model::Scalar, Model::ParamCount, typenum::U1>) {
let slice: &[Model::Scalar] = x.data.as_ref();
let arr =
GenericArray::<Entity::Scalar, <Entity::ParamCount as Conv>::TNum>::from_slice(slice)
GenericArray::<Model::Scalar, <Model::ParamCount as Conv>::TNum>::from_slice(slice)
.clone();
self.entity.set_params(arr);
}

fn params(&self) -> GenericMatrix<Entity::Scalar, Entity::ParamCount, typenum::U1> {
let pars: GenericArray<Entity::Scalar, <Entity::ParamCount as Conv>::TNum> =
fn params(&self) -> GenericMatrix<Model::Scalar, Model::ParamCount, typenum::U1> {
let pars: GenericArray<Model::Scalar, <Model::ParamCount as Conv>::TNum> =
self.entity.get_params().into();
GenericMatrix::from_data(GenericArrayStorage(GenericArray::from_array([pars])))
}

fn residuals(&self) -> Option<OMatrix<Entity::Scalar, Dyn, nalgebra::U1>> {
let mat: OMatrix<Entity::Scalar, Dyn, nalgebra::U1> = self.x.zip_map(&self.y, |x, y| {
fn residuals(&self) -> Option<OMatrix<Model::Scalar, Dyn, nalgebra::U1>> {
let mat: OMatrix<Model::Scalar, Dyn, nalgebra::U1> = self.x.zip_map(&self.y, |x, y| {
(self.weights)(x, y) * (self.entity.evaluate(&x) - y)
});
Some(mat)
}

fn jacobian(&self) -> Option<OMatrix<Entity::Scalar, Dyn, <Entity::ParamCount as Conv>::Nalg>> {
fn jacobian(&self) -> Option<OMatrix<Model::Scalar, Dyn, <Model::ParamCount as Conv>::Nalg>> {
let mut res =
OMatrix::<Entity::Scalar, Dyn, <Entity::ParamCount as Conv>::Nalg>::zeros_generic(
OMatrix::<Model::Scalar, Dyn, <Model::ParamCount as Conv>::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<_, <Entity::ParamCount as Conv>::TNum> =
let jacobian_x: GenericArray<_, <Model::ParamCount as Conv>::TNum> =
self.entity.jacobian(&self.x[i_x]).into();
let arr = jacobian_x.map(|v| GenericArray::<_, typenum::U1>::from_array([v]));
let mat = GenericMatrix::<Entity::Scalar, nalgebra::U1, Entity::ParamCount>::from_data(
let mat = GenericMatrix::<Model::Scalar, nalgebra::U1, Model::ParamCount>::from_data(
GenericArrayStorage(arr),
);
res.set_row(i_x, &mat.row(0));
Expand Down
78 changes: 39 additions & 39 deletions nacfahi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity::Scalar, Self::Nalg, <Entity::ParamCount as Conv>::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<Model::Scalar, Self::Nalg, <Model::ParamCount as Conv>::Nalg> + 'd
where
Entity::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Entity::ParamCount as Conv>::Nalg, nalgebra::U1>,
Model::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Model::ParamCount as Conv>::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, <Entity::ParamCount as Conv>::Nalg>;
DefaultAllocator: Allocator<Self::Nalg, <Model::ParamCount as Conv>::Nalg>;
}

impl<const POINTS: usize> CreateProblem for typenum::Const<POINTS>
Expand All @@ -137,19 +137,19 @@ where
{
type Nalg = nalgebra::Const<POINTS>;

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<Entity::Scalar, Self::Nalg, <Entity::ParamCount as Conv>::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<Model::Scalar, Self::Nalg, <Model::ParamCount as Conv>::Nalg> + 'd
where
Entity::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Entity::ParamCount as Conv>::Nalg, nalgebra::U1>,
Model::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Model::ParamCount as Conv>::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, <Entity::ParamCount as Conv>::Nalg>,
DefaultAllocator: Allocator<Self::Nalg, <Model::ParamCount as Conv>::Nalg>,
{
ConstOptimizationProblem::<'d, Self, Entity, _> {
ConstOptimizationProblem::<'d, Self, Model, _> {
entity,
x,
y,
Expand All @@ -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<Entity::Scalar, Self::Nalg, <Entity::ParamCount as Conv>::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<Model::Scalar, Self::Nalg, <Model::ParamCount as Conv>::Nalg> + 'd
where
Entity::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Entity::ParamCount as Conv>::Nalg, nalgebra::U1>,
Model::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Model::ParamCount as Conv>::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, <Entity::ParamCount as Conv>::Nalg>,
DefaultAllocator: Allocator<Self::Nalg, <Model::ParamCount as Conv>::Nalg>,
{
ConstOptimizationProblem::<'d, Self, Entity, _> {
ConstOptimizationProblem::<'d, Self, Model, _> {
entity,
x,
y,
Expand All @@ -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<Entity::Scalar, Self::Nalg, <Entity::ParamCount as Conv>::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<Model::Scalar, Self::Nalg, <Model::ParamCount as Conv>::Nalg> + 'd
where
Entity::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Entity::ParamCount as Conv>::Nalg, nalgebra::U1>,
Model::Scalar: ComplexField + Copy,
DefaultAllocator: Allocator<<Model::ParamCount as Conv>::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, nalgebra::U1>,
DefaultAllocator: Allocator<Self::Nalg, <Entity::ParamCount as Conv>::Nalg>,
DefaultAllocator: Allocator<Self::Nalg, <Model::ParamCount as Conv>::Nalg>,
{
DynOptimizationProblem::<'d, Entity, _> {
DynOptimizationProblem::<'d, Model, _> {
entity,
x,
y,
Expand Down

0 comments on commit b20fdcd

Please sign in to comment.