Sha256: 120d1f00b7620c13a2180d82536c8520c9ad0d41b8209789c4138540daa6cf98
Contents?: true
Size: 2 KB
Versions: 1
Compression:
Stored size: 2 KB
Contents
require 'inline' module MadeByMany module ActsAsRecommendable class Optimizations module InlineC inline do |builder| builder.include "<math.h>" builder.include "<ruby.h>" builder.c ' double c_sim_pearson(VALUE items, int n, VALUE prefs1, VALUE prefs2) { double sum1 = 0.0; double sum2 = 0.0; double sum1Sq = 0.0; double sum2Sq = 0.0; double pSum = 0.0; double num; double den; int i; VALUE *items_a = RARRAY_PTR(items); for(i=0; i<n; i++) { double prefs1_item; double prefs2_item; VALUE prefs1_item_ob; VALUE prefs2_item_ob; if (!st_lookup(RHASH(prefs1)->ntbl, items_a[i], &prefs1_item_ob)) { prefs1_item = 0.0; } else { prefs1_item = NUM2DBL(prefs1_item_ob); } if (!st_lookup(RHASH(prefs2)->ntbl, items_a[i], &prefs2_item_ob)) { prefs2_item = 0.0; } else { prefs2_item = NUM2DBL(prefs2_item_ob); } sum1 += prefs1_item; sum2 += prefs2_item; sum1Sq += pow(prefs1_item, 2); sum2Sq += pow(prefs2_item, 2); pSum += prefs2_item * prefs1_item; } num = pSum - ( ( sum1 * sum2 ) / n ); den = sqrt( ( sum1Sq - ( pow(sum1, 2) ) / n ) * ( sum2Sq - ( pow(sum2, 2) ) / n ) ); if(den == 0){ return 0.0; } else { return num / den; } }' end end class << self include InlineC end end module Logic # Pearson score def self.sim_pearson(prefs, items, person1, person2) n = items.length return 0 if n == 0 Optimizations.c_sim_pearson(items, n, prefs[person1], prefs[person2]) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
acts_as_recommendable-0.0.2 | lib/optimizations.rb |