diff --git a/Makefile b/Makefile index 06becf234277f8e97b311740b0656e0ca573cdac..c1fe22529a49b0de0af79f7b74f00a9f1b4f0249 100644 --- a/Makefile +++ b/Makefile @@ -124,6 +124,7 @@ LDSCRIPT= $(STARTUPLD)/STM32F411xE.ld CSRC = $(ALLCSRC) \ $(SRCDIR)/crc32.c \ $(SRCDIR)/main.c \ + $(SRCDIR)/random.c \ $(SRCDIR)/serial.c \ $(SRCDIR)/usb.c \ $(SRCDIR)/tests.c diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000000000000000000000000000000000000..080aa7ba7877ac98b0c848170014ce14d3701aca --- /dev/null +++ b/src/random.c @@ -0,0 +1,84 @@ +// This file is part of the execution-time evaluation for the qronos observer abstractions. +// Copyright (C) 2022-2023 Tim Rheinfels <tim.rheinfels@fau.de> +// See https://gitlab.cs.fau.de/qronos-state-abstractions/execution-time +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +/// +/// @file random.c +/// +/// @brief Provides the implementation of the pRNG utilities +/// +/// @author Tim Rheinfels (tim.rheinfels@fau.de) +/// + +#include "random.h" + +#include <stdlib.h> +#include <string.h> + +void random_init(void) +{ + srand(0u); +} + +float random_float(void) +{ + return rand() / (float) RAND_MAX; +} + +void random_matrix(float *M, size_t n, size_t m) +{ + for(size_t i = 0u; i < n; ++i) + { + for(size_t j = 0u; j < m; ++j) + { + M[i*m + j] = 2.0f * (random_float() - 0.5f); + } + } +} + +void random_matrix_cholesky(float *M, size_t n) +{ + memset(M, '\0', n*n*sizeof(float)); + for(size_t i = 0u; i < n; ++i) + { + for(size_t j = i; j < n; ++j) + { + M[i*n + j] = 2.0f * (random_float() - 0.5f); + } + + // Make sure the matrix is positive definite + M[i*n + i] += n; + } +} + +void random_matrix_spd(float *M, size_t n) +{ + for(size_t i = 0u; i < n; ++i) + { + for(size_t j = i; j < n; ++j) + { + M[i*n + j] = 2.0f * (random_float() - 0.5f); + + if(i != j) + { + M[j*n + i] = M[i*n + j]; + } + } + + // Make sure the matrix is positive definite + M[i*n + i] += n; + } +} diff --git a/src/random.h b/src/random.h new file mode 100644 index 0000000000000000000000000000000000000000..64027f5f2c98d629fa72f8000070d3f3bea073cc --- /dev/null +++ b/src/random.h @@ -0,0 +1,72 @@ +// This file is part of the execution-time evaluation for the qronos observer abstractions. +// Copyright (C) 2022-2023 Tim Rheinfels <tim.rheinfels@fau.de> +// See https://gitlab.cs.fau.de/qronos-state-abstractions/execution-time +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +/// +/// @file random.h +/// +/// @brief Provides the interface for the pRNG utilities +/// +/// @author Tim Rheinfels (tim.rheinfels@fau.de) +/// + +#ifndef RANDOM_H +#define RANDOM_H + +#include <stddef.h> + +/// +/// @brief Initializes the pRNG utilities +/// +void random_init(void); + +/// +/// @brief Draws a pseudo-random number @f$ x @f$ distributed uniformely on the interval [0, 1] as +/// @f[ x \sim \mathcal{U}[0, 1] @f] +/// +/// @returns Pseudo-random number @f$ x @f$ distributed uniformely on the interval [0, 1] +/// +float random_float(void); + +/// +/// @brief Initializes a matrix @p M of size @p n by @p m by uniformely drawing its components from the interval [-1, +1] as +/// @f[ X_{ij} \sim \mathcal{U}[-1, +1] @f] +/// +/// @param[out] M Matrix to draw components for +/// @param[in] n Number of rows +/// @param[in] m Number of columns +/// +void random_matrix(float *M, size_t n, size_t m); + +/// +/// @brief Initializes a matrix @p M of size @p n by @p n by drawing its components randomly such that it is triangular and positive definite as +/// @f[ X_{ij} \sim n \cdot \delta_{ij} + \mathcal{U}[-1, +1] \text{ if } i \leq j \text{ or } X_{ij} = 0 \text{ otherwise} @f] +/// +/// @param[out] M Matrix to draw components for +/// @param[in] n Number of rows and columns +/// +void random_matrix_cholesky(float *M, size_t n); + +/// +/// @brief Initializes a matrix @p M of size @p n by @p n by drawing its components randomly such that it symmetric and positive definite as +/// @f[ X_{ij} \sim n \cdot \delta_{ij} + \mathcal{U}[-1, +1] @f] +/// +/// @param[out] M Matrix to draw components for +/// @param[in] n Number of rows and columns +/// +void random_matrix_spd(float *M, size_t n); + +#endif // RANDOM_H