matd.h
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #ifndef _MATD_H
34 #define _MATD_H
35 
36 #include <assert.h>
37 #include <stddef.h>
38 #include <string.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
51 typedef struct
52 {
53  unsigned int nrows, ncols;
54  double data[];
55 // double *data;
56 } matd_t;
57 
58 #define MATD_ALLOC(name, nrows, ncols) double name ## _storage [nrows*ncols]; matd_t name = { .nrows = nrows, .ncols = ncols, .data = &name ## _storage };
59 
65 #define MATD_EPS 1e-8
66 
71 #define MATD_EL(m, row, col) (m)->data[((row)*(m)->ncols + (col))]
72 
79 matd_t *matd_create(int rows, int cols);
80 
88 matd_t *matd_create_data(int rows, int cols, const double *data);
89 
97 matd_t *matd_create_dataf(int rows, int cols, const float *data);
98 
105 matd_t *matd_identity(int dim);
106 
116 matd_t *matd_create_scalar(double v);
117 
122 double matd_get(const matd_t *m, int row, int col);
123 
128 void matd_put(matd_t *m, int row, int col, double value);
129 
134 double matd_get_scalar(const matd_t *m);
135 
140 void matd_put_scalar(matd_t *m, double value);
141 
146 matd_t *matd_copy(const matd_t *m);
147 
156 matd_t *matd_select(const matd_t *a, int r0, int r1, int c0, int c1);
157 
163 void matd_print(const matd_t *m, const char *fmt);
164 
170 void matd_print_transpose(const matd_t *m, const char *fmt);
171 
178 matd_t *matd_add(const matd_t *a, const matd_t *b);
179 
185 void matd_add_inplace(matd_t *a, const matd_t *b);
186 
193 matd_t *matd_subtract(const matd_t *a, const matd_t *b);
194 
200 void matd_subtract_inplace(matd_t *a, const matd_t *b);
201 
207 matd_t *matd_scale(const matd_t *a, double s);
208 
213 void matd_scale_inplace(matd_t *a, double s);
214 
222 matd_t *matd_multiply(const matd_t *a, const matd_t *b);
223 
228 matd_t *matd_transpose(const matd_t *a);
229 
233 double matd_det(const matd_t *a);
234 
245 matd_t *matd_inverse(const matd_t *a);
246 
247 static inline void matd_set_data(matd_t *m, const double *data)
248 {
249  memcpy(m->data, data, m->nrows * m->ncols * sizeof(double));
250 }
251 
256 static inline int matd_is_scalar(const matd_t *a)
257 {
258  assert(a != NULL);
259  return a->ncols == 0 || a->nrows == 0;
260 }
261 
267 static inline int matd_is_vector(const matd_t *a)
268 {
269  assert(a != NULL);
270  return a->ncols == 1 || a->nrows == 1;
271 }
272 
277 static inline int matd_is_vector_len(const matd_t *a, int len)
278 {
279  assert(a != NULL);
280  return (a->ncols == 1 && a->nrows == len) || (a->ncols == len && a->nrows == 1);
281 }
282 
286 double matd_vec_mag(const matd_t *a);
287 
293 double matd_vec_dist(const matd_t *a, const matd_t *b);
294 
295 
299 double matd_vec_dist_n(const matd_t *a, const matd_t *b, int n);
300 
306 double matd_vec_dot_product(const matd_t *a, const matd_t *b);
307 
315 matd_t *matd_vec_normalize(const matd_t *a);
316 
323 matd_t *matd_crossproduct(const matd_t *a, const matd_t *b);
324 
325 double matd_err_inf(const matd_t *a, const matd_t *b);
326 
355 matd_t *matd_op(const char *expr, ...);
356 
361 void matd_destroy(matd_t *m);
362 
363 typedef struct
364 {
368 } matd_svd_t;
369 
382 
383 #define MATD_SVD_NO_WARNINGS 1
384  matd_svd_t matd_svd_flags(matd_t *A, int flags);
385 
387 // PLU Decomposition
388 
389 // All square matrices (even singular ones) have a partially-pivoted
390 // LU decomposition such that A = PLU, where P is a permutation
391 // matrix, L is a lower triangular matrix, and U is an upper
392 // triangular matrix.
393 //
394 typedef struct
395 {
396  // was the input matrix singular? When a zero pivot is found, this
397  // flag is set to indicate that this has happened.
398  int singular;
399 
400  unsigned int *piv; // permutation indices
401  int pivsign; // either +1 or -1
402 
403  // The matd_plu_t object returned "owns" the enclosed LU matrix. It
404  // is not expected that the returned object is itself useful to
405  // users: it contains the L and U information all smushed
406  // together.
407  matd_t *lu; // combined L and U matrices, permuted so they can be triangular.
408 } matd_plu_t;
409 
410 matd_plu_t *matd_plu(const matd_t *a);
411 void matd_plu_destroy(matd_plu_t *mlu);
412 double matd_plu_det(const matd_plu_t *lu);
413 matd_t *matd_plu_p(const matd_plu_t *lu);
414 matd_t *matd_plu_l(const matd_plu_t *lu);
415 matd_t *matd_plu_u(const matd_plu_t *lu);
416 matd_t *matd_plu_solve(const matd_plu_t *mlu, const matd_t *b);
417 
418 // uses LU decomposition internally.
419 matd_t *matd_solve(matd_t *A, matd_t *b);
420 
422 // Cholesky Factorization
423 
429 //matd_t *matd_cholesky(const matd_t *A);
430 
431 typedef struct
432 {
433  int is_spd;
435 } matd_chol_t;
436 
438 matd_t *matd_chol_solve(const matd_chol_t *chol, const matd_t *b);
439 void matd_chol_destroy(matd_chol_t *chol);
440 // only sensible on PSD matrices
442 
443 void matd_ltransposetriangle_solve(matd_t *u, const double *b, double *x);
444 void matd_ltriangle_solve(matd_t *u, const double *b, double *x);
445 void matd_utriangle_solve(matd_t *u, const double *b, double *x);
446 
447 
448 double matd_max(matd_t *m);
449 
450 #ifdef __cplusplus
451 }
452 #endif
453 
454 #endif
matd_t * matd_create_scalar(double v)
void matd_print_transpose(const matd_t *m, const char *fmt)
Definition: matd.c:208
matd_svd_t matd_svd(matd_t *A)
Definition: matd.c:1448
void matd_destroy(matd_t *m)
Definition: matd.c:226
double matd_err_inf(const matd_t *a, const matd_t *b)
Definition: matd.c:955
matd_t * matd_plu_l(const matd_plu_t *lu)
Definition: matd.c:1613
int is_spd
Definition: matd.h:433
matd_t * matd_inverse(const matd_t *a)
Definition: matd.c:485
matd_t * matd_op(const char *expr,...)
Definition: matd.c:798
void matd_ltriangle_solve(matd_t *u, const double *b, double *x)
double data[]
Definition: matd.h:54
double matd_get_scalar(const matd_t *m)
Definition: matd.c:142
matd_t * matd_plu_solve(const matd_plu_t *mlu, const matd_t *b)
Definition: matd.c:1648
void matd_utriangle_solve(matd_t *u, const double *b, double *x)
matd_t * matd_chol_inverse(matd_t *a)
Definition: matd.c:1990
unsigned int nrows
Definition: matd.h:53
matd_t * matd_create_dataf(int rows, int cols, const float *data)
Definition: matd.c:87
int singular
Definition: matd.h:398
matd_t * matd_solve(matd_t *A, matd_t *b)
Definition: matd.c:1681
double matd_vec_dot_product(const matd_t *a, const matd_t *b)
Definition: matd.c:906
unsigned int ncols
Definition: matd.h:53
matd_t * matd_identity(int dim)
Definition: matd.c:99
matd_t * u
Definition: matd.h:434
matd_t * matd_subtract(const matd_t *a, const matd_t *b)
Definition: matd.c:336
matd_t * matd_create(int rows, int cols)
Definition: matd.c:50
double matd_vec_dist_n(const matd_t *a, const matd_t *b, int n)
Definition: matd.c:870
void matd_plu_destroy(matd_plu_t *mlu)
Definition: matd.c:1580
void matd_put_scalar(matd_t *m, double value)
double matd_vec_mag(const matd_t *a)
Definition: matd.c:847
static int matd_is_scalar(const matd_t *a)
Definition: matd.h:256
matd_t * matd_scale(const matd_t *a, double s)
Definition: matd.c:261
void matd_put(matd_t *m, int row, int col, double value)
matd_t * matd_add(const matd_t *a, const matd_t *b)
Definition: matd.c:295
matd_svd_t matd_svd_flags(matd_t *A, int flags)
Definition: matd.c:1453
matd_t * S
Definition: matd.h:366
unsigned int * piv
Definition: matd.h:400
matd_t * matd_copy(const matd_t *m)
Definition: matd.c:158
int pivsign
Definition: matd.h:401
static int matd_is_vector(const matd_t *a)
Definition: matd.h:267
matd_t * lu
Definition: matd.h:407
double matd_vec_dist(const matd_t *a, const matd_t *b)
Definition: matd.c:859
matd_t * U
Definition: matd.h:365
matd_plu_t * matd_plu(const matd_t *a)
Definition: matd.c:1501
double matd_plu_det(const matd_plu_t *lu)
Definition: matd.c:1588
matd_t * matd_plu_u(const matd_plu_t *lu)
Definition: matd.c:1629
Definition: matd.h:51
matd_t * matd_select(const matd_t *a, int r0, int r1, int c0, int c1)
Definition: matd.c:171
matd_t * matd_vec_normalize(const matd_t *a)
Definition: matd.c:923
matd_t * matd_multiply(const matd_t *a, const matd_t *b)
Definition: matd.c:235
matd_t * matd_plu_p(const matd_plu_t *lu)
Definition: matd.c:1601
matd_t * matd_crossproduct(const matd_t *a, const matd_t *b)
Definition: matd.c:940
void matd_add_inplace(matd_t *a, const matd_t *b)
Definition: matd.c:316
void matd_ltransposetriangle_solve(matd_t *u, const double *b, double *x)
matd_t * V
Definition: matd.h:367
matd_t * matd_transpose(const matd_t *a)
Definition: matd.c:377
void matd_scale_inplace(matd_t *a, double s)
Definition: matd.c:279
double matd_det(const matd_t *a)
Definition: matd.c:425
matd_t * matd_create_data(int rows, int cols, const double *data)
double matd_max(matd_t *m)
Definition: matd.c:2004
static void matd_set_data(matd_t *m, const double *data)
Definition: matd.h:247
void matd_subtract_inplace(matd_t *a, const matd_t *b)
Definition: matd.c:357
void matd_chol_destroy(matd_chol_t *chol)
Definition: matd.c:1887
matd_chol_t * matd_chol(matd_t *A)
Definition: matd.c:1840
void matd_print(const matd_t *m, const char *fmt)
Definition: matd.c:190
static int matd_is_vector_len(const matd_t *a, int len)
Definition: matd.h:277
matd_t * matd_chol_solve(const matd_chol_t *chol, const matd_t *b)
Definition: matd.c:1938
double matd_get(const matd_t *m, int row, int col)
Definition: matd.c:112


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32