glucose 0.2.3

multipurpose math and physics crate for my projects
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use crate::algebra::linear::{Scalar, Vector};
use fructose::algebra::lattice::Lattice;
use fructose::operators::{
    ClosedAdd, ClosedDiv, ClosedMul, ClosedNeg, ClosedOps, ClosedRem, ClosedSub,
};
use fructose::properties::helpers::identity::{One, Zero};
use fructose::properties::helpers::sign::Signed;
use std::alloc::Layout;
use std::fmt::{Display, Formatter};
use std::ops::{
    Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Rem, RemAssign, Sub,
    SubAssign,
};
use std::str::FromStr;

pub type SquareMatrix<T, const N: usize> = Matrix<T, { N }, { N }>;

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Matrix<T, const M: usize, const N: usize> {
    pub data: [[T; M]; N],
}

impl<T: Default + Copy, const M: usize, const N: usize> Default for Matrix<T, { M }, { N }> {
    #[inline]
    fn default() -> Self {
        Self {
            data: [[T::default(); M]; N],
        }
    }
}

impl<T: Display, const M: usize, const N: usize> Display for Matrix<T, { M }, { N }> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut string = String::new();
        for m in 0..M {
            &string.push_str("|");
            for n in 0..N {
                if n == N - 1 {
                    &string.push_str(&format!("{}", self[[m, n]]));
                    break;
                }
                &string.push_str(&format!("{} ", self[[m, n]]));
            }
            &string.push_str("|\n");
        }
        write!(f, "{}", string)
    }
}

impl<T, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    #[inline]

    pub const fn new(data: [[T; M]; N]) -> Self {
        Self { data }
    }

    #[inline]
    pub const fn len(&self) -> usize {
        M * N
    }

    #[inline]
    pub const fn size(&self) -> (usize, usize) {
        (M, N)
    }

    #[inline]
    pub fn layout() -> Layout {
        Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<[T; M]>())
            .unwrap()
    }

    #[inline]
    pub fn as_array(&self) -> &[T; N] {
        use std::convert::TryInto;
        self.as_slice().try_into().unwrap()
    }

    #[inline]
    pub fn as_array_mut(&mut self) -> &mut [T; N] {
        use std::convert::TryInto;
        self.as_slice_mut().try_into().unwrap()
    }

    #[inline]
    pub fn as_slice(&self) -> &[T] {
        // this is safe because the underlying data structure of a matrix has length M * N
        unsafe { std::slice::from_raw_parts(self as *const Self as *const T, M * N) }
    }

    #[inline]
    pub fn as_slice_mut(&mut self) -> &mut [T] {
        unsafe { std::slice::from_raw_parts_mut(self as *mut Self as *mut T, M * N) }
    }

    #[inline]
    pub const fn as_ptr(&self) -> *const T {
        self as *const Self as *const T
    }

    #[inline]
    pub fn as_mut_ptr(&mut self) -> *mut T {
        self as *mut Self as *mut T
    }

    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(
                self as *const Self as *const u8,
                M * N * std::mem::size_of::<Self>(),
            )
        }
    }
}

impl<T: Default + Copy, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    #[inline]
    pub fn to_vectors(&self) -> [Vector<T, { M }>; { N }] {
        let mut vectors = [Vector::default(); N];
        for n in 0..N {
            let vec = Vector::from(self.data[n]);
            vectors[0] = vec;
        }
        vectors
    }
}

impl<T: Scalar, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    #[inline]
    pub fn broadcast(value: T) -> Self {
        Self {
            data: [[value; M]; N],
        }
    }

    #[inline]
    pub fn map<F: Fn(T) -> T>(&self, f: F) -> Self {
        let mut vector = *self;
        vector
            .data
            .iter_mut()
            .for_each(|e| e.iter_mut().for_each(|e| *e = f(*e)));
        vector
    }

    #[inline]
    pub fn apply<F: Fn(T) -> T>(&mut self, f: F) {
        self.data
            .iter_mut()
            .for_each(|e| e.iter_mut().for_each(|e| *e = f(*e)));
    }
}

impl<T: Scalar + Signed, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    #[inline]
    pub fn abs(&mut self) {
        self.data
            .iter_mut()
            .for_each(|e| e.iter_mut().for_each(|e| *e = e.abs()));
    }

    // I can't find a good name for this, expect a rename in the future
    #[inline]
    pub fn abs_copy(&self) -> Self {
        let mut vec = *self;
        vec.abs();
        vec
    }
}

