# Ublas :

Subject: Re: [ublas] Solving simple vector equation *newbie*
From: petros (pmamales_at_[hidden])
Date: 2012-02-29 14:54:20

OK,
you have a system of 7 equations in 3 unknowns.
generally there is no solution to this ( unless 4 extra equations are linear
combinations of the rest three ).
So you have to do a least squares fit (i.e. a regression).
So, call
x = (a,b,c)' ( ' means transpose here )
and
M the matrix whose columns are X, Y and Z.
Then your solution is given by : (
http://en.wikipedia.org/wiki/Linear_regression_model )

x = (M'*M)^(-1)*M'*R
i.e.
solve the system:
(M'*M) * x = M' * R

Use trans(A) to obtain the transpose of a matrix.
Use prod( A, B) for the product of matrices - really look into the "overview
of matrix and vector operations" (google the line ;-)) )

You will need some version of LAPACK. If you have intel's mkl, this provides
with a nice C api. O/wise there is the freely avail
CLAPACK from netlib. More modern is the LAPACKE- but have no personal
experience w/it.

You can use the DGESV function from lapack - there are variants for
symmetric and possibly positive definite matrices - but for
a system 7x7 it is no worth the trouble ( different data layout is
required - don't define the result of M'*M to be symmetric because
you will get into deeper waters ).

The signature of the function lookslike this (in MKL) :
void dgesv( const MKL_INT* n, const MKL_INT* nrhs, double* a,
const MKL_INT* lda, MKL_INT* ipiv, double* b, const MKL_INT*
ldb,
MKL_INT* info );
MKL_INT ( glorified int for CLAPACK- unless 64 bit build) n = 7;
MKL_INT nrhs = 1;
double * a = &( N(0,0) ); // N = M'*M
... lda = 7;
ipiv = new MKL_INT[7];
double *b = &q[0] ; // q = M'*R
.. ldb = 7;
MKL_INT info;
And ready to make the call.
Good luck,
HTH
P-

-----Original Message-----
From: Andrew Hill
Sent: Wednesday, February 29, 2012 2:24 PM
To: ublas mailing list
Subject: Re: [ublas] Solving simple vector equation *newbie*

Can you provide a more specific example? I know nothing about how to
use ublas. I'm looking for some simple code example. I don't have
any idea how to turn what petros said into code using ublas.

Adding to what petros said:
1) a, b, c are scalars
2) X, Y, Z are are 7-d column vectors
3) entries 3, 4, 5 of 7-d vectors have the most significance

Understanding how to do this, will really set me up for more
significant use of ublas going forward. I really appreciated any help
I can get.

Thanks!

On Wed, Feb 29, 2012 at 10:40 AM, petros <pmamales_at_[hidden]> wrote:
> this is equivalent to solving the system :
> [ X Y Z ] * ( a b c ) T
> for abc.
> if X,Y,Z have length higher than 3 you need to do leas squares
> (apply the regression formula)
> hth
> p-
>
> -----Original Message----- From: Andrew Hill Sent: Wednesday, February 29,
> 2012 11:18 AM To: ublas_at_[hidden] Subject: [ublas] Solving simple
> vector equation *newbie*
> I'm 100% new to ublas. Can you help me get started?
>
> I need to solve for a, b, c in the vector equation:
>
> R = a*X + b*Y + c*Z
>
> where R, X, Y, Z are known column vectors.
>
> How do I do this in ublas? Will you create or point me to a simple
> example of how to do this? Details will really help me here as I
> haven't used ublas before, but I need it for several projects going
> forward.
>
> Thanks!
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]

> Sent to: pmamales_at_[hidden]
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]

> Sent to: andy.bob.hill_at_[hidden]
_______________________________________________
ublas mailing list
ublas_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: pmamales_at_[hidden]