-
Notifications
You must be signed in to change notification settings - Fork 703
/
Matrix.scala
976 lines (835 loc) · 39.2 KB
/
Matrix.scala
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
Copyright 2012 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.twitter.scalding.mathematics
import com.twitter.algebird.{Monoid, Group, Ring, Field}
import com.twitter.scalding._
import cascading.pipe.assembly._
import cascading.pipe.joiner._
import cascading.pipe.Pipe
import cascading.tuple.Fields
import cascading.tuple._
import cascading.flow._
import cascading.tap._
import com.twitter.scalding.Dsl._
import scala.math.max
import scala.annotation.tailrec
/**
* Matrix class - represents an infinite (hopefully sparse) matrix.
* any elements without a row are interpretted to be zero.
* the pipe hold ('rowIdx, 'colIdx, 'val) where in principle
* each row/col/value type is generic, with the constraint that ValT is a Ring[T]
* In practice, RowT and ColT are going to be Strings, Integers or Longs in the usual case.
*
* WARNING:
* It is NOT OKAY to use the same instance of Matrix/Row/Col with DIFFERENT Monoids/Rings/Fields.
* If you want to change, midstream, the Monoid on your ValT, you have to construct a new Matrix.
* This is due to caching of internal computation graphs.
*
* RowVector - handles matrices of row dimension one. It is the result of some of the matrix methods and has methods
* that return ColVector and diagonal matrix
*
* ColVector - handles matrices of col dimension one. It is the result of some of the matrix methods and has methods
* that return RowVector and diagonal matrix
*/
// Implicit coversions
// Add methods we want to add to pipes here:
class MatrixPipeExtensions(pipe : Pipe) {
def toMatrix[RowT,ColT,ValT](fields : Fields)
(implicit conv : TupleConverter[(RowT,ColT,ValT)], setter : TupleSetter[(RowT,ColT,ValT)]) = {
val matPipe = RichPipe(pipe).mapTo(fields -> ('row,'col,'val))((tup : (RowT,ColT,ValT)) => tup)(conv,setter)
new Matrix[RowT,ColT,ValT]('row, 'col, 'val, matPipe)
}
def mapToMatrix[T,RowT,ColT,ValT](fields : Fields)(mapfn : T => (RowT,ColT,ValT))
(implicit conv : TupleConverter[T], setter : TupleSetter[(RowT,ColT,ValT)]) = {
val matPipe = RichPipe(pipe).mapTo(fields -> ('row,'col,'val))(mapfn)(conv,setter)
new Matrix[RowT,ColT,ValT]('row, 'col, 'val, matPipe)
}
def flatMapToMatrix[T,RowT,ColT,ValT](fields : Fields)(flatMapfn : T => Iterable[(RowT,ColT,ValT)])
(implicit conv : TupleConverter[T], setter : TupleSetter[(RowT,ColT,ValT)]) = {
val matPipe = RichPipe(pipe).flatMapTo(fields -> ('row,'col,'val))(flatMapfn)(conv,setter)
new Matrix[RowT,ColT,ValT]('row, 'col, 'val, matPipe)
}
def toColVector[RowT,ValT](fields : Fields)
(implicit conv : TupleConverter[(RowT,ValT)], setter : TupleSetter[(RowT,ValT)]) = {
val vecPipe = RichPipe(pipe).mapTo(fields -> ('row, 'val))((tup : (RowT, ValT)) => tup)(conv,setter)
new ColVector[RowT,ValT]('row, 'val, vecPipe)
}
def mapToColVector[T,RowT,ValT](fields : Fields)(mapfn : T => (RowT,ValT))
(implicit conv : TupleConverter[T], setter : TupleSetter[(RowT,ValT)]) = {
val vecPipe = RichPipe(pipe).mapTo(fields -> ('row, 'val))(mapfn)(conv,setter)
new ColVector[RowT,ValT]('row, 'val, vecPipe)
}
def flatMapToColVector[T,RowT,ValT](fields : Fields)(flatMapfn : T => Iterable[(RowT,ValT)])
(implicit conv : TupleConverter[T], setter : TupleSetter[(RowT,ValT)]) = {
val vecPipe = RichPipe(pipe).flatMapTo(fields -> ('row, 'val))(flatMapfn)(conv,setter)
new ColVector[RowT,ValT]('row, 'val, vecPipe)
}
def toRowVector[ColT,ValT](fields : Fields)
(implicit conv : TupleConverter[(ColT,ValT)], setter : TupleSetter[(ColT,ValT)]) = {
val vecPipe = RichPipe(pipe).mapTo(fields -> ('col, 'val))((tup : (ColT, ValT)) => tup)(conv,setter)
new RowVector[ColT,ValT]('col, 'val, vecPipe)
}
def mapToRowVector[T,ColT,ValT](fields : Fields)(mapfn : T => (ColT,ValT))
(implicit conv : TupleConverter[T], setter : TupleSetter[(ColT,ValT)]) = {
val vecPipe = RichPipe(pipe).mapTo(fields -> ('col, 'val))(mapfn)(conv,setter)
new RowVector[ColT,ValT]('col, 'val, vecPipe)
}
def flatMapToRowVector[T,ColT,ValT](fields : Fields)(flatMapfn : T => Iterable[(ColT,ValT)])
(implicit conv : TupleConverter[T], setter : TupleSetter[(ColT,ValT)]) = {
val vecPipe = RichPipe(pipe).flatMapTo(fields -> ('col, 'val))(flatMapfn)(conv,setter)
new RowVector[ColT,ValT]('col, 'val, vecPipe)
}
}
/** This is the enrichment pattern on Mappable[T] for converting to Matrix types
*/
class MatrixMappableExtensions[T](mappable: Mappable[T])(implicit fd: FlowDef) {
def toMatrix[Row,Col,Val](implicit ev: <:<[T,(Row,Col,Val)],
setter: TupleSetter[(Row,Col,Val)]) : Matrix[Row,Col,Val] =
mapToMatrix { _.asInstanceOf[(Row,Col,Val)] }
def mapToMatrix[Row,Col,Val](fn: (T) => (Row,Col,Val))
(implicit setter: TupleSetter[(Row,Col,Val)]) : Matrix[Row,Col,Val] = {
val fields = ('row, 'col, 'val)
val matPipe = mappable.mapTo(fields)(fn)
new Matrix[Row,Col,Val]('row, 'col, 'val, matPipe)
}
def toRow[Row,Val](implicit ev: <:<[T,(Row,Val)], setter: TupleSetter[(Row,Val)])
: RowVector[Row,Val] = mapToRow { _.asInstanceOf[(Row,Val)] }
def mapToRow[Row,Val](fn: (T) => (Row,Val))
(implicit setter: TupleSetter[(Row,Val)], fd: FlowDef) : RowVector[Row,Val] = {
val fields = ('row, 'val)
val rowPipe = mappable.mapTo(fields)(fn)
new RowVector[Row,Val]('row,'val, rowPipe)
}
def toCol[Col,Val](implicit ev: <:<[T,(Col,Val)], setter: TupleSetter[(Col,Val)]) : ColVector[Col,Val] =
mapToCol { _.asInstanceOf[(Col,Val)] }
def mapToCol[Col,Val](fn: (T) => (Col,Val))
(implicit setter: TupleSetter[(Col,Val)]) : ColVector[Col,Val] = {
val fields = ('col, 'val)
val colPipe = mappable.mapTo(fields)(fn)
new ColVector[Col,Val]('col,'val, colPipe)
}
}
object Matrix {
// If this function is implicit, you can use the PipeExtensions methods on pipe
implicit def pipeExtensions[P <% Pipe](p : P) = new MatrixPipeExtensions(p)
implicit def mappableExtensions[T](mt: Mappable[T])(implicit fd: FlowDef) =
new MatrixMappableExtensions(mt)(fd)
def filterOutZeros[ValT](fSym : Symbol, group : Monoid[ValT])(fpipe : Pipe) : Pipe = {
fpipe.filter(fSym) { tup : Tuple1[ValT] => group.isNonZero(tup._1) }
}
def meanCenter[T](vct: Iterable[(T,Double)]) : Iterable[(T,Double)] = {
val valList = vct.map { _._2 }
val sum = valList.sum
val count = valList.size
val avg = sum / count
vct.map { tup => (tup._1, tup._2 - avg) }
}
implicit def literalToScalar[ValT](v : ValT) = new LiteralScalar(v)
// Converts to Matrix for addition
implicit def diagonalToMatrix[RowT,ValT](diag : DiagonalMatrix[RowT,ValT]) : Matrix[RowT,RowT,ValT] = {
val colSym = newSymbol(Set(diag.idxSym, diag.valSym), 'col)
val newPipe = diag.pipe.map(diag.idxSym -> colSym) { (x : RowT) => x }
new Matrix[RowT,RowT,ValT](diag.idxSym, colSym, diag.valSym, newPipe, diag.sizeHint)
}
}
// The linear algebra objects (Matrix, *Vector, Scalar) wrap pipes and have some
// common properties. The main common pattern is the desire to write them to sources
// without needless duplication of code.
trait WrappedPipe {
def fields : Fields
def pipe : Pipe
def writePipe(src : Source, outFields : Fields = Fields.NONE)(implicit fd : FlowDef) {
val toWrite = if (outFields.isNone) pipe else pipe.rename(fields -> outFields)
toWrite.write(src)
}
}
class Matrix[RowT, ColT, ValT]
(val rowSym : Symbol, val colSym : Symbol, val valSym : Symbol,
inPipe : Pipe, val sizeHint : SizeHint = NoClue)
extends WrappedPipe with java.io.Serializable {
import Matrix._
import MatrixProduct._
import Dsl.ensureUniqueFields
import Dsl.getField
//The access function for inPipe. Ensures the right order of: row,col,val
lazy val pipe = inPipe.project(rowSym,colSym,valSym)
def fields = rowColValSymbols
def pipeAs(toFields : Fields) = pipe.rename((rowSym,colSym,valSym) -> toFields)
def hasHint = sizeHint != NoClue
override def hashCode = inPipe.hashCode
override def equals(that : Any) : Boolean = {
(that != null) && (that.isInstanceOf[Matrix[_,_,_]]) && {
val thatM = that.asInstanceOf[Matrix[RowT,ColT,ValT]]
(this.rowSym == thatM.rowSym) && (this.colSym == thatM.colSym) &&
(this.valSym == thatM.valSym) && (this.pipe == thatM.pipe)
}
}
// Value operations
def mapValues[ValU](fn:(ValT) => ValU)(implicit mon : Monoid[ValU]) : Matrix[RowT,ColT,ValU] = {
val newPipe = pipe.flatMap(valSym -> valSym) { imp : Tuple1[ValT] => //Ensure an arity of 1
//This annoying Tuple1 wrapping ensures we can handle ValT that may itself be a Tuple.
mon.nonZeroOption(fn(imp._1)).map { Tuple1(_) }
}
new Matrix[RowT,ColT,ValU](this.rowSym, this.colSym, this.valSym, newPipe, sizeHint)
}
/** like zipWithIndex.map but ONLY CHANGES THE VALUE not the index.
* Note you will only see non-zero elements on the matrix. This does not enumerate the zeros
*/
def mapWithIndex[ValNew](fn: (ValT,RowT,ColT) => ValNew)(implicit mon: Monoid[ValNew]):
Matrix[RowT,ColT,ValNew] = {
val newPipe = pipe.flatMap(fields -> fields) { imp : (RowT,ColT,ValT) =>
mon.nonZeroOption(fn(imp._3, imp._1, imp._2)).map { (imp._1, imp._2, _) }
}
new Matrix[RowT,ColT,ValNew](rowSym, colSym, valSym, newPipe, sizeHint)
}
// Filter values
def filterValues(fn : (ValT) => Boolean) : Matrix[RowT,ColT,ValT] = {
val newPipe = pipe.filter(valSym) { imp : Tuple1[ValT] => //Ensure an arity of 1
//This annoying Tuple1 wrapping ensures we can handle ValT that may itself be a Tuple.
fn(imp._1)
}
new Matrix[RowT,ColT,ValT](this.rowSym, this.colSym, this.valSym, newPipe, sizeHint)
}
// Binarize values, all x != 0 become 1
def binarizeAs[NewValT](implicit mon : Monoid[ValT], ring : Ring[NewValT]) : Matrix[RowT,ColT,NewValT] = {
mapValues( x => if ( mon.isNonZero(x) ) { ring.one } else { ring.zero } )(ring)
}
// Row Operations
// Get a specific row
def getRow (index : RowT) : RowVector[ColT,ValT] = {
val newPipe = inPipe
.filter(rowSym){ input : RowT => input == index }
.project(colSym,valSym)
val newHint = sizeHint.setRows(1L)
new RowVector[ColT,ValT](colSym,valSym,newPipe,newHint)
}
// Reduce all rows to a single row (zeros or ignored)
def reduceRowVectors(fn: (ValT,ValT) => ValT)(implicit mon : Monoid[ValT]) : RowVector[ColT,ValT] = {
val newPipe = filterOutZeros(valSym, mon) {
pipe.groupBy(colSym) {
_.reduce(valSym) { (x : Tuple1[ValT], y: Tuple1[ValT]) => Tuple1(fn(x._1,y._1)) }
// Matrices are generally huge and cascading has problems with diverse key spaces and
// mapside operations
// TODO continually evaluate if this is needed to avoid OOM
.forceToReducers
}
}
val newHint = sizeHint.setRows(1L)
new RowVector[ColT,ValT](colSym,valSym,newPipe,newHint)
}
// Sums all the rows per column
def sumRowVectors(implicit mon : Monoid[ValT]) : RowVector[ColT,ValT] = {
this.reduceRowVectors((x,y) => mon.plus(x,y))
}
// Maps rows using a per-row mapping function
// Use this for non-decomposable vector processing functions
// and with vectors that can fit in one-single machine memory
def mapRows (fn: Iterable[(ColT,ValT)] => Iterable[(ColT,ValT)])(implicit mon : Monoid[ValT])
: Matrix[RowT,ColT,ValT] = {
val newListSym = Symbol(colSym.name + "_" + valSym.name + "_list")
// TODO, I think we can count the rows/cols for free here
val newPipe = filterOutZeros(valSym, mon) {
pipe.groupBy(rowSym) {
_.toList[(ColT,ValT)]((colSym,valSym) -> newListSym)
}
.flatMapTo( (rowSym, newListSym) -> (rowSym,colSym,valSym) ) { tup : (RowT,List[(ColT,ValT)]) =>
val row = tup._1
val list = fn(tup._2)
// Now flatten out to (row, col, val):
list.map{ imp : (ColT,ValT) => (row,imp._1,imp._2) }
}
}
new Matrix[RowT,ColT,ValT](rowSym, colSym, valSym, newPipe, sizeHint)
}
def topRowElems( k : Int )(implicit ord : Ordering[ValT]) : Matrix[RowT,ColT,ValT] = {
if (k < 1000) {
topRowWithTiny(k)
}
else {
val newPipe = pipe.groupBy(rowSym){ _
.sortBy(valSym)
.reverse
.take(k)
}
.project(rowSym,colSym,valSym)
new Matrix[RowT,ColT,ValT](rowSym, colSym, valSym, newPipe, FiniteHint(-1L,k))
}
}
protected def topRowWithTiny( k : Int )(implicit ord : Ordering[ValT]) : Matrix[RowT,ColT,ValT] = {
val topSym = Symbol(colSym.name + "_topK")
val newPipe = pipe.groupBy(rowSym){ _
.sortWithTake( (colSym, valSym) -> 'top_vals, k ) ( (t0 :(ColT,ValT), t1:(ColT,ValT)) => ord.gt(t0._2,t1._2) )
}
.flatMapTo((0,1) ->(rowSym,topSym,valSym)) { imp:(RowT,List[(ColT,ValT)]) =>
val row = imp._1
val list = imp._2
list.map{ imp : (ColT,ValT) => (row,imp._1,imp._2) }
}
new Matrix[RowT,ColT,ValT](rowSym, topSym, valSym, newPipe, FiniteHint(-1L,k))
}
protected lazy val rowL0Norm = {
val matD = this.asInstanceOf[Matrix[RowT,ColT,Double]]
(matD.mapValues { x => 1.0 }
.sumColVectors
.diag
.inverse) * matD
}
def rowL0Normalize(implicit ev : =:=[ValT,Double]) : Matrix[RowT,ColT,Double] = rowL0Norm
protected lazy val rowL1Norm = {
val matD = this.asInstanceOf[Matrix[RowT,ColT,Double]]
(matD.mapValues { x => x.abs }
.sumColVectors
.diag
.inverse) * matD
}
// Row L1 normalization, only makes sense for Doubles
// At the end of L1 normalization, sum of row values is one
def rowL1Normalize(implicit ev : =:=[ValT,Double]) : Matrix[RowT,ColT,Double] = rowL1Norm
protected lazy val rowL2Norm = {
val matD = this.asInstanceOf[Matrix[RowT,ColT,Double]]
(matD.mapValues { x => x*x }
.sumColVectors
.diag
.mapValues { x => scala.math.sqrt(x) }
.diagonal
.inverse) * matD
}
// Row L2 normalization (can only be called for Double)
// After this operation, the sum(|x|^2) along each row will be 1.
def rowL2Normalize(implicit ev : =:=[ValT,Double]) : Matrix[RowT,ColT,Double] = rowL2Norm
// Remove the mean of each row from each value in a row.
// Double ValT only (only over the observed values, not dividing by the unobserved ones)
def rowMeanCentering(implicit ev : =:=[ValT,Double]) = {
val matD = this.asInstanceOf[Matrix[RowT,ColT,Double]]
matD.mapRows { Matrix.meanCenter }
}
// Row non-zeroes, ave and standard deviation in one pass - Double ValT only
// It produces a matrix with the same number of rows, but the cols are the three moments.
// (moments are computed only over the observed values, not taking into account the unobserved ones)
def rowSizeAveStdev(implicit ev : =:=[ValT,Double]) = {
val newColSym = Symbol(colSym.name + "_newCol")
val newValSym = Symbol(valSym.name + "_newVal")
val newPipe = inPipe
.groupBy(rowSym) { _.sizeAveStdev((valSym)->('size,'ave,'stdev)) }
.flatMapTo( (rowSym,'size,'ave,'stdev) -> (rowSym,newColSym,newValSym) ) { tup : (RowT,Long,Double,Double) =>
val row = tup._1
val size = tup._2.toDouble
val avg = tup._3
val stdev = tup._4
List((row,1,size),(row,2,avg),(row,3,stdev))
}
val newHint = sizeHint.setCols(3L)
new Matrix[RowT,Int,Double](rowSym, newColSym, newValSym, newPipe, newHint)
}
def rowColValSymbols : Fields = (rowSym, colSym, valSym)
// Column operations - see Row operations above
def getCol (index : ColT) : ColVector[RowT,ValT] = {
this.transpose.getRow(index).transpose
}
def reduceColVectors (fn: (ValT,ValT) => ValT)( implicit mon: Monoid[ValT] ) : ColVector[RowT,ValT] = {
this.transpose.reduceRowVectors(fn)(mon).transpose
}
def sumColVectors( implicit mon : Monoid[ValT] ) : ColVector[RowT,ValT] = {
this.transpose.sumRowVectors(mon).transpose
}
def mapCols(fn: Iterable[(RowT,ValT)] => Iterable[(RowT,ValT)])( implicit mon : Monoid[ValT] ) : Matrix[RowT,ColT,ValT] = {
this.transpose.mapRows(fn)(mon).transpose
}
def topColElems( k : Int )(implicit ord : Ordering[ValT]) : Matrix[RowT,ColT,ValT] = {
this.transpose.topRowElems(k)(ord).transpose
}
def colL0Normalize(implicit ev : =:=[ValT,Double]) = {
this.transpose.rowL0Normalize.transpose
}
def colL1Normalize(implicit ev : =:=[ValT,Double]) = {
this.transpose.rowL1Normalize.transpose
}
def colL2Normalize(implicit ev : =:=[ValT,Double]) = {
this.transpose.rowL2Normalize.transpose
}
def colMeanCentering(implicit ev : =:=[ValT,Double]) = {
this.transpose.rowMeanCentering.transpose
}
def colSizeAveStdev(implicit ev : =:=[ValT,Double]) = {
this.transpose.rowSizeAveStdev
}
def *[That,Res](that : That)(implicit prod : MatrixProduct[Matrix[RowT,ColT,ValT],That,Res]) : Res = {
prod(this, that)
}
def /(that : LiteralScalar[ValT])(implicit field : Field[ValT]) = {
field.assertNotZero(that.value)
mapValues(elem => field.div(elem, that.value))(field)
}
def /(that : Scalar[ValT])(implicit field : Field[ValT]) = {
nonZerosWith(that)
.mapValues({leftRight : (ValT,ValT) =>
val (left, right) = leftRight
field.div(left, right)
})(field)
}
// Between Matrix value reduction - Generalizes matrix addition with an arbitrary value aggregation function
// It assumes that the function fn(0,0) = 0
// This function assumes only one value in each matrix for a given row and column index. (no stacking of operations yet)
// TODO: Optimize this later and be lazy on groups and joins.
def elemWiseOp(that : Matrix[RowT,ColT,ValT])(fn : (ValT,ValT) => ValT)(implicit mon : Monoid[ValT])
: Matrix[RowT,ColT,ValT] = {
// If the following is not true, it's not clear this is meaningful
// assert(mon.isZero(fn(mon.zero,mon.zero)), "f is illdefined")
zip(that).mapValues({ pair => fn(pair._1, pair._2) })(mon)
}
// Matrix summation
def +(that : Matrix[RowT,ColT,ValT])(implicit mon : Monoid[ValT]) : Matrix[RowT,ColT,ValT] = {
if (equals(that)) {
// No need to do any groupBy operation
mapValues { v => mon.plus(v,v) }(mon)
}
else {
elemWiseOp(that)((x,y) => mon.plus(x,y))(mon)
}
}
// Matrix difference
def -(that : Matrix[RowT,ColT,ValT])(implicit grp : Group[ValT]) : Matrix[RowT,ColT,ValT] = {
elemWiseOp(that)((x,y) => grp.minus(x,y))(grp)
}
// Matrix elementwise product / Hadamard product
// see http://en.wikipedia.org/wiki/Hadamard_product_(matrices)
def hProd(mat: Matrix[RowT,ColT,ValT])(implicit ring : Ring[ValT]) : Matrix[RowT,ColT,ValT] = {
elemWiseOp(mat)((x,y) => ring.times(x,y))(ring)
}
/** Considering the matrix as a graph, propagate the column:
* Does the calculation: \sum_{j where M(i,j) == true) c_j
*/
def propagate[ColValT](vec: ColVector[ColT,ColValT])(implicit ev: =:=[ValT,Boolean], monT: Monoid[ColValT])
: ColVector[RowT,ColValT] = {
//This cast will always succeed:
val boolMat = this.asInstanceOf[Matrix[RowT,ColT,Boolean]]
boolMat.zip(vec.transpose)
.mapValues { boolT => if (boolT._1) boolT._2 else monT.zero }
.sumColVectors
}
// Compute the sum of the main diagonal. Only makes sense cases where the row and col type are
// equal
def trace(implicit mon : Monoid[ValT], ev : =:=[RowT,ColT]) : Scalar[ValT] = {
diagonal.trace(mon)
}
// Compute the sum of all the elements in the matrix
def sum(implicit mon : Monoid[ValT]) : Scalar[ValT] = {
sumRowVectors.sum
}
def transpose : Matrix[ColT, RowT, ValT] = {
new Matrix[ColT,RowT,ValT](colSym, rowSym, valSym, inPipe, sizeHint.transpose)
}
// This should only be called by def diagonal, which verifies that RowT == ColT
protected lazy val mainDiagonal : DiagonalMatrix[RowT,ValT] = {
val diagPipe = pipe.filter(rowSym, colSym) { input : (RowT, RowT) =>
(input._1 == input._2)
}
.project(rowSym, valSym)
new DiagonalMatrix[RowT,ValT](rowSym, valSym, diagPipe, SizeHint.asDiagonal(sizeHint))
}
// This method will only work if the row type and column type are the same
// the type constraint below means there is evidence that RowT and ColT are
// the same type
def diagonal(implicit ev : =:=[RowT,ColT]) = mainDiagonal
/*
* This just removes zeros after the join inside a zip
*/
private def cleanUpZipJoin[ValU](otherVSym : Fields, pairMonoid : Monoid[(ValT,ValU)])(joinedPipe : Pipe)
: Pipe = {
joinedPipe
//Make sure the zeros are set correctly:
.map(valSym -> valSym) { (x : ValT) =>
if (null == x) pairMonoid.zero._1 else x
}
.map(otherVSym -> otherVSym) { (x : ValU) =>
if (null == x) pairMonoid.zero._2 else x
}
//Put the pair into a single item, ugly in scalding sadly...
.map(valSym.append(otherVSym) -> valSym) { tup : (ValT,ValU) => Tuple1(tup) }
.project(rowColValSymbols)
}
/*
* This ensures both side rows and columns have correct indexes (fills in nulls from the other side
* in the case of outerjoins)
*/
private def cleanUpIndexZipJoin(fields : Fields, joinedPipe : RichPipe)
: Pipe = {
def anyRefOr( tup : (AnyRef, AnyRef)) : (AnyRef, AnyRef) = {
val newRef = Option(tup._1).getOrElse(tup._2)
(newRef, newRef)
}
joinedPipe
.map(fields -> fields) { tup : (AnyRef, AnyRef) => anyRefOr(tup) }
}
// Similar to zip, but combine the scalar on the right with all non-zeros in this matrix:
def nonZerosWith[ValU](that : Scalar[ValU]) : Matrix[RowT,ColT,(ValT,ValU)] = {
val (newRFields, newRPipe) = ensureUniqueFields(rowColValSymbols, that.valSym, that.pipe)
val newPipe = inPipe.crossWithTiny(newRPipe)
.map(valSym.append(getField(newRFields, 0)) -> valSym) { leftRight : (ValT, ValU) => Tuple1(leftRight) }
.project(rowColValSymbols)
new Matrix[RowT,ColT,(ValT,ValU)](rowSym, colSym, valSym, newPipe, sizeHint)
}
// Similar to zip, but combine the scalar on the right with all non-zeros in this matrix:
def nonZerosWith[ValU](that : LiteralScalar[ValU]) : Matrix[RowT,ColT,(ValT,ValU)] = {
val newPipe = inPipe.map(valSym -> valSym) { left : Tuple1[ValT] =>
Tuple1((left._1, that.value))
}
.project(rowColValSymbols)
new Matrix[RowT,ColT,(ValT,ValU)](rowSym, colSym, valSym, newPipe, sizeHint)
}
// Override the size hint
def withSizeHint(sh : SizeHint) : Matrix[RowT,ColT,ValT] = {
new Matrix[RowT,ColT,ValT](rowSym, colSym, valSym, pipe, sh)
}
// Zip the given row with all the rows of the matrix
def zip[ValU](that : ColVector[RowT,ValU])(implicit pairMonoid : Monoid[(ValT,ValU)])
: Matrix[RowT,ColT,(ValT,ValU)] = {
val (newRFields, newRPipe) = ensureUniqueFields(rowColValSymbols, (that.rowS, that.valS), that.pipe)
// we must do an outer join to preserve zeros on one side or the other.
// joinWithTiny can't do outer. And since the number
// of values for each key is 1,2 it doesn't matter if we do joinWithSmaller or Larger:
// TODO optimize the number of reducers
val zipped = cleanUpZipJoin(getField(newRFields, 1), pairMonoid) {
pipe
.joinWithSmaller(rowSym -> getField(newRFields, 0), newRPipe, new OuterJoin)
.then{ p : RichPipe => cleanUpIndexZipJoin(rowSym.append(getField(newRFields, 0)),p) }
}
new Matrix[RowT,ColT,(ValT,ValU)](rowSym, colSym, valSym, zipped, sizeHint + that.sizeH)
}
// Zip the given row with all the rows of the matrix
def zip[ValU](that : RowVector[ColT,ValU])(implicit pairMonoid : Monoid[(ValT,ValU)])
: Matrix[RowT,ColT,(ValT,ValU)] = {
val (newRFields, newRPipe) = ensureUniqueFields(rowColValSymbols, (that.colS, that.valS), that.pipe)
// we must do an outer join to preserve zeros on one side or the other.
// joinWithTiny can't do outer. And since the number
// of values for each key is 1,2 it doesn't matter if we do joinWithSmaller or Larger:
// TODO optimize the number of reducers
val zipped = cleanUpZipJoin(getField(newRFields, 1), pairMonoid) {
pipe
.joinWithSmaller(colSym -> getField(newRFields, 0), newRPipe, new OuterJoin)
.then{ p : RichPipe => cleanUpIndexZipJoin(colSym.append(getField(newRFields, 0)),p) }
}
new Matrix[RowT,ColT,(ValT,ValU)](rowSym, colSym, valSym, zipped, sizeHint + that.sizeH)
}
// This creates the matrix with pairs for the entries
def zip[ValU](that : Matrix[RowT,ColT,ValU])(implicit pairMonoid : Monoid[(ValT,ValU)])
: Matrix[RowT,ColT,(ValT,ValU)] = {
val (newRFields, newRPipe) = ensureUniqueFields(rowColValSymbols, that.rowColValSymbols, that.pipe)
// we must do an outer join to preserve zeros on one side or the other.
// joinWithTiny can't do outer. And since the number
// of values for each key is 1,2 it doesn't matter if we do joinWithSmaller or Larger:
// TODO optimize the number of reducers
val zipped = cleanUpZipJoin[ValU](getField(newRFields, 2), pairMonoid) {
pipe
.joinWithSmaller((rowSym, colSym) ->
(getField(newRFields, 0).append(getField(newRFields, 1))),
newRPipe, new OuterJoin)
.then{ p : RichPipe => cleanUpIndexZipJoin(rowSym.append(getField(newRFields,0)),p) }
.then{ p : RichPipe => cleanUpIndexZipJoin(colSym.append(getField(newRFields,1)),p) }
}
new Matrix[RowT,ColT,(ValT,ValU)](rowSym, colSym, valSym, zipped, sizeHint + that.sizeHint)
}
/**
* removes any elements in this matrix that also appear in the argument matrix
*/
def removeElementsBy[ValU](that : Matrix[RowT,ColT,ValU]) : Matrix[RowT,ColT,ValT] = {
val filterR = '___filterR___
val filterC = '___filterC___
val filterV = '___filterV___
val joined = pipe.joinWithSmaller((rowSym, colSym) -> (filterR, filterC),
that.pipe.rename((that.rowSym, that.colSym, that.valSym) -> (filterR, filterC, filterV)), new LeftJoin)
val filtered = joined.filter(filterV){ x : ValU => null == x }
new Matrix[RowT,ColT,ValT](rowSym,colSym,valSym, filtered.project(rowSym,colSym,valSym))
}
/**
* keep only elements in this matrix that also appear in the argument matrix
*/
def keepElementsBy[ValU](that : Matrix[RowT,ColT,ValU]) : Matrix[RowT,ColT,ValT] = {
val keepR = '___keepR___
val keepC = '___keepC___
val keepV = '___keepV___
val joined = pipe.joinWithSmaller((rowSym, colSym) -> (keepR, keepC),
that.pipe.rename((that.rowSym, that.colSym, that.valSym) -> (keepR, keepC, keepV)))
new Matrix[RowT,ColT,ValT](rowSym,colSym,valSym, joined.project(rowSym,colSym,valSym))
}
/**
* keeps only those rows that are in the joining column
*/
def keepRowsBy[ValU](that : ColVector[RowT,ValU]) : Matrix[RowT,ColT,ValT] = {
val index = '____index____
val joined = pipe.joinWithSmaller(rowSym -> index, that.pipe.rename(that.rowS -> index).project(index))
new Matrix[RowT,ColT,ValT](rowSym,colSym,valSym, joined.project(rowSym,colSym,valSym))
}
/**
* keeps only those cols that are in the joining column
*/
def keepColsBy[ValU](that : ColVector[ColT,ValU]) : Matrix[RowT,ColT,ValT] = {
val index = '____index____
val joined = pipe.joinWithSmaller(colSym -> index, that.pipe.rename(that.rowS -> index).project(index))
new Matrix[RowT,ColT,ValT](rowSym,colSym,valSym, joined.project(rowSym,colSym,valSym))
}
/** Write the matrix, optionally renaming row,col,val fields to the given fields
* then return this.
*/
def write(src : Source, outFields : Fields = Fields.NONE)(implicit fd : FlowDef)
: Matrix[RowT,ColT,ValT] = {
writePipe(src, outFields)
this
}
}
class LiteralScalar[ValT](val value : ValT) extends java.io.Serializable {
def *[That,Res](that : That)(implicit prod : MatrixProduct[LiteralScalar[ValT],That,Res]) : Res
= { prod(this, that) }
}
class Scalar[ValT](val valSym : Symbol, inPipe : Pipe) extends WrappedPipe with java.io.Serializable {
def pipe = inPipe
def fields = valSym
def *[That,Res](that : That)(implicit prod : MatrixProduct[Scalar[ValT],That,Res]) : Res
= { prod(this, that) }
/** Write the Scalar, optionally renaming val fields to the given fields
* then return this.
*/
def write(src : Source, outFields : Fields = Fields.NONE)(implicit fd : FlowDef) = {
writePipe(src, outFields)
this
}
}
class DiagonalMatrix[IdxT,ValT](val idxSym : Symbol,
val valSym : Symbol, inPipe : Pipe, val sizeHint : SizeHint = FiniteHint(1L, -1L))
extends WrappedPipe with java.io.Serializable {
def *[That,Res](that : That)(implicit prod : MatrixProduct[DiagonalMatrix[IdxT,ValT],That,Res]) : Res
= { prod(this, that) }
def pipe = inPipe
def fields = (idxSym, valSym)
def trace(implicit mon : Monoid[ValT]) : Scalar[ValT] = {
val scalarPipe = inPipe.groupAll {
_.reduce(valSym -> valSym) { (left : Tuple1[ValT], right : Tuple1[ValT]) =>
Tuple1(mon.plus(left._1, right._1))
}
}
new Scalar[ValT](valSym, scalarPipe)
}
def toCol : ColVector[IdxT,ValT] = {
new ColVector[IdxT,ValT](idxSym, valSym, inPipe, sizeHint.setRows(1L))
}
def toRow : RowVector[IdxT,ValT] = {
new RowVector[IdxT,ValT](idxSym, valSym, inPipe, sizeHint.setCols(1L))
}
// Inverse of this matrix *IGNORING ZEROS*
def inverse(implicit field : Field[ValT]) : DiagonalMatrix[IdxT, ValT] = {
val diagPipe = inPipe.flatMap(valSym -> valSym) { element : ValT =>
field.nonZeroOption(element)
.map { field.inverse }
}
new DiagonalMatrix[IdxT,ValT](idxSym, valSym, diagPipe, sizeHint)
}
// Value operations
def mapValues[ValU](fn:(ValT) => ValU)(implicit mon : Monoid[ValU]) : DiagonalMatrix[IdxT,ValU] = {
val newPipe = pipe.flatMap(valSym -> valSym) { imp : Tuple1[ValT] => // Ensure an arity of 1
//This annoying Tuple1 wrapping ensures we can handle ValT that may itself be a Tuple.
mon.nonZeroOption(fn(imp._1)).map { Tuple1(_) }
}
new DiagonalMatrix[IdxT,ValU](this.idxSym, this.valSym, newPipe, sizeHint)
}
/** Write optionally renaming val fields to the given fields
* then return this.
*/
def write(src : Source, outFields : Fields = Fields.NONE)(implicit fd : FlowDef) = {
writePipe(src, outFields)
this
}
}
class RowVector[ColT,ValT] (val colS:Symbol, val valS:Symbol, inPipe: Pipe, val sizeH: SizeHint = FiniteHint(1L, -1L))
extends java.io.Serializable with WrappedPipe {
def pipe = inPipe.project(colS,valS)
def fields = (colS,valS)
def *[That,Res](that : That)(implicit prod : MatrixProduct[RowVector[ColT,ValT],That,Res]) : Res
= { prod(this, that) }
def +(that : RowVector[ColT,ValT])(implicit mon : Monoid[ValT]) = (this.toMatrix(true) + that.toMatrix(true)).getRow(true)
def -(that : RowVector[ColT,ValT])(implicit group : Group[ValT]) = (this.toMatrix(true) - that.toMatrix(true)).getRow(true)
def hProd(that: RowVector[ColT,ValT])(implicit ring: Ring[ValT]) : RowVector[ColT,ValT] = (this.transpose hProd that.transpose).transpose
def transpose : ColVector[ColT,ValT] = {
new ColVector[ColT,ValT](colS, valS, inPipe, sizeH.transpose)
}
def diag : DiagonalMatrix[ColT,ValT] = {
val newHint = SizeHint.asDiagonal(sizeH.setRowsToCols)
new DiagonalMatrix[ColT,ValT](colS, valS, inPipe, newHint)
}
/** like zipWithIndex.map but ONLY CHANGES THE VALUE not the index.
* Note you will only see non-zero elements on the vector. This does not enumerate the zeros
*/
def mapWithIndex[ValNew](fn: (ValT,ColT) => ValNew)(implicit mon: Monoid[ValNew]):
RowVector[ColT,ValNew] = {
val newPipe = pipe.mapTo((valS,colS) -> (valS,colS)) { tup: (ValT,ColT) => (fn(tup._1, tup._2), tup._2) }
.filter(valS) { (v: ValNew) => mon.isNonZero(v) }
new RowVector(colS, valS, newPipe, sizeH)
}
// Value operations
def mapValues[ValU](fn:(ValT) => ValU)(implicit mon : Monoid[ValU]) : RowVector[ColT,ValU] = {
val newPipe = pipe.flatMap(valS -> valS) { imp : Tuple1[ValT] => // Ensure an arity of 1
//This annoying Tuple1 wrapping ensures we can handle ValT that may itself be a Tuple.
mon.nonZeroOption(fn(imp._1)).map { Tuple1(_) }
}
new RowVector[ColT,ValU](this.colS, this.valS, newPipe, sizeH)
}
/** Do a right-propogation of a row, transpose of Matrix.propagate
*/
def propagate[MatColT](mat: Matrix[ColT,MatColT,Boolean])(implicit monT: Monoid[ValT])
: RowVector[MatColT,ValT] = {
mat.transpose.propagate(this.transpose).transpose
}
def L0Normalize(implicit ev : =:=[ValT,Double]) : RowVector[ColT,ValT] = {
val normedMatrix = this.toMatrix(0).rowL0Normalize
new RowVector(normedMatrix.colSym,
normedMatrix.valSym,
normedMatrix.pipe.project(normedMatrix.colSym, normedMatrix.valSym))
}
def L1Normalize(implicit ev : =:=[ValT,Double]) : RowVector[ColT,ValT] = {
val normedMatrix = this.toMatrix(0).rowL1Normalize
new RowVector(normedMatrix.colSym,
normedMatrix.valSym,
normedMatrix.pipe.project(normedMatrix.colSym, normedMatrix.valSym))
}
def sum(implicit mon : Monoid[ValT]) : Scalar[ValT] = {
val scalarPipe = pipe.groupAll{ _.reduce(valS -> valS) { (left : Tuple1[ValT], right : Tuple1[ValT]) =>
Tuple1(mon.plus(left._1, right._1))
}
}
new Scalar[ValT](valS, scalarPipe)
}
def topElems( k : Int )(implicit ord : Ordering[ValT]) : RowVector[ColT,ValT] = {
// TODO this should be tunable:
if (k < 1000) { topWithTiny(k) }
else {
val fieldName = valS.toString
val ordValS = new Fields(fieldName)
ordValS.setComparator(fieldName, ord)
val newPipe = pipe.groupAll{ _
.sortBy(ordValS)
.reverse
.take(k)
}.project(colS,valS)
new RowVector[ColT,ValT](colS, valS, newPipe, sizeH.setCols(k).setRows(1L))
}
}
protected def topWithTiny( k : Int )(implicit ord : Ordering[ValT]) : RowVector[ColT,ValT] = {
val topSym = Symbol(colS.name + "_topK")
val newPipe = pipe.groupAll{ _
.sortWithTake( (colS, valS) -> 'top_vals, k ) ( (t0 :(ColT,ValT), t1:(ColT,ValT)) => ord.gt(t0._2,t1._2) )
}
.flatMap('top_vals ->(topSym, valS)) { imp:List[(ColT,ValT)] => imp }
new RowVector[ColT,ValT](topSym, valS, newPipe, sizeH.setCols(k).setRows(1L))
}
def toMatrix[RowT](rowId : RowT) : Matrix[RowT,ColT,ValT] = {
val rowSym = newSymbol(Set(colS, valS), 'row) //Matrix.newSymbol(Set(colS, valS), 'row)
val newPipe = inPipe.map(() -> rowSym){ u: Unit => rowId }
.project(rowSym, colS, valS)
new Matrix[RowT,ColT,ValT](rowSym, colS, valS, newPipe, sizeH.setRows(1L))
}
// Override the size hint
def withColsHint(cols : Long) : RowVector[ColT,ValT] = {
new RowVector[ColT,ValT](colS, valS, pipe, sizeH.setRows(1L).setCols(cols))
}
/** Write optionally renaming val fields to the given fields
* then return this.
*/
def write(src : Source, outFields : Fields = Fields.NONE)(implicit fd : FlowDef) = {
writePipe(src, outFields)
this
}
}
class ColVector[RowT,ValT] (val rowS:Symbol, val valS:Symbol, inPipe : Pipe, val sizeH: SizeHint = FiniteHint(-1L, 1L))
extends java.io.Serializable with WrappedPipe {
def pipe = inPipe.project(rowS,valS)
def fields = (rowS,valS)
def *[That,Res](that : That)(implicit prod : MatrixProduct[ColVector[RowT,ValT],That,Res]) : Res
= { prod(this, that) }
def +(that : ColVector[RowT,ValT])(implicit mon : Monoid[ValT]) = (this.toMatrix(true) + that.toMatrix(true)).getCol(true)
def -(that : ColVector[RowT,ValT])(implicit group : Group[ValT]) = (this.toMatrix(true) - that.toMatrix(true)).getCol(true)
def hProd(that: ColVector[RowT,ValT])(implicit ring: Ring[ValT]) : ColVector[RowT,ValT] = (this.toMatrix(true) hProd that.toMatrix(true)).getCol(true)
def transpose : RowVector[RowT,ValT] = {
new RowVector[RowT,ValT](rowS, valS, inPipe, sizeH.transpose)
}
def diag : DiagonalMatrix[RowT,ValT] = {
val newHint = SizeHint.asDiagonal(sizeH.setRowsToCols)
new DiagonalMatrix[RowT,ValT](rowS, valS, inPipe, newHint)
}
/** like zipWithIndex.map but ONLY CHANGES THE VALUE not the index.
* Note you will only see non-zero elements on the vector. This does not enumerate the zeros
*/
def mapWithIndex[ValNew](fn: (ValT,RowT) => ValNew)(implicit mon: Monoid[ValNew]):
ColVector[RowT,ValNew] = transpose.mapWithIndex(fn).transpose
// Value operations
def mapValues[ValU](fn:(ValT) => ValU)(implicit mon : Monoid[ValU]) : ColVector[RowT,ValU] = {
val newPipe = pipe.flatMap(valS -> valS) { imp : Tuple1[ValT] => // Ensure an arity of 1
//This annoying Tuple1 wrapping ensures we can handle ValT that may itself be a Tuple.
mon.nonZeroOption(fn(imp._1)).map { Tuple1(_) }
}
new ColVector[RowT,ValU](this.rowS, this.valS, newPipe, sizeH)
}
def sum(implicit mon : Monoid[ValT]) : Scalar[ValT] = {
val scalarPipe = pipe.groupAll{ _.reduce(valS -> valS) { (left : Tuple1[ValT], right : Tuple1[ValT]) =>
Tuple1(mon.plus(left._1, right._1))
}
}
new Scalar[ValT](valS, scalarPipe)
}
def L0Normalize(implicit ev : =:=[ValT,Double]) : ColVector[RowT,ValT] = {
val normedMatrix = this.toMatrix(0).colL0Normalize
new ColVector(normedMatrix.rowSym,
normedMatrix.valSym,
normedMatrix.pipe.project(normedMatrix.rowSym, normedMatrix.valSym))
}
def L1Normalize(implicit ev : =:=[ValT,Double]) : ColVector[RowT,ValT] = {
val normedMatrix = this.toMatrix(0).colL1Normalize
new ColVector(normedMatrix.rowSym,
normedMatrix.valSym,
normedMatrix.pipe.project(normedMatrix.rowSym, normedMatrix.valSym))
}
def topElems( k : Int )(implicit ord : Ordering[ValT]) : ColVector[RowT,ValT] = {
if (k < 1000) { topWithTiny(k) }
else {
val newPipe = pipe.groupAll{ _
.sortBy(valS)
.reverse
.take(k)
}.project(rowS,valS)
new ColVector[RowT,ValT](rowS, valS, newPipe, sizeH.setCols(1L).setRows(k))
}
}
protected def topWithTiny( k : Int )(implicit ord : Ordering[ValT]) : ColVector[RowT,ValT] = {
val topSym = Symbol(rowS.name + "_topK")
val newPipe = pipe.groupAll{ _
.sortWithTake( (rowS, valS) -> 'top_vals, k ) ( (t0 :(RowT,ValT), t1:(RowT,ValT)) => ord.gt(t0._2,t1._2) )
}
.flatMap('top_vals ->(topSym, valS)) { imp:List[(RowT,ValT)] => imp }
new ColVector[RowT,ValT](topSym, valS, newPipe, sizeH.setCols(1L).setRows(k))
}
def toMatrix[ColT](colIdx : ColT) : Matrix[RowT,ColT,ValT] = {
val colSym = newSymbol(Set(rowS, valS), 'col) //Matrix.newSymbol(Set(rowS, valS), 'col)
val newPipe = inPipe.map(() -> colSym){ u:Unit => colIdx }
.project(rowS, colSym, valS)
new Matrix[RowT,ColT,ValT](rowS, colSym, valS, newPipe, sizeH.setCols(1L))
}
// Override the size hint
def withRowsHint(rows : Long) : ColVector[RowT,ValT] = {
new ColVector[RowT,ValT](rowS, valS, pipe, sizeH.setRows(rows).setCols(1L))
}
/** Write optionally renaming val fields to the given fields
* then return this.
*/
def write(src : Source, outFields : Fields = Fields.NONE)(implicit fd : FlowDef) = {
writePipe(src, outFields)
this
}
}