impl<T: Scalar + Lattice, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
    #[inline]
    pub fn clamp(&mut self, min: Self, max: Self) {
        for j in 0..M {
            for i in 0..N {
                self[[j, i]] = *self[[j, i]]
                    .partial_min(&min[[j, i]])
                    .unwrap()
                    .partial_max(&max[[j, i]])
                    .unwrap()
            }
        }
    }

    #[inline]
    pub fn clamped(&self, min: Self, max: Self) -> Self {
        let mut mat = *self;
        mat.clamp(min, max);
        mat
    }

    /// returns a new VecN' with each component having the bigger number from either VecN1 or VecN2
    #[inline]
    pub fn max_by_component(&self, other: &Self) -> Self {
        let mut mat = *self;
        for j in 0..M {
            for i in 0..N {
                mat[[j, i]] = *self[[j, i]].partial_max(&other[[j, i]]).unwrap();
            }
        }
        mat
    }

    /// returns a new VecN' with each component having the smaller number from either VecN1 or VecN2
    #[inline]
    pub fn min_by_component(&self, other: &Self) -> Self {
        let mut mat = *self;
        for j in 0..M {
            for i in 0..N {
                mat[[j, i]] = *self[[j, i]].partial_min(&other[[j, i]]).unwrap();
            }
        }
        mat
    }
}

impl<T: Scalar + Zero + One + ClosedOps, const M: usize> SquareMatrix<T, { M }> {
    #[inline]
    pub fn mul_identity() -> Self {
        let mut mat = Self::zero();
        for m in 0..M {
            mat[[m, m]] = T::one();
        }
        mat
    }

    #[inline]
    pub fn determinant(&self) -> T {
        match M {
            0 => T::one(),
            1 => self[[0, 0]],
            2 => self[[0, 0]] * self[[1, 1]] - self[[1, 0]] * self[[1, 0]],
            3 => {
                let e11 = self[[0, 0]];
                let e12 = self[[0, 1]];
                let e13 = self[[0, 2]];

                let e21 = self[[1, 0]];
                let e22 = self[[1, 1]];
                let e23 = self[[1, 2]];

                let e31 = self[[2, 0]];
                let e32 = self[[2, 1]];
                let e33 = self[[2, 2]];

                let minor_1 = e22 * e33 - e32 * e23;
                let minor_2 = e21 * e33 - e31 * e23;
                let minor_3 = e21 * e32 - e31 * e22;

                e11 * minor_1 - e12 * minor_2 + e13 * minor_3
            }
            _ => {
                unimplemented!("TODO: Add LU Decomposition")
            }
        }
    }
}

impl<T, const M: usize, const N: usize> Index<[usize; 2]> for Matrix<T, { M }, { N }> {
    type Output = T;

    fn index(&self, index: [usize; 2]) -> &Self::Output {
        &self.data[index[1]][index[0]]
    }
}

impl<T, const M: usize, const N: usize> IndexMut<[usize; 2]> for Matrix<T, { M }, { N }> {
    fn index_mut(&mut self, index: [usize; 2]) -> &mut Self::Output {
        &mut self.data[index[1]][index[0]]
    }
}

impl<T: Scalar + ClosedAdd, const M: usize, const N: usize> Add for Matrix<T, { M }, { N }> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        let mut mat = Matrix::default();
        for m in 0..M {
            for n in 0..N {
                mat[[m, n]] = self[[m, n]] + rhs[[m, n]];
            }
        }
        mat
    }
}

impl<T: Scalar + ClosedAdd, const M: usize, const N: usize> AddAssign for Matrix<T, { M }, { N }> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        for m in 0..M {
            for n in 0..N {
                self[[m, n]] += rhs[[m, n]];
            }
        }
    }
}

impl<T: Scalar + ClosedSub, const M: usize, const N: usize> Sub for Matrix<T, { M }, { N }> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        let mut mat = Matrix::default();
        for m in 0..M {
            for n in 0..N {
                mat[[m, n]] = self[[m, n]] - rhs[[m, n]];
            }
        }
        mat
    }
}

impl<T: Scalar + ClosedSub, const M: usize, const N: usize> SubAssign for Matrix<T, { M }, { N }> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        for m in 0..M {
            for n in 0..N {
                self[[m, n]] -= rhs[[m, n]];
            }
        }
    }
}

