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 
58 typedef size_t aa_la_size;
59 
63 struct aa_dvec {
65  double *data;
67 };
68 
72 struct aa_dmat {
75  double *data;
77 };
78 
79 
85 #define AA_DVEC_REF(v,i) ((v)->data[(i) * (v)->inc])
86 
94 #define AA_DMAT_REF(M,i,j) (((M)->data)[(M)->ld*(j) + (i)])
95 
96 
100 typedef void
101 (aa_la_err_fun)( const char *message );
102 
106 AA_API void
107 aa_la_err( const char *message );
108 
112 AA_API void
114 
115 
119 #define AA_VEC_ARGS(X) (X->data), ((int)(X->inc))
120 
124 #define AA_MAT_ARGS(X) (X->data), ((int)(X->ld))
125 
126 /* Construction */
127 
135 static inline struct aa_dvec
137 {
138  struct aa_dvec vec = {len, data, inc};
139  return vec;
140 }
141 
150 static inline void
152 {
153  *vec = AA_DVEC_INIT(len,data,inc);
154 }
155 
164 AA_API void
166 
167 
177 AA_API void
178 aa_dmat_view( struct aa_dmat *mat, aa_la_size rows, aa_la_size cols, double *data, aa_la_size ld );
179 
183 AA_API void
185  const struct aa_dmat *src,
186  aa_la_size row_start, aa_la_size col_start,
187  aa_la_size rows, aa_la_size cols );
188 
192 AA_API void
193 aa_dvec_slice( const struct aa_dvec *src,
194  aa_la_size start,
195  aa_la_size stop,
196  aa_la_size step,
197  struct aa_dvec *dst );
198 
199 
203 AA_API void
204 aa_dmat_block( const struct aa_dmat *src,
205  aa_la_size row_start, aa_la_size col_start,
206  aa_la_size row_end, aa_la_size col_end,
207  struct aa_dmat *dst );
208 
212 AA_API void
213 aa_dmat_row_vec( const struct aa_dmat *src, aa_la_size row, struct aa_dvec *dst );
214 
218 AA_API void
219 aa_dmat_col_vec( const struct aa_dmat *src, aa_la_size col, struct aa_dvec *dst );
220 
224 AA_API void
225 aa_dmat_diag_vec( const struct aa_dmat *src, struct aa_dvec *dst );
226 
227 
236 static inline struct aa_dmat
238 {
239  struct aa_dmat mat;
240  mat.rows = rows;
241  mat.cols = cols;
242  mat.data = data;
243  mat.ld = ld;
244  return mat;
245 }
246 
256 static inline void
258 {
259  *mat = AA_DMAT_INIT(rows,cols,data,ld);
260 }
261 
265 #define AA_MAT_DIAG(VEC,MAT) \
266  aa_dvec_view((VEC), (MAT)->cols, (MAT)->data, 1+(MAT)->ld);
267 
273 AA_API struct aa_dvec *
275 
276 
282 AA_API struct aa_dvec *
283 aa_dvec_dup( struct aa_mem_region *reg, const struct aa_dvec *src);
284 
290 AA_API struct aa_dvec *
291 aa_dvec_mdup(const struct aa_dvec *src);
292 
298 AA_API struct aa_dmat *
299 aa_dmat_dup( struct aa_mem_region *reg, const struct aa_dmat *src);
300 
306 AA_API struct aa_dmat *
307 aa_dmat_mdup(const struct aa_dmat *src);
308 
314 AA_API struct aa_dmat *
316 
323 AA_API struct aa_dvec *
325 
332 AA_API struct aa_dmat *
334 
338 AA_API void
339 aa_dvec_zero( struct aa_dvec *vec );
340 
344 AA_API void
345 aa_dvec_set( struct aa_dvec *vec, double alpha );
346 
354 AA_API void
355 aa_dmat_set( struct aa_dmat *A, double alpha, double beta );
356 
360 AA_API void
361 aa_dmat_zero( struct aa_dmat *mat );
362 
363 /* Level 1 BLAS */
364 
370 AA_API void
371 aa_dvec_swap( struct aa_dvec *x, struct aa_dvec *y );
372 
378 AA_API void
379 aa_dvec_scal( double alpha, struct aa_dvec *x );
380 
386 AA_API void
387 aa_dvec_inc( double alpha, struct aa_dvec *x );
388 
394 AA_API void
395 aa_dvec_copy( const struct aa_dvec *x, struct aa_dvec *y );
396 
397 
403 AA_API void
404 aa_dvec_axpy( double a, const struct aa_dvec *x, struct aa_dvec *y );
405 
411 AA_API double
412 aa_dvec_dot( const struct aa_dvec *x, struct aa_dvec *y );
413 
419 AA_API double
420 aa_dvec_nrm2( const struct aa_dvec *x );
421 
422 /* Level 2 BLAS */
423 
429 AA_API void
430 aa_dmat_gemv( AA_CBLAS_TRANSPOSE trans,
431  double alpha, const struct aa_dmat *A,
432  const struct aa_dvec *x,
433  double beta, struct aa_dvec *y );
434 
435 
436 
437 /* Level 3 BLAS */
438 
444 AA_API void
445 aa_dmat_gemm( AA_CBLAS_TRANSPOSE transA, AA_CBLAS_TRANSPOSE transB,
446  double alpha, const struct aa_dmat *A,
447  const struct aa_dmat *B,
448  double beta, struct aa_dmat *C );
449 
450 
451 
452 
453 /* LAPACK */
454 
476 AA_API void
477 aa_dmat_lacpy( const char uplo[1],
478  const struct aa_dmat *A,
479  struct aa_dmat *B );
480 
481 
482 
483 /* Matrix/Vector Functions */
484 
488 AA_API double
489 aa_dvec_ssd( const struct aa_dvec *x, const struct aa_dvec *y);
490 
491 
495 AA_API void
496 aa_dmat_axpy( double alpha, const struct aa_dmat *X, struct aa_dmat *Y);
497 
501 AA_API double
502 aa_dmat_ssd( const struct aa_dmat *x, const struct aa_dmat *y);
503 
507 AA_API void
508 aa_dmat_scal( struct aa_dmat *A, double alpha );
509 
513 AA_API void
514 aa_dmat_inc( struct aa_dmat *A, double alpha );
515 
519 AA_API double
520 aa_dmat_nrm2( const struct aa_dmat *x );
521 
522 
526 AA_API void
527 aa_dmat_trans( const struct aa_dmat *A, struct aa_dmat *At);
528 
529 
533 AA_API int
534 aa_dmat_inv( struct aa_dmat *A);
535 
536 
543 AA_API int
544 aa_dmat_pinv( const struct aa_dmat *A, double tol, struct aa_dmat *As);
545 
549 AA_API int
550 aa_dmat_dpinv( const struct aa_dmat *A, double k, struct aa_dmat *As);
551 
555 AA_API int
556 aa_dmat_dzdpinv( const struct aa_dmat *A, double s_min, struct aa_dmat *As);
557 
561 AA_API void
562 aa_dmat_copy( const struct aa_dmat *A, struct aa_dmat *B);
563 
564 
565 #endif /* AMINO_MAT_H */
#define AA_API
calling and name mangling convention for functions
Definition: amino.h:95
AA_API void aa_dvec_set(struct aa_dvec *vec, double alpha)
Fill a vector.
AA_API void aa_dmat_view_block(struct aa_dmat *dst, const struct aa_dmat *src, aa_la_size row_start, aa_la_size col_start, aa_la_size rows, aa_la_size cols)
View a block of a matrix.
AA_API void aa_dvec_view(struct aa_dvec *vec, aa_la_size len, double *data, aa_la_size inc)
Fill in a vector descriptor.
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_zero(struct aa_dmat *mat)
Zero a matrix.
static struct aa_dmat AA_DMAT_INIT(aa_la_size rows, aa_la_size cols, double *data, aa_la_size ld)
Fill in a matrix descriptor.
Definition: mat.h:237
AA_API void aa_dmat_scal(struct aa_dmat *A, double alpha)
Scale the matrix A by alpha.
AA_API void aa_dvec_inc(double alpha, struct aa_dvec *x)
Increment x by alpha.
static struct aa_dvec AA_DVEC_INIT(aa_la_size len, double *data, aa_la_size inc)
Fill in a vector descriptor.
Definition: mat.h:136
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_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_dvec * aa_dvec_alloc(struct aa_mem_region *reg, aa_la_size len)
Region-allocate a vector.
void() aa_la_err_fun(const char *message)
Callback type for linear algebra errors.
Definition: mat.h:101
AA_API struct aa_dmat * aa_dmat_malloc(aa_la_size rows, aa_la_size cols)
Heap-allocate a matrix.
AA_API struct aa_dvec * aa_dvec_malloc(aa_la_size len)
Heap-allocate a vector.
AA_API void aa_dmat_gemv(AA_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_API void aa_dmat_inc(struct aa_dmat *A, double alpha)
Increment the matrix A by alpha.
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 void aa_la_err(const char *message)
Default handler function for linear algebra errors.
AA_API void aa_dvec_scal(double alpha, struct aa_dvec *x)
Scale x by alpha.
AA_API void aa_dmat_set(struct aa_dmat *A, double alpha, double beta)
Fill a matrix diagonal and off-diagonal elements.
AA_API double aa_dvec_nrm2(const struct aa_dvec *x)
Euclidean Norm.
AA_API void aa_dmat_view(struct aa_dmat *mat, aa_la_size rows, aa_la_size cols, double *data, aa_la_size ld)
Fill in a matrix descriptor.
static void AA_DMAT_VIEW(struct aa_dmat *mat, aa_la_size rows, aa_la_size cols, double *data, aa_la_size ld)
Fill in a matrix descriptor.
Definition: mat.h:257
AA_API void aa_dmat_block(const struct aa_dmat *src, aa_la_size row_start, aa_la_size col_start, aa_la_size row_end, aa_la_size col_end, struct aa_dmat *dst)
View a block of a matrix.
AA_API void aa_dmat_gemm(AA_CBLAS_TRANSPOSE transA, AA_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 double aa_dvec_dot(const struct aa_dvec *x, struct aa_dvec *y)
Dot product.
AA_API void aa_dmat_copy(const struct aa_dmat *A, struct aa_dmat *B)
Copy a matrix.
AA_API void aa_dmat_col_vec(const struct aa_dmat *src, aa_la_size col, struct aa_dvec *dst)
View a column of a matrix as a vector.
AA_API void aa_dvec_copy(const struct aa_dvec *x, struct aa_dvec *y)
Copy x to y.
AA_API void aa_dmat_trans(const struct aa_dmat *A, struct aa_dmat *At)
Matrix transpose.
AA_API double aa_dmat_ssd(const struct aa_dmat *x, const struct aa_dmat *y)
sum-square-differences of two matrices
AA_API struct aa_dmat * aa_dmat_alloc(struct aa_mem_region *reg, aa_la_size rows, aa_la_size cols)
Region-allocate a matrix.
AA_API void aa_dmat_row_vec(const struct aa_dmat *src, aa_la_size row, struct aa_dvec *dst)
View a row of a matrix as a vector.
static void AA_DVEC_VIEW(struct aa_dvec *vec, aa_la_size len, double *data, aa_la_size inc)
Fill in a vector descriptor.
Definition: mat.h:151
AA_API int aa_dmat_inv(struct aa_dmat *A)
Matrix inverse, in-place.
AA_API void aa_dvec_zero(struct aa_dvec *vec)
Zero a vector.
AA_API double aa_dvec_ssd(const struct aa_dvec *x, const struct aa_dvec *y)
sum-square-differences of two vectors
AA_API int aa_dmat_dpinv(const struct aa_dmat *A, double k, struct aa_dmat *As)
Damped pseudo-inverse.
size_t aa_la_size
Type for sizes of vectors and matrices.
Definition: mat.h:58
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 struct aa_dmat * aa_dmat_mdup(const struct aa_dmat *src)
Duplicate matrix in the heap.
AA_API void aa_la_set_err(aa_la_err_fun *fun)
Set the handler function for linear algebra errors.
AA_API int aa_dmat_pinv(const struct aa_dmat *A, double tol, struct aa_dmat *As)
Pseudo-inverse.
AA_API struct aa_dvec * aa_dvec_mdup(const struct aa_dvec *src)
Duplicate vector in the heap.
AA_API void aa_dmat_axpy(double alpha, const struct aa_dmat *X, struct aa_dmat *Y)
Y += alpha * X.
AA_API double aa_dmat_nrm2(const struct aa_dmat *x)
Euclidean Norm.
AA_API void aa_dvec_slice(const struct aa_dvec *src, aa_la_size start, aa_la_size stop, aa_la_size step, struct aa_dvec *dst)
View a slice of 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_swap(struct aa_dvec *x, struct aa_dvec *y)
Swap x and y.
Descriptor for a block matrix.
Definition: mat.h:72
aa_la_size rows
number of rows in matrix
Definition: mat.h:73
aa_la_size cols
number of columns
Definition: mat.h:74
aa_la_size ld
Leading dimension of matrix.
Definition: mat.h:76
double * data
Pointer to matrix data.
Definition: mat.h:75
Descriptor for a vector.
Definition: mat.h:63
aa_la_size len
Number of elements in vector.
Definition: mat.h:64
aa_la_size inc
Increment between successive vector elements.
Definition: mat.h:66
double * data
Pointer to data.
Definition: mat.h:65
Data Structure for Region-Based memory allocation.
Definition: mem.h:199