1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::{
    complex::{Complex, Quaternion},
    matrix::Matrix2x2,
    traits::{Dot, Normalize},
};
use core::ops::{Add, Div, Mul, Neg};
use num_traits::{Num, NumCast, One, Zero};

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Moebius<T> {
    mat: Matrix2x2<T>,
}

impl<T> Moebius<T> {
    pub fn new(a: T, b: T, c: T, d: T) -> Self {
        Self::from([[a, b], [c, d]])
    }

    pub fn from_matrix(mat: Matrix2x2<T>) -> Self {
        Self { mat }
    }
    pub fn into_matrix(self) -> Matrix2x2<T> {
        self.mat
    }
    pub fn as_matrix(&self) -> &Matrix2x2<T> {
        &self.mat
    }
    pub fn as_mut_matrix(&mut self) -> &mut Matrix2x2<T> {
        &mut self.mat
    }

    pub fn from_array(array: [[T; 2]; 2]) -> Self {
        Self::from_matrix(Matrix2x2::from_array_of_arrays(array))
    }
    pub fn into_array(self) -> [[T; 2]; 2] {
        self.into_matrix().into_array_of_arrays()
    }
    pub fn as_array_of_arrays(&self) -> &[[T; 2]; 2] {
        &self.as_matrix().as_array_of_arrays()
    }
    pub fn as_mut_array_of_arrays(&mut self) -> &mut [[T; 2]; 2] {
        self.as_mut_matrix().as_mut_array_of_arrays()
    }

    pub fn from_tuple(tup: (T, T, T, T)) -> Self {
        let (a, b, c, d) = tup;
        Self::new(a, b, c, d)
    }
    pub fn into_tuple(self) -> (T, T, T, T) {
        let (ab, cd) = self.into_matrix().into_vector_of_vectors().into();
        let ((a, b), (c, d)) = (ab.into(), cd.into());
        (a, b, c, d)
    }
}

impl<T: Copy> Moebius<T> {
    pub fn a(&self) -> T {
        self.mat[(0, 0)]
    }
    pub fn b(&self) -> T {
        self.mat[(0, 1)]
    }
    pub fn c(&self) -> T {
        self.mat[(1, 0)]
    }
    pub fn d(&self) -> T {
        self.mat[(1, 1)]
    }
}

impl<T> Moebius<T> {
    pub fn a_ref(&self) -> &T {
        &self.mat[(0, 0)]
    }
    pub fn b_ref(&self) -> &T {
        &self.mat[(0, 1)]
    }
    pub fn c_ref(&self) -> &T {
        &self.mat[(1, 0)]
    }
    pub fn d_ref(&self) -> &T {
        &self.mat[(1, 1)]
    }

    pub fn a_mut(&mut self) -> &mut T {
        &mut self.mat[(0, 0)]
    }
    pub fn b_mut(&mut self) -> &mut T {
        &mut self.mat[(0, 1)]
    }
    pub fn c_mut(&mut self) -> &mut T {
        &mut self.mat[(1, 0)]
    }
    pub fn d_mut(&mut self) -> &mut T {
        &mut self.mat[(1, 1)]
    }
}

impl<T: Zero + One> Moebius<T> {
    pub fn identity() -> Self {
        Self::from(Matrix2x2::one())
    }
}

impl<T> Moebius<T>
where
    T: Add<Output = T> + Mul<Output = T> + Div<Output = T> + Copy,
{
    pub fn chain(self, other: Self) -> Self {
        Self::from(Dot::dot(self.mat, other.mat))
    }
}

impl<T> Moebius<T> {
    pub fn apply<U>(&self, x: U) -> U
    where
        T: Mul<U, Output = U> + Copy,
        U: Add<T, Output = U> + Div<Output = U> + Copy,
    {
        (self.a() * x + self.b()) / (self.c() * x + self.d())
    }
}

impl<T: Neg<Output = T> + Num + Copy> Moebius<T> {
    pub fn det(&self) -> T {
        self.mat.det()
    }
}
impl<T: Neg<Output = T> + Num + Copy> Normalize for Moebius<T> {
    fn normalize(self) -> Self {
        let det = self.det();
        (self.mat / det).into()
    }
}

impl<T> Moebius<Complex<T>>
where
    T: Neg<Output = T> + Num + Copy,
{
    pub fn deriv(&self, p: Complex<T>) -> Complex<T> {
        let u = self.a() * p + self.b();
        let d = self.c() * p + self.d();
        (self.a() * d - u * self.c()) / (d * d)
    }
}

impl<T> Moebius<Complex<T>>
where
    T: Neg<Output = T> + Num + NumCast + Copy,
{
    pub fn deriv_dir(&self, p: Quaternion<T>, v: Quaternion<T>) -> Quaternion<T> {
        let u = self.a() * p + self.b();
        let d = self.c() * p + self.d();
        let d2 = d.norm_sqr();
        let g1 = (self.a() * v) / d;
        let g21 = (self.c() * v).conj();
        let g22 = d.conj() * (d.dot(self.c() * v) * T::from(2.0).unwrap() / d2);
        let g2 = u * ((g21 - g22) / d2);
        g1 + g2
    }
}

impl<T> Moebius<T>
where
    T: Neg<Output = T> + Num + Copy,
{
    pub fn inv(self) -> Self {
        self.into_matrix().inv().into()
    }
}

impl<T> From<Matrix2x2<T>> for Moebius<T> {
    fn from(mat: Matrix2x2<T>) -> Self {
        Self::from_matrix(mat)
    }
}
impl<T> From<Moebius<T>> for Matrix2x2<T> {
    fn from(mo: Moebius<T>) -> Self {
        mo.into_matrix()
    }
}
impl<'a, T> From<&'a Moebius<T>> for &'a Matrix2x2<T> {
    fn from(mo: &'a Moebius<T>) -> Self {
        mo.as_matrix()
    }
}
impl<'a, T> From<&'a mut Moebius<T>> for &'a mut Matrix2x2<T> {
    fn from(mo: &'a mut Moebius<T>) -> Self {
        mo.as_mut_matrix()
    }
}
impl<T> From<[[T; 2]; 2]> for Moebius<T> {
    fn from(array: [[T; 2]; 2]) -> Self {
        Self::from_array(array)
    }
}
impl<T> From<Moebius<T>> for [[T; 2]; 2] {
    fn from(mo: Moebius<T>) -> Self {
        mo.into_array()
    }
}
impl<'a, T> From<&'a Moebius<T>> for &'a [[T; 2]; 2] {
    fn from(mo: &'a Moebius<T>) -> Self {
        mo.as_array_of_arrays()
    }
}
impl<'a, T> From<&'a mut Moebius<T>> for &'a mut [[T; 2]; 2] {
    fn from(mo: &'a mut Moebius<T>) -> Self {
        mo.as_mut_array_of_arrays()
    }
}