Skip to content

Commit

Permalink
Add 'nmf' instance method to GSL::Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
romanbsd committed Feb 12, 2009
1 parent c881b78 commit b441641
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 9 additions & 7 deletions ext/nmf_wrap.c
Expand Up @@ -48,16 +48,18 @@ static VALUE difcost_wrap(VALUE obj, VALUE matrix1, VALUE matrix2)
return rb_float_new(difcost(m1, m2));
}

void Init_gsl_matrix_nmf(void) {
/*
cgsl = rb_define_module("GSL");
cgsl_matrix = rb_define_module_under(cgsl, "Matrix");
cgsl_matrix = rb_const_get(rb_const_get(rb_cObject, rb_intern("GSL")),
rb_intern("Matrix"));
*/
/* call-seq:
* nmf(cols) -> [GSL::Matrix, GSL::Matrix]
*/
static VALUE matrix_nmf(VALUE obj, VALUE cols)
{
nmf_wrap(cgsl_matrix, obj, cols);
}

void Init_gsl_matrix_nmf(void) {
mNMF = rb_define_module_under(cgsl_matrix, "NMF");

rb_define_singleton_method(mNMF, "nmf", nmf_wrap, 2);
rb_define_singleton_method(mNMF, "difcost", difcost_wrap, 2);
rb_define_method(cgsl_matrix, "nmf", matrix_nmf, 1);
}
12 changes: 11 additions & 1 deletion tests/matrix/matrix_nmf_test.rb
@@ -1,8 +1,9 @@
#!/usr/bin/env ruby

$:.unshift(*['lib','ext'].collect{|d| File.join(File.dirname(__FILE__),'..','..',d)})
require 'rubygems'
require 'test/unit'
require 'gsl'
#require 'nmf'

class MatrixNmfTest < Test::Unit::TestCase

Expand All @@ -25,4 +26,13 @@ def test_nmf
assert(cost <= 0.000001, "Cols: #{cols}, Delta: #{cost}")
end
end
def test_matrix_nmf
[2, 3, 4, 5].each do |cols|
res = @m1.nmf(cols)
assert_equal([3,cols], res[0].size)
assert_equal([cols,3], res[1].size)
cost = GSL::Matrix::NMF.difcost(@m1, res[0]*res[1])
assert(cost <= 0.000001, "Cols: #{cols}, Delta: #{cost}")
end
end
end

0 comments on commit b441641

Please sign in to comment.