vmm 0.2.1

A math library focused on vectors and square matrices (Not in development)
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
pub mod math;
pub use math::*;

use std::ops::{IndexMut, Index, Add, Sub, Mul};
use crate::{SinCosTan, UnitValue, Vec2, Vec3, VecN};

/// Generic object representing a mathematical square matrix, with elements of type `T` and a fixed size `N`.
///
/// # Type Parameters
///
/// - `T`: The type of each element in the matrix.
/// - `N`: The fixed size of the matrix.
///
/// # Examples
///
/// ```
/// # use vmm::*;
/// let empty_mat: MatN<i32, 2> = MatN::new();
/// let filled_mat: MatN<f64, 2>= MatN::new_with(3.1415);
/// 
/// assert_eq!(empty_mat.to_mat(), [[0, 0], [0, 0]]);
/// assert_eq!(filled_mat.to_mat(), [[3.1415, 3.1415], [3.1415, 3.1415]]);
/// ```
///
/// # Notes
///
/// - Uses the type VecN as its rows.
///
/// # See Also
/// 
/// - [`VecN`].
/// - [`Mat2`], [`Mat3`] and [`Mat4`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MatN<T, const N: usize>
where
    T: Default + Copy,
{
    data: [VecN<T, N>; N]    
}
impl<T, const N: usize> MatN<T, N>
where
    T: Default + Copy,
{
    /// Creates a new instance of the `MatN` object with default values for each element.
    ///
    /// This function initializes a new matrix of fixed size `N` with each element set to its default value.
    ///
    /// # Returns
    ///
    /// A new `MatN` instance with elements initialized to their default values.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    ///
    /// let mat = MatN::<f64, 2>::new();
    ///
    /// assert_eq!(mat.to_mat(), [[0.0, 0.0], [0.0, 0.0]]);
    /// ```
    ///
    /// # Notes
    ///
    /// - The default value for each element is determined by the `Default` trait implementation for `T`.
    /// - The size of the matrix is fixed at compile time based on the constant `N`.
    pub fn new() -> Self
    {
        Self { data: [VecN::default(); N] }
    }
    
    /// Creates a new instance of the `MatN` object with `value` as the value for each element.
    ///
    /// This function initializes a new matrix of fixed size `N` with each element set to its default value.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to initialize the `VecN` with.
    ///
    /// # Returns
    ///
    /// A new `MatN` instance with elements initialized to `value`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    /// let mat = MatN::<f64, 3>::new_with(6.9);
    /// 
    /// assert_eq!(mat.to_mat(), [[6.9, 6.9, 6.9], [6.9, 6.9, 6.9], [6.9, 6.9, 6.9]]);
    /// ```
    ///
    /// # Notes
    ///
    /// - The size of the matrix is fixed at compile time based on the constant `N`.
    pub fn new_with(value: T) -> Self
    {
        Self { data: [VecN::new_with(value); N] } 
    }

    /// This function constructs a new matrix of fixed size `N` using the elements from the provided
    /// array of `VecN` reference `data`.
    ///
    /// # arguments
    ///
    /// * `data` - a reference to an array containing `VecN` to initialize the matrix.
    ///
    /// # returns
    ///
    /// a new `MatN` instance with elements copied from the provided array.
    ///
    /// # examples
    ///
    /// ```
    /// # use vmm::*;
    /// let array = [vec2![1.2, 4.20], vec2![6.0, 9.0]];
    /// let mat = MatN::from_mat_vec(&array);
    /// 
    /// assert_eq!(mat.to_mat_vec(), &array);
    /// ```
    ///
    /// # notes
    ///
    /// - the size of the matrix is fixed at compile time based on the constant `N`.
    pub fn from_mat_vec(data: &[VecN<T, N>; N]) -> Self
    {
        Self { data: *data }
    }

    /// This function constructs a new matrix of fixed size `N` using the elements from the provided
    /// 2D array reference `data`.
    ///
    /// # arguments
    ///
    /// * `data` - a reference to a 2D array to initialize the matrix.
    ///
    /// # returns
    ///
    /// a new `MatN` instance with elements copied from the provided array.
    ///
    /// # examples
    ///
    /// ```
    /// # use vmm::*;
    /// let array = [[1.2, 4.20], [6.0, 9.0]];
    /// let mat = MatN::from_mat(&array);
    /// 
    /// assert_eq!(mat.to_mat(), array);
    /// ```
    ///
    /// # notes
    ///
    /// - the size of the matrix is fixed at compile time based on the constant `N`.
    /// - This function creates a new `MatN` with copied elements, leaving the original 2D array unchanged 
    /// - maybe a little more expensive than `from_mat_vec()`.
    pub fn from_mat(data: &[[T; N]; N]) -> Self
    {
        let mut result = Self::new();
        
        for (vec, other) in result.data.iter_mut().zip(data.iter())
        {
            *vec = VecN::from_array(other);
        }
        
        result
    }
    
    /// Returns a reference to the underlying 2D array.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    ///
    /// let mat = mat2_raw![[4, 3], [1, 2]];
    /// 
    /// assert_eq!(mat.to_mat_vec(), &[vec2![4, 3], vec2![1, 2]]);
    /// ```
    pub fn to_mat_vec(&self) -> &[VecN<T, N>; N]
    {
        &self.data
    }

    /// Returns a mutable reference to the underlying 2D array.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    ///
    /// let mut mat = mat2_raw![[4, 3], [1, 2]];
    /// mat.to_mut_mat_vec()[0][1] = 180;
    /// 
    /// assert_eq!(mat.to_mat_vec(), &[vec2![4, 180], vec2![1, 2]]);
    /// ```
    pub fn to_mut_mat_vec(&mut self) -> &mut [VecN<T, N>; N]
    {
        &mut self.data
    }
    
    /// Returns a `copy` of the underlying raw 2D array.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    ///
    /// let mat = mat2_raw![[4, 3], [1, 2]];
    /// 
    /// assert_eq!(mat.to_mat(), [[4, 3], [1, 2]]);
    /// ```
    ///
    /// # Notes
    /// 
    /// - More expensive than `to_mat_vec`.
    pub fn to_mat(&self) -> [[T; N]; N]
    {
        let mut result = [[T::default(); N]; N];
        
        for (val, other) in result.iter_mut().zip(self.data.iter())
        {
            *val = other.to_arr().clone();
        }
        
        result
    }
    
    /// Fills all elements of `MatN` with `value`.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to fill the vector with.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    ///
    /// let mut mat = mat2_raw![[1.0, 3.0], [2.0, 4.0]];
    /// mat.fill(42.0);
    /// 
    /// assert_eq!(mat.to_mat(), [[42.0, 42.0], [42.0, 42.0]]);
    /// ```
    ///
    /// # Notes
    ///
    /// - This method directly delegates to the `fill` method of the underlying array.
    ///
    /// # See Also
    ///
    /// - [`fill`](https://doc.rust-lang.org/std/primitive.array.html#method.fill): The standard library method
    ///   used internally to fill the underlying array.
    pub fn fill(&mut self, value: T)
    {
        self.data.fill(VecN::new_with(value));
    }

    /// Transposes the matrix, swapping rows with columns.
    ///
    /// The transpose of a matrix is obtained by swapping its rows and columns.
    ///
    /// # Returns
    ///
    /// A new matrix representing the transpose of the original matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vmm::*;
    ///
    /// let mat = mat2_raw![[1.0, 2.0], [3.0, 4.0]];
    ///
    /// assert_eq!(mat.transpose().to_mat(), [[1.0, 3.0], [2.0, 4.0]]);
    /// ```
    ///
    /// # Notes
    ///
    /// - The transpose operation swaps the positions of each element across the main diagonal of the matrix.
    /// - This method assumes that the element type `T` implements `Clone` to create a new matrix.
    /// - This has a time complexity of `O(n^2)`.
    pub fn transpose(&self) -> Self
    {
        let mut result = self.clone();

        for i in 0..N {
            for j in 0..N
            {
                result[i][j] = self[j][i];
            }
        }

        result
    }
    pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, VecN<T, N>>
    {
        self.data.iter()
    }
    pub fn iter_mut<'a>(&'a mut self) -> std::slice::IterMut<'a, VecN<T, N>>
    {
        self.data.iter_mut()
    }
}

