WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
functionutils.hpp
Go to the documentation of this file.
1/******************************************************************************
2* Copyright (c) ATTX INC 2025. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, INC. 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, INC.
15******************************************************************************/
16/*
17Function Utils header file
18-------------------------
19The function utilities header file contains utiltities for
20less common but practical functions. E.g. saturation functions
21and activation functions.
22
23Author: James Tabony
24
25Update Log
26----------
27Date, Name, Description
28*/
29
30#ifndef UTILS_FUNCTION_UTILS_HPP
31#define UTILS_FUNCTION_UTILS_HPP
32
34#include <cmath>
35
36namespace warptwin {
37
38 // Lookup table of factorials for optimaized computation organized as {n, n!}
39 const std::map<int, double> FACTORIAL_CACHE = {
40 { 0, 1.0},
41 { 5, 120.0},
42 {10, 3628800.0},
43 {15, 1307674368000.0},
44 {20, 2432902008176640000.0},
45 {25, 15511210043330985984000000.0},
46 {30, 265252859812191058636308480000000.0},
47 };
48
56 template<typename T>
57 int satlin(T input, T &output, T limit = 1.0) {
58
59 if (input > limit) {output = limit;}
60 else if (input < 0) {output = 0.0;}
61 else {output = input;}
62 return NO_ERROR;
63 }
64
72 template<typename T>
73 int satlins(T input, T &output, T limit = 1.0) {
74
75 if (input > limit) {output = limit;}
76 else if (input < -limit) {output = -limit;}
77 else {output = input;}
78 return NO_ERROR;
79 }
80
87 template<typename T>
88 int poslin(T input, T &output) {
89
90 if (input > 0) {output = input;}
91 else {output = 0.0;}
92 return NO_ERROR;
93 }
94
102 template<typename T>
103 int Heaviside(T input, T &output, T center = 0.0) {
104
105 if (input < center) {output = 0.0;}
106 else {output = 1.0;}
107 return NO_ERROR;
108 }
109
116 template<typename T>
117 int signum(T input, T &output) {
118 output = (input > 0) - (input < 0);
119 return NO_ERROR;
120 }
121
131 template<typename T>
132 int deadzoneTrimmed(T input, T bounds, T &output) {
133 if (bounds < 0.0) {return ERROR_INVALID_RANGE;}
134 T _signum_input; signum(input, _signum_input);
135 output = (std::abs(input) > bounds) ? (input - _signum_input*bounds) : 0.0;
136 return NO_ERROR;
137 }
138
148 template<typename T>
149 int deadzone(T input, T bounds, T &output) {
150 if (bounds < 0.0) {return ERROR_INVALID_RANGE;}
151 output = (std::abs(input) > bounds) ? (input) : 0.0;
152 return NO_ERROR;
153 }
154
165 template<typename T>
166 int clamp(T input, T min_val, T max_val, T &output) {
167 output = std::max(min_val, std::min(input, max_val));
168 return NO_ERROR;
169 }
170
175 template<typename T>
176 int factorial(int n, T &output) {
177 if (n < 0) return ERROR_DIVIDE_BY_ZERO;
178
179 // Check if n is in cache
180 auto it = FACTORIAL_CACHE.find(n);
181 if (it != FACTORIAL_CACHE.end()) {
182 output = it->second;
183 return NO_ERROR;
184 }
185
186 // Find the largest key <= n
187 auto upper = FACTORIAL_CACHE.upper_bound(n); // First element with key > n
188 if (upper == FACTORIAL_CACHE.begin()) {
189 // Shouldn't happen since n >= 0 and cache has 0
190 return ERROR_INVALID_RANGE;
191 }
192 --upper; // Now upper->first <= n
193
194 int base = upper->first;
195 T result = upper->second;
196 for (int i = base + 1; i <= n; ++i)
197 result *= (T)i;
198
199 output = result;
200 return NO_ERROR;
201 }
202
204 template <typename T>
205 T factorial(int n) {
206 T output;
207 factorial(n, output);
208 return output;
209 }
210
211}
212
213#endif
#define ERROR_INVALID_RANGE
Definition clockwerkerrors.h:53
#define ERROR_DIVIDE_BY_ZERO
Error code in the case where some fool tried to divide by zero.
Definition clockwerkerrors.h:49
Class to propagate CR3BP dynamics in characteristic units.
Definition statistics.hpp:22
int Heaviside(T input, T &output, T center=0.0)
This function is the Heaviside step function (or more colloquially the unit step function)....
Definition functionutils.hpp:103
int clamp(T input, T min_val, T max_val, T &output)
This function will force a value to be between the defined minimum and maximum values....
Definition functionutils.hpp:166
const std::map< int, double > FACTORIAL_CACHE
Definition functionutils.hpp:39
int factorial(int n, T &output)
Computes the factorial of a non-negative integer. Currently the gamma function is not supported.
Definition functionutils.hpp:176
int satlin(T input, T &output, T limit=1.0)
This function is a saturated linear function that behaves like a linear function in the range of [0,...
Definition functionutils.hpp:57
int satlins(T input, T &output, T limit=1.0)
This function is a symmetric saturated linear function that behaves like a linear function in the ran...
Definition functionutils.hpp:73
int poslin(T input, T &output)
This function is a positive linear transfer function that behaves like a linear function only when x ...
Definition functionutils.hpp:88
int deadzone(T input, T bounds, T &output)
This function will supress all inputs within (±) the specified bounds. The deadzone function is NOT p...
Definition functionutils.hpp:149
int signum(T input, T &output)
This function is the signum function (typically denoted sgn). Described simply, it outputs the sign (...
Definition functionutils.hpp:117
const int NO_ERROR
Definition simlicense.cpp:30
int deadzoneTrimmed(T input, T bounds, T &output)
This function will supress all inputs within (±) the specified bounds. The trimmed deadzone function ...
Definition functionutils.hpp:132