@@ -437,6 +437,54 @@ class ML::SparseMatrixRecommender
437437 return self ;
438438 }
439439
440+ # #========================================================
441+ # # Apply tag weights
442+ # #========================================================
443+ method apply-tag-weights (@ weights ) {
444+ my $ col-count = self . take-M. columns-count;
445+ die " The first argument is expected to be a list of numbers of length $ col-count ."
446+ unless @ weights . elems == $ col-count && @ weights . all ~~ Numeric : D ;
447+
448+ my $ W = Math::SparseMatrix. new (diagonal => @ weights );
449+ my $ mat-res = self . take-M. clone . dot($ W );
450+
451+ $ mat-res . set-row-names(self . take-M. row-names);
452+ $ mat-res . set-column-names(self . take-M. column-names);
453+
454+ self . set-M($ mat-res );
455+ return self ;
456+ }
457+
458+ # #========================================================
459+ # # Apply tag type weights
460+ # #========================================================
461+ method apply-tag-type-weights ($ weights ) {
462+ my @ weights-list ;
463+
464+ if $ weights ~~ (Array : D | List : D | Seq : D ) {
465+ die " A list first argument is expected to match the number of sub-matrices ({ % ! matrices . elems } ) and have numeric elements."
466+ unless $ weights . elems == % ! matrices . elems && $ weights . all ~~ Numeric : D ;
467+
468+ my @ keys = % ! matrices . keys ;
469+ for $ weights . kv -> $ i , $ w {
470+ @ weights-list . append ( $ w xx % ! matrices {@ keys [$ i ]}. columns-count );
471+ }
472+ } elsif $ weights ~~ Map : D {
473+ my @ unknown-keys = $ weights . keys . grep ({ % ! matrices {$ _ }:! exists });
474+ die " Unknown tag types in weights argument: { @ unknown-keys } ."
475+ if @ unknown-keys ;
476+
477+ for % ! matrices . keys -> $ key {
478+ my $ w = $ weights {$ key } // 1 ;
479+ @ weights-list . append ( $ w xx % ! matrices {$ key }. columns-count );
480+ }
481+ } else {
482+ die " The first argument must be a list or a hashmap of length { % ! matrices . elems } ." ;
483+ }
484+
485+ return self . apply-tag-weights(@ weights-list );
486+ }
487+
440488 # #========================================================
441489 # # Profile
442490 # #========================================================
0 commit comments