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 This software was developed in the APRIL Robotics Lab under the
4 direction of Edwin Olson, ebolson@umich.edu. This software may be
5 available under alternative licensing terms; contact the address above.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 The views and conclusions contained in the software and documentation are those
24 of the authors and should not be interpreted as representing official policies,
25 either expressed or implied, of the Regents of The University of Michigan.
26 */
27 
28 #pragma once
29 
30 #include <assert.h>
31 #include <stddef.h>
32 #include <string.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
45 typedef struct
46 {
47  unsigned int nrows, ncols;
48  double data[];
49 // double *data;
50 } matd_t;
51 
52 #define MATD_ALLOC(name, nrows, ncols) double name ## _storage [nrows*ncols]; matd_t name = { .nrows = nrows, .ncols = ncols, .data = &name ## _storage };
53 
59 #define MATD_EPS 1e-8
60 
65 #define MATD_EL(m, row, col) (m)->data[((row)*(m)->ncols + (col))]
66 
73 matd_t *matd_create(int rows, int cols);
74 
82 matd_t *matd_create_data(int rows, int cols, const double *data);
83 
91 matd_t *matd_create_dataf(int rows, int cols, const float *data);
92 
99 matd_t *matd_identity(int dim);
100 
110 matd_t *matd_create_scalar(double v);
111 
116 double matd_get(const matd_t *m, int row, int col);
117 
122 void matd_put(matd_t *m, int row, int col, double value);
123 
128 double matd_get_scalar(const matd_t *m);
129 
134 void matd_put_scalar(matd_t *m, double value);
135 
140 matd_t *matd_copy(const matd_t *m);
141 
150 matd_t *matd_select(const matd_t *a, int r0, int r1, int c0, int c1);
151 
157 void matd_print(const matd_t *m, const char *fmt);
158 
164 void matd_print_transpose(const matd_t *m, const char *fmt);
165 
172 matd_t *matd_add(const matd_t *a, const matd_t *b);
173 
179 void matd_add_inplace(matd_t *a, const matd_t *b);
180 
187 matd_t *matd_subtract(const matd_t *a, const matd_t *b);
188 
194 void matd_subtract_inplace(matd_t *a, const matd_t *b);
195 
201 matd_t *matd_scale(const matd_t *a, double s);
202 
207 void matd_scale_inplace(matd_t *a, double s);
208 
216 matd_t *matd_multiply(const matd_t *a, const matd_t *b);
217 
222 matd_t *matd_transpose(const matd_t *a);
223 
227 double matd_det(const matd_t *a);
228 
239 matd_t *matd_inverse(const matd_t *a);
240 
241 static inline void matd_set_data(matd_t *m, const double *data)
242 {
243  memcpy(m->data, data, m->nrows * m->ncols * sizeof(double));
244 }
245 
250 static inline int matd_is_scalar(const matd_t *a)
251 {
252  assert(a != NULL);
253  return a->ncols <= 1 && a->nrows <= 1;
254 }
255 
261 static inline int matd_is_vector(const matd_t *a)
262 {
263  assert(a != NULL);
264  return a->ncols == 1 || a->nrows == 1;
265 }
266 
271 static inline int matd_is_vector_len(const matd_t *a, int len)
272 {
273  assert(a != NULL);
274  return (a->ncols == 1 && a->nrows == (unsigned int)len) || (a->ncols == (unsigned int)len && a->nrows == 1);
275 }
276 
280 double matd_vec_mag(const matd_t *a);
281 
287 double matd_vec_dist(const matd_t *a, const matd_t *b);
288 
289 
293 double matd_vec_dist_n(const matd_t *a, const matd_t *b, int n);
294 
300 double matd_vec_dot_product(const matd_t *a, const matd_t *b);
301 
309 matd_t *matd_vec_normalize(const matd_t *a);
310 
317 matd_t *matd_crossproduct(const matd_t *a, const matd_t *b);
318 
319 double matd_err_inf(const matd_t *a, const matd_t *b);
320 
349 matd_t *matd_op(const char *expr, ...);
350 
355 void matd_destroy(matd_t *m);
356 
357 typedef struct
358 {
362 } matd_svd_t;
363 
376 
377 #define MATD_SVD_NO_WARNINGS 1
378  matd_svd_t matd_svd_flags(matd_t *A, int flags);
379 
381 // PLU Decomposition
382 
383 // All square matrices (even singular ones) have a partially-pivoted
384 // LU decomposition such that A = PLU, where P is a permutation
385 // matrix, L is a lower triangular matrix, and U is an upper
386 // triangular matrix.
387 //
388 typedef struct
389 {
390  // was the input matrix singular? When a zero pivot is found, this
391  // flag is set to indicate that this has happened.
392  int singular;
393 
394  unsigned int *piv; // permutation indices
395  int pivsign; // either +1 or -1
396 
397  // The matd_plu_t object returned "owns" the enclosed LU matrix. It
398  // is not expected that the returned object is itself useful to
399  // users: it contains the L and U information all smushed
400  // together.
401  matd_t *lu; // combined L and U matrices, permuted so they can be triangular.
402 } matd_plu_t;
403 
404 matd_plu_t *matd_plu(const matd_t *a);
405 void matd_plu_destroy(matd_plu_t *mlu);
406 double matd_plu_det(const matd_plu_t *lu);
407 matd_t *matd_plu_p(const matd_plu_t *lu);
408 matd_t *matd_plu_l(const matd_plu_t *lu);
409 matd_t *matd_plu_u(const matd_plu_t *lu);
410 matd_t *matd_plu_solve(const matd_plu_t *mlu, const matd_t *b);
411 
412 // uses LU decomposition internally.
413 matd_t *matd_solve(matd_t *A, matd_t *b);
414 
416 // Cholesky Factorization
417 
423 //matd_t *matd_cholesky(const matd_t *A);
424 
425 typedef struct
426 {
427  int is_spd;
429 } matd_chol_t;
430 
432 matd_t *matd_chol_solve(const matd_chol_t *chol, const matd_t *b);
433 void matd_chol_destroy(matd_chol_t *chol);
434 // only sensible on PSD matrices
436 
437 void matd_ltransposetriangle_solve(matd_t *u, const double *b, double *x);
438 void matd_ltriangle_solve(matd_t *u, const double *b, double *x);
439 void matd_utriangle_solve(matd_t *u, const double *b, double *x);
440 
441 
442 double matd_max(matd_t *m);
443 
444 #ifdef __cplusplus
445 }
446 #endif
matd_t * matd_create_scalar(double v)
void matd_print_transpose(const matd_t *m, const char *fmt)
Definition: matd.c:204
matd_svd_t matd_svd(matd_t *A)
Definition: matd.c:1459
void matd_destroy(matd_t *m)
Definition: matd.c:222
double matd_err_inf(const matd_t *a, const matd_t *b)
Definition: matd.c:954
matd_t * matd_plu_l(const matd_plu_t *lu)
Definition: matd.c:1625
int is_spd
Definition: matd.h:427
matd_t * matd_inverse(const matd_t *a)
Definition: matd.c:481
matd_t * matd_op(const char *expr,...)
Definition: matd.c:794
void matd_ltriangle_solve(matd_t *u, const double *b, double *x)
double data[]
Definition: matd.h:48
double matd_get_scalar(const matd_t *m)
Definition: matd.c:138
matd_t * matd_plu_solve(const matd_plu_t *mlu, const matd_t *b)
Definition: matd.c:1660
void matd_utriangle_solve(matd_t *u, const double *b, double *x)
matd_t * matd_chol_inverse(matd_t *a)
Definition: matd.c:2002
unsigned int nrows
Definition: matd.h:47
matd_t * matd_create_dataf(int rows, int cols, const float *data)
Definition: matd.c:83
int singular
Definition: matd.h:392
matd_t * matd_solve(matd_t *A, matd_t *b)
Definition: matd.c:1693
double matd_vec_dot_product(const matd_t *a, const matd_t *b)
Definition: matd.c:905
unsigned int ncols
Definition: matd.h:47
matd_t * matd_identity(int dim)
Definition: matd.c:95
matd_t * u
Definition: matd.h:428
matd_t * matd_subtract(const matd_t *a, const matd_t *b)
Definition: matd.c:332
matd_t * matd_create(int rows, int cols)
Definition: matd.c:46
double matd_vec_dist_n(const matd_t *a, const matd_t *b, int n)
Definition: matd.c:869
void matd_plu_destroy(matd_plu_t *mlu)
Definition: matd.c:1592
void matd_put_scalar(matd_t *m, double value)
double matd_vec_mag(const matd_t *a)
Definition: matd.c:846
static int matd_is_scalar(const matd_t *a)
Definition: matd.h:250
matd_t * matd_scale(const matd_t *a, double s)
Definition: matd.c:257
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:291
matd_svd_t matd_svd_flags(matd_t *A, int flags)
Definition: matd.c:1464
matd_t * S
Definition: matd.h:360
unsigned int * piv
Definition: matd.h:394
matd_t * matd_copy(const matd_t *m)
Definition: matd.c:154
int pivsign
Definition: matd.h:395
static int matd_is_vector(const matd_t *a)
Definition: matd.h:261
matd_t * lu
Definition: matd.h:401
double matd_vec_dist(const matd_t *a, const matd_t *b)
Definition: matd.c:858
matd_t * U
Definition: matd.h:359
matd_plu_t * matd_plu(const matd_t *a)
Definition: matd.c:1512
double matd_plu_det(const matd_plu_t *lu)
Definition: matd.c:1600
matd_t * matd_plu_u(const matd_plu_t *lu)
Definition: matd.c:1641
Definition: matd.h:45
matd_t * matd_select(const matd_t *a, int r0, int r1, int c0, int c1)
Definition: matd.c:167
matd_t * matd_vec_normalize(const matd_t *a)
Definition: matd.c:922
matd_t * matd_multiply(const matd_t *a, const matd_t *b)
Definition: matd.c:231
matd_t * matd_plu_p(const matd_plu_t *lu)
Definition: matd.c:1613
matd_t * matd_crossproduct(const matd_t *a, const matd_t *b)
Definition: matd.c:939
void matd_add_inplace(matd_t *a, const matd_t *b)
Definition: matd.c:312
void matd_ltransposetriangle_solve(matd_t *u, const double *b, double *x)
matd_t * V
Definition: matd.h:361
matd_t * matd_transpose(const matd_t *a)
Definition: matd.c:373
void matd_scale_inplace(matd_t *a, double s)
Definition: matd.c:275
double matd_det(const matd_t *a)
Definition: matd.c:421
matd_t * matd_create_data(int rows, int cols, const double *data)
double matd_max(matd_t *m)
Definition: matd.c:2016
static void matd_set_data(matd_t *m, const double *data)
Definition: matd.h:241
void matd_subtract_inplace(matd_t *a, const matd_t *b)
Definition: matd.c:353
void matd_chol_destroy(matd_chol_t *chol)
Definition: matd.c:1899
matd_chol_t * matd_chol(matd_t *A)
Definition: matd.c:1852
void matd_print(const matd_t *m, const char *fmt)
Definition: matd.c:186
static int matd_is_vector_len(const matd_t *a, int len)
Definition: matd.h:271
matd_t * matd_chol_solve(const matd_chol_t *chol, const matd_t *b)
Definition: matd.c:1950
double matd_get(const matd_t *m, int row, int col)
Definition: matd.c:108


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Mon Jun 26 2023 02:26:12