impl<T, const N: usize> Identity for MatN<T, N>
where
    T: Default + Copy + UnitValue,
{
    fn identity() -> Self 
    {
        let mut result = MatN::new();
         
        for i in 0..N
        {
            result[i][i] = T::unit_value();
        }
        
        result
    }
}

impl<T, const N: usize> Index<usize> for MatN<T, N>
where
    T: Default + Copy,
{
    type Output = VecN<T, N>; 
    
    fn index(&self, index: usize) -> &Self::Output 
    {
        &self.data[index]     
    }
}

impl<T, const N: usize> IndexMut<usize> for MatN<T, N>
where
    T: Default + Copy,
{
    fn index_mut(&mut self, index: usize) -> &mut Self::Output 
    {
        &mut self.data[index]     
    }
}

impl<T, const N: usize> Default for MatN<T, N>
where
    T: Default + Copy,
{
    fn default() -> Self 
    {
        Self { data: [VecN::default(); N] }     
    }
}

impl<T: Add<Output = T>, const N: usize> Add for MatN<T, N>
where
    T: Default + Copy,
{
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output
    {
        let mut result = self;
        
        for (val, other) in result.data.iter_mut().zip(rhs.data.iter())
        {
            *val = *val + *other; 
        }
        
        result
    } 
}
impl<T: Sub<Output = T>, const N: usize> Sub for MatN<T, N>
where
    T: Default + Copy,
{
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output
    {
        let mut result = self;
        
        for (val, other) in result.data.iter_mut().zip(rhs.data.iter())
        {
            *val = *val - *other; 
        }
        
        result
    } 
}
impl<T: Mul<Output = T>, const N: usize> Mul for MatN<T, N>
where
    T: Default + Copy + std::ops::Add<Output = T>,
{
    type Output = Self; 
    
    fn mul(self, rhs: Self) -> Self::Output 
    {
        let mut result = MatN::new();
        
        for i in 0..N {
            for j in 0..N {
                for k in 0..N
                {
                    result[i][j] = result[i][j] + self[i][k] * rhs[k][j];
                }
            }
        }

        result
    }
}

