Failing to solve a simple least squares fit with Ruby GSL -


i have following ruby script, running rb-gsl (1.16.0.6) under ruby-2.2.1

require("gsl") include gsl  m = gsl::matrix::alloc([0.18, 0.60, 0.57], [0.24, 0.99, 0.58],                 [0.14, 0.30, 0.97], [0.51,  0.19, 0.85], [0.34, 0.91, 0.18])  b = gsl::vector[1, 2, 3, 4, 5] qr, tau = m.qr_decomp x, res = qr.qr_lssolve(tau,b) 

the resulting error is:

testls.rb:9:in qr_lssolve: ruby/gsl error code 19, matrix size must match >solution size (file qr.c, line 193), matrix/vector sizes not conformant (gsl::error::ebadlen) testls.rb:9:in

i think matrices have right dimensions over-determined ls problem, can't understand error message.

in matlab, can write:

m=[[0.18, 0.60, 0.57]; [0.24, 0.99, 0.58];...         [0.14, 0.30, 0.97]; [0.51,  0.19, 0.85]; [0.34, 0.91, 0.18]]; b=[1, 2, 3, 4, 5]';  x=m\b 

and get

x =  8.0683 0.8844 0.2319 

i make matrix/vector sizes conformant , still express over-determined problem. seem gsl documentation that

 gsl_linalg_qr_lssolve (const gsl_matrix * qr, const gsl_vector * tau, const  gsl_vector * b, gsl_vector * x, gsl_vector * residual)  

is well-suited task @ hand, binding ruby broken or understanding of correct usage? appreciated.

answering own question: seems have preallocate vectors solution x, , residual , pass them arguments:

require("gsl") include gsl  m = gsl::matrix::alloc([0.18, 0.60, 0.57], [0.24, 0.99, 0.58],                 [0.14, 0.30, 0.97], [0.51,  0.19, 0.85], [0.34, 0.91, 0.18])  b = gsl::vector[1, 2, 3, 4, 5] qr, tau = m.qr_decomp x = gsl::vector.alloc(3) r = gsl::vector.alloc(5) qr.qr_lssolve(tau,b,x,r)  p x 

this indeed yield

gsl::vector [ 8.068e+00 8.844e-01 2.319e-01 ] 

charles.


Comments