RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
utils.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
3  * 2020 Freie Universität Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser General
6  * Public License v2.1. See the file LICENSE in the top level directory for more
7  * details.
8  */
9 
23 #include <math.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 
27 #include "utils.h"
28 #include "shell_sort.h"
29 
30 // from degree to radian
31 double utils_to_radian(double deg_angle)
32 {
33  double radians_angle = deg_angle * M_PI / 180.0;
34 
35  return radians_angle;
36 }
37 
38 //Sine of argument in degrees
39 double utils_sind(double deg_angle)
40 {
41  double radians_angle = deg_angle * M_PI / 180.0;
42 
43  return sin(radians_angle);
44 }
45 
46 void utils_swap(uint8_t *a, uint8_t *b)
47 {
48  uint8_t tmp;
49 
50  tmp = *a;
51  *a = *b;
52  *b = tmp;
53 }
54 
55 double utils_max(double a, double b)
56 {
57  if (a < b) {
58  return b;
59  }
60  else {
61  return a;
62  }
63 }
64 
65 double utils_min(double a, double b)
66 {
67  if (a < b) {
68  return a;
69  }
70  else {
71  return b;
72  }
73 }
74 
75 uint8_t utils_u8_max(uint8_t a, uint8_t b)
76 {
77  if (a < b) {
78  return b;
79  }
80  else {
81  return a;
82  }
83 }
84 
85 uint8_t utils_u8_min(uint8_t a, uint8_t b)
86 {
87  if (a < b) {
88  return a;
89  }
90  else {
91  return b;
92  }
93 }
94 
95 // Enables to use variable format strings as well as argument lists
96 void utils_printf(char *format_str, ...)
97 {
98  va_list args;
99 
100  va_start(args, format_str);
101  vprintf(format_str, args);
102  va_end(args);
103 }
104 
105 double utils_mean(uint8_t arr_size, vector_t in_arr[])
106 {
107  double mean = 0.0;
108 
109  if (arr_size <= 0) {
110  puts("Not valid input array size !!!");
111  return mean;
112  }
113 
114  for (uint8_t i = 0; i < arr_size; i++) {
115  mean += in_arr[i];
116  }
117 
118  return mean / arr_size;
119 }
120 
121 void utils_moving_average(uint8_t arr_size, vector_t in_arr[],
122  uint8_t window_size,
123  vector_t out_arr[])
124 {
125  double sum = 0.0;
126  double trail_value = 0;
127  uint8_t pos = 0;
128 
129  if (arr_size <= 0) {
130  puts("Not valid input array size !!!");
131  return;
132  }
133 
134  if (window_size <= 0) {
135  puts("Not valid window size !!!");
136  return;
137  }
138 
139  for (uint8_t i = 0; i < arr_size; i++) {
140  sum = sum - trail_value + in_arr[i];
141  out_arr[i] = sum / window_size;
142 
143  pos++;
144  if (pos >= window_size) {
145 
146  trail_value = in_arr[pos - window_size];
147 
148  }
149  }
150 }
151 
152 double utils_get_median(vector_t arr[], uint8_t length)
153 {
154  int middle = length / 2;
155 
156  shell_sort(arr, length);
157 
158  if (length % 2 == 1) {
159  return arr[middle];
160  }
161  else {
162  return (arr[middle - 1] + arr[middle]) / 2.0;
163  }
164 
165 }
166 
168 double utils_get_save_square_root(double x, double y)
169 {
170  double sqr_root;
171 
172  if (fabs(x) > fabs(y)) {
173  sqr_root = y / x;
174  sqr_root = fabs(x) * sqrt(1 + sqr_root * sqr_root);
175  }
176  else if (y != 0) {
177  sqr_root = x / y;
178  sqr_root = fabs(y) * sqrt(1 + sqr_root * sqr_root);
179  }
180  else {
181  sqr_root = 0.0;
182  }
183 
184  return sqr_root;
185 }
shell_sort
void shell_sort(vector_t *arr, uint8_t length)
Sort a data set of type utils_t by using the Shell sort algorithm.
Definition: shell_sort.c:49
utils_u8_max
uint8_t utils_u8_max(uint8_t a, uint8_t b)
Returns the greater of two numbers from type uint8_t.
Definition: utils.c:75
vector_t
#define vector_t
Define the data type of the vector elements.
Definition: vector.h:33
utils_swap
void utils_swap(uint8_t *a, uint8_t *b)
Interchange the values of two variables of type uint8_t.
Definition: utils.c:46
utils_sind
double utils_sind(double deg_angle)
Compute the sine of a variable in degrees.
Definition: utils.c:39
utils_to_radian
double utils_to_radian(double deg_angle)
Convert the angle from degrees to radians.
Definition: utils.c:31
utils_get_median
double utils_get_median(vector_t arr[], uint8_t length)
Compute the median of a finite array of numbers.
Definition: utils.c:152
utils.h
Utilities for linear algebra.
utils_u8_min
uint8_t utils_u8_min(uint8_t a, uint8_t b)
Returns the smaller of two numbers from type uint8_t.
Definition: utils.c:85
M_PI
#define M_PI
Definition: matrix.h:54
utils_moving_average
void utils_moving_average(uint8_t arr_size, vector_t in_arr[], uint8_t window_size, vector_t out_arr[])
Compute the moving average of a data set.
Definition: utils.c:121
utils_min
double utils_min(double a, double b)
Returns the smaller of two real numbers.
Definition: utils.c:65
utils_get_save_square_root
double utils_get_save_square_root(double x, double y)
sqrt(a^2 + b^2) without under/overflow.
Definition: utils.c:168
shell_sort.h
Implement the Shell sort algorithm.
utils_mean
double utils_mean(uint8_t arr_size, vector_t in_arr[])
Compute the mean value of a data set.
Definition: utils.c:105
utils_max
double utils_max(double a, double b)
Returns the greater of two real numbers.
Definition: utils.c:55
utils_printf
void utils_printf(char *format_str,...)
Print by using variable format string as well as argument lists.
Definition: utils.c:96