1use super::*;
3
4pub type ColumnVector<T, const N: usize> = Matrix<T, N, 1>;
86
87impl<T> ColumnVector<T, 1>
88where
89 T: One,
90{
91 pub fn unit_x() -> Self {
93 Matrix([[T::one()]])
94 }
95}
96
97impl<T> ColumnVector<T, 2>
98where
99 T: One + Zero,
100{
101 pub fn unit_x() -> Self {
103 Matrix([[T::one(), T::zero()]])
104 }
105
106 pub fn unit_y() -> Self {
108 Matrix([[T::zero(), T::one()]])
109 }
110}
111
112impl<T> ColumnVector<T, 3>
113where
114 T: One + Zero,
115{
116 pub fn unit_x() -> Self {
118 Matrix([[T::one(), T::zero(), T::zero()]])
119 }
120
121 pub fn unit_y() -> Self {
123 Matrix([[T::zero(), T::one(), T::zero()]])
124 }
125
126 pub fn unit_z() -> Self {
128 Matrix([[T::zero(), T::zero(), T::one()]])
129 }
130}
131
132impl<T> ColumnVector<T, 4>
133where
134 T: One + Zero,
135{
136 pub fn unit_x() -> Self {
138 Matrix([[T::one(), T::zero(), T::zero(), T::zero()]])
139 }
140
141 pub fn unit_y() -> Self {
143 Matrix([[T::zero(), T::one(), T::zero(), T::zero()]])
144 }
145
146 pub fn unit_z() -> Self {
148 Matrix([[T::zero(), T::zero(), T::one(), T::zero()]])
149 }
150
151 pub fn unit_w() -> Self {
153 Matrix([[T::zero(), T::zero(), T::zero(), T::one()]])
154 }
155}
156
157impl<T, const N: usize> ColumnVector<T, N> {
158 pub fn into_inner(self) -> [T; N] {
160 let Matrix([inner]) = self;
161 inner
162 }
163
164 pub fn from_fn<Out, F>(mut f: F) -> ColumnVector<Out, N>
167 where
168 F: FnMut(usize) -> Out,
169 {
170 let mut to = MaybeUninit::<ColumnVector<Out, N>>::uninit();
171 let top = &mut to as *mut MaybeUninit<Matrix<Out, N, 1>> as *mut Out;
172 for i in 0..N {
173 unsafe { top.add(i).write(f(i)) }
174 }
175 unsafe { to.assume_init() }
176 }
177
178 pub fn indexed_map<Out, F>(self, mut f: F) -> ColumnVector<Out, N>
179 where
180 F: FnMut(usize, T) -> Out,
181 {
182 let mut from = MaybeUninit::new(self);
183 let mut to = MaybeUninit::<ColumnVector<Out, N>>::uninit();
184 let fromp = &mut from as *mut MaybeUninit<Matrix<T, N, 1>> as *mut MaybeUninit<T>;
185 let top = &mut to as *mut MaybeUninit<Matrix<Out, N, 1>> as *mut Out;
186 for i in 0..N {
187 unsafe {
188 top.add(i).write(f(
189 i,
190 fromp.add(i).replace(MaybeUninit::uninit()).assume_init(),
191 ));
192 }
193 }
194 unsafe { to.assume_init() }
195 }
196}
197
198impl<T, const N: usize> ColumnVector<T, N>
215where
216 T: Clone,
217{
218 pub fn first<const M: usize>(&self) -> ColumnVector<T, { M }> {
223 if M > N {
224 panic!("attempt to return {} elements from a {}-vector", M, N);
225 }
226 let mut head = MaybeUninit::<ColumnVector<T, { M }>>::uninit();
227 let headp = &mut head as *mut MaybeUninit<Matrix<T, M, 1>> as *mut T;
228 for i in 0..M {
229 unsafe {
230 headp.add(i).write(self.0[0][i].clone());
231 }
232 }
233 unsafe { head.assume_init() }
234 }
235
236 pub fn last<const M: usize>(&self) -> ColumnVector<T, { M }> {
241 if M > N {
242 panic!("attempt to return {} elements from a {}-vector", M, N);
243 }
244 let mut tail = MaybeUninit::<ColumnVector<T, { M }>>::uninit();
245 let tailp = &mut tail as *mut MaybeUninit<Matrix<T, M, 1>> as *mut T;
246 for i in 0..M {
247 unsafe {
248 tailp.add(i + N - M).write(self.0[0][i].clone());
249 }
250 }
251 unsafe { tail.assume_init() }
252 }
253}
254
255impl<T> ColumnVector3<T>
256where
257 T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Clone,
258{
259 pub fn cross(self, rhs: ColumnVector3<T>) -> Self {
261 let Matrix([[x0, y0, z0]]) = self;
262 let Matrix([[x1, y1, z1]]) = rhs;
263 Self::from([[
264 (y0.clone() * z1.clone()) - (z0.clone() * y1.clone()),
265 (z0 * x1.clone()) - (x0.clone() * z1),
266 (x0 * y1) - (y0 * x1),
267 ]])
268 }
269}
270
271impl<T, const N: usize> ColumnVector<T, N>
272where
273 T: Clone + PartialOrd,
274{
275 pub fn argmax(&self) -> (usize, T) {
278 let mut i_max = 0;
279 let mut v_max = self.0[0][0].clone();
280 for i in 1..N {
281 if self.0[0][i] > v_max {
282 i_max = i;
283 v_max = self.0[0][i].clone();
284 }
285 }
286 (i_max, v_max)
287 }
288
289 pub fn max(&self) -> T {
291 let mut v_max = self.0[0][0].clone();
292 for i in 1..N {
293 if self.0[0][i] > v_max {
294 v_max = self.0[0][i].clone();
295 }
296 }
297 v_max
298 }
299
300 pub fn argmin(&self) -> (usize, T) {
303 let mut i_min = 0;
304 let mut v_min = self.0[0][0].clone();
305 for i in 1..N {
306 if self.0[0][i] < v_min {
307 i_min = i;
308 v_min = self.0[0][i].clone();
309 }
310 }
311 (i_min, v_min)
312 }
313
314 pub fn min(&self) -> T {
316 let mut v_min = self.0[0][0].clone();
317 for i in 1..N {
318 if self.0[0][i] < v_min {
319 v_min = self.0[0][i].clone();
320 }
321 }
322 v_min
323 }
324}
325
326impl<T, const N: usize> From<[T; N]> for ColumnVector<T, N> {
327 fn from(array: [T; N]) -> Self {
328 Matrix([array])
329 }
330}
331
332impl<T, const N: usize> From<ColumnVector<T, N>> for [T; N] {
333 fn from(v: ColumnVector<T, N>) -> [T; N] {
334 let Matrix([vec]) = v;
335 vec
336 }
337}
338
339pub type ColumnVector1<T> = ColumnVector<T, 1>;
341
342pub type ColumnVector2<T> = ColumnVector<T, 2>;
344
345impl<T> ColumnVector2<T> {
346 pub fn from_vec1(v: ColumnVector1<T>, y: T) -> Self {
348 let Matrix([[x]]) = v;
349 Matrix([[x, y]])
350 }
351}
352
353pub type ColumnVector3<T> = ColumnVector<T, 3>;
355
356impl<T> ColumnVector3<T> {
357 pub fn from_vec1(v: ColumnVector1<T>, y: T, z: T) -> Self {
359 let Matrix([[x]]) = v;
360 Matrix([[x, y, z]])
361 }
362
363 pub fn from_vec2(v: ColumnVector2<T>, z: T) -> Self {
365 let Matrix([[x, y]]) = v;
366 Matrix([[x, y, z]])
367 }
368}
369
370pub type ColumnVector4<T> = ColumnVector<T, 4>;
372
373impl<T> ColumnVector4<T> {
374 pub fn from_vec1(v: ColumnVector1<T>, y: T, z: T, w: T) -> Self {
376 let Matrix([[x]]) = v;
377 Matrix([[x, y, z, w]])
378 }
379
380 pub fn from_vec2(v: ColumnVector2<T>, z: T, w: T) -> Self {
382 let Matrix([[x, y]]) = v;
383 Matrix([[x, y, z, w]])
384 }
385
386 pub fn from_vec3(v: ColumnVector3<T>, w: T) -> Self {
388 let Matrix([[x, y, z]]) = v;
389 Matrix([[x, y, z, w]])
390 }
391}
392
393#[macro_export]
402macro_rules! column_vector {
403 ( $($elem:expr),* $(,)? ) => {
404 $crate::matrix![
405 $([ $elem ]),*
406 ]
407 }
408}
409
410impl<T, const N: usize> ColumnVector<T, N> {
419 pub fn x(&self) -> &T {
424 &self.0[0][0]
425 }
426
427 pub fn x_mut(&mut self) -> &mut T {
428 &mut self.0[0][0]
429 }
430
431 pub fn y(&self) -> &T {
436 &self.0[0][1]
437 }
438
439 pub fn y_mut(&mut self) -> &mut T {
440 &mut self.0[0][1]
441 }
442
443 pub fn z(&self) -> &T {
448 &self.0[0][2]
449 }
450
451 pub fn z_mut(&mut self) -> &mut T {
452 &mut self.0[0][2]
453 }
454
455 pub fn w(&self) -> &T {
460 &self.0[0][3]
461 }
462
463 pub fn w_mut(&mut self) -> &mut T {
464 &mut self.0[0][3]
465 }
466
467 pub fn r(&self) -> &T {
469 self.x()
470 }
471
472 pub fn r_mut(&mut self) -> &mut T {
473 self.x_mut()
474 }
475
476 pub fn g(&self) -> &T {
478 self.y()
479 }
480
481 pub fn g_mut(&mut self) -> &mut T {
482 self.y_mut()
483 }
484
485 pub fn b(&self) -> &T {
487 self.z()
488 }
489
490 pub fn b_mut(&mut self) -> &mut T {
491 self.z_mut()
492 }
493
494 pub fn a(&self) -> &T {
496 self.w()
497 }
498
499 pub fn a_mut(&mut self) -> &mut T {
500 self.w_mut()
501 }
502}
503
504macro_rules! swizzle {
506 ($a:ident, $x:ident, $y:ident, $z:ident, $w:ident) => {
509 swizzle!{ $a, $x, $x, $y, $z, $w }
511 swizzle!{ $a, $y, $x, $y, $z, $w }
512 swizzle!{ $a, $z, $x, $y, $z, $w }
513 swizzle!{ $a, $w, $x, $y, $z, $w }
514 };
515 ($a:ident, $b:ident, $x:ident, $y:ident, $z:ident, $w:ident) => {
518 paste::item! {
519 #[doc(hidden)]
520 pub fn [< $a $b >](&self) -> ColumnVector<T, 2> {
521 ColumnVector::<T, 2>::from([
522 self.$a().clone(),
523 self.$b().clone(),
524 ])
525 }
526 }
527
528 swizzle!{ $a, $b, $x, $x, $y, $z, $w }
530 swizzle!{ $a, $b, $y, $x, $y, $z, $w }
531 swizzle!{ $a, $b, $z, $x, $y, $z, $w }
532 swizzle!{ $a, $b, $w, $x, $y, $z, $w }
533 };
534 ($a:ident, $b:ident, $c:ident, $x:ident, $y:ident, $z:ident, $w:ident) => {
537 paste::item! {
538 #[doc(hidden)]
539 pub fn [< $a $b $c >](&self) -> ColumnVector<T, 3> {
540 ColumnVector::<T, 3>::from([
541 self.$a().clone(),
542 self.$b().clone(),
543 self.$c().clone(),
544 ])
545 }
546 }
547
548 swizzle!{ $a, $b, $c, $x }
551 swizzle!{ $a, $b, $c, $y }
552 swizzle!{ $a, $b, $c, $z }
553 swizzle!{ $a, $b, $c, $w }
554 };
555 ($a:ident, $b:ident, $c:ident, $d:ident) => {
559 paste::item! {
560 #[doc(hidden)]
561 pub fn [< $a $b $c $d >](&self) -> ColumnVector<T, 4> {
562 ColumnVector::<T, 4>::from([
563 self.$a().clone(),
564 self.$b().clone(),
565 self.$c().clone(),
566 self.$d().clone(),
567 ])
568 }
569 }
570 };
571}
572
573impl<T, const N: usize> ColumnVector<T, N>
574where
575 T: Clone,
576{
577 swizzle! {x, x, y, z, w}
578 swizzle! {y, x, y, z, w}
579 swizzle! {z, x, y, z, w}
580 swizzle! {w, x, y, z, w}
581 swizzle! {r, r, g, b, a}
582 swizzle! {g, r, g, b, a}
583 swizzle! {b, r, g, b, a}
584 swizzle! {a, r, g, b, a}
585}
586
587impl<T, const N: usize> VectorSpace for ColumnVector<T, N>
588where
589 T: Clone + Zero,
590 T: Add<T, Output = T>,
591 T: Sub<T, Output = T>,
592 T: Mul<T, Output = T>,
593 T: Div<T, Output = T>,
594{
595 type Scalar = T;
596}
597
598impl<T, const N: usize> MetricSpace for ColumnVector<T, N>
599where
600 Self: InnerSpace,
601{
602 type Metric = <Self as VectorSpace>::Scalar;
603
604 fn distance2(self, other: Self) -> Self::Metric {
605 (other - self).magnitude2()
606 }
607}
608
609impl<T, const N: usize> InnerSpace for ColumnVector<T, N>
610where
611 T: Clone + Zero,
612 T: Add<T, Output = T>,
613 T: Sub<T, Output = T>,
614 T: Mul<T, Output = T>,
615 T: Div<T, Output = T>,
616 Self: Clone,
617{
618 fn dot(self, rhs: Self) -> T {
619 let mut lhs = MaybeUninit::new(self);
620 let mut rhs = MaybeUninit::new(rhs);
621 let mut sum = <T as Zero>::zero();
622 let lhsp = &mut lhs as *mut MaybeUninit<Matrix<T, N, 1>> as *mut MaybeUninit<T>;
623 let rhsp = &mut rhs as *mut MaybeUninit<Matrix<T, N, 1>> as *mut MaybeUninit<T>;
624 for i in 0..N {
625 sum = sum
626 + unsafe {
627 lhsp.add(i).replace(MaybeUninit::uninit()).assume_init()
628 * rhsp.add(i).replace(MaybeUninit::uninit()).assume_init()
629 };
630 }
631 sum
632 }
633}
634
635#[cfg(feature = "mint")]
636impl<T: Copy> From<ColumnVector<T, 2>> for mint::Vector2<T> {
637 fn from(v: ColumnVector<T, 2>) -> mint::Vector2<T> {
638 mint::Vector2 {
639 x: *v.x(),
640 y: *v.y(),
641 }
642 }
643}
644
645#[cfg(feature = "mint")]
646impl<T> From<mint::Vector2<T>> for ColumnVector<T, 2> {
647 fn from(mint_vec: mint::Vector2<T>) -> Self {
648 Matrix([[mint_vec.x, mint_vec.y]])
649 }
650}
651
652#[cfg(feature = "mint")]
653impl<T: Copy> From<ColumnVector<T, 3>> for mint::Vector3<T> {
654 fn from(v: ColumnVector<T, 3>) -> mint::Vector3<T> {
655 mint::Vector3 {
656 x: *v.x(),
657 y: *v.y(),
658 z: *v.z(),
659 }
660 }
661}
662
663#[cfg(feature = "mint")]
664impl<T> From<mint::Vector3<T>> for ColumnVector<T, 3> {
665 fn from(mint_vec: mint::Vector3<T>) -> Self {
666 Matrix([[mint_vec.x, mint_vec.y, mint_vec.z]])
667 }
668}
669
670#[cfg(feature = "mint")]
671impl<T: Copy> From<ColumnVector<T, 4>> for mint::Vector4<T> {
672 fn from(v: ColumnVector<T, 4>) -> mint::Vector4<T> {
673 mint::Vector4 {
674 x: *v.x(),
675 y: *v.y(),
676 z: *v.z(),
677 w: *v.w(),
678 }
679 }
680}
681
682#[cfg(feature = "mint")]
683impl<T> From<mint::Vector4<T>> for ColumnVector<T, 4> {
684 fn from(mint_vec: mint::Vector4<T>) -> Self {
685 Matrix([[mint_vec.x, mint_vec.y, mint_vec.z, mint_vec.w]])
686 }
687}
688
689#[cfg(test)]
690mod tests {
691 use super::*;
692
693 type Vector1<T> = ColumnVector<T, 1>;
694 type Vector2<T> = ColumnVector<T, 2>;
695 type Vector3<T> = ColumnVector<T, 3>;
696 type Vector4<T> = ColumnVector<T, 4>;
697
698 #[test]
699 fn test_zero() {
700 let a = Vector3::<u32>::zero();
701 assert_eq!(a, column_vector![0, 0, 0]);
702 }
703
704 #[test]
705 fn test_index() {
706 let a = Vector1::<u32>::from([0]);
707 assert_eq!(*a.x(), 0_u32);
708 let mut b = Vector2::<u32>::from([1, 2]);
709 *b.y_mut() += 3;
710 assert_eq!(*b.y(), 5);
711 }
712
713 #[test]
714 fn test_eq() {
715 let a = Vector1::<u32>::from([0]);
716 let b = Vector1::<u32>::from([1]);
717 let c = Vector1::<u32>::from([0]);
718 let d = [[0u32]];
719 assert_ne!(a, b);
720 assert_eq!(a, c);
721 assert_eq!(a, &d);
722 }
723
724 #[test]
725 fn test_addition() {
726 let a = Vector1::<u32>::from([0]);
727 let b = Vector1::<u32>::from([1]);
728 let c = Vector1::<u32>::from([2]);
729 assert_eq!(a + b, b);
730 assert_eq!(b + b, c);
731 let a = Vector2::<u32>::from([0, 1]);
734 let b = Vector2::<u32>::from([1, 2]);
735 let c = Vector2::<u32>::from([1, 3]);
736 let d = Vector2::<u32>::from([2, 5]);
737 assert_eq!(a + b, c);
738 assert_eq!(b + c, d);
739 let mut c = Vector2::<u32>::from([1, 3]);
740 let d = Vector2::<u32>::from([2, 5]);
741 c += d;
742 let e = Vector2::<u32>::from([3, 8]);
743 assert_eq!(c, e);
744 }
745
746 #[test]
747 fn test_subtraction() {
748 let mut a = Vector1::<u32>::from([3]);
749 let b = Vector1::<u32>::from([1]);
750 let c = Vector1::<u32>::from([2]);
751 assert_eq!(a - c, b);
752 a -= b;
753 assert_eq!(a, c);
754 }
755
756 #[test]
757 fn test_negation() {
758 let a = Vector4::<i32>::from([1, 2, 3, 4]);
759 let b = Vector4::<i32>::from([-1, -2, -3, -4]);
760 assert_eq!(-a, b);
761 }
762
763 #[test]
764 fn test_scale() {
765 let a = Vector4::<f32>::from([2.0, 4.0, 2.0, 4.0]);
766 let b = Vector4::<f32>::from([4.0, 8.0, 4.0, 8.0]);
767 let c = Vector4::<f32>::from([1.0, 2.0, 1.0, 2.0]);
768 assert_eq!(a * 2.0, b);
769 assert_eq!(a / 2.0, c);
770 }
771
772 #[test]
773 fn test_cross() {
774 let a = column_vector!(1isize, 2isize, 3isize);
775 let b = column_vector!(4isize, 5isize, 6isize);
776 let r = column_vector!(-3isize, 6isize, -3isize);
777 assert_eq!(a.cross(b), r);
778 }
779
780 #[test]
781 fn test_distance() {
782 let a = Vector1::<f32>::from([0.0]);
783 let b = Vector1::<f32>::from([1.0]);
784 assert_eq!(a.distance2(b), 1.0);
785 let a = Vector1::<f32>::from([0.0]);
786 let b = Vector1::<f32>::from([2.0]);
787 assert_eq!(a.distance2(b), 4.0);
788 assert_eq!(a.distance(b), 2.0);
789 let a = Vector2::<f32>::from([0.0, 0.0]);
790 let b = Vector2::<f32>::from([1.0, 1.0]);
791 assert_eq!(a.distance2(b), 2.0);
792
793 let a = column_vector!(1i32, 1);
794 let b = column_vector!(5i32, 5);
795 assert_eq!(a.distance2(b), 32); assert_eq!((b - a).magnitude2(), 32);
797 }
798
799 #[test]
800 fn test_normalize() {
801 let a = column_vector!(5.0);
802 assert_eq!(a.magnitude(), 5.0);
803 let a_norm = a.normalize();
804 assert_eq!(a_norm, column_vector!(1.0));
805 }
806
807 #[test]
808 fn test_transpose() {
809 let v = column_vector!(1i32, 2, 3, 4);
810 let m = Matrix::<i32, 1, 4>::from([[1i32], [2], [3], [4]]);
811 assert_eq!(v.transpose(), m);
812 }
813
814 #[test]
815 fn test_from_fn() {
816 let indices: ColumnVector<usize, 10> = column_vector!(0usize, 1, 2, 3, 4, 5, 6, 7, 8, 9);
817 assert_eq!(ColumnVector::<usize, 10>::from_fn(|i| i), indices);
818 }
819
820 #[test]
821 fn test_map() {
822 let int = column_vector!(1i32, 0, 1, 1, 0, 1, 1, 0, 0, 0);
823 let boolean =
824 column_vector!(true, false, true, true, false, true, true, false, false, false);
825 assert_eq!(int.map(|i| i != 0), boolean);
826 }
827
828 #[test]
829 fn test_indexed_map() {
830 let boolean =
831 column_vector!(true, false, true, true, false, true, true, false, false, false);
832 let indices = column_vector!(0usize, 1, 2, 3, 4, 5, 6, 7, 8, 9);
833 assert_eq!(boolean.indexed_map(|i, _| i), indices);
834 }
835
836 #[test]
837 fn test_from_iter() {
838 let v = vec![1i32, 2, 3, 4];
839 let vec = ColumnVector::<i32, 4>::from_iter(v);
840 assert_eq!(vec, column_vector![1i32, 2, 3, 4])
841 }
842
843 #[test]
844 fn test_linear_interpolate() {
845 let v1 = column_vector!(0.0, 0.0, 0.0);
846 let v2 = column_vector!(1.0, 2.0, 3.0);
847 assert_eq!(v1.lerp(v2, 0.5), column_vector!(0.5, 1.0, 1.5));
848 }
849
850 #[test]
851 fn test_reflect() {
852 let v = column_vector!(1, 0);
854 let n = column_vector!(-1, 0);
855 let r = v.reflect(unsafe { Unit::new_unchecked(n) });
856 assert_eq!(r, column_vector!(-1, 0));
857
858 let v = column_vector!(1, 1);
860 let n = column_vector!(-1, 0);
861 let r = v.reflect(unsafe { Unit::new_unchecked(n) });
862 assert_eq!(r, column_vector!(-1, 1));
863 }
864}