1use super::traits::*;
6use crate::{Scalar, Transcendental};
7
8#[derive(Copy, Clone, Debug, PartialEq)]
13pub struct ScalarVector1<T: Scalar>([T; 1]);
14
15#[derive(Copy, Clone, Debug, PartialEq)]
17pub struct ScalarVector2<T: Scalar>([T; 2]);
18
19#[derive(Copy, Clone, Debug, PartialEq)]
21pub struct ScalarVector4<T: Scalar>([T; 4]);
22
23#[derive(Copy, Clone, Debug, PartialEq)]
25pub struct ScalarVector8<T: Scalar>([T; 8]);
26
27impl<T: Scalar> Vector<T, 4> for ScalarVector4<T> {
32 fn splat(value: T) -> Self {
33 ScalarVector4([value; 4])
34 }
35
36 fn load(slice: &[T]) -> Self {
37 let mut arr = [T::ZERO; 4];
38 arr.copy_from_slice(&slice[0..4]);
39 ScalarVector4(arr)
40 }
41
42 fn store(&self, slice: &mut [T]) {
43 slice[0..4].copy_from_slice(&self.0);
44 }
45
46 fn extract(&self, index: usize) -> T {
47 self.0[index]
48 }
49
50 fn insert(&self, index: usize, value: T) -> Self {
51 let mut arr = self.0;
52 arr[index] = value;
53 ScalarVector4(arr)
54 }
55
56 fn add(&self, other: &Self) -> Self {
57 ScalarVector4(core::array::from_fn(|i| self.0[i] + other.0[i]))
58 }
59
60 fn sub(&self, other: &Self) -> Self {
61 ScalarVector4(core::array::from_fn(|i| self.0[i] - other.0[i]))
62 }
63
64 fn mul(&self, other: &Self) -> Self {
65 ScalarVector4(core::array::from_fn(|i| self.0[i] * other.0[i]))
66 }
67
68 fn div(&self, other: &Self) -> Self {
69 ScalarVector4(core::array::from_fn(|i| self.0[i] / other.0[i]))
70 }
71
72 fn rem(&self, other: &Self) -> Self {
73 ScalarVector4(core::array::from_fn(|i| self.0[i] % other.0[i]))
74 }
75
76 fn neg(&self) -> Self {
77 ScalarVector4(core::array::from_fn(|i| -self.0[i]))
78 }
79
80 fn abs(&self) -> Self {
81 ScalarVector4(core::array::from_fn(|i| self.0[i].abs()))
82 }
83
84 fn min(&self, other: &Self) -> Self {
85 ScalarVector4(core::array::from_fn(|i| self.0[i].min(other.0[i])))
86 }
87
88 fn max(&self, other: &Self) -> Self {
89 ScalarVector4(core::array::from_fn(|i| self.0[i].max(other.0[i])))
90 }
91
92 fn clamp(&self, min: &Self, max: &Self) -> Self {
93 ScalarVector4(core::array::from_fn(|i| {
94 self.0[i].clamp(min.0[i], max.0[i])
95 }))
96 }
97}
98
99impl<T: Transcendental> VectorTranscendental<T, 4> for ScalarVector4<T> {
100 fn sqrt(&self) -> Self {
101 ScalarVector4(core::array::from_fn(|i| self.0[i].sqrt()))
102 }
103 fn exp(&self) -> Self {
104 ScalarVector4(core::array::from_fn(|i| self.0[i].exp()))
105 }
106 fn ln(&self) -> Self {
107 ScalarVector4(core::array::from_fn(|i| self.0[i].ln()))
108 }
109 fn sin(&self) -> Self {
110 ScalarVector4(core::array::from_fn(|i| self.0[i].sin()))
111 }
112 fn cos(&self) -> Self {
113 ScalarVector4(core::array::from_fn(|i| self.0[i].cos()))
114 }
115 fn tan(&self) -> Self {
116 ScalarVector4(core::array::from_fn(|i| self.0[i].tan()))
117 }
118}
119
120impl<T: Scalar> Default for ScalarVector4<T> {
121 fn default() -> Self {
122 ScalarVector4([T::ZERO; 4])
123 }
124}
125
126impl<T: Scalar> Vector<T, 1> for ScalarVector1<T> {
127 fn splat(value: T) -> Self {
128 ScalarVector1([value; 1])
129 }
130
131 fn load(slice: &[T]) -> Self {
132 let mut arr = [T::ZERO; 1];
133 arr.copy_from_slice(&slice[0..1]);
134 ScalarVector1(arr)
135 }
136
137 fn store(&self, slice: &mut [T]) {
138 slice[0..1].copy_from_slice(&self.0);
139 }
140
141 fn extract(&self, index: usize) -> T {
142 self.0[index]
143 }
144
145 fn insert(&self, index: usize, value: T) -> Self {
146 let mut arr = self.0;
147 arr[index] = value;
148 ScalarVector1(arr)
149 }
150
151 fn add(&self, other: &Self) -> Self {
152 let mut arr = [T::ZERO; 1];
153 arr[0] = self.0[0] + other.0[0];
154 ScalarVector1(arr)
155 }
156
157 fn sub(&self, other: &Self) -> Self {
158 let mut arr = [T::ZERO; 1];
159 arr[0] = self.0[0] - other.0[0];
160 ScalarVector1(arr)
161 }
162
163 fn mul(&self, other: &Self) -> Self {
164 let mut arr = [T::ZERO; 1];
165 arr[0] = self.0[0] * other.0[0];
166 ScalarVector1(arr)
167 }
168
169 fn div(&self, other: &Self) -> Self {
170 let mut arr = [T::ZERO; 1];
171 arr[0] = self.0[0] / other.0[0];
172 ScalarVector1(arr)
173 }
174
175 fn rem(&self, other: &Self) -> Self {
176 let mut arr = [T::ZERO; 1];
177 arr[0] = self.0[0] % other.0[0];
178 ScalarVector1(arr)
179 }
180
181 fn neg(&self) -> Self {
182 let mut arr = [T::ZERO; 1];
183 arr[0] = -self.0[0];
184 ScalarVector1(arr)
185 }
186
187 fn abs(&self) -> Self {
188 let mut arr = [T::ZERO; 1];
189 arr[0] = self.0[0].abs();
190 ScalarVector1(arr)
191 }
192
193 fn min(&self, other: &Self) -> Self {
194 let mut arr = [T::ZERO; 1];
195 arr[0] = self.0[0].min(other.0[0]);
196 ScalarVector1(arr)
197 }
198
199 fn max(&self, other: &Self) -> Self {
200 let mut arr = [T::ZERO; 1];
201 arr[0] = self.0[0].max(other.0[0]);
202 ScalarVector1(arr)
203 }
204
205 fn clamp(&self, min: &Self, max: &Self) -> Self {
206 let mut arr = [T::ZERO; 1];
207 arr[0] = self.0[0].clamp(min.0[0], max.0[0]);
208 ScalarVector1(arr)
209 }
210}
211
212impl<T: Transcendental> VectorTranscendental<T, 1> for ScalarVector1<T> {
213 fn sqrt(&self) -> Self {
214 let mut a = [T::ZERO; 1];
215 a[0] = self.0[0].sqrt();
216 ScalarVector1(a)
217 }
218 fn exp(&self) -> Self {
219 let mut a = [T::ZERO; 1];
220 a[0] = self.0[0].exp();
221 ScalarVector1(a)
222 }
223 fn ln(&self) -> Self {
224 let mut a = [T::ZERO; 1];
225 a[0] = self.0[0].ln();
226 ScalarVector1(a)
227 }
228 fn sin(&self) -> Self {
229 let mut a = [T::ZERO; 1];
230 a[0] = self.0[0].sin();
231 ScalarVector1(a)
232 }
233 fn cos(&self) -> Self {
234 let mut a = [T::ZERO; 1];
235 a[0] = self.0[0].cos();
236 ScalarVector1(a)
237 }
238 fn tan(&self) -> Self {
239 let mut a = [T::ZERO; 1];
240 a[0] = self.0[0].tan();
241 ScalarVector1(a)
242 }
243}
244
245impl<T: Scalar> Default for ScalarVector1<T> {
246 fn default() -> Self {
247 ScalarVector1([T::ZERO; 1])
248 }
249}
250
251impl<T: Scalar> Vector<T, 2> for ScalarVector2<T> {
254 fn splat(value: T) -> Self {
255 ScalarVector2([value; 2])
256 }
257
258 fn load(slice: &[T]) -> Self {
259 let mut arr = [T::ZERO; 2];
260 arr.copy_from_slice(&slice[0..2]);
261 ScalarVector2(arr)
262 }
263
264 fn store(&self, slice: &mut [T]) {
265 slice[0..2].copy_from_slice(&self.0);
266 }
267
268 fn extract(&self, index: usize) -> T {
269 self.0[index]
270 }
271
272 fn insert(&self, index: usize, value: T) -> Self {
273 let mut arr = self.0;
274 arr[index] = value;
275 ScalarVector2(arr)
276 }
277
278 fn add(&self, other: &Self) -> Self {
279 ScalarVector2(core::array::from_fn(|i| self.0[i] + other.0[i]))
280 }
281
282 fn sub(&self, other: &Self) -> Self {
283 ScalarVector2(core::array::from_fn(|i| self.0[i] - other.0[i]))
284 }
285
286 fn mul(&self, other: &Self) -> Self {
287 ScalarVector2(core::array::from_fn(|i| self.0[i] * other.0[i]))
288 }
289
290 fn div(&self, other: &Self) -> Self {
291 ScalarVector2(core::array::from_fn(|i| self.0[i] / other.0[i]))
292 }
293
294 fn rem(&self, other: &Self) -> Self {
295 ScalarVector2(core::array::from_fn(|i| self.0[i] % other.0[i]))
296 }
297
298 fn neg(&self) -> Self {
299 ScalarVector2(core::array::from_fn(|i| -self.0[i]))
300 }
301
302 fn abs(&self) -> Self {
303 ScalarVector2(core::array::from_fn(|i| self.0[i].abs()))
304 }
305
306 fn min(&self, other: &Self) -> Self {
307 ScalarVector2(core::array::from_fn(|i| self.0[i].min(other.0[i])))
308 }
309
310 fn max(&self, other: &Self) -> Self {
311 ScalarVector2(core::array::from_fn(|i| self.0[i].max(other.0[i])))
312 }
313
314 fn clamp(&self, min: &Self, max: &Self) -> Self {
315 ScalarVector2(core::array::from_fn(|i| {
316 self.0[i].clamp(min.0[i], max.0[i])
317 }))
318 }
319}
320
321impl<T: Transcendental> VectorTranscendental<T, 2> for ScalarVector2<T> {
322 fn sqrt(&self) -> Self {
323 ScalarVector2(core::array::from_fn(|i| self.0[i].sqrt()))
324 }
325 fn exp(&self) -> Self {
326 ScalarVector2(core::array::from_fn(|i| self.0[i].exp()))
327 }
328 fn ln(&self) -> Self {
329 ScalarVector2(core::array::from_fn(|i| self.0[i].ln()))
330 }
331 fn sin(&self) -> Self {
332 ScalarVector2(core::array::from_fn(|i| self.0[i].sin()))
333 }
334 fn cos(&self) -> Self {
335 ScalarVector2(core::array::from_fn(|i| self.0[i].cos()))
336 }
337 fn tan(&self) -> Self {
338 ScalarVector2(core::array::from_fn(|i| self.0[i].tan()))
339 }
340}
341
342impl<T: Scalar> Default for ScalarVector2<T> {
343 fn default() -> Self {
344 ScalarVector2([T::ZERO; 2])
345 }
346}
347
348use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
353
354impl<T: Scalar> Add for ScalarVector4<T> {
355 type Output = Self;
356
357 fn add(self, rhs: Self) -> Self {
358 ScalarVector4(core::array::from_fn(|i| self.0[i] + rhs.0[i]))
359 }
360}
361
362impl<T: Scalar> Sub for ScalarVector4<T> {
363 type Output = Self;
364
365 fn sub(self, rhs: Self) -> Self {
366 ScalarVector4(core::array::from_fn(|i| self.0[i] - rhs.0[i]))
367 }
368}
369
370impl<T: Scalar> Mul for ScalarVector4<T> {
371 type Output = Self;
372
373 fn mul(self, rhs: Self) -> Self {
374 ScalarVector4(core::array::from_fn(|i| self.0[i] * rhs.0[i]))
375 }
376}
377
378impl<T: Scalar> Div for ScalarVector4<T> {
379 type Output = Self;
380
381 fn div(self, rhs: Self) -> Self {
382 ScalarVector4(core::array::from_fn(|i| self.0[i] / rhs.0[i]))
383 }
384}
385
386impl<T: Scalar> Rem for ScalarVector4<T> {
387 type Output = Self;
388
389 fn rem(self, rhs: Self) -> Self {
390 ScalarVector4(core::array::from_fn(|i| self.0[i] % rhs.0[i]))
391 }
392}
393
394impl<T: Scalar> Neg for ScalarVector4<T> {
395 type Output = Self;
396
397 fn neg(self) -> Self {
398 ScalarVector4(core::array::from_fn(|i| -self.0[i]))
399 }
400}
401
402impl<T: Scalar> Add for ScalarVector2<T> {
403 type Output = Self;
404
405 fn add(self, rhs: Self) -> Self {
406 ScalarVector2(core::array::from_fn(|i| self.0[i] + rhs.0[i]))
407 }
408}
409
410impl<T: Scalar> Sub for ScalarVector2<T> {
411 type Output = Self;
412
413 fn sub(self, rhs: Self) -> Self {
414 ScalarVector2(core::array::from_fn(|i| self.0[i] - rhs.0[i]))
415 }
416}
417
418impl<T: Scalar> Mul for ScalarVector2<T> {
419 type Output = Self;
420
421 fn mul(self, rhs: Self) -> Self {
422 ScalarVector2(core::array::from_fn(|i| self.0[i] * rhs.0[i]))
423 }
424}
425
426impl<T: Scalar> Div for ScalarVector2<T> {
427 type Output = Self;
428
429 fn div(self, rhs: Self) -> Self {
430 ScalarVector2(core::array::from_fn(|i| self.0[i] / rhs.0[i]))
431 }
432}
433
434impl<T: Scalar> Rem for ScalarVector2<T> {
435 type Output = Self;
436
437 fn rem(self, rhs: Self) -> Self {
438 ScalarVector2(core::array::from_fn(|i| self.0[i] % rhs.0[i]))
439 }
440}
441
442impl<T: Scalar> Neg for ScalarVector2<T> {
443 type Output = Self;
444
445 fn neg(self) -> Self {
446 ScalarVector2(core::array::from_fn(|i| -self.0[i]))
447 }
448}
449
450impl<T: Scalar> Add for ScalarVector1<T> {
451 type Output = Self;
452
453 fn add(self, rhs: Self) -> Self {
454 let mut arr = [T::ZERO; 1];
455 arr[0] = self.0[0] + rhs.0[0];
456 ScalarVector1(arr)
457 }
458}
459
460impl<T: Scalar> Sub for ScalarVector1<T> {
461 type Output = Self;
462
463 fn sub(self, rhs: Self) -> Self {
464 let mut arr = [T::ZERO; 1];
465 arr[0] = self.0[0] - rhs.0[0];
466 ScalarVector1(arr)
467 }
468}
469
470impl<T: Scalar> Mul for ScalarVector1<T> {
471 type Output = Self;
472
473 fn mul(self, rhs: Self) -> Self {
474 let mut arr = [T::ZERO; 1];
475 arr[0] = self.0[0] * rhs.0[0];
476 ScalarVector1(arr)
477 }
478}
479
480impl<T: Scalar> Div for ScalarVector1<T> {
481 type Output = Self;
482
483 fn div(self, rhs: Self) -> Self {
484 let mut arr = [T::ZERO; 1];
485 arr[0] = self.0[0] / rhs.0[0];
486 ScalarVector1(arr)
487 }
488}
489
490impl<T: Scalar> Rem for ScalarVector1<T> {
491 type Output = Self;
492
493 fn rem(self, rhs: Self) -> Self {
494 let mut arr = [T::ZERO; 1];
495 arr[0] = self.0[0] % rhs.0[0];
496 ScalarVector1(arr)
497 }
498}
499
500impl<T: Scalar> Neg for ScalarVector1<T> {
501 type Output = Self;
502
503 fn neg(self) -> Self {
504 let mut arr = [T::ZERO; 1];
505 arr[0] = -self.0[0];
506 ScalarVector1(arr)
507 }
508}
509
510impl<T: Scalar> Mul<T> for ScalarVector4<T> {
515 type Output = Self;
516
517 fn mul(self, rhs: T) -> Self {
518 self * Self::splat(rhs)
519 }
520}
521
522impl<T: Scalar> Div<T> for ScalarVector4<T> {
523 type Output = Self;
524
525 fn div(self, rhs: T) -> Self {
526 self / Self::splat(rhs)
527 }
528}
529
530impl<T: Scalar> Add<T> for ScalarVector4<T> {
531 type Output = Self;
532
533 fn add(self, rhs: T) -> Self {
534 self + Self::splat(rhs)
535 }
536}
537
538impl<T: Scalar> Sub<T> for ScalarVector4<T> {
539 type Output = Self;
540
541 fn sub(self, rhs: T) -> Self {
542 self - Self::splat(rhs)
543 }
544}
545
546impl<T: Scalar> Rem<T> for ScalarVector4<T> {
547 type Output = Self;
548
549 fn rem(self, rhs: T) -> Self {
550 self % Self::splat(rhs)
551 }
552}
553
554impl<T: Scalar> Mul<T> for ScalarVector2<T> {
555 type Output = Self;
556
557 fn mul(self, rhs: T) -> Self {
558 self * Self::splat(rhs)
559 }
560}
561
562impl<T: Scalar> Div<T> for ScalarVector2<T> {
563 type Output = Self;
564
565 fn div(self, rhs: T) -> Self {
566 self / Self::splat(rhs)
567 }
568}
569
570impl<T: Scalar> Add<T> for ScalarVector2<T> {
571 type Output = Self;
572
573 fn add(self, rhs: T) -> Self {
574 self + Self::splat(rhs)
575 }
576}
577
578impl<T: Scalar> Sub<T> for ScalarVector2<T> {
579 type Output = Self;
580
581 fn sub(self, rhs: T) -> Self {
582 self - Self::splat(rhs)
583 }
584}
585
586impl<T: Scalar> Rem<T> for ScalarVector2<T> {
587 type Output = Self;
588
589 fn rem(self, rhs: T) -> Self {
590 self % Self::splat(rhs)
591 }
592}
593
594impl<T: Scalar> Mul<T> for ScalarVector1<T> {
595 type Output = Self;
596
597 fn mul(self, rhs: T) -> Self {
598 self * Self::splat(rhs)
599 }
600}
601
602impl<T: Scalar> Div<T> for ScalarVector1<T> {
603 type Output = Self;
604
605 fn div(self, rhs: T) -> Self {
606 self / Self::splat(rhs)
607 }
608}
609
610impl<T: Scalar> Add<T> for ScalarVector1<T> {
611 type Output = Self;
612
613 fn add(self, rhs: T) -> Self {
614 self + Self::splat(rhs)
615 }
616}
617
618impl<T: Scalar> Sub<T> for ScalarVector1<T> {
619 type Output = Self;
620
621 fn sub(self, rhs: T) -> Self {
622 self - Self::splat(rhs)
623 }
624}
625
626impl<T: Scalar> Rem<T> for ScalarVector1<T> {
627 type Output = Self;
628
629 fn rem(self, rhs: T) -> Self {
630 self % Self::splat(rhs)
631 }
632}
633
634#[cfg(test)]
639mod tests {
640 use super::*;
641
642 #[test]
643 fn test_scalar_vector4_basic() {
644 let a = ScalarVector4::<f32>::splat(2.0);
645 let b = ScalarVector4::<f32>::splat(3.0);
646 let c = a + b;
647 assert_eq!(c.extract(0), 5.0);
648 assert_eq!(c.extract(3), 5.0);
649 }
650
651 #[test]
652 fn test_scalar_vector4_math() {
653 let a = ScalarVector4::<f32>::splat(0.0);
654 let b = a.sin();
655 assert_eq!(b.extract(0), 0.0);
656
657 let c = ScalarVector4::<f32>::splat(1.0);
658 let d = c.sqrt();
659 assert_eq!(d.extract(0), 1.0);
660 }
661
662 #[test]
663 fn test_scalar_vector2() {
664 let a = ScalarVector2::<f64>::splat(5.0);
665 let b = ScalarVector2::<f64>::splat(2.0);
666 let c = a * b;
667 assert_eq!(c.extract(0), 10.0);
668 assert_eq!(c.extract(1), 10.0);
669 }
670}