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 } matd_t;
50 
51 #define MATD_ALLOC(name, nrows, ncols) double name ## _storage [nrows*ncols]; matd_t name = { .nrows = nrows, .ncols = ncols, .data = &name ## _storage };
52 
58 #define MATD_EPS 1e-8
59 
64 #define MATD_EL(m, row, col) (m)->data[((row)*(m)->ncols + (col))]
65 
72 matd_t *matd_create(int rows, int cols);
73 
81 matd_t *matd_create_data(int rows, int cols, const double *data);
82 
90 matd_t *matd_create_dataf(int rows, int cols, const float *data);
91 
98 matd_t *matd_identity(int dim);
99 
109 matd_t *matd_create_scalar(double v);
110 
115 double matd_get(const matd_t *m, unsigned int row, unsigned int col);
116 
121 void matd_put(matd_t *m, unsigned int row, unsigned int col, double value);
122 
127 double matd_get_scalar(const matd_t *m);
128 
133 void matd_put_scalar(matd_t *m, double value);
134 
139 matd_t *matd_copy(const matd_t *m);
140 
149 matd_t *matd_select(const matd_t *a, unsigned int r0, int r1, unsigned int c0, int c1);
150 
156 void matd_print(const matd_t *m, const char *fmt);
157 
163 void matd_print_transpose(const matd_t *m, const char *fmt);
164 
171 matd_t *matd_add(const matd_t *a, const matd_t *b);
172 
178 void matd_add_inplace(matd_t *a, const matd_t *b);
179 
186 matd_t *matd_subtract(const matd_t *a, const matd_t *b);
187 
193 void matd_subtract_inplace(matd_t *a, const matd_t *b);
194 
200 matd_t *matd_scale(const matd_t *a, double s);
201 
206 void matd_scale_inplace(matd_t *a, double s);
207 
215 matd_t *matd_multiply(const matd_t *a, const matd_t *b);
216 
221 matd_t *matd_transpose(const matd_t *a);
222 
226 double matd_det(const matd_t *a);
227 
238 matd_t *matd_inverse(const matd_t *a);
239 
240 static inline void matd_set_data(matd_t *m, const double *data)
241 {
242  memcpy(m->data, data, m->nrows * m->ncols * sizeof(double));
243 }
244 
249 static inline int matd_is_scalar(const matd_t *a)
250 {
251  assert(a != NULL);
252  return a->ncols <= 1 && a->nrows <= 1;
253 }
254 
260 static inline int matd_is_vector(const matd_t *a)
261 {
262  assert(a != NULL);
263  return a->ncols == 1 || a->nrows == 1;
264 }
265 
270 static inline int matd_is_vector_len(const matd_t *a, int len)
271 {
272  assert(a != NULL);
273  return (a->ncols == 1 && a->nrows == (unsigned int)len) || (a->ncols == (unsigned int)len && a->nrows == 1);
274 }
275 
279 double matd_vec_mag(const matd_t *a);
280 
286 double matd_vec_dist(const matd_t *a, const matd_t *b);
287 
288 
292 double matd_vec_dist_n(const matd_t *a, const matd_t *b, int n);
293 
299 double matd_vec_dot_product(const matd_t *a, const matd_t *b);
300 
308 matd_t *matd_vec_normalize(const matd_t *a);
309 
316 matd_t *matd_crossproduct(const matd_t *a, const matd_t *b);
317 
318 double matd_err_inf(const matd_t *a, const matd_t *b);
319 
348 matd_t *matd_op(const char *expr, ...);
349 
354 void matd_destroy(matd_t *m);
355 
356 typedef struct
357 {
361 } matd_svd_t;
362 
375 
376 #define MATD_SVD_NO_WARNINGS 1
377  matd_svd_t matd_svd_flags(matd_t *A, int flags);
378 
380 // PLU Decomposition
381 
382 // All square matrices (even singular ones) have a partially-pivoted
383 // LU decomposition such that A = PLU, where P is a permutation
384 // matrix, L is a lower triangular matrix, and U is an upper
385 // triangular matrix.
386 //
387 typedef struct
388 {
389  // was the input matrix singular? When a zero pivot is found, this
390  // flag is set to indicate that this has happened.
391  int singular;
392 
393  unsigned int *piv; // permutation indices
394  int pivsign; // either +1 or -1
395 
396  // The matd_plu_t object returned "owns" the enclosed LU matrix. It
397  // is not expected that the returned object is itself useful to
398  // users: it contains the L and U information all smushed
399  // together.
400  matd_t *lu; // combined L and U matrices, permuted so they can be triangular.
401 } matd_plu_t;
402 
403 matd_plu_t *matd_plu(const matd_t *a);
404 void matd_plu_destroy(matd_plu_t *mlu);
405 double matd_plu_det(const matd_plu_t *lu);
406 matd_t *matd_plu_p(const matd_plu_t *lu);
407 matd_t *matd_plu_l(const matd_plu_t *lu);
408 matd_t *matd_plu_u(const matd_plu_t *lu);
409 matd_t *matd_plu_solve(const matd_plu_t *mlu, const matd_t *b);
410 
411 // uses LU decomposition internally.
412 matd_t *matd_solve(matd_t *A, matd_t *b);
413 
415 // Cholesky Factorization
416 
422 //matd_t *matd_cholesky(const matd_t *A);
423 
424 typedef struct
425 {
426  int is_spd;
428 } matd_chol_t;
429 
431 matd_t *matd_chol_solve(const matd_chol_t *chol, const matd_t *b);
432 void matd_chol_destroy(matd_chol_t *chol);
433 // only sensible on PSD matrices
435 
436 void matd_ltransposetriangle_solve(matd_t *u, const double *b, double *x);
437 void matd_ltriangle_solve(matd_t *u, const double *b, double *x);
438 void matd_utriangle_solve(matd_t *u, const double *b, double *x);
439 
440 
441 double matd_max(matd_t *m);
442 
443 #ifdef __cplusplus
444 }
445 #endif
matd_t
Definition: matd.h:45
matd_plu_p
matd_t * matd_plu_p(const matd_plu_t *lu)
Definition: matd.c:1615
matd_ltransposetriangle_solve
void matd_ltransposetriangle_solve(matd_t *u, const double *b, double *x)
matd_max
double matd_max(matd_t *m)
Definition: matd.c:2018
matd_multiply
matd_t * matd_multiply(const matd_t *a, const matd_t *b)
Definition: matd.c:230
matd_crossproduct
matd_t * matd_crossproduct(const matd_t *a, const matd_t *b)
Definition: matd.c:941
matd_det
double matd_det(const matd_t *a)
Definition: matd.c:420
matd_is_scalar
static int matd_is_scalar(const matd_t *a)
Definition: matd.h:249
matd_plu_t::singular
int singular
Definition: matd.h:391
matd_add
matd_t * matd_add(const matd_t *a, const matd_t *b)
Definition: matd.c:290
matd_copy
matd_t * matd_copy(const matd_t *m)
Definition: matd.c:152
matd_plu_t::lu
matd_t * lu
Definition: matd.h:400
matd_subtract_inplace
void matd_subtract_inplace(matd_t *a, const matd_t *b)
Definition: matd.c:352
matd_put_scalar
void matd_put_scalar(matd_t *m, double value)
matd_print
void matd_print(const matd_t *m, const char *fmt)
Definition: matd.c:184
matd_create_scalar
matd_t * matd_create_scalar(double v)
matd_chol_solve
matd_t * matd_chol_solve(const matd_chol_t *chol, const matd_t *b)
Definition: matd.c:1952
matd_chol_destroy
void matd_chol_destroy(matd_chol_t *chol)
Definition: matd.c:1901
matd_vec_dist
double matd_vec_dist(const matd_t *a, const matd_t *b)
Definition: matd.c:857
matd_svd_t::U
matd_t * U
Definition: matd.h:358
matd_print_transpose
void matd_print_transpose(const matd_t *m, const char *fmt)
Definition: matd.c:202
matd_is_vector
static int matd_is_vector(const matd_t *a)
Definition: matd.h:260
matd_t::data
double * data
Definition: matd.h:48
matd_transpose
matd_t * matd_transpose(const matd_t *a)
Definition: matd.c:372
matd_ltriangle_solve
void matd_ltriangle_solve(matd_t *u, const double *b, double *x)
matd_utriangle_solve
void matd_utriangle_solve(matd_t *u, const double *b, double *x)
matd_chol_t
Definition: matd.h:424
matd_vec_normalize
matd_t * matd_vec_normalize(const matd_t *a)
Definition: matd.c:924
matd_plu_t::pivsign
int pivsign
Definition: matd.h:394
matd_err_inf
double matd_err_inf(const matd_t *a, const matd_t *b)
Definition: matd.c:956
matd_plu
matd_plu_t * matd_plu(const matd_t *a)
Definition: matd.c:1514
matd_chol_inverse
matd_t * matd_chol_inverse(matd_t *a)
Definition: matd.c:2004
matd_inverse
matd_t * matd_inverse(const matd_t *a)
Definition: matd.c:480
matd_vec_dot_product
double matd_vec_dot_product(const matd_t *a, const matd_t *b)
Definition: matd.c:906
matd_identity
matd_t * matd_identity(int dim)
Definition: matd.c:97
matd_plu_t::piv
unsigned int * piv
Definition: matd.h:393
matd_get
double matd_get(const matd_t *m, unsigned int row, unsigned int col)
Definition: matd.c:110
matd_put
void matd_put(matd_t *m, unsigned int row, unsigned int col, double value)
matd_scale
matd_t * matd_scale(const matd_t *a, double s)
Definition: matd.c:256
matd_svd_flags
matd_svd_t matd_svd_flags(matd_t *A, int flags)
Definition: matd.c:1466
matd_select
matd_t * matd_select(const matd_t *a, unsigned int r0, int r1, unsigned int c0, int c1)
Definition: matd.c:165
matd_vec_dist_n
double matd_vec_dist_n(const matd_t *a, const matd_t *b, int n)
Definition: matd.c:868
matd_create_data
matd_t * matd_create_data(int rows, int cols, const double *data)
matd_create
matd_t * matd_create(int rows, int cols)
Definition: matd.c:46
matd_chol
matd_chol_t * matd_chol(matd_t *A)
Definition: matd.c:1854
matd_svd_t::V
matd_t * V
Definition: matd.h:360
matd_solve
matd_t * matd_solve(matd_t *A, matd_t *b)
Definition: matd.c:1695
matd_set_data
static void matd_set_data(matd_t *m, const double *data)
Definition: matd.h:240
matd_is_vector_len
static int matd_is_vector_len(const matd_t *a, int len)
Definition: matd.h:270
matd_subtract
matd_t * matd_subtract(const matd_t *a, const matd_t *b)
Definition: matd.c:331
matd_plu_destroy
void matd_plu_destroy(matd_plu_t *mlu)
Definition: matd.c:1594
matd_destroy
void matd_destroy(matd_t *m)
Definition: matd.c:220
matd_svd_t
Definition: matd.h:356
matd_plu_det
double matd_plu_det(const matd_plu_t *lu)
Definition: matd.c:1602
matd_svd
matd_svd_t matd_svd(matd_t *A)
Definition: matd.c:1461
matd_plu_l
matd_t * matd_plu_l(const matd_plu_t *lu)
Definition: matd.c:1627
matd_op
matd_t * matd_op(const char *expr,...)
Definition: matd.c:793
matd_chol_t::u
matd_t * u
Definition: matd.h:427
matd_svd_t::S
matd_t * S
Definition: matd.h:359
matd_chol_t::is_spd
int is_spd
Definition: matd.h:426
matd_add_inplace
void matd_add_inplace(matd_t *a, const matd_t *b)
Definition: matd.c:311
matd_plu_solve
matd_t * matd_plu_solve(const matd_plu_t *mlu, const matd_t *b)
Definition: matd.c:1662
matd_plu_u
matd_t * matd_plu_u(const matd_plu_t *lu)
Definition: matd.c:1643
matd_plu_t
Definition: matd.h:387
matd_t::ncols
unsigned int ncols
Definition: matd.h:47
matd_t::nrows
unsigned int nrows
Definition: matd.h:47
matd_create_dataf
matd_t * matd_create_dataf(int rows, int cols, const float *data)
Definition: matd.c:85
matd_scale_inplace
void matd_scale_inplace(matd_t *a, double s)
Definition: matd.c:274
matd_get_scalar
double matd_get_scalar(const matd_t *m)
Definition: matd.c:136
matd_vec_mag
double matd_vec_mag(const matd_t *a)
Definition: matd.c:845


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Sun Apr 20 2025 02:08:19