impl<T: Scalar + ClosedMul + ClosedAdd, const M: usize, const N: usize, const P: usize>
    Mul<Matrix<T, { N }, { P }>> for Matrix<T, { M }, { N }>
{
    type Output = Matrix<T, { M }, { P }>;
    #[inline]
    fn mul(self, rhs: Matrix<T, { N }, { P }>) -> Self::Output {
        let mut mat = Matrix::default();
        for m in 0..M {
            for p in 0..P {
                for n in 0..N {
                    mat[[m, p]] += self[[m, n]] * rhs[[n, p]];
                }
            }
        }
        mat
    }
}

impl<T: Scalar + ClosedMul, const M: usize, const N: usize> Mul<T> for Matrix<T, { M }, { N }> {
    type Output = Self;

    fn mul(self, rhs: T) -> Self::Output {
        let mut mat = self;
        for m in 0..M {
            for n in 0..N {
                mat[[m, n]] *= rhs
            }
        }
        self
    }
}

impl<T: Scalar + ClosedMul, const M: usize, const N: usize> MulAssign<T>
    for Matrix<T, { M }, { N }>
{
    fn mul_assign(&mut self, rhs: T) {
        for m in 0..M {
            for n in 0..N {
                self[[m, n]] *= rhs
            }
        }
    }
}

impl<T: Scalar + ClosedDiv, const M: usize, const N: usize> Div<T> for Matrix<T, { M }, { N }> {
    type Output = Self;

    fn div(self, rhs: T) -> Self::Output {
        let mut mat = self;
        for m in 0..M {
            for n in 0..N {
                mat[[m, n]] /= rhs
            }
        }
        self
    }
}

impl<T: Scalar + ClosedDiv, const M: usize, const N: usize> DivAssign<T>
    for Matrix<T, { M }, { N }>
{
    fn div_assign(&mut self, rhs: T) {
        for m in 0..M {
            for n in 0..N {
                self[[m, n]] /= rhs
            }
        }
    }
}

impl<T: Scalar + ClosedNeg, const M: usize, const N: usize> Neg for Matrix<T, { M }, { N }> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        let mut mat = self;
        for m in 0..M {
            for n in 0..N {
                mat[[m, n]] = -mat[[m, n]]
            }
        }
        mat
    }
}

impl<T: Scalar + ClosedRem, const M: usize, const N: usize> Rem<T> for Matrix<T, { M }, { N }> {
    type Output = Self;

    fn rem(self, rhs: T) -> Self::Output {
        let mut mat = self;
        for m in 0..M {
            for n in 0..N {
                mat[[m, n]] %= rhs;
            }
        }
        mat
    }
}

impl<T: Scalar + ClosedRem, const M: usize, const N: usize> RemAssign<T>
    for Matrix<T, { M }, { N }>
{
    fn rem_assign(&mut self, rhs: T) {
        for m in 0..M {
            for n in 0..N {
                self[[m, n]] %= rhs;
            }
        }
    }
}

// // this sadly doesnt work really
// // TODO: number conversion
// impl<T: Default + Copy, const M: usize, const N: usize> Matrix<T, { M }, { N }> {
//     #[inline]
//     pub fn to_other_type<U: Default + Copy + CastInts>(&self) -> Matrix<U, { M }, { N }> {
//         let mut mat = Matrix::default();
//
//         for m in 0..M {
//             for n in 0..N {
//                 mat[[m, n]] = U::try_from(self[[m, n]]).unwrap_or_default()
//             }
//         }
//
//         mat
//     }
// }

impl<T: FromStr + Default + Copy, const M: usize, const N: usize> From<String>
    for Matrix<T, { M }, { N }>
{
    fn from(rhs: String) -> Self {
        let mut mat = Matrix::default();

        rhs.split(";").into_iter().enumerate().for_each(|(n, col)| {
            col.split(" ").into_iter().enumerate().for_each(|(m, val)| {
                mat[[m, n]] = val.to_string().parse().unwrap_or_else(|t| T::default())
            })
        });

        mat
    }
}

#[cfg(test)]
mod mat_tests {
    use crate::algebra::linear::{Matrix, Vector};

    #[test]
    fn parse() {
        let vec_string = String::from("2 3 -5");
        let mat_string = String::from("2 3;-1 4;0 -2");
        let vec = Vector::<i32, 3>::from(vec_string);
        let mat = Matrix::<i32, 2, 3>::from(mat_string);
        assert_eq!(vec, Vector::new([[2, 3, -5]]));
        assert_eq!(mat, Matrix::new([[2, 3], [-1, 4], [0, -2]]));
    }
}