src/nalgebra_support.rs

Fri, 20 Dec 2024 16:14:17 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 20 Dec 2024 16:14:17 -0500
branch
dev
changeset 79
d63e40672dd6
parent 78
cebedc4a8331
permissions
-rw-r--r--

AXPY as LinSpace attempts, difficulties with Pairs and nalgebra

nalgebra should allow various storages, so InstanceMut as &self, but that won't work.

5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
1 /*!
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
2 Integration with nalgebra.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
3
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
4 This module mainly implements [`Euclidean`], [`Norm`], [`Dot`], [`Linear`], etc. for [`nalgebra`]
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
5 matrices and vectors.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
6 It also provides [`ToNalgebraRealField`] as a vomit-inducingly ugly workaround to nalgebra
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
7 force-feeding its own versions of the same basic mathematical methods on `f32` and `f64` as
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
8 [`num_traits`] does.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
9 */
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 use nalgebra::{
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 Matrix, Storage, StorageMut, OMatrix, Dim, DefaultAllocator, Scalar,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
13 ClosedAddAssign, ClosedMulAssign, SimdComplexField, Vector, OVector, RealField,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 LpNorm, UniformNorm
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 use nalgebra::Norm as NalgebraNorm;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 use nalgebra::base::constraint::{
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 ShapeConstraint, SameNumberOfRows, SameNumberOfColumns
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 use nalgebra::base::dimension::*;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 use nalgebra::base::allocator::Allocator;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 use std::ops::Mul;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 use num_traits::identities::{Zero, One};
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 use crate::linops::*;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
25 use crate::euclidean::*;
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
26 use crate::mapping::{Space, BasicDecomposition};
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 use crate::types::Float;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 use crate::norms::*;
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
29 use crate::instance::Instance;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
31 impl<SM,N,M,E> Space for Matrix<E,N,M,SM>
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
32 where
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
33 SM: Storage<E,N,M> + Clone,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
34 N : Dim, M : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
35 DefaultAllocator : Allocator<N,M>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
36 {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
37 type Decomp = BasicDecomposition;
13
465fa2121ccb Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
38 }
465fa2121ccb Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
39
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
40 impl<SM,SV,N,M,K,E> Mapping<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
41 where SM: Storage<E,N,M>, SV: Storage<E,M,K> + Clone,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
42 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
43 DefaultAllocator : Allocator<N,K>,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
44 DefaultAllocator : Allocator<M,K>,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
45 DefaultAllocator : Allocator<N,M>,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
46 DefaultAllocator : Allocator<M,N> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 type Codomain = OMatrix<E,N,K>;
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
48
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
49 #[inline]
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
50 fn apply<I : Instance<Matrix<E,M,K,SV>>>(
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
51 &self, x : I
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
52 ) -> Self::Codomain {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
53 x.either(|owned| self.mul(owned), |refr| self.mul(refr))
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
54 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
55 }
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
56
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
57
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
58 impl<'a, SM,SV,N,M,K,E> Linear<Matrix<E,M,K,SV>> for Matrix<E,N,M,SM>
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
59 where SM: Storage<E,N,M>, SV: Storage<E,M,K> + Clone,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
60 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + ClosedAddAssign + ClosedMulAssign,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
61 DefaultAllocator : Allocator<N,K>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
62 DefaultAllocator : Allocator<M,K>,
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
63 DefaultAllocator : Allocator<N,M>,
79
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
64 DefaultAllocator : Allocator<M,N>
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
65 {
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
66 type LinCodomain = OMatrix<E,N,K>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68
79
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
69 impl<SM,SV1,N,M,K,E> GEMV<Matrix<E,M,K,SV1>> for Matrix<E,N,M,SM>
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
70 where SM: Storage<E,N,M>, SV1: Storage<E,M,K> + Clone,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
71 N : Dim, M : Dim, K : Dim, E : Scalar + Zero + One + Float,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
72 DefaultAllocator : Allocator<N,K>,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
73 DefaultAllocator : Allocator<M,K>,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
74 DefaultAllocator : Allocator<N,M>,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
75 DefaultAllocator : Allocator<M,N> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 #[inline]
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
78 fn gemv<I : Instance<Matrix<E,M,K,SV1>>>(
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
79 &self, y : &mut Matrix<E,N,K,SV2>, α : E, x : I, β : E
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
80 ) {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
81 x.eval(|x̃| Matrix::gemm(y, α, self, x̃, β))
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 #[inline]
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
85 fn apply_mut<'a, I : Instance<Matrix<E,M,K,SV1>>>(&self, y : &mut Matrix<E,N,K,SV2>, x : I) {
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
86 x.eval(|x̃| self.mul_to(x̃, y))
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89
79
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
90 impl<SM,M,E> AXPY for Vector<E,M,SM>
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
91 where SM: StorageMut<E,M> + Clone,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
92 M : Dim, E : Scalar + Zero + One + Float,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
93 DefaultAllocator : Allocator<M> {
79
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
94 type Field = E;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 #[inline]
79
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
97 fn axpy<I : Instance<Vector<E,M,SM>>>(&mut self, α : E, x : I, β : E) {
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
98 x.eval(|x̃| Matrix::axpy(self, α, x̃, β))
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 #[inline]
79
d63e40672dd6 AXPY as LinSpace attempts, difficulties with Pairs and nalgebra
Tuomo Valkonen <tuomov@iki.fi>
parents: 78
diff changeset
102 fn copy_from<I : Instance<Vector<E,M,SM>>>(&mut self, y : I) {
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
103 y.eval(|ỹ| Matrix::copy_from(self, ỹ))
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
107 /* Implemented automatically as Euclidean.
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
108 impl<SM,M,E> Projection<E, L2> for Vector<E,M,SM>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
109 where SM: StorageMut<E,M> + Clone,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
110 M : Dim, E : Scalar + Zero + One + Float + RealField,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
111 DefaultAllocator : Allocator<M> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
112 #[inline]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
113 fn proj_ball_mut(&mut self, ρ : E, _ : L2) {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
114 let n = self.norm(L2);
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
115 if n > ρ {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
116 self.iter_mut().for_each(|v| *v *= ρ/n)
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
117 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
118 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
119 }*/
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
120
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 impl<SM,M,E> Projection<E, Linfinity> for Vector<E,M,SM>
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
122 where SM: StorageMut<E,M> + Clone,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
123 M : Dim, E : Scalar + Zero + One + Float + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
124 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 fn proj_ball_mut(&mut self, ρ : E, _ : Linfinity) {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 self.iter_mut().for_each(|v| *v = num_traits::clamp(*v, -ρ, ρ))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130
78
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
131 impl<'own,SV,SM,N,M,E> Adjointable<Vector<E,M,SV>, E>
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 for Matrix<E,N,M,SM>
78
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
133 where
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
134 SM: Storage<E,N,M>, SV: Storage<E,M> + Clone,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
135 N : Dim, M : Dim, E : Scalar + Zero + One + RealField + Float,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
136 DefaultAllocator : Allocator<M>,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
137 DefaultAllocator : Allocator<N>,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
138 DefaultAllocator : Allocator<N,M>,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
139 DefaultAllocator : Allocator<M,N>,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
140 OMatrix<E,M,N> : Linear<OVector<E, N>, Codomain = OVector<E, M>>,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
141 {
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
142 type AdjointCodomain = OVector<E, M>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143 type Adjoint<'a> = OMatrix<E,M,N> where SM : 'a;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146 fn adjoint(&self) -> Self::Adjoint<'_> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147 Matrix::adjoint(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
148 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151 impl<E,M,S,Si> Dot<Vector<E,M,Si>,E>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 where M : Dim,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
154 E : Float + Scalar + Zero + One,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 S : Storage<E,M>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 Si : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
157 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 fn dot(&self, other : &Vector<E,M,Si>) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 Vector::<E,M,S>::dot(self, other)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 /// This function is [`nalgebra::EuclideanNorm::metric_distance`] without the `sqrt`.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 fn metric_distance_squared<T, R1, C1, S1, R2, C2, S2>(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 /*ed: &EuclideanNorm,*/
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 m1: &Matrix<T, R1, C1, S1>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170 m2: &Matrix<T, R2, C2, S2>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 ) -> T::SimdRealField
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 where
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 T: SimdComplexField,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 R1: Dim,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175 C1: Dim,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 S1: Storage<T, R1, C1>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177 R2: Dim,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 C2: Dim,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 S2: Storage<T, R2, C2>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180 ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2>,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181 {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 m1.zip_fold(m2, T::SimdRealField::zero(), |acc, a, b| {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
183 let diff = a - b;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 acc + diff.simd_modulus_squared()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185 })
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 // TODO: should allow different input storages in `Euclidean`.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
190 impl<E,M,S> Euclidean<E>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 where M : Dim,
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
193 S : StorageMut<E,M> + Clone,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
194 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
195 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 type Output = OVector<E, M>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200 fn similar_origin(&self) -> OVector<E, M> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 OVector::zeros_generic(M::from_usize(self.len()), Const)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 fn norm2_squared(&self) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 Vector::<E,M,S>::norm_squared(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 fn dist2_squared(&self, other : &Self) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 metric_distance_squared(self, other)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
213 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
214
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215 impl<E,M,S> StaticEuclidean<E>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 where M : DimName,
59
9226980e45a7 Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents: 56
diff changeset
218 S : StorageMut<E,M> + Clone,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
219 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
220 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
223 fn origin() -> OVector<E, M> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224 OVector::zeros()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
227
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
228 /// The default norm for `Vector` is [`L2`].
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
229 impl<E,M,S> Normed<E>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
230 for Vector<E,M,S>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
231 where M : Dim,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
232 S : Storage<E,M> + Clone,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
233 E : Float + Scalar + Zero + One + RealField,
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
234 DefaultAllocator : Allocator<M> {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
235
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
236 type NormExp = L2;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
237
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
238 #[inline]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
239 fn norm_exponent(&self) -> Self::NormExp {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
240 L2
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
241 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
242
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
243 #[inline]
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
244 fn is_zero(&self) -> bool {
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
245 Vector::<E,M,S>::norm_squared(self) == E::ZERO
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
246 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
247 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
248
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
249 impl<E,M,S> HasDual<E>
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
250 for Vector<E,M,S>
78
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
251 where
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
252 M : Dim,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
253 S : Storage<E,M> + Clone,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
254 E : Float + Scalar + Zero + One + RealField,
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
255 DefaultAllocator : Allocator<M>
cebedc4a8331 Try to use HasDual with adjoints. Problem with nalgebra Instances.
Tuomo Valkonen <tuomov@iki.fi>
parents: 60
diff changeset
256 {
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
257 // TODO: Doesn't work with different storage formats.
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
258 type DualSpace = Self;
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
259 }
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
260
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
261 impl<E,M,S> Norm<E, L1>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
262 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
263 where M : Dim,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
264 S : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
265 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
266 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
267
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
268 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
269 fn norm(&self, _ : L1) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 LpNorm(1).norm(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
271 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
272 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
273
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
274 impl<E,M,S> Dist<E, L1>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
275 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
276 where M : Dim,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
277 S : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
278 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
279 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
280 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
281 fn dist(&self, other : &Self, _ : L1) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
282 LpNorm(1).metric_distance(self, other)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
283 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
284 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
285
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
286 impl<E,M,S> Norm<E, L2>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
287 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
288 where M : Dim,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
289 S : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
290 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
291 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
292
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
293 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
294 fn norm(&self, _ : L2) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
295 LpNorm(2).norm(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
296 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
297 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
298
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
299 impl<E,M,S> Dist<E, L2>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
300 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
301 where M : Dim,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
302 S : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
303 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
304 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
305 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
306 fn dist(&self, other : &Self, _ : L2) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
307 LpNorm(2).metric_distance(self, other)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
308 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
309 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
310
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
311 impl<E,M,S> Norm<E, Linfinity>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
312 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
313 where M : Dim,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
314 S : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
315 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
316 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
317
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
318 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
319 fn norm(&self, _ : Linfinity) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
320 UniformNorm.norm(self)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
321 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
322 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
323
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
324 impl<E,M,S> Dist<E, Linfinity>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
325 for Vector<E,M,S>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
326 where M : Dim,
60
848ecc05becf More convexity, normed spaces, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
327 S : Storage<E,M>,
56
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
328 E : Float + Scalar + Zero + One + RealField,
5e3c1874797d Update dependencies. Nalgebra update required code changes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 13
diff changeset
329 DefaultAllocator : Allocator<M> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
330 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
331 fn dist(&self, other : &Self, _ : Linfinity) -> E {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
332 UniformNorm.metric_distance(self, other)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
333 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
334 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
335
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
336 /// Helper trait to hide the symbols of [`nalgebra::RealField`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
337 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
338 /// By assuming `ToNalgebraRealField` intead of `nalgebra::RealField` as a trait bound,
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
339 /// functions can piggyback `nalgebra::RealField` without exponsing themselves to it.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
340 /// Thus methods from [`num_traits`] can be used directly without similarly named methods
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
341 /// from [`nalgebra`] conflicting with them. Only when absolutely necessary to work with
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
342 /// nalgebra, one can convert to the nalgebra view of the same type using the methods of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
343 /// this trait.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
344 pub trait ToNalgebraRealField : Float {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
345 /// The nalgebra type corresponding to this type. Usually same as `Self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
346 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
347 /// This type only carries `nalgebra` traits.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
348 type NalgebraType : RealField;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
349 /// The “mixed” type corresponding to this type. Usually same as `Self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
350 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
351 /// This type carries both `num_traits` and `nalgebra` traits.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
352 type MixedType : RealField + Float;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
353
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
354 /// Convert to the nalgebra view of `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
355 fn to_nalgebra(self) -> Self::NalgebraType;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
356
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
357 /// Convert to the mixed (nalgebra and num_traits) view of `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
358 fn to_nalgebra_mixed(self) -> Self::MixedType;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
359
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
360 /// Convert from the nalgebra view of `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
361 fn from_nalgebra(t : Self::NalgebraType) -> Self;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
362
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
363 /// Convert from the mixed (nalgebra and num_traits) view to `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
364 fn from_nalgebra_mixed(t : Self::MixedType) -> Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
365 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
366
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
367 impl ToNalgebraRealField for f32 {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
368 type NalgebraType = f32;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
369 type MixedType = f32;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
370
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
371 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
372 fn to_nalgebra(self) -> Self::NalgebraType { self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
373
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
374 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
375 fn to_nalgebra_mixed(self) -> Self::MixedType { self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
376
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
377 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
378 fn from_nalgebra(t : Self::NalgebraType) -> Self { t }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
379
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
380 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
381 fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
382
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
383 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
384
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
385 impl ToNalgebraRealField for f64 {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
386 type NalgebraType = f64;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
387 type MixedType = f64;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
388
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
389 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
390 fn to_nalgebra(self) -> Self::NalgebraType { self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
391
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
392 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
393 fn to_nalgebra_mixed(self) -> Self::MixedType { self }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
394
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
395 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
396 fn from_nalgebra(t : Self::NalgebraType) -> Self { t }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
397
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
398 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
399 fn from_nalgebra_mixed(t : Self::MixedType) -> Self { t }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
400 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
401

mercurial