impl<T, const N: usize> MatVecMath<T, N> for MatN<T, N>
where
    T: Default + Copy
        + std::ops::Mul<Output = T>
        + std::ops::Add<Output = T>,
{
    fn mul_mat_vec(&self, vec: &VecN<T, N>) -> VecN<T, N> 
    {
        let mut result = VecN::new();

        for (i, vector) in self.data.iter().enumerate()
        {
            for (j, val) in vector.to_arr().iter().enumerate()
            {
                result[i] = result[i] + vec[j] * *val
            }
        }

        result
    } 
}

pub type Mat2<T> = MatN<T, 2>;
pub type Mat3<T> = MatN<T, 3>;
pub type Mat4<T> = MatN<T, 4>;

impl<T> MatTransforms<T, 2> for Mat3<T>
where
    T: Default + Copy + UnitValue + SinCosTan
        + std::ops::Neg<Output = T>
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>,
{
    fn translate(&self, vec: &Vec2<T>) -> Self 
    {
        let mut result = Mat3::identity();
        result[0][2] = vec[0];
        result[1][2] = vec[1];
        
        result
    }
    fn rotate(&self, angle: T, axis: &Vec3<T>) -> Self 
    {
        let x_angle = axis[0] * angle; 
        let y_angle = axis[1] * angle;
        let z_angle = axis[2] * angle;

        let x_cos = x_angle.coss();
        let x_sin = x_angle.sinn();
        let y_cos = y_angle.coss();
        let y_sin = y_angle.sinn();
        let z_cos = z_angle.coss();
        let z_sin = z_angle.sinn();

        let mut x_mat = Mat3::identity();
        x_mat[1][1] = x_cos;
        x_mat[1][2] = -x_sin;
        x_mat[2][1] = x_sin;
        x_mat[2][2] = x_cos;
        
        let mut y_mat = Mat3::identity();
        y_mat[0][0] = y_cos;
        y_mat[0][2] = y_sin;
        y_mat[2][0] = -y_sin;
        y_mat[2][2] = y_cos;
        
        let mut z_mat = Mat3::identity();
        z_mat[0][0] = z_cos;
        z_mat[0][1] = -z_sin;
        z_mat[1][0] = z_sin;
        z_mat[1][1] = z_cos;
        
        *self * (x_mat * y_mat * z_mat)
    }
    fn scale(&self, values: &Vec3<T>) -> Self 
    {
        let mut result = Mat3::identity();     
        
        result[0][0] = values[0];
        result[1][1] = values[1];
        result[2][2] = values[2];
        
        result
    }
}
impl<T> MatTransforms<T, 3> for Mat4<T>
where
    T: Default + Copy + UnitValue + SinCosTan
        + std::ops::Neg<Output = T>
        + std::ops::Add<Output = T>
        + std::ops::Mul<Output = T>,
{
    fn translate(&self, vec: &Vec3<T>) -> Self 
    {
        let mut result = Mat4::identity();             
        result[0][3] = vec[0];
        result[1][3] = vec[1];
        result[2][3] = vec[2];
        
        result
    }
    fn rotate(&self, angle: T, axis: &Vec3<T>) -> Self 
    {
        let x_angle = axis[0] * angle; 
        let y_angle = axis[1] * angle;
        let z_angle = axis[2] * angle;

        let x_cos = x_angle.coss();
        let x_sin = x_angle.sinn();
        let y_cos = y_angle.coss();
        let y_sin = y_angle.sinn();
        let z_cos = z_angle.coss();
        let z_sin = z_angle.sinn();

        let mut x_mat = Mat4::identity();
        x_mat[1][1] = x_cos;
        x_mat[1][2] = -x_sin;
        x_mat[2][1] = x_sin;
        x_mat[2][2] = x_cos;
        
        let mut y_mat = Mat4::identity();
        y_mat[0][0] = y_cos;
        y_mat[0][2] = y_sin;
        y_mat[2][0] = -y_sin;
        y_mat[2][2] = y_cos;
        
        let mut z_mat = Mat4::identity();
        z_mat[0][0] = z_cos;
        z_mat[0][1] = -z_sin;
        z_mat[1][0] = z_sin;
        z_mat[1][1] = z_cos;
        
        *self * (x_mat * y_mat * z_mat)
    }
    fn scale(&self, values: &Vec3<T>) -> Self 
    {
        let mut result = Mat4::identity();     
        
        result[0][0] = values[0];
        result[1][1] = values[1];
        result[2][2] = values[2];
        
        result
    }
}