23 #include <gsl/gsl_blas.h>
25 #include <yarp/gsl/Gsl.h>
30 namespace learningmachine {
33 void dchud(
double* r,
int ldr,
int p,
double*
x,
double*
z,
int ldz,
int nz,
34 double*
y,
double* rho,
double* c,
double* s,
35 unsigned char rtrans,
unsigned char ztrans) {
37 double* work = (
double*) 0x0;
38 double* tbuff = (
double*) 0x0;
41 double scale, workscale, rhoscale;
44 work = (
double*) malloc(
p *
sizeof(
double));
45 cblas_dcopy(
p,
x, 1, work, 1);
47 stp = (rtrans == 1) ?
p : 1;
50 for(i = 0, tbuff = r; (int)i <
p; tbuff+=(
p+1), i++) {
52 cblas_drotg(tbuff, work+i, c+i, s+i);
63 cblas_drot(
p-i-1, tbuff+stp, stp, work+i+1, 1, c[i], s[i]);
70 work = (
double*) malloc(nz *
sizeof(
double));
71 cblas_dcopy(nz,
y, 1, work, 1);
73 stp = (ztrans == 1) ? 1 : nz;
74 stp2 = (ztrans == 1) ? ldz : 1;
77 for(i = 0; (int)i <
p; i++) {
78 cblas_drot(nz,
z+i*stp, stp2, work, 1, c[i], s[i]);
82 for(i = 0; (int)i < nz; i++) {
87 scale = work[i] + rho[i];
88 workscale = work[i] / scale;
89 rhoscale = rho[i] / scale;
90 rho[i] = scale * sqrt((workscale)*(workscale) + (rhoscale)*(rhoscale));
100 gsl_matrix* Z, gsl_vector*
y, gsl_vector* rho,
101 unsigned char rtrans,
unsigned char ztrans) {
110 ldr = rtrans ? R->size2 : R->size1;
111 p = rtrans ? R->size1 : R->size2;
114 if(Z != NULL &&
y != NULL && rho != NULL) {
115 ldz = ztrans ? Z->size2 : Z->size1;
116 nz = ztrans ? Z->size1 : Z->size2;
122 dchud(R->data, ldr,
p,
x->data, Zp, ldz, nz, yp, rhop, c->data, s->data, rtrans, ztrans);
125 for(i = 0; i <
p; i++) {
126 for(j = 0; j < i; j++) {
128 gsl_matrix_set(R, j, i, gsl_matrix_get(R, i, j));
130 gsl_matrix_set(R, i, j, gsl_matrix_get(R, j, i));
136 void cholupdate(yarp::sig::Matrix& R,
const yarp::sig::Vector&
x, yarp::sig::Vector& c, yarp::sig::Vector& s,
137 yarp::sig::Matrix& Z,
const yarp::sig::Vector&
y, yarp::sig::Vector& rho,
bool rtrans,
bool ztrans) {
138 yarp::gsl::GslMatrix RGslMat(R), ZGslMat(Z);
139 yarp::gsl::GslVector xGslVec(
x), yGslVec(
y), rhoGslVec(rho), cGslVec(c), sGslVec(s);
141 gsl_matrix* Rgsl = (gsl_matrix*) RGslMat.getGslMatrix();
142 gsl_vector* xgsl = (gsl_vector*) xGslVec.getGslVector();
143 gsl_matrix* Zgsl = (gsl_matrix*) ZGslMat.getGslMatrix();
144 gsl_vector* ygsl = (gsl_vector*) yGslVec.getGslVector();
145 gsl_vector* rhogsl = (gsl_vector*) rhoGslVec.getGslVector();
146 gsl_vector* cgsl = (gsl_vector*) cGslVec.getGslVector();
147 gsl_vector* sgsl = (gsl_vector*) sGslVec.getGslVector();
152 void cholupdate(yarp::sig::Matrix& R,
const yarp::sig::Vector&
x,
bool rtrans) {
153 yarp::sig::Vector c(R.cols());
154 yarp::sig::Vector s(R.cols());
156 yarp::gsl::GslMatrix RGslMat(R);
157 yarp::gsl::GslVector xGslVec(
x), cGslVec(c), sGslVec(s);
159 gsl_matrix* Rgsl = (gsl_matrix*) RGslMat.getGslMatrix();
160 gsl_vector* xgsl = (gsl_vector*) xGslVec.getGslVector();
161 gsl_vector* cgsl = (gsl_vector*) cGslVec.getGslVector();
162 gsl_vector* sgsl = (gsl_vector*) sGslVec.getGslVector();
167 void cholsolve(
const yarp::sig::Matrix& R,
const yarp::sig::Matrix& B, yarp::sig::Matrix& X) {
168 assert(B.rows() == X.rows());
169 assert(B.cols() == X.cols());
170 assert(R.rows() == R.cols());
171 assert(R.cols() == B.cols());
175 for(
int r = 0; r < B.rows(); r++) {
185 yarp::sig::Matrix
cholsolve(
const yarp::sig::Matrix& R,
const yarp::sig::Matrix& B) {
186 yarp::sig::Matrix X(B.rows(), B.cols());
191 void cholsolve(
const yarp::sig::Matrix& R,
const yarp::sig::Vector& b, yarp::sig::Vector&
x) {
194 yarp::gsl::GslMatrix RGslMat(R);
195 yarp::gsl::GslVector bGslVec(b), xGslVec(
x);
197 gsl_matrix* Rgsl = (gsl_matrix*) RGslMat.getGslMatrix();
198 gsl_vector* bgsl = (gsl_vector*) bGslVec.getGslVector();
199 gsl_vector* xgsl = (gsl_vector*) xGslVec.getGslVector();
201 info = gsl_linalg_cholesky_solve(Rgsl, bgsl, xgsl);
203 throw std::runtime_error(gsl_strerror(info));
207 yarp::sig::Vector
cholsolve(
const yarp::sig::Matrix& R,
const yarp::sig::Vector& b) {
208 yarp::sig::Vector
x(b.size());
213 yarp::sig::Matrix
outerprod(
const yarp::sig::Vector& v1,
const yarp::sig::Vector& v2) {
214 yarp::sig::Matrix
out(v1.size(), v2.size());
215 for(
int r = 0; r <
out.rows(); r++) {
216 for(
int c = 0; c <
out.cols(); c++) {
217 out(r, c) = v1(r) * v2(c);
223 yarp::sig::Vector&
addvec(yarp::sig::Vector& v,
double val) {
224 for(
size_t i = 0; i < v.size(); i++) {
230 void trsolve(
const yarp::sig::Matrix&
A,
const yarp::sig::Vector& b, yarp::sig::Vector&
x,
bool transa) {
231 yarp::gsl::GslMatrix AGslMat(
A);
232 yarp::gsl::GslVector bGslVec(b), xGslVec(
x);
234 gsl_matrix* Agsl = (gsl_matrix*) AGslMat.getGslMatrix();
235 gsl_vector* bgsl = (gsl_vector*) bGslVec.getGslVector();
236 gsl_vector* xgsl = (gsl_vector*) xGslVec.getGslVector();
238 gsl_vector_memcpy(xgsl, bgsl);
239 CBLAS_TRANSPOSE trans = transa ? CblasTrans : CblasNoTrans;
240 gsl_blas_dtrsv(CblasUpper, trans, CblasNonUnit, Agsl, xgsl);
243 yarp::sig::Vector
trsolve(
const yarp::sig::Matrix&
A,
const yarp::sig::Vector& b,
bool transa) {
244 yarp::sig::Vector
x(b.size());
249 void fillrandom(yarp::sig::Vector& v, yarp::math::RandScalar& prng) {
251 for(i = 0; i < v.size(); i++) {
256 void fillrandom(yarp::sig::Matrix& M, yarp::math::RandScalar& prng) {
258 for(r = 0; r < M.rows(); r++) {
259 for(c = 0; c < M.cols(); c++) {
260 M(r, c) = prng.get();
265 void fillrandom(yarp::sig::Vector& v, yarp::math::RandnScalar& prng) {
267 for(i = 0; i < v.size(); i++) {
272 void fillrandom(yarp::sig::Matrix& M, yarp::math::RandnScalar& prng) {
274 for(r = 0; r < M.rows(); r++) {
275 for(c = 0; c < M.cols(); c++) {
276 M(r, c) = prng.get();
281 yarp::sig::Vector
random(
int length, yarp::math::RandScalar& prng) {
282 yarp::sig::Vector v(length);
287 yarp::sig::Matrix
random(
int rows,
int columns, yarp::math::RandScalar& prng) {
288 yarp::sig::Matrix M(rows, columns);
293 yarp::sig::Vector
random(
int length, yarp::math::RandnScalar& prng) {
294 yarp::sig::Vector v(length);
299 yarp::sig::Matrix
random(
int rows,
int columns, yarp::math::RandnScalar& prng) {
300 yarp::sig::Matrix M(rows, columns);
305 yarp::sig::Vector&
map(yarp::sig::Vector& v,
double (op)(
double)) {
306 for(
size_t i = 0; i < v.size(); i++) {
312 yarp::sig::Matrix&
map(yarp::sig::Matrix& M,
double (op)(
double)) {
313 for(
int r = 0; r < M.rows(); r++) {
314 for(
int c = 0; c < M.cols(); c++) {
315 M(r, c) = op(M(r, c));
321 yarp::sig::Vector
map(
const yarp::sig::Vector& v,
double (op)(
double)) {
322 yarp::sig::Vector
out(v.size());
323 for(
size_t i = 0; i <
out.size(); i++) {
329 yarp::sig::Matrix
map(
const yarp::sig::Matrix& M,
double (op)(
double)) {
330 yarp::sig::Matrix
out(M.rows(), M.cols());
331 for(
int r = 0; r <
out.rows(); r++) {
332 for(
int c = 0; c <
out.cols(); c++) {
333 out(r, c) = op(M(r, c));
339 yarp::sig::Vector&
cosvec(yarp::sig::Vector& v){
340 return map(v, std::cos);
343 yarp::sig::Vector&
sinvec(yarp::sig::Vector& v){
344 return map(v, std::sin);
347 yarp::sig::Matrix&
cosmat(yarp::sig::Matrix& M) {
348 return map(M, std::cos);
351 yarp::sig::Matrix&
sinmat(yarp::sig::Matrix& M) {
352 return map(M, std::sin);
355 yarp::sig::Vector
cosvec(
const yarp::sig::Vector& v){
356 return map(v, std::cos);
359 yarp::sig::Vector
sinvec(
const yarp::sig::Vector& v){
360 return map(v, std::sin);
363 yarp::sig::Matrix
cosmat(
const yarp::sig::Matrix& M) {
364 return map(M, std::cos);
367 yarp::sig::Matrix
sinmat(
const yarp::sig::Matrix& M) {
368 return map(M, std::sin);
void cholupdate(yarp::sig::Matrix &R, const yarp::sig::Vector &x, yarp::sig::Vector &c, yarp::sig::Vector &s, yarp::sig::Matrix &Z, const yarp::sig::Vector &y, yarp::sig::Vector &rho, bool rtrans=0, bool ztrans=0)
Perform a rank-1 update to a Cholesky factor, while updating additional vectors using the used Given'...
yarp::sig::Vector & cosvec(yarp::sig::Vector &v)
Computes the cosine of a vector element-wise inplace.
yarp::sig::Matrix outerprod(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)
Computes the outer product of two vectors.
void gsl_linalg_cholesky_update(gsl_matrix *R, gsl_vector *x, gsl_vector *c, gsl_vector *s, gsl_matrix *Z=NULL, gsl_vector *y=NULL, gsl_vector *rho=NULL, unsigned char rtrans=0, unsigned char ztrans=0)
void dchud(double *r, int ldr, int p, double *x, double *z, int ldz, int nz, double *y, double *rho, double *c, double *s, unsigned char rtrans=0, unsigned char ztrans=0)
Mathematical helper functions for use in the learningMachine library.
void fillrandom(yarp::sig::Vector &v, yarp::math::RandScalar &prng)
Fills an entire vector using the provided pseudo random number generator.
yarp::sig::Vector & map(yarp::sig::Vector &v, double(op)(double))
Performs a unary operator inplace on each element of a vector.
yarp::sig::Vector random(int length, yarp::math::RandScalar &prng)
Returns a random vector with given dimensionality.
void cholsolve(const yarp::sig::Matrix &R, const yarp::sig::Matrix &B, yarp::sig::Matrix &X)
Solves a system A*x=b for multiple row vectors in B using a precomputed Cholesky factor R.
yarp::sig::Vector & addvec(yarp::sig::Vector &v, double val)
Adds a scalar to a vector inplace.
yarp::sig::Matrix & cosmat(yarp::sig::Matrix &M)
Computes the cosine of a matrix element-wise inplace.
yarp::sig::Matrix & sinmat(yarp::sig::Matrix &M)
Computes the sine of a matrix element-wise inplace.
yarp::sig::Vector & sinvec(yarp::sig::Vector &v)
Computes the sine of a vector element-wise inplace.
void trsolve(const yarp::sig::Matrix &A, const yarp::sig::Vector &b, yarp::sig::Vector &x, bool transa=false)
Solves a triangular linear system Ax=b where A is triangular.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.