0

I have the following ruby script, running with 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 are not conformant (GSL::ERROR::EBADLEN) from testls.rb:9:in

I think my matrices have the right dimensions for an over-determined LS problem, so I can't understand the error message.

In Matlab, I 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 would like to make the matrix/vector sizes conformant and still express an over-determined problem. It would seem from the 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 to task at hand, so is it the binding to Ruby that is broken or my understanding of the correct usage? All help will be appreciated.

1 Answers1

0

Answering my own question: It seems you have to preallocate vectors for the solution x, and the residual and pass them as 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 does indeed yield

GSL::Vector
[ 8.068e+00 8.844e-01 2.319e-01 ]

Charles.