@@ -36,7 +36,7 @@ impl<N> DMat<N> {
36
36
}
37
37
}
38
38
39
- impl < N : Zero + Clone > DMat < N > {
39
+ impl < N : Zero + Clone + Copy > DMat < N > {
40
40
/// Builds a matrix filled with zeros.
41
41
///
42
42
/// # Arguments
@@ -69,15 +69,15 @@ impl<N: Rand> DMat<N> {
69
69
}
70
70
}
71
71
72
- impl < N : One + Clone > DMat < N > {
72
+ impl < N : One + Clone + Copy > DMat < N > {
73
73
/// Builds a matrix filled with a given constant.
74
74
#[ inline]
75
75
pub fn new_ones ( nrows : uint , ncols : uint ) -> DMat < N > {
76
76
DMat :: from_elem ( nrows, ncols, :: one ( ) )
77
77
}
78
78
}
79
79
80
- impl < N : Clone > DMat < N > {
80
+ impl < N : Clone + Copy > DMat < N > {
81
81
/// Builds a matrix filled with a given constant.
82
82
#[ inline]
83
83
pub fn from_elem ( nrows : uint , ncols : uint , val : N ) -> DMat < N > {
@@ -169,7 +169,7 @@ impl<N> DMat<N> {
169
169
170
170
// FIXME: add a function to modify the dimension (to avoid useless allocations)?
171
171
172
- impl < N : One + Zero + Clone > Eye for DMat < N > {
172
+ impl < N : One + Zero + Clone + Copy > Eye for DMat < N > {
173
173
/// Builds an identity matrix.
174
174
///
175
175
/// # Arguments
@@ -196,7 +196,7 @@ impl<N> DMat<N> {
196
196
197
197
}
198
198
199
- impl < N : Clone > Indexable < ( uint , uint ) , N > for DMat < N > {
199
+ impl < N : Copy > Indexable < ( uint , uint ) , N > for DMat < N > {
200
200
/// Changes the value of a component of the matrix.
201
201
///
202
202
/// # Arguments
@@ -235,7 +235,8 @@ impl<N: Clone> Indexable<(uint, uint), N> for DMat<N> {
235
235
#[ inline]
236
236
unsafe fn unsafe_at ( & self , rowcol : ( uint , uint ) ) -> N {
237
237
let ( row, col) = rowcol;
238
- ( * self . mij . as_slice ( ) . unsafe_get ( self . offset ( row, col) ) ) . clone ( )
238
+
239
+ * self . mij . as_slice ( ) . unsafe_get ( self . offset ( row, col) )
239
240
}
240
241
241
242
#[ inline]
@@ -283,8 +284,8 @@ impl<N> IndexMut<(uint, uint), N> for DMat<N> {
283
284
}
284
285
}
285
286
286
- impl < N : Clone + Mul < N , N > + Add < N , N > + Zero > Mul < DMat < N > , DMat < N > > for DMat < N > {
287
- fn mul ( & self , right : & DMat < N > ) -> DMat < N > {
287
+ impl < N : Copy + Mul < N , N > + Add < N , N > + Zero > Mul < DMat < N > , DMat < N > > for DMat < N > {
288
+ fn mul ( self , right : DMat < N > ) -> DMat < N > {
288
289
assert ! ( self . ncols == right. nrows) ;
289
290
290
291
let mut res = unsafe { DMat :: new_uninitialized ( self . nrows , right. ncols ) } ;
@@ -308,8 +309,8 @@ impl<N: Clone + Mul<N, N> + Add<N, N> + Zero> Mul<DMat<N>, DMat<N>> for DMat<N>
308
309
}
309
310
}
310
311
311
- impl < N : Clone + Add < N , N > + Mul < N , N > + Zero > Mul < DVec < N > , DVec < N > > for DMat < N > {
312
- fn mul ( & self , right : & DVec < N > ) -> DVec < N > {
312
+ impl < N : Copy + Add < N , N > + Mul < N , N > + Zero > Mul < DVec < N > , DVec < N > > for DMat < N > {
313
+ fn mul ( self , right : DVec < N > ) -> DVec < N > {
313
314
assert ! ( self . ncols == right. at. len( ) ) ;
314
315
315
316
let mut res : DVec < N > = unsafe { DVec :: new_uninitialized ( self . nrows ) } ;
@@ -331,8 +332,8 @@ impl<N: Clone + Add<N, N> + Mul<N, N> + Zero> Mul<DVec<N>, DVec<N>> for DMat<N>
331
332
}
332
333
333
334
334
- impl < N : Clone + Add < N , N > + Mul < N , N > + Zero > Mul < DMat < N > , DVec < N > > for DVec < N > {
335
- fn mul ( & self , right : & DMat < N > ) -> DVec < N > {
335
+ impl < N : Copy + Add < N , N > + Mul < N , N > + Zero > Mul < DMat < N > , DVec < N > > for DVec < N > {
336
+ fn mul ( self , right : DMat < N > ) -> DVec < N > {
336
337
assert ! ( right. nrows == self . at. len( ) ) ;
337
338
338
339
let mut res : DVec < N > = unsafe { DVec :: new_uninitialized ( right. ncols ) } ;
@@ -353,7 +354,7 @@ impl<N: Clone + Add<N, N> + Mul<N, N> + Zero> Mul<DMat<N>, DVec<N>> for DVec<N>
353
354
}
354
355
}
355
356
356
- impl < N : Clone + BaseNum + Zero + One > Inv for DMat < N > {
357
+ impl < N : BaseNum + Clone > Inv for DMat < N > {
357
358
#[ inline]
358
359
fn inv_cpy ( & self ) -> Option < DMat < N > > {
359
360
let mut res: DMat < N > = self . clone ( ) ;
@@ -439,7 +440,7 @@ impl<N: Clone + BaseNum + Zero + One> Inv for DMat<N> {
439
440
}
440
441
}
441
442
442
- impl < N : Clone > Transpose for DMat < N > {
443
+ impl < N : Clone + Copy > Transpose for DMat < N > {
443
444
#[ inline]
444
445
fn transpose_cpy ( & self ) -> DMat < N > {
445
446
if self . nrows == self . ncols {
@@ -485,7 +486,7 @@ impl<N: Clone> Transpose for DMat<N> {
485
486
}
486
487
}
487
488
488
- impl < N : BaseNum + Cast < f64 > + Zero + Clone > Mean < DVec < N > > for DMat < N > {
489
+ impl < N : BaseNum + Cast < f64 > + Clone > Mean < DVec < N > > for DMat < N > {
489
490
fn mean ( & self ) -> DVec < N > {
490
491
let mut res: DVec < N > = DVec :: new_zeros ( self . ncols ) ;
491
492
let normalizer: N = Cast :: from ( 1.0f64 / Cast :: from ( self . nrows ) ) ;
@@ -503,7 +504,7 @@ impl<N: BaseNum + Cast<f64> + Zero + Clone> Mean<DVec<N>> for DMat<N> {
503
504
}
504
505
}
505
506
506
- impl < N : Clone + BaseNum + Cast < f64 > + Div < N , N > > Cov < DMat < N > > for DMat < N > {
507
+ impl < N : BaseNum + Cast < f64 > + Clone > Cov < DMat < N > > for DMat < N > {
507
508
// FIXME: this could be heavily optimized, removing all temporaries by merging loops.
508
509
fn cov ( & self ) -> DMat < N > {
509
510
assert ! ( self . nrows > 1 ) ;
@@ -528,7 +529,7 @@ impl<N: Clone + BaseNum + Cast<f64> + Div<N, N>> Cov<DMat<N>> for DMat<N> {
528
529
}
529
530
}
530
531
531
- impl < N : Clone > ColSlice < DVec < N > > for DMat < N > {
532
+ impl < N : Copy + Clone > ColSlice < DVec < N > > for DMat < N > {
532
533
fn col_slice ( & self , col_id : uint , row_start : uint , row_end : uint ) -> DVec < N > {
533
534
assert ! ( col_id < self . ncols) ;
534
535
assert ! ( row_start < row_end) ;
@@ -542,7 +543,7 @@ impl<N: Clone> ColSlice<DVec<N>> for DMat<N> {
542
543
}
543
544
}
544
545
545
- impl < N : Clone > RowSlice < DVec < N > > for DMat < N > {
546
+ impl < N : Copy > RowSlice < DVec < N > > for DMat < N > {
546
547
fn row_slice ( & self , row_id : uint , col_start : uint , col_end : uint ) -> DVec < N > {
547
548
assert ! ( row_id < self . nrows) ;
548
549
assert ! ( col_start < col_end) ;
@@ -561,7 +562,7 @@ impl<N: Clone> RowSlice<DVec<N>> for DMat<N> {
561
562
}
562
563
}
563
564
564
- impl < N : Clone + Zero > Diag < DVec < N > > for DMat < N > {
565
+ impl < N : Copy + Clone + Zero > Diag < DVec < N > > for DMat < N > {
565
566
#[ inline]
566
567
fn from_diag ( diag : & DVec < N > ) -> DMat < N > {
567
568
let mut res = DMat :: new_zeros ( diag. len ( ) , diag. len ( ) ) ;
@@ -609,7 +610,7 @@ impl<N: ApproxEq<N>> ApproxEq<N> for DMat<N> {
609
610
}
610
611
}
611
612
612
- impl < N : Show + Clone > Show for DMat < N > {
613
+ impl < N : Show + Copy > Show for DMat < N > {
613
614
fn fmt ( & self , form : & mut Formatter ) -> Result {
614
615
for i in range ( 0 u, self . nrows ( ) ) {
615
616
for j in range ( 0 u, self . ncols ( ) ) {
@@ -621,46 +622,54 @@ impl<N: Show + Clone> Show for DMat<N> {
621
622
}
622
623
}
623
624
624
- impl < N : Mul < N , N > > Mul < N , DMat < N > > for DMat < N > {
625
+ impl < N : Copy + Mul < N , N > > Mul < N , DMat < N > > for DMat < N > {
625
626
#[ inline]
626
- fn mul ( & self , right : & N ) -> DMat < N > {
627
- DMat {
628
- nrows : self . nrows ,
629
- ncols : self . ncols ,
630
- mij : self . mij . iter ( ) . map ( |a| * a * * right) . collect ( )
627
+ fn mul ( self , right : N ) -> DMat < N > {
628
+ let mut res = self ;
629
+
630
+ for mij in res . mij . iter_mut ( ) {
631
+ * mij = * mij * right;
631
632
}
633
+
634
+ res
632
635
}
633
636
}
634
637
635
- impl < N : Div < N , N > > Div < N , DMat < N > > for DMat < N > {
638
+ impl < N : Copy + Div < N , N > > Div < N , DMat < N > > for DMat < N > {
636
639
#[ inline]
637
- fn div ( & self , right : & N ) -> DMat < N > {
638
- DMat {
639
- nrows : self . nrows ,
640
- ncols : self . ncols ,
641
- mij : self . mij . iter ( ) . map ( |a| * a / * right) . collect ( )
640
+ fn div ( self , right : N ) -> DMat < N > {
641
+ let mut res = self ;
642
+
643
+ for mij in res . mij . iter_mut ( ) {
644
+ * mij = * mij / right;
642
645
}
646
+
647
+ res
643
648
}
644
649
}
645
650
646
- impl < N : Add < N , N > > Add < N , DMat < N > > for DMat < N > {
651
+ impl < N : Copy + Add < N , N > > Add < N , DMat < N > > for DMat < N > {
647
652
#[ inline]
648
- fn add ( & self , right : & N ) -> DMat < N > {
649
- DMat {
650
- nrows : self . nrows ,
651
- ncols : self . ncols ,
652
- mij : self . mij . iter ( ) . map ( |a| * a + * right) . collect ( )
653
+ fn add ( self , right : N ) -> DMat < N > {
654
+ let mut res = self ;
655
+
656
+ for mij in res . mij . iter_mut ( ) {
657
+ * mij = * mij + right;
653
658
}
659
+
660
+ res
654
661
}
655
662
}
656
663
657
- impl < N : Sub < N , N > > Sub < N , DMat < N > > for DMat < N > {
664
+ impl < N : Copy + Sub < N , N > > Sub < N , DMat < N > > for DMat < N > {
658
665
#[ inline]
659
- fn sub ( & self , right : & N ) -> DMat < N > {
660
- DMat {
661
- nrows : self . nrows ,
662
- ncols : self . ncols ,
663
- mij : self . mij . iter ( ) . map ( |a| * a - * right) . collect ( )
666
+ fn sub ( self , right : N ) -> DMat < N > {
667
+ let mut res = self ;
668
+
669
+ for mij in res . mij . iter_mut ( ) {
670
+ * mij = * mij - right;
664
671
}
672
+
673
+ res
665
674
}
666
675
}
0 commit comments