iCub-main
Math.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #ifndef LM_MATH__
20 #define LM_MATH__
21 
22 #include <gsl/gsl_linalg.h>
23 
24 #include <yarp/sig/Matrix.h>
25 #include <yarp/sig/Vector.h>
26 #include <yarp/math/RandScalar.h>
27 #include <yarp/math/RandnScalar.h>
28 
29 
30 namespace iCub {
31 namespace learningmachine {
32 namespace math {
33 
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 /*
45  * Rank-1 update of a Cholesky factor
46  *
47  * Input:
48  * r: upper triangular cholesky factor (ldr x p, typically ldr == p)
49  * x: update vector
50  * z: set of nz column vectors to be updated (ldz x nz, typically ldz == p)
51  * y: nz dimensional vector
52  * rho: nz dimensional vector of norms of residuals
53  * Output:
54  * r: updated cholesky factor
55  * z: updated column vectors
56  * rho: updated residual norm
57  * c: cosines of performed givens rotations
58  * s: sines of performed givens rotations
59  *
60  *
61  * Note: Follows LINPACKs' dchud, but with the following differences
62  * - accepts transposed Cholesky factor R (non-transposed is faster)
63  * - uses CBLAS' drot and drotg for Given's rotations (as in Mattias Seeger's
64  * implementation)
65  * - accepts transposed Z (non-transposed is faster)
66  * - optional: enforces positive elements on the diagonal of R (as in Mattias
67  * Seeger's implementation, uncomment to enable)
68  *
69  * Please see the LINPACK User Guide for more detailed information on the
70  * update routine and a less terse description of the parameters.
71  */
72 void dchud(double* r, int ldr, int p, double* x, double* z, int ldz, int nz,
73  double* y, double* rho, double* c, double* s,
74  unsigned char rtrans = 0, unsigned char ztrans = 0);
75 
76 /*
77  * GSL type wrapper function for C implementation of dchud.
78  */
79 void gsl_linalg_cholesky_update(gsl_matrix* R, gsl_vector* x, gsl_vector* c, gsl_vector* s,
80  gsl_matrix* Z = NULL, gsl_vector* y = NULL, gsl_vector* rho = NULL,
81  unsigned char rtrans = 0, unsigned char ztrans = 0);
82 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
83 
100 void cholupdate(yarp::sig::Matrix& R, const yarp::sig::Vector& x, yarp::sig::Vector& c, yarp::sig::Vector& s,
101  yarp::sig::Matrix& Z, const yarp::sig::Vector& y, yarp::sig::Vector& rho, bool rtrans = 0, bool ztrans = 0);
102 
112 void cholupdate(yarp::sig::Matrix& R, const yarp::sig::Vector& x, bool rtrans = 0);
113 
122 void cholsolve(const yarp::sig::Matrix& R, const yarp::sig::Matrix& B, yarp::sig::Matrix& X);
123 
132 yarp::sig::Matrix cholsolve(const yarp::sig::Matrix& R, const yarp::sig::Matrix& B);
133 
141 void cholsolve(const yarp::sig::Matrix& R, const yarp::sig::Vector& b, yarp::sig::Vector& x);
142 
150 yarp::sig::Vector cholsolve(const yarp::sig::Matrix& R, const yarp::sig::Vector& b);
151 
159 yarp::sig::Matrix outerprod(const yarp::sig::Vector& v1, const yarp::sig::Vector& v2);
160 
168 yarp::sig::Vector& addvec(yarp::sig::Vector& v, double val);
169 
178 void trsolve(const yarp::sig::Matrix& A, const yarp::sig::Vector& b, yarp::sig::Vector& x, bool transa = false);
179 
180 
189 yarp::sig::Vector trsolve(const yarp::sig::Matrix& A, const yarp::sig::Vector& b, bool transa = false);
190 
197 void fillrandom(yarp::sig::Vector& v, yarp::math::RandScalar& prng);
198 
205 void fillrandom(yarp::sig::Matrix& M, yarp::math::RandScalar& prng);
206 
213 void fillrandom(yarp::sig::Vector& v, yarp::math::RandnScalar& prng);
214 
221 void fillrandom(yarp::sig::Matrix& M, yarp::math::RandnScalar& prng);
222 
230 yarp::sig::Vector random(int length, yarp::math::RandScalar& prng);
231 
240 yarp::sig::Matrix random(int rows, int columns, yarp::math::RandScalar& prng);
241 
249 // why do RandScalar and RandnScalar not share a common base class?
250 yarp::sig::Vector random(int length, yarp::math::RandnScalar& prng);
251 
260 yarp::sig::Matrix random(int rows, int columns, yarp::math::RandnScalar& prng);
261 
269 yarp::sig::Vector& map(yarp::sig::Vector& v, double (op)(double));
270 
278 yarp::sig::Matrix& map(yarp::sig::Matrix& M, double (op)(double));
279 
287 yarp::sig::Vector map(const yarp::sig::Vector& v, double (op)(double));
288 
296 yarp::sig::Matrix map(const yarp::sig::Matrix& M, double (op)(double));
297 
305 yarp::sig::Matrix& cosmat(yarp::sig::Matrix& M);
306 
314 yarp::sig::Matrix& sinmat(yarp::sig::Matrix& M);
315 
323 yarp::sig::Vector& cosvec(yarp::sig::Vector& v);
324 
332 yarp::sig::Vector& sinvec(yarp::sig::Vector& v);
333 
341 yarp::sig::Matrix cosmat(const yarp::sig::Matrix& M);
342 
350 yarp::sig::Matrix sinmat(const yarp::sig::Matrix& M);
351 
359 yarp::sig::Vector cosvec(const yarp::sig::Vector& v);
360 
368 yarp::sig::Vector sinvec(const yarp::sig::Vector& v);
369 
370 } // math
371 } // learningmachine
372 } // iCub
373 
374 #endif
static int v
Definition: iCub_Sim.cpp:42
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'...
Definition: Math.cpp:136
yarp::sig::Vector & cosvec(yarp::sig::Vector &v)
Computes the cosine of a vector element-wise inplace.
Definition: Math.cpp:339
yarp::sig::Matrix outerprod(const yarp::sig::Vector &v1, const yarp::sig::Vector &v2)
Computes the outer product of two vectors.
Definition: Math.cpp:213
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)
Definition: Math.cpp:99
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.
Definition: Math.cpp:33
void fillrandom(yarp::sig::Vector &v, yarp::math::RandScalar &prng)
Fills an entire vector using the provided pseudo random number generator.
Definition: Math.cpp:249
yarp::sig::Vector & map(yarp::sig::Vector &v, double(op)(double))
Performs a unary operator inplace on each element of a vector.
Definition: Math.cpp:305
yarp::sig::Vector random(int length, yarp::math::RandScalar &prng)
Returns a random vector with given dimensionality.
Definition: Math.cpp:281
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.
Definition: Math.cpp:167
yarp::sig::Vector & addvec(yarp::sig::Vector &v, double val)
Adds a scalar to a vector inplace.
Definition: Math.cpp:223
yarp::sig::Matrix & cosmat(yarp::sig::Matrix &M)
Computes the cosine of a matrix element-wise inplace.
Definition: Math.cpp:347
yarp::sig::Matrix & sinmat(yarp::sig::Matrix &M)
Computes the sine of a matrix element-wise inplace.
Definition: Math.cpp:351
yarp::sig::Vector & sinvec(yarp::sig::Vector &v)
Computes the sine of a vector element-wise inplace.
Definition: Math.cpp:343
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.
Definition: Math.cpp:230
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
A
Definition: sine.m:16