Permalink
Browse files

git-svn-id: http://rb-gsl.rubyforge.org/svn/trunk/rb-gsl@99 6e764f74-…

…f39f-46f8-8c54-8307d62afe8d
  • Loading branch information...
1 parent 345315b commit 91d9401e5c77cf1d3169264b6bb1001a54250b59 ytsunesada committed Aug 17, 2009
View
@@ -1,3 +1,6 @@
+Sun Aug 16 19:01:31 BOT 2009
+ * Added Non-negative Matrix Factorization (NMF) support (by Roman Shterenzon)
+
Sat Aug 8 22:36:24 BOT 2009
* Added GSL-1.13 features:
* Added GSL::Monte::Miser#params_get, GSL::Monte::Vegas#params_get
View
2 THANKS
@@ -14,4 +14,4 @@ Daigo Moriwaki (Bug report)
Joe Wölfel (Ruby-1.9 support)
Cyrus Hall (patch for Ruby-1.9)
Dave MacMahon (patch for FFT and interface refinements)
-
+Roman Shterenzon (Non-negative Matrix Factorization)
View
@@ -60,8 +60,8 @@ static VALUE rb_alf_Plm_array(int argc, VALUE *argv, VALUE obj)
Data_Get_Struct(argv[1], gsl_vector, res);
lmax = w->lmax;
if (res->size < alf_array_size(lmax)) {
- rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", res->size,
- alf_array_size(lmax));
+ rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", (int) res->size,
+ (int) alf_array_size(lmax));
}
ret = argv[1];
} else {
@@ -85,8 +85,8 @@ static VALUE rb_alf_Plm_array(int argc, VALUE *argv, VALUE obj)
CHECK_VECTOR(argv[2]);
Data_Get_Struct(argv[2], gsl_vector, res);
if (res->size < alf_array_size(lmax)) {
- rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", res->size,
- alf_array_size(lmax));
+ rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", (int) res->size,
+ (int) alf_array_size(lmax));
}
ret = argv[2];
}
@@ -154,12 +154,12 @@ static VALUE rb_alf_Plm_deriv_array(int argc, VALUE *argv, VALUE obj)
Data_Get_Struct(argv[2], gsl_vector, res);
Data_Get_Struct(argv[3], gsl_vector, deriv);
if (res->size < alf_array_size(lmax)) {
- rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", res->size,
- alf_array_size(lmax));
+ rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", (int) res->size,
+ (int) alf_array_size(lmax));
}
if (deriv->size < alf_array_size(lmax)) {
- rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", res->size,
- alf_array_size(lmax));
+ rb_raise(rb_eRuntimeError, "Vector length is too small. (%d for >= %d\n", (int) res->size,
+ (int) alf_array_size(lmax));
}
ret1 = argv[2];
ret2 = argv[3];
View
@@ -33,11 +33,13 @@ FILE* rb_gsl_open_writefile(VALUE io, int *flag)
break;
case T_FILE:
GetOpenFile(io, fptr);
+ /*
#ifdef RUBY_1_9_LATER
name = STR2CSTR(fptr->pathv);
#else
name = fptr->path;
#endif
+ */
rb_io_check_writable(fptr);
#ifdef RUBY_1_9_LATER
fp = rb_io_stdio_file(fptr);
@@ -50,7 +52,8 @@ FILE* rb_gsl_open_writefile(VALUE io, int *flag)
rb_raise(rb_eTypeError, "argv 1 String or File expected");
break;
}
- if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
+ // if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
+ if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file.");
return fp;
}
@@ -71,11 +74,13 @@ FILE* rb_gsl_open_readfile(VALUE io, int *flag)
break;
case T_FILE:
GetOpenFile(io, fptr);
+ /*
#ifdef RUBY_1_9_LATER
name = STR2CSTR(fptr->pathv);
#else
name = fptr->path;
#endif
+ */
rb_io_check_readable(fptr);
#ifdef RUBY_1_9_LATER
fp = rb_io_stdio_file(fptr);
@@ -88,7 +93,8 @@ FILE* rb_gsl_open_readfile(VALUE io, int *flag)
rb_raise(rb_eTypeError, "argv 1 String or File expected");
break;
}
- if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
+ // if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file %s.", name);
+ if (fp == NULL) rb_raise(rb_eIOError, "Cannot open file");
return fp;
}
View
@@ -357,7 +357,7 @@ static VALUE rb_gsl_matrix_complex_set(int argc, VALUE *argv, VALUE obj)
Data_Get_Struct(other, gsl_matrix_complex, mother);
if(n1 * n2 != mother->size1 * mother->size2) {
rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d x %d)",
- n1, n2, mother->size1, mother->size2);
+ (int) n1, (int) n2, (int) mother->size1, (int) mother->size2);
}
// TODO Change to gsl_matrix_memmove if/when GSL has such a function
// because gsl_matrix_memcpy does not handle overlapping regions (e.g.
@@ -375,7 +375,7 @@ static VALUE rb_gsl_matrix_complex_set(int argc, VALUE *argv, VALUE obj)
// m[...] = [[row0], [row1], ...] # multiple rows
if(n1 != RARRAY_LEN(other)) {
rb_raise(rb_eRangeError, "row counts do not match (%d != %d)",
- n1, RARRAY_LEN(other));
+ (int) n1, (int) RARRAY_LEN(other));
}
for(k = 0; k < n1; k++) {
vv = gsl_matrix_complex_row(&mv.matrix, k);
@@ -387,7 +387,7 @@ static VALUE rb_gsl_matrix_complex_set(int argc, VALUE *argv, VALUE obj)
// m[...] = beg..end
get_range_beg_en_n(other, &beg, &end, &nother, &step);
if(n1 * n2 != nother) {
- rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d)", n1, n2, nother);
+ rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d)", (int) n1, (int) n2, (int) nother);
}
tmp = gsl_complex_rect(beg, 0.0);
for(k = 0; k < nother; k++) {
@@ -493,7 +493,7 @@ static VALUE rb_gsl_matrix_complex_get(int argc, VALUE *argv, VALUE obj)
*c = gsl_matrix_complex_get(m, (size_t)ii, (size_t)ij);
retval = Data_Wrap_Struct(cgsl_complex, 0, free, c);
} else {
- rb_raise(rb_eArgError, "Array index must have length 2, not %d", RARRAY_LEN(argv[0]));
+ rb_raise(rb_eArgError, "Array index must have length 2, not %d", (int) RARRAY_LEN(argv[0]));
}
} else {
retval = rb_gsl_matrix_complex_submatrix(argc, argv, obj);
View
@@ -706,7 +706,7 @@ static VALUE FUNCTION(rb_gsl_matrix,get)(int argc, VALUE *argv, VALUE obj)
if(ij < 0) ij += m->size2;
retval = C_TO_VALUE2(FUNCTION(gsl_matrix,get)(m, (size_t)ii, (size_t)ij));
} else {
- rb_raise(rb_eArgError, "Array index must have length 2, not %d", RARRAY_LEN(argv[0]));
+ rb_raise(rb_eArgError, "Array index must have length 2, not %d", (int) RARRAY_LEN(argv[0]));
}
} else {
retval = FUNCTION(rb_gsl_matrix,submatrix)(argc, argv, obj);
@@ -763,7 +763,7 @@ static VALUE FUNCTION(rb_gsl_matrix,set)(int argc, VALUE *argv, VALUE obj)
Data_Get_Struct(other, GSL_TYPE(gsl_matrix), mother);
if(n1 * n2 != mother->size1 * mother->size2) {
rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d x %d)",
- n1, n2, mother->size1, mother->size2);
+ (int) n1, (int) n2, (int) mother->size1, (int) mother->size2);
}
// TODO Change to gsl_matrix_memmove if/when GSL has such a function
// because gsl_matrix_memcpy does not handle overlapping regions (e.g.
@@ -781,7 +781,7 @@ static VALUE FUNCTION(rb_gsl_matrix,set)(int argc, VALUE *argv, VALUE obj)
// m[...] = [[row0], [row1], ...] # multiple rows
if(n1 != RARRAY_LEN(other)) {
rb_raise(rb_eRangeError, "row counts do not match (%d != %d)",
- n1, RARRAY_LEN(other));
+ (int) n1, (int) RARRAY_LEN(other));
}
for(k = 0; k < n1; k++) {
vv = FUNCTION(gsl_matrix,row)(&mv.matrix, k);
@@ -793,7 +793,7 @@ static VALUE FUNCTION(rb_gsl_matrix,set)(int argc, VALUE *argv, VALUE obj)
// m[...] = beg..end
FUNCTION(get_range,beg_en_n)(other, &beg, &end, &nother, &step);
if(n1 * n2 != nother) {
- rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d)", n1, n2, nother);
+ rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d)", (int) n1, (int) n2, (int) nother);
}
for(k = 0; k < nother; k++) {
FUNCTION(gsl_matrix,set)(&mv.matrix, k / n2, k % n2, beg);
@@ -2265,7 +2265,7 @@ static VALUE FUNCTION(rb_gsl_matrix,horzcat)(VALUE obj, VALUE mm2)
Data_Get_Struct(mm2, GSL_TYPE(gsl_matrix), m2);
if (m->size1 != m2->size1)
rb_raise(rb_eRuntimeError, "Different number of rows (%d and %d).",
- m->size1, m2->size1);
+ (int) m->size1, (int) m2->size1);
mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2 + m2->size2);
for (j = 0, k = 0; j < m->size2; j++, k++) {
v = FUNCTION(gsl_matrix,column)(m, j);
@@ -2295,7 +2295,7 @@ static VALUE FUNCTION(rb_gsl_matrix,vertcat)(VALUE obj, VALUE mm2)
Data_Get_Struct(mm2, GSL_TYPE(gsl_matrix), m2);
if (m->size2 != m2->size2)
rb_raise(rb_eRuntimeError, "Different number of columns (%d and %d).",
- m->size2, m2->size2);
+ (int) m->size2, (int) m2->size2);
mnew = FUNCTION(gsl_matrix,alloc)(m->size1 + m2->size1, m->size2);
for (i = 0, k = 0; i < m->size1; i++, k++) {
v = FUNCTION(gsl_matrix,row)(m, i);
View
@@ -1,3 +1,10 @@
+/**
+ * NMF: Non-Negative Matrix Factorization
+ *
+ * Written by Roman Shterenzon
+ * (Slightly modified by Y.Tsunesada: just added "const" qualifiers etc.)
+ */
+
#include <math.h>
#include <time.h>
#include <gsl/gsl_matrix.h>
@@ -9,7 +16,8 @@
#undef DEBUG
#define mm(a, b) gsl_matrix_mult(a, b)
-gsl_matrix * gsl_matrix_mult(gsl_matrix *a, gsl_matrix *b)
+//gsl_matrix * gsl_matrix_mult(gsl_matrix *a, gsl_matrix *b)
+gsl_matrix * gsl_matrix_mult(const gsl_matrix *a, const gsl_matrix *b)
{
gsl_matrix *c;
c = gsl_matrix_alloc(a->size1, b->size2);
@@ -19,7 +27,8 @@ gsl_matrix * gsl_matrix_mult(gsl_matrix *a, gsl_matrix *b)
}
// pretty print
-void pp(gsl_matrix *m)
+//void pp(gsl_matrix *m)
+void pp(const gsl_matrix *m)
{
int r, c;
@@ -32,16 +41,19 @@ void pp(gsl_matrix *m)
}
/* Returns a distance cost */
-double difcost(gsl_matrix *a, gsl_matrix *b)
+//double difcost(gsl_matrix *a, gsl_matrix *b)
+double difcost(const gsl_matrix *a, const gsl_matrix *b)
{
int i, j;
- double dif=0;
+ double dif=0, d;
for (i=0; i < a->size1; i++)
{
for (j=0; j < a->size2; j++)
{
- dif += pow(gsl_matrix_get(a, i, j) - gsl_matrix_get(b, i, j), 2);
+ d = gsl_matrix_get(a, i, j) - gsl_matrix_get(b, i, j);
+ dif += d*d;
+ // dif += pow(gsl_matrix_get(a, i, j) - gsl_matrix_get(b, i, j), 2);
}
}
return dif;
View
@@ -1,10 +1,19 @@
+/**
+ * NMF: Non-Negative Matrix Factorization - Ruby wrapper
+ *
+ * Written by Roman Shterenzon
+ *
+ */
+
#include <ruby.h>
#include <gsl/gsl_matrix.h>
int gsl_matrix_nmf(gsl_matrix *v, int cols, gsl_matrix **w, gsl_matrix **h);
-double difcost(gsl_matrix *a, gsl_matrix *b);
+//double difcost(gsl_matrix *a, gsl_matrix *b);
+double difcost(const gsl_matrix *a, const gsl_matrix *b);
-VALUE mNMF;
+//VALUE mNMF;
+static VALUE mNMF;
extern VALUE cgsl_matrix;
/*
@@ -53,7 +62,8 @@ static VALUE difcost_wrap(VALUE obj, VALUE matrix1, VALUE matrix2)
*/
static VALUE matrix_nmf(VALUE obj, VALUE cols)
{
- nmf_wrap(cgsl_matrix, obj, cols);
+ // nmf_wrap(cgsl_matrix, obj, cols);
+ return nmf_wrap(cgsl_matrix, obj, cols);
}
void Init_gsl_matrix_nmf(void) {
View
@@ -1637,7 +1637,7 @@ static VALUE rb_gsl_poly_eval_derivs_singleton(int argc, VALUE *argv, VALUE klas
int shape[1];
#endif
- if (argc < 2) rb_raise(rb_eArgError, "Wrong number of arguments (%d for >= 2)");
+ if (argc < 2) rb_raise(rb_eArgError, "Wrong number of arguments (%d for >= 2)", argc);
if (rb_obj_is_kind_of(argv[0], rb_cArray)) {
v = gsl_vector_alloc(RARRAY_LEN(argv[0]));
lenc = v->size;
@@ -1695,7 +1695,7 @@ static VALUE rb_gsl_poly_eval_derivs(int argc, VALUE *argv, VALUE obj)
lenres = FIX2INT(argv[1]);
break;
default:
- rb_raise(rb_eArgError, "Wrong number of arguments (%d for > 1)");
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for > 1)", argc);
}
v2 = gsl_vector_alloc(lenres);
gsl_poly_eval_derivs(v->data, lenc, NUM2DBL(argv[0]), v2->data, lenres);
View
@@ -242,7 +242,7 @@ void rb_gsl_vector_complex_set_subvector(int argc, VALUE *argv, gsl_vector_compl
if(rb_obj_is_kind_of(other, cgsl_vector_complex)) {
Data_Get_Struct(other, gsl_vector_complex, vother);
if(n != vother->size) {
- rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", n, vother->size);
+ rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", (int) n, (int) vother->size);
}
// TODO Change to gsl_vector_complex_memmove if/when GSL has such a
// function because gsl_vector_memcpy does not handle overlapping regions
@@ -251,7 +251,7 @@ void rb_gsl_vector_complex_set_subvector(int argc, VALUE *argv, gsl_vector_compl
} else if(rb_obj_is_kind_of(other, rb_cArray)) {
// TODO Support other forms of Array contents as well
if(n != RARRAY_LEN(other)) {
- rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", n, RARRAY_LEN(other));
+ rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", (int) n, (int) RARRAY_LEN(other));
}
for(i = 0; i < n; i++) {
tmp = rb_gsl_obj_to_gsl_complex(rb_ary_entry(other, i), NULL);
@@ -260,7 +260,7 @@ void rb_gsl_vector_complex_set_subvector(int argc, VALUE *argv, gsl_vector_compl
} else if(rb_obj_is_kind_of(other, rb_cRange)) {
get_range_beg_en_n(other, &beg, &end, &nother, &step);
if(n != nother) {
- rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", n, nother);
+ rb_raise(rb_eRangeError, "lengths do not match (%d != %d)", (int) n, (int) nother);
}
GSL_SET_IMAG(&tmp, 0.0);
for(i = 0; i < n; i++) {
View
@@ -84,7 +84,7 @@ static VALUE rb_gsl_vector_linspace(int argc, VALUE *argv, VALUE klass)
/* do nothing */
break;
default:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)");
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
break;
}
Need_Float(argv[0]); Need_Float(argv[1]);
@@ -104,7 +104,7 @@ static VALUE rb_gsl_vector_logspace(int argc, VALUE *argv, VALUE klass)
/* do nothing */
break;
default:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)");
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
break;
}
Need_Float(argv[0]); Need_Float(argv[1]);
@@ -124,7 +124,7 @@ static VALUE rb_gsl_vector_logspace2(int argc, VALUE *argv, VALUE klass)
/* do nothing */
break;
default:
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)");
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
break;
}
Need_Float(argv[0]); Need_Float(argv[1]);
@@ -732,7 +732,7 @@ static void draw_vector_array(VALUE ary, FILE *fp)
break;
default:
rb_raise(rb_eRuntimeError, "wrong array length (%d for 1 or 2)",
- RARRAY_LEN(ary));
+ (int) RARRAY_LEN(ary));
break;
}
switch (flag) {
Oops, something went wrong.

0 comments on commit 91d9401

Please sign in to comment.