ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
statistics.hpp
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16#ifndef STATISTICS_HPP
17#define STATISTICS_HPP
18
19#include "core/clockwerkerrors.h"
20#include "core/safemath.hpp"
21
22namespace modelspace {
23
24 template <typename T>
25 int mean(T* array, unsigned int size, T& result) {
26 T sum = 0;
27 T* val = array;
28 for(unsigned int i = 0; i < size; i++) {
29 sum += *val;
30 val++;
31 }
32
33 return clockwerk::safeDivide(sum, (T)size, result);
34 }
35
36 template <typename T>
37 int variance(T* array, unsigned int size, T& result) {
38 // Calculate mean
39 int err = mean(array, size, result);
40 if(err) {return err;}
41
42 T sum = 0;
43 T var = 0;
44 T* val = array;
45 for(unsigned int i = 0; i < size; i++) {
46 var = *val - result;
47 sum += var*var;
48 val++;
49 }
50
51 return clockwerk::safeDivide(sum, (T)(size - 1), result);
52 }
53
54 template <typename T>
55 int stdev(T* array, unsigned int size, T& result) {
56 // Calculate mean
57 int err = variance(array, size, result);
58 if(err) {return err;}
59
60 double tmp;
61 err = clockwerk::safeSqrt((double)result, tmp);
62 result = (T)tmp;
63 return err;
64 }
65
66 template <typename T>
67 int max(T* array, unsigned int size, T& result) {
68 if(size > 0) {
69 result = *array;
70 } else {
71 return ERROR_DIMENSIONS;
72 }
73 T* val = array;
74 for(unsigned int i = 0; i < size; i++) {
75 if(*val > result) {
76 result = *val;
77 }
78 val++;
79 }
80
81 return NO_ERROR;
82 }
83
84 template <typename T>
85 int min(T* array, unsigned int size, T& result) {
86 if(size > 0) {
87 result = *array;
88 } else {
89 return ERROR_DIMENSIONS;
90 }
91 T* val = array;
92 for(unsigned int i = 0; i < size; i++) {
93 if(*val < result) {
94 result = *val;
95 }
96 val++;
97 }
98
99 return NO_ERROR;
100 }
101
102}
103
104#endif
#define NO_ERROR
Definition clockwerkerrors.h:31
#define ERROR_DIMENSIONS
Definition clockwerkerrors.h:38
Class to propagate CR3BP dynamics in characteristic units.
Definition ConfigurationWriter.cpp:18