# Brahe - a heterogenous collection of mathematical tools

## Main Index
Home Page

Created by Scott Robert Ladd

mathtools.h
Go to the documentation of this file.
1 /*
2 Brahe is a heterogenous collection of mathematical tools, written in Standard C.
3
4 Copyright 2011 Scott Robert Ladd. All rights reserved.
5
6 Brahe is user-supported open source software. Its continued development is dependent
7 on financial support from the community. You can provide funding by visiting the Brahe
8 website at:
9
10 http://www.coyotegulch.com
11
12 You may license Brahe in one of two fashions:
13
14 1) Simplified BSD License (FreeBSD License)
15
16 Redistribution and use in source and binary forms, with or without modification, are
17 permitted provided that the following conditions are met:
18
19 1. Redistributions of source code must retain the above copyright notice, this list of
20 conditions and the following disclaimer.
21
22 2. Redistributions in binary form must reproduce the above copyright notice, this list
23 of conditions and the following disclaimer in the documentation and/or other materials
24 provided with the distribution.
25
26 THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
28 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
29 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 The views and conclusions contained in the software and documentation are those of the
37 authors and should not be interpreted as representing official policies, either expressed
38 or implied, of Scott Robert Ladd.
39
40 2) Closed-Source Proprietary License
41
42 If your project is a closed-source or proprietary project, the Simplified BSD License may
43 not be appropriate or desirable. In such cases, contact the Brahe copyright holder to
44 arrange your purchase of an appropriate license.
45
46 The author can be contacted at:
47
48 scott.ladd@coyotegulch.com
49 scott.ladd@gmail.com
50 http:www.coyotegulch.com
51 */
52
53 #if !defined(LIBBRAHE_MATHTOOLS_H)
54 #define LIBBRAHE_MATHTOOLS_H
55
56 #include <stddef.h>
57 #include <math.h>
58 #include <limits.h>
59 #include <float.h>
60
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64
65 #if defined(_MSC_VER)
66 #pragma warning (disable: 4244 4267 4996)
67 #if !defined(__cplusplus)
68 typedef char bool;
69 static const int true = 1;
70 static const int false = 0;
71 #endif
72 typedef unsigned __int64 uint64_t;
73 typedef __int64 int64_t;
74 typedef unsigned __int32 uint32_t;
75 typedef __int32 int32_t;
76 typedef unsigned __int16 uint16_t;
77 typedef __int16 int16_t;
78 typedef unsigned __int8 uint8_t;
79 typedef __int8 int8_t;
80 #else
81 // ISO C standard compilers
82 #include <stdbool.h>
83 #include <stdint.h>
84 #endif
85
86 //-----------------------------------------------------------------------------
87 // Rounding
88 //-----------------------------------------------------------------------------
89
91
94 double brahe_round_nearest(const double x);
95
97
104 double brahe_sigdig(const double x, const uint16_t n);
105
106 //-----------------------------------------------------------------------------
107 // Lowest Common Multple (LCM) and Lowest Common Denominator (GCD)
108 //-----------------------------------------------------------------------------
109
111
117 uint64_t brahe_lcm(const uint64_t x, const uint64_t y);
118
120
126 uint64_t brahe_gcf(uint64_t x, uint64_t y);
127
128 //-----------------------------------------------------------------------------
129 // Logarithms
130 //-----------------------------------------------------------------------------
131
133
140 double brahe_log2base(const double x, const double base);
141
143
148 int brahe_sizepow2(const int n);
149
150 //-----------------------------------------------------------------------------
151 // Statistical functions
152 //-----------------------------------------------------------------------------
153
155 typedef enum brahe_pretty_format_t
156 {
158 BRAHE_PRETTY_TEXT,
160 BRAHE_PRETTY_COMMA
161 }
162 brahe_pretty_format;
163
165
171 char * brahe_pretty_int(int64_t n, brahe_pretty_format fmt);
172
173 //-----------------------------------------------------------------------------
174 // Statistical functions
175 //-----------------------------------------------------------------------------
176
178 typedef struct brahe_statistics_t
179 {
181 double min;
183 double max;
185 double mean;
187 double variance;
189 double sigma;
190 }
191 brahe_statistics;
192
194
200 brahe_statistics brahe_get_statistics(double * data, size_t n);
201
203
211 double * brahe_moving_average(const double * data, const int n, const int distance);
212
213 //-----------------------------------------------------------------------------
214 // Digital Signal Processing
215 //-----------------------------------------------------------------------------
216
218
226 double * brahe_simple_fft(const double * data, const int n);
227
229
237 double * brahe_simple_fft2(const double * data, const int n);
238
240
243 typedef struct
244 {
246 double wavelength;
248 double amplitude;
249 }
250 brahe_wave_factor_t;
251
253
263 double * brahe_make_sinusoid(const brahe_wave_factor_t * factors, const size_t factor_n, const size_t array_n);
264
266
274 void brahe_add_noise(double * a, const size_t n, double noise);
275
276 //-----------------------------------------------------------------------------
277 // Trigonometry
278 //-----------------------------------------------------------------------------
280
285 double brahe_asinh(const double x);
286
288
293 double brahe_acosh(const double x);
294
296
301 double brahe_atanh(const double x);
302
303 //-----------------------------------------------------------------------------
304 // Constants
305 //-----------------------------------------------------------------------------
306
308 #define BRAHE_E 2.71828182845904523536028747135
309
311 #define BRAHE_LOG2_E 1.44269504088896340735992468100
312
314 #define BRAHE_LOG10_E 0.43429448190325182765112891892
315
317 #define BRAHE_SQRT_2 1.41421356237309504880168872421
318
320 #define BRAHE_SQRT_HALF 0.70710678118654752440084436210
321
323 #define BRAHE_SQRT_3 1.73205080756887729352744634151
324
326 #define BRAHE_PI 3.14159265358979323846264338328
327
329 #define BRAHE_TAU 6.28318530717958647692528676656
330
332 #define BRAHE_PI_DIV_2 1.57079632679489661923132169164
333
335 #define BRAHE_PI_DIV_3 1.04719755119659774615421446109
336
338 #define BRAHE_PI_DIV_4 0.78539816339744830961566084582
339
341 #define BRAHE_PI_DIV_6 0.52359877559829887307710723055
342
344 #define BRAHE_PI_DIV_9 0.34906585039886591538473815370
345
347 #define BRAHE_PI_DIV_12 0.26179938779914943653855361527
348
350 #define BRAHE_PI_DIV_18 0.17453292519943295769236907685
351
353 #define BRAHE_PI_DIV_36 0.08726646259971647884618453842
354
356 #define BRAHE_DEG_PER_RAD 57.2957795130823208767981548141
357
359 #define BRAHE_RAD_PER_DEG 0.01745329251994329576923690768
360
362 #define BRAHE_SQRT_PI 1.77245385090551602729816748334
363
365 #define BRAHE_TWO_DIV_SQRT_PI 1.12837916709551257389615890312
366
368 #define BRAHE_ONE_DIV_PI 0.31830988618379067153776752675
369
371 #define BRAHE_TWO_DIV_PI 0.63661977236758134307553505349
372
374 #define BRAHE_LN_10 2.30258509299404568401799145468
375
377 #define BRAHE_LN_2 0.69314718055994530941723212146
378
380 #define BRAHE_LOG_2 0.30102999566398119521373889472
381
383 #define BRAHE_LN_PI 1.14472988584940017414342735135
384
386 #define BRAHE_EULER 0.57721566490153286060651209008
387
388 #if defined(__cplusplus)
389 }
390 #endif
391
392 #endif
BRAHE_PRETTY_TEXT
english text (nine thousand, two hundred eleven)
Definition: mathtools.h:158
brahe_get_statistics
brahe_statistics brahe_get_statistics(double *data, size_t n)
statistics for array of double
brahe_wave_factor_t::amplitude
double amplitude
arbitrary wave amplitude
Definition: mathtools.h:248
brahe_statistics_t::min
double min
minimum value from array
Definition: mathtools.h:181
brahe_statistics_t::variance
double variance
variance
Definition: mathtools.h:187
brahe_wave_factor_t
Sine wave definition.
Definition: mathtools.h:243
brahe_statistics_t::mean
double mean
mean (average)
Definition: mathtools.h:185
brahe_statistics_t::max
double max
maximum value
Definition: mathtools.h:183
brahe_pretty_format_t
brahe_pretty_format_t
Formats for pretty-printing integers.
Definition: mathtools.h:155
brahe_add_noise
void brahe_add_noise(double *a, const size_t n, double noise)
Apply noise to a signal.
brahe_asinh
double brahe_asinh(const double x)
Hyperbolic arcsine.
BRAHE_PRETTY_COMMA
comma delimited, (1,234,567,890)
Definition: mathtools.h:160
brahe_simple_fft2
double * brahe_simple_fft2(const double *data, const int n)
Simple real-to-real fft (power of 2 length)
brahe_sizepow2
int brahe_sizepow2(const int n)
Smallest power of 2 that includes a given value.
brahe_log2base
double brahe_log2base(const double x, const double base)
Logarithm to a specified base.
brahe_gcf
uint64_t brahe_gcf(uint64_t x, uint64_t y)
Greatest common factor (denominator)
brahe_atanh
double brahe_atanh(const double x)
Hyperbolic arctangent.
brahe_statistics_t::sigma
double sigma
standard deviation
Definition: mathtools.h:189
brahe_simple_fft
double * brahe_simple_fft(const double *data, const int n)
Simple real-to-real fft (arbitrary length)
brahe_pretty_int
char * brahe_pretty_int(int64_t n, brahe_pretty_format fmt)
Turn a 64-bit integer into a pretty string.
brahe_sigdig
double brahe_sigdig(const double x, const uint16_t n)
Set number of significant digits in a floating-point value.
brahe_lcm
uint64_t brahe_lcm(const uint64_t x, const uint64_t y)
Lowest common multiple.
brahe_acosh
double brahe_acosh(const double x)
Hyperbolic arccosine.
brahe_statistics
struct brahe_statistics_t brahe_statistics
Structure containing statistical values calculate from a double array.
brahe_statistics_t
Structure containing statistical values calculate from a double array.
Definition: mathtools.h:178
brahe_round_nearest
double brahe_round_nearest(const double x)
Round to nearest value.
brahe_wave_factor_t::wavelength
double wavelength
wavelength
Definition: mathtools.h:246
brahe_make_sinusoid
double * brahe_make_sinusoid(const brahe_wave_factor_t *factors, const size_t factor_n, const size_t array_n)
Sine wave based artificial signal generator.
brahe_pretty_format
enum brahe_pretty_format_t brahe_pretty_format
Formats for pretty-printing integers.
brahe_moving_average
double * brahe_moving_average(const double *data, const int n, const int distance)
Moving average.

© 2011 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.