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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! Array - An implementation of a mathematical vector/array
//!
//! This module contains structures and functions for manipulating vectors/arrays in Linear
//! Algebra.
use std::alloc::{alloc, Layout};
use std::fmt::*;
use std::ops::{Add, Deref, DerefMut, Index, IndexMut, Mul, Neg, Sub};
/// A representation of a mathematical array/vector
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Array {
/// Number of elements in the Array
len: usize,
/// Elements of the Array, stored as a mutable pointer
arr: *mut f64,
}
impl Array {
/// Returns a new Array with no elements
///
/// # Examples
///
/// ```
/// // Create a new empty Array
/// use moonalloy::linalg::array::Array;
/// let array = Array::new();
/// ```
pub fn new() -> Array {
let arr_slice = unsafe {
let layout = Layout::new::<f64>();
let ptr = alloc(layout);
std::slice::from_raw_parts_mut(ptr as *mut f64, 0)
};
Array {
len: 0,
arr: arr_slice.as_mut_ptr(),
}
}
/// Creates a new Array from a slice of elements
///
/// # Arguments
///
/// * `slice` - A mutable slice of float values. This will become the internal values of the
/// Array.
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
/// ```
pub fn from(slice: &mut [f64]) -> Array {
Array {
len: slice.len(),
arr: slice.as_mut_ptr(),
}
}
/// Calculate the sum of all the elements in the Array
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(6.0, array.sum());
/// ```
pub fn sum(&self) -> f64 {
let mut s: f64 = 0.0;
let v = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
for i in 0..self.len() {
s += v[i];
}
s
}
/// Calculate the average of all the elements in the Array
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(2.0, array.average());
/// ```
pub fn average(&self) -> f64 {
let mut s: f64 = 0.0;
let v = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
for i in 0..self.len() {
s += v[i];
}
s / self.len as f64
}
/// Calculate the norm of the Array
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 3.0 and 4.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [3.0, 4.0]);
///
/// assert_eq!(5.0, array.norm());
/// ```
pub fn norm(&self) -> f64 {
let mut n: f64 = 0.0;
let v = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
for i in 0..self.len() {
n += v[i] * v[i];
}
n.sqrt()
}
/// Add a scalar value to every element in the Array
///
/// # Arguments
///
/// * `scalar` - scalar value to add.
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(Array::from(&mut [3.0, 4.0, 5.0]), array.scalar_add(2.0));
/// ```
pub fn scalar_add(&self, scalar: f64) -> Array {
let result = unsafe {
let layout = Layout::array::<f64>(self.len()).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, self.len())
};
let arr_slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
for i in 0..self.len() {
result[i] = scalar + arr_slice[i];
}
Array {
arr: result.as_mut_ptr(),
len: self.len,
}
}
/// Subtract a scalar value from every element in the Array
///
/// # Arguments
///
/// * `scalar` - scalar value to subtract.
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(Array::from(&mut [-1.0, 0.0, 1.0]), array.scalar_sub(2.0));
/// ```
pub fn scalar_sub(&self, scalar: f64) -> Array {
let result = unsafe {
let layout = Layout::array::<f64>(self.len()).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, self.len())
};
let arr_slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
for i in 0..self.len() {
result[i] = arr_slice[i] - scalar;
}
Array {
arr: result.as_mut_ptr(),
len: self.len,
}
}
/// Multiply every element in the Array with a scalar value
///
/// # Arguments
///
/// * `scalar` - scalar value to multiply with.
///
/// # Examples
///
/// ```
/// // Create a new Array containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(Array::from(&mut [2.0, 4.0, 6.0]), array.scalar_mult(2.0));
/// ```
pub fn scalar_mult(&self, scalar: f64) -> Array {
let result = unsafe {
let layout = Layout::array::<f64>(self.len()).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, self.len())
};
let arr_slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
for i in 0..self.len() {
result[i] = scalar * arr_slice[i];
}
Array {
arr: result.as_mut_ptr(),
len: self.len,
}
}
/// Add two Arrays without modifying either Array.
///
/// # Arguments
///
/// * `other` - the other Array to add
///
/// # Examples
///
/// ```
/// // Creates two new Arrays both containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let a = Array::from(&mut [1.0, 2.0, 3.0]);
/// let b = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(Array::from(&mut [2.0, 4.0, 6.0]), a.plus(&b));
/// // You can use the `+`-operator as a shorthand for this
/// assert_eq!(Array::from(&mut [2.0, 4.0, 6.0]), a + b);
/// ```
pub fn plus(&self, other: &Array) -> Array {
assert_eq!(self.len(), other.len(), "Lengths are different!");
let result = unsafe {
let layout = Layout::array::<f64>(self.len()).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, self.len())
};
let arr1 = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
let arr2 = unsafe { std::slice::from_raw_parts_mut(other.arr, other.len()) };
for i in 0..self.len() {
result[i] = arr1[i] + arr2[i];
}
Array {
len: result.len(),
arr: result.as_mut_ptr(),
}
}
/// Performs substraction on two Arrays without modifying either Array.
///
/// # Arguments
///
/// * `other` - the other Array to substract
///
/// # Examples
///
/// ```
/// // Creates two new Arrays both containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let a = Array::from(&mut [1.0, 2.0, 3.0]);
/// let b = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(Array::from(&mut [0.0, 0.0, 0.0]), a.minus(&b));
/// // You can use the `-`-operator as a shorthand for this
/// assert_eq!(Array::from(&mut [0.0, 0.0, 0.0]), a - b);
/// ```
pub fn minus(&self, other: &Array) -> Array {
assert_eq!(self.len(), other.len(), "Lengths are different!");
let result = unsafe {
let layout = Layout::array::<f64>(self.len()).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, self.len())
};
let arr1 = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
let arr2 = unsafe { std::slice::from_raw_parts_mut(other.arr, other.len()) };
for i in 0..self.len() {
result[i] = arr1[i] - arr2[i];
}
Array {
len: result.len(),
arr: result.as_mut_ptr(),
}
}
/// Performs multiplication on two Arrays without modifying either Array.
///
/// # Arguments
///
/// * `other` - the other Array to multiply with
///
/// # Examples
///
/// ```
/// // Creates two new Arrays both containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let a = Array::from(&mut [1.0, 2.0, 3.0]);
/// let b = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(Array::from(&mut [1.0, 4.0, 9.0]), a.mult(&b));
/// // You can use the `*`-operator as a shorthand for this
/// assert_eq!(Array::from(&mut [1.0, 4.0, 9.0]), a * b);
/// ```
pub fn mult(&self, other: &Array) -> Array {
assert_eq!(self.len(), other.len(), "Lengths are different!");
let result = unsafe {
let layout = Layout::array::<f64>(self.len()).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, self.len())
};
let arr1 = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
let arr2 = unsafe { std::slice::from_raw_parts_mut(other.arr, other.len()) };
for i in 0..self.len() {
result[i] = arr1[i] * arr2[i];
}
Array {
len: result.len(),
arr: result.as_mut_ptr(),
}
}
/// Calculates the dot product on two Arrays without modifying either Array.
/// Returns a single floating-point value.
///
/// # Arguments
///
/// * `other` - the other Array calculate the dot product with
///
/// # Examples
///
/// ```
/// // Creates two new Arrays both containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let a = Array::from(&mut [1.0, 2.0, 3.0]);
/// let b = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(14.0, a.dotp(&b));
/// ```
pub fn dotp(&self, other: &Array) -> f64 {
let arr = self.mult(other);
let v = unsafe { std::slice::from_raw_parts_mut(arr.arr, arr.len()) };
v.iter().sum()
}
/// Concatenate with another Array. This will modify the original array.
///
/// # Arguments
///
/// * `other` - the other Array calculate the dot product with
///
/// # Examples
///
/// ```
/// // Creates two new Arrays both containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let a = Array::from(&mut [1.0, 2.0, 3.0]);
/// let b = Array::from(&mut [4.0, 5.0]);
/// a.concat(&b);
///
/// assert_eq!(Array::from(&mut [1.0, 2.0, 3.0, 4.0, 5.0]), a.dotp(&b));
/// ```
pub fn concat(&self, other: &Array) -> Array {
let len = self.len() + other.len();
let result = unsafe {
let layout = Layout::array::<f64>(len).unwrap();
let ptr = alloc(layout) as *mut f64;
std::slice::from_raw_parts_mut(ptr, len)
};
let arr1 = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
let arr2 = unsafe { std::slice::from_raw_parts_mut(other.arr, other.len()) };
let mut i = 0;
for elem in arr1.iter() {
result[i] = *elem;
i += 1;
}
for elem in arr2.iter() {
result[i] = *elem;
i += 1;
}
Array {
len: result.len(),
arr: result.as_mut_ptr(),
}
}
/// Returns a string representation of the Array.
///
///
/// # Examples
///
/// ```
/// // Creates two new Arrays both containing the values 1.0, 2.0 and 3.0
/// use moonalloy::linalg::array::Array;
/// let a = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// println!("{}", a.to_string());
/// // The to_string() is not necessary since Array implements the `Display` trait.
/// println!("{}", a);
/// ```
pub fn to_string(&self) -> String {
let array_slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
format!("Array: {:?}", array_slice)
}
/// Returns a raw mutable pointer to the Array.
/// This is useful for FFI purposes.
///
/// # Arguments
///
/// * `arr` - the Array to be converted into a raw pointer
pub fn to_raw(arr: Array) -> *mut Array {
Box::into_raw(Box::new(arr))
}
/// Creates a new Array of length `len` all where all elements have the value of `val`.
///
/// # Arguments
///
/// * `val` - the value for all elements in the new Array
/// * `len` - the number of elements in the new Array
///
/// # Examples
///
/// ```
/// // Create an Array with 3 elements, where all have the value of 2.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::of(2.0, 3);
///
/// assert_eq!(Array::from(&mut [2.0, 2.0, 2.0]), array);
/// ```
pub fn of(val: f64, len: usize) -> Array {
let arr_slice = unsafe {
let layout = Layout::array::<f64>(len).unwrap();
let ptr = alloc(layout);
std::slice::from_raw_parts_mut(ptr as *mut f64, len)
};
for i in 0..len {
arr_slice[i] = val;
}
Array {
arr: arr_slice.as_mut_ptr(),
len,
}
}
/// Creates a new Array of length `len` all where all elements are set to 0.0.
///
/// # Arguments
///
/// * `len` - the number of elements in the new Array
///
/// # Examples
///
/// ```
/// // Create an Array with 3 elements, where all have the value of 0.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::zeros(3);
///
/// assert_eq!(Array::from(&mut [0.0, 0.0, 0.0]), array);
/// ```
pub fn zeros(len: usize) -> Array {
Array::of(0.0, len)
}
/// Creates a new Array of length `len` all where all elements are set to 1.0.
///
/// # Arguments
///
/// * `len` - the number of elements in the new Array
///
/// # Examples
///
/// ```
/// // Create an Array with 3 elements, where all have the value of 1.0
/// use moonalloy::linalg::array::Array;
/// let array = Array::ones(3);
///
/// assert_eq!(Array::from(&mut [1.0, 1.0, 1.0]), array);
/// ```
pub fn ones(len: usize) -> Array {
Array::of(1.0, len)
}
/// Returns the value at index: `index` in the Array.
///
/// # Arguments
///
/// * `index` - the index of the requested value.
///
/// # Panics
///
/// The `index` must be smaller than the length of the Array otherwise the code will panic with
/// an index out of bounds error.
///
/// # Examples
///
/// ```
/// // Create an Array with 3 elements
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// assert_eq!(2.0, array.get(1));
/// // The shorthand for this is the `[]`-operator
/// assert_eq!(2.0, array[1]);
/// ```
pub fn get(&self, index: usize) -> f64 {
assert!(
index < self.len(),
"ERROR - Array get: Index out of bounds."
);
let slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
slice[index]
}
/// Mutates the value at index: `index` in the Array.
///
/// # Arguments
///
/// * `val` - the new value to be set.
/// * `index` - the index of the requested value.
///
/// # Panics
///
/// The `index` must be smaller than the length of the Array otherwise the code will panic with
/// an index out of bounds error.
///
/// # Examples
///
/// ```
/// // Create an Array with 3 elements
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0]);
///
/// array.set(5.0, 1);
/// // use the `[]`-operator as a shorthand
/// // array[1] = 5.0;
/// assert_eq!(5.0, array[1]);
/// ```
pub fn set(&mut self, val: f64, index: usize) {
assert!(
index < self.len(),
"ERROR - Array get: Index out of bounds."
);
let slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
slice[index] = val;
}
/// Returns a copy of a section of the Array
///
/// # Arguments
///
/// * `first` - first index of the Array to copy from.
/// * `last` - last index (exclusive) of the Array to copy from.
///
/// # Panics
///
/// `first` must be strictly smaller than `last` otherwise the code will panic.
///
/// # Examples
///
/// ```
/// use moonalloy::linalg::array::Array;
/// let array = Array::from(&mut [1.0, 2.0, 3.0, 4.0, 5.0]);
///
/// assert_eq(Array::from(&mut [2.0, 3.0]), array.splice(1, 3));
/// ```
pub fn splice(&self, first: usize, last: usize) -> Array {
assert!(
first < last,
"ERROR - Array splice: first index must be before last index"
);
let arr_slice = unsafe {
let layout = Layout::array::<f64>(last - first).unwrap();
let ptr = alloc(layout);
std::slice::from_raw_parts_mut(ptr as *mut f64, last - first)
};
for i in first..last {
arr_slice[i - first] = self.get(i);
}
Array::from(arr_slice)
}
/// Returns the contents of the Array as a slice of floating-point values.
pub fn as_slice(&self) -> &[f64] {
unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) }
}
/// Returns the number of elements in the Array
pub fn len(&self) -> usize {
self.len
}
}
impl std::fmt::Display for Array {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.to_string())
}
}
impl PartialEq for Array {
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
let slice1 = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
let slice2 = unsafe { std::slice::from_raw_parts_mut(other.arr, other.len()) };
for i in 0..self.len() {
if slice1[i] != slice2[i] {
return false;
}
}
true
}
}
impl Deref for Array {
type Target = [f64];
fn deref(&self) -> &[f64] {
unsafe { std::slice::from_raw_parts(self.arr, self.len()) }
}
}
impl DerefMut for Array {
fn deref_mut(&mut self) -> &mut [f64] {
unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) }
}
}
impl Index<usize> for Array {
type Output = f64;
fn index(&self, i: usize) -> &Self::Output {
assert!(i < self.len(), "ERROR - Array: Index out of bounds.");
let slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
&slice[i]
}
}
impl IndexMut<usize> for Array {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
assert!(
index < self.len(),
"ERROR - Array get: Index out of bounds."
);
let slice = unsafe { std::slice::from_raw_parts_mut(self.arr, self.len()) };
&mut slice[index]
}
}
impl Add for Array {
type Output = Self;
fn add(self, other: Self) -> Self {
self.plus(&other)
}
}
impl Sub for Array {
type Output = Self;
fn sub(self, other: Self) -> Self {
self.minus(&other)
}
}
impl Mul for Array {
type Output = Self;
fn mul(self, other: Self) -> Self {
self.mult(&other)
}
}
impl Neg for Array {
type Output = Self;
fn neg(self) -> Self::Output {
self.scalar_mult(-1.0)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_new() {
let n = Array::new();
let f = Array::from(&mut []);
assert_eq!(n, f);
}
#[test]
fn test_index() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
assert_eq!(2.0, a[1]);
}
#[test]
#[should_panic]
fn test_index_out_of_bounds() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
a[3];
}
#[test]
fn test_sum() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
assert_eq!(6.0, a.sum());
}
#[test]
fn test_scalar_mult() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
let r = Array::from(&mut [2.0, 4.0, 6.0]);
assert_eq!(r, a.scalar_mult(2.0))
}
#[test]
fn test_neg() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
let r = Array::from(&mut [-1.0, -2.0, -3.0]);
assert_eq!(r, -a)
}
#[test]
fn test_add() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
let b = Array::from(&mut [2.0, 3.0, 5.0]);
let r = Array::from(&mut [3.0, 5.0, 8.0]);
assert_eq!(r, a + b);
}
#[test]
fn test_sub() {
let a = Array::from(&mut [2.0, 3.0, 5.0]);
let b = Array::from(&mut [1.0, 2.0, 3.0]);
let r = Array::from(&mut [1.0, 1.0, 2.0]);
assert_eq!(r, a - b);
}
#[test]
fn test_mult() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
let b = Array::from(&mut [2.0, 3.0, 5.0]);
let r = Array::from(&mut [2.0, 6.0, 15.0]);
assert_eq!(r, a * b);
}
#[test]
fn test_dotp() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
let b = Array::from(&mut [2.0, 3.0, 5.0]);
assert_eq!(23.0, a.dotp(&b));
}
#[test]
fn test_concat() {
let a = Array::from(&mut [1.0, 2.0]);
let b = Array::from(&mut [3.0, 5.0]);
let r = Array::from(&mut [1.0, 2.0, 3.0, 5.0]);
assert_eq!(r, a.concat(&b));
}
#[test]
fn test_zeros() {
let a = Array::zeros(3);
let r = Array::from(&mut [0.0, 0.0, 0.0]);
assert_eq!(r, a);
}
#[test]
fn test_ones() {
let a = Array::ones(3);
let r = Array::from(&mut [1.0, 1.0, 1.0]);
assert_eq!(r, a);
}
#[test]
fn test_get() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
assert_eq!(2.0, a.get(1));
}
#[test]
fn test_set() {
let mut a = Array::from(&mut [1.0, 2.0, 3.0]);
let r = Array::from(&mut [5.0, 2.0, 3.0]);
a.set(5.0, 0);
assert_eq!(r, a);
}
#[test]
fn test_iterator() {
let a = Array::from(&mut [1.0, 2.0, 3.0]);
let mut it = a.iter();
assert_eq!(*it.next().unwrap(), 1.0_f64);
assert_eq!(*it.next().unwrap(), 2.0_f64);
assert_eq!(*it.next().unwrap(), 3.0_f64);
}
#[test]
fn test_splice() {
let expected = Array::from(&mut [2.0, 3.0]);
let a = Array::from(&mut [1.0, 2.0, 3.0, 4.0, 5.0]);
let actual = a.splice(1, 3);
assert_eq!(expected, actual);
}
}