amino  1.0-beta2
Lightweight Robot Utility Library
mat.h
Go to the documentation of this file.
1 /* -*- mode: C; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2018-2019, Colorado School of Mines
5  * All rights reserved.
6  *
7  * Author(s): Neil T. Dantam <ndantam@miens.edu>
8  * Georgia Tech Humanoid Robotics Lab
9  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
10  *
11  *
12  * This file is provided under the following "BSD-style" License:
13  *
14  *
15  * Redistribution and use in source and binary forms, with or
16  * without modification, are permitted provided that the following
17  * conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42 
43 #ifndef AMINO_MAT_H
44 #define AMINO_MAT_H
45 
46 #include <cblas.h>
47 
55 typedef size_t aa_la_size;
56 
60 struct aa_dvec {
61  aa_la_size len;
62  double *data;
63  aa_la_size inc;
64 };
65 
69 struct aa_dmat {
70  aa_la_size rows;
71  aa_la_size cols;
72  double *data;
73  aa_la_size ld;
74 };
75 
76 
77 #define AA_DVEC_REF(v,i) ((v)->data[(i) * (v)->inc])
78 
86 #define AA_DMAT_REF(M,i,j) (((M)->data)[(M)->ld*(j) + (i)])
87 
88 
89 
90 typedef void
91 (aa_la_err_fun)( const char *message );
92 
93 AA_API void
94 aa_la_err( const char *message );
95 
96 AA_API void
97 aa_la_set_err( aa_la_err_fun *fun );
98 
99 
103 #define AA_VEC_ARGS(X) (X->data), ((int)(X->inc))
104 
108 #define AA_MAT_ARGS(X) (X->data), ((int)(X->ld))
109 
110 /* Construction */
111 
119 static inline struct aa_dvec
120 AA_DVEC_INIT( size_t len, double *data, size_t inc )
121 {
122  struct aa_dvec vec;
123  vec.len = len;
124  vec.data = data;
125  vec.inc = inc;
126  return vec;
127 }
128 
137 AA_API void
138 aa_dvec_view( struct aa_dvec *vec, size_t len, double *data, size_t inc );
139 
149 AA_API void
150 aa_dmat_view( struct aa_dmat *mat, size_t rows, size_t cols, double *data, size_t ld );
151 
155 AA_API void
156 aa_dmat_view_block( struct aa_dmat *dst,
157  const struct aa_dmat *src,
158  size_t row_start, size_t col_start,
159  size_t rows, size_t cols );
160 
164 AA_API void
165 aa_dvec_slice( const struct aa_dvec *src,
166  size_t start,
167  size_t stop,
168  size_t step,
169  struct aa_dvec *dst );
170 
171 
175 AA_API void
176 aa_dmat_block( const struct aa_dmat *src,
177  size_t row_start, size_t col_start,
178  size_t row_end, size_t col_end,
179  struct aa_dmat *dst );
180 
184 AA_API void
185 aa_dmat_row_vec( const struct aa_dmat *src, size_t row, struct aa_dvec *dst );
186 
190 AA_API void
191 aa_dmat_col_vec( const struct aa_dmat *src, size_t col, struct aa_dvec *dst );
192 
196 AA_API void
197 aa_dmat_diag_vec( const struct aa_dmat *src, struct aa_dvec *dst );
198 
199 
208 static inline struct aa_dmat
209 AA_DMAT_INIT( size_t rows, size_t cols, double *data, size_t ld )
210 {
211  struct aa_dmat mat;
212  mat.rows = rows;
213  mat.cols = cols;
214  mat.data = data;
215  mat.ld = ld;
216  return mat;
217 }
218 
219 
220 #define AA_MAT_DIAG(VEC,MAT) \
221  aa_dvec_view((VEC), (MAT)->cols, (MAT)->data, 1+(MAT)->ld);
222 
228 AA_API struct aa_dvec *
229 aa_dvec_alloc( struct aa_mem_region *reg, size_t len );
230 
231 
237 AA_API struct aa_dvec *
238 aa_dvec_dup( struct aa_mem_region *reg, const struct aa_dvec *src);
239 
245 AA_API struct aa_dmat *
246 aa_dmat_dup( struct aa_mem_region *reg, const struct aa_dmat *src);
247 
253 AA_API struct aa_dmat *
254 aa_dmat_alloc( struct aa_mem_region *reg, size_t rows, size_t cols );
255 
262 AA_API struct aa_dvec *
263 aa_dvec_malloc( size_t len );
264 
271 AA_API struct aa_dmat *
272 aa_dmat_malloc( size_t rows, size_t cols );
273 
277 AA_API void
278 aa_dvec_zero( struct aa_dvec *vec );
279 
283 AA_API void
284 aa_dvec_set( struct aa_dvec *vec, double alpha );
285 
293 AA_API void
294 aa_dmat_set( struct aa_dmat *A, double alpha, double beta );
295 
299 AA_API void
300 aa_dmat_zero( struct aa_dmat *mat );
301 
302 /* Level 1 BLAS */
303 
309 AA_API void
310 aa_dvec_swap( struct aa_dvec *x, struct aa_dvec *y );
311 
317 AA_API void
318 aa_dvec_scal( double alpha, struct aa_dvec *x );
319 
325 AA_API void
326 aa_dvec_inc( double alpha, struct aa_dvec *x );
327 
333 AA_API void
334 aa_dvec_copy( const struct aa_dvec *x, struct aa_dvec *y );
335 
336 
342 AA_API void
343 aa_dvec_axpy( double a, const struct aa_dvec *x, struct aa_dvec *y );
344 
350 AA_API double
351 aa_dvec_dot( const struct aa_dvec *x, struct aa_dvec *y );
352 
358 AA_API double
359 aa_dvec_nrm2( const struct aa_dvec *x );
360 
361 /* Level 2 BLAS */
362 
368 AA_API void
369 aa_dmat_gemv( CBLAS_TRANSPOSE trans,
370  double alpha, const struct aa_dmat *A,
371  const struct aa_dvec *x,
372  double beta, struct aa_dvec *y );
373 
374 
375 
376 /* Level 3 BLAS */
377 
383 AA_API void
384 aa_dmat_gemm( CBLAS_TRANSPOSE transA, CBLAS_TRANSPOSE transB,
385  double alpha, const struct aa_dmat *A,
386  const struct aa_dmat *B,
387  double beta, struct aa_dmat *C );
388 
389 
390 
391 
392 /* LAPACK */
393 
415 AA_API void
416 aa_dmat_lacpy( const char uplo[1],
417  const struct aa_dmat *A,
418  struct aa_dmat *B );
419 
420 
421 
422 /* Matrix/Vector Functions */
423 
427 AA_API double
428 aa_dvec_ssd( const struct aa_dvec *x, const struct aa_dvec *y);
429 
430 
434 AA_API void
435 aa_dmat_axpy( double alpha, const struct aa_dmat *X, struct aa_dmat *Y);
436 
440 AA_API double
441 aa_dmat_ssd( const struct aa_dmat *x, const struct aa_dmat *y);
442 
446 AA_API void
447 aa_dmat_scal( struct aa_dmat *A, double alpha );
448 
452 AA_API void
453 aa_dmat_inc( struct aa_dmat *A, double alpha );
454 
458 AA_API double
459 aa_dmat_nrm2( const struct aa_dmat *x );
460 
461 
465 AA_API void
466 aa_dmat_trans( const struct aa_dmat *A, struct aa_dmat *At);
467 
468 
472 AA_API int
473 aa_dmat_inv( struct aa_dmat *A);
474 
475 
482 AA_API int
483 aa_dmat_pinv( const struct aa_dmat *A, double tol, struct aa_dmat *As);
484 
488 AA_API int
489 aa_dmat_dpinv( const struct aa_dmat *A, double k, struct aa_dmat *As);
490 
494 AA_API int
495 aa_dmat_dzdpinv( const struct aa_dmat *A, double s_min, struct aa_dmat *As);
496 
500 AA_API void
501 aa_dmat_copy( const struct aa_dmat *A, struct aa_dmat *B);
502 
503 
504 #endif /* AMINO_MAT_H */
AA_API void aa_dvec_copy(const struct aa_dvec *x, struct aa_dvec *y)
Copy x to y.
aa_la_size inc
Increment between successive vector elements.
Definition: mat.h:63
AA_API int aa_dmat_dzdpinv(const struct aa_dmat *A, double s_min, struct aa_dmat *As)
Dead-zone damped pseudo-inverse.
AA_API double aa_dvec_dot(const struct aa_dvec *x, struct aa_dvec *y)
Dot product.
AA_API void aa_dvec_swap(struct aa_dvec *x, struct aa_dvec *y)
Swap x and y.
AA_API int aa_dmat_inv(struct aa_dmat *A)
Matrix inverse, in-place.
static struct aa_dvec AA_DVEC_INIT(size_t len, double *data, size_t inc)
Fill in a vector descriptor.
Definition: mat.h:120
AA_API void aa_dmat_scal(struct aa_dmat *A, double alpha)
Scale the matrix A by alpha.
AA_API void aa_dmat_zero(struct aa_dmat *mat)
Zero a matrix.
aa_la_size len
Number of elements in vector.
Definition: mat.h:61
aa_la_size cols
number of columns
Definition: mat.h:71
static struct aa_dmat AA_DMAT_INIT(size_t rows, size_t cols, double *data, size_t ld)
Fill in a matrix descriptor.
Definition: mat.h:209
AA_API double aa_dvec_nrm2(const struct aa_dvec *x)
Euclidean Norm.
AA_API struct aa_dmat * aa_dmat_dup(struct aa_mem_region *reg, const struct aa_dmat *src)
Duplicate matrix out of region.
AA_API struct aa_dmat * aa_dmat_alloc(struct aa_mem_region *reg, size_t rows, size_t cols)
Region-allocate a matrix.
AA_API void aa_dvec_scal(double alpha, struct aa_dvec *x)
Scale x by alpha.
AA_API double aa_dmat_nrm2(const struct aa_dmat *x)
Euclidean Norm.
AA_API void aa_dmat_trans(const struct aa_dmat *A, struct aa_dmat *At)
Matrix transpose.
AA_API void aa_dmat_row_vec(const struct aa_dmat *src, size_t row, struct aa_dvec *dst)
View a row of a matrix as a vector.
Data Structure for Region-Based memory allocation.
Definition: mem.h:198
AA_API void aa_dmat_block(const struct aa_dmat *src, size_t row_start, size_t col_start, size_t row_end, size_t col_end, struct aa_dmat *dst)
View a block of a matrix.
AA_API void aa_dvec_set(struct aa_dvec *vec, double alpha)
Fill a vector.
AA_API void aa_dmat_lacpy(const char uplo[1], const struct aa_dmat *A, struct aa_dmat *B)
Copies all or part of a two-dimensional matrix A to another matrix B.
AA_API struct aa_dmat * aa_dmat_malloc(size_t rows, size_t cols)
Heap-allocate a matrix.
AA_API void aa_dmat_view(struct aa_dmat *mat, size_t rows, size_t cols, double *data, size_t ld)
Fill in a matrix descriptor.
Descriptor for a vector.
Definition: mat.h:60
AA_API int aa_dmat_dpinv(const struct aa_dmat *A, double k, struct aa_dmat *As)
Damped pseudo-inverse.
AA_API void aa_dmat_col_vec(const struct aa_dmat *src, size_t col, struct aa_dvec *dst)
View a column of a matrix as a vector.
AA_API void aa_dmat_gemv(CBLAS_TRANSPOSE trans, double alpha, const struct aa_dmat *A, const struct aa_dvec *x, double beta, struct aa_dvec *y)
General Matrix-Vector multiply.
aa_la_size ld
Leading dimension of matrix.
Definition: mat.h:73
AA_API struct aa_dvec * aa_dvec_alloc(struct aa_mem_region *reg, size_t len)
Region-allocate a vector.
AA_API void aa_dvec_axpy(double a, const struct aa_dvec *x, struct aa_dvec *y)
Alpha x plus y.
AA_API void aa_dvec_view(struct aa_dvec *vec, size_t len, double *data, size_t inc)
Fill in a vector descriptor.
#define AA_API
calling and name mangling convention for functions
Definition: amino.h:95
AA_API void aa_dmat_diag_vec(const struct aa_dmat *src, struct aa_dvec *dst)
View the diagonal of a matrix as a vector.
aa_la_size rows
number of rows in matrix
Definition: mat.h:70
AA_API void aa_dmat_axpy(double alpha, const struct aa_dmat *X, struct aa_dmat *Y)
Y += alpha * X.
AA_API double aa_dvec_ssd(const struct aa_dvec *x, const struct aa_dvec *y)
sum-square-differences of two vectors
AA_API void aa_dmat_copy(const struct aa_dmat *A, struct aa_dmat *B)
Copy a matrix.
AA_API void aa_dmat_inc(struct aa_dmat *A, double alpha)
Increment the matrix A by alpha.
AA_API void aa_dvec_inc(double alpha, struct aa_dvec *x)
Increment x by alpha.
double * data
Pointer to matrix data.
Definition: mat.h:72
Descriptor for a block matrix.
Definition: mat.h:69
double * data
Pointer to data.
Definition: mat.h:62
AA_API void aa_dvec_slice(const struct aa_dvec *src, size_t start, size_t stop, size_t step, struct aa_dvec *dst)
View a slice of a vector.
AA_API double aa_dmat_ssd(const struct aa_dmat *x, const struct aa_dmat *y)
sum-square-differences of two matrices
AA_API void aa_dvec_zero(struct aa_dvec *vec)
Zero a vector.
AA_API struct aa_dvec * aa_dvec_dup(struct aa_mem_region *reg, const struct aa_dvec *src)
Duplicate vector out of region.
AA_API void aa_dmat_set(struct aa_dmat *A, double alpha, double beta)
Fill a matrix diagonal and off-diagonal elements.
AA_API void aa_dmat_view_block(struct aa_dmat *dst, const struct aa_dmat *src, size_t row_start, size_t col_start, size_t rows, size_t cols)
View a block of a matrix.
AA_API struct aa_dvec * aa_dvec_malloc(size_t len)
Heap-allocate a vector.
AA_API void aa_dmat_gemm(CBLAS_TRANSPOSE transA, CBLAS_TRANSPOSE transB, double alpha, const struct aa_dmat *A, const struct aa_dmat *B, double beta, struct aa_dmat *C)
General Matrix-Matrix multiply.
AA_API int aa_dmat_pinv(const struct aa_dmat *A, double tol, struct aa_dmat *As)
Pseudo-inverse.