-
Notifications
You must be signed in to change notification settings - Fork 0
/
logistic-function
executable file
·140 lines (127 loc) · 4.01 KB
/
logistic-function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
#########################################################
# The logistic function in GNU bash and bc
#########################################################
# Author: cgomesu
# Repo: https://github.com/cgomesu/bash-logistic-function
#########################################################
defaults () {
if [[ -z $DEBUG ]]; then DEBUG='false'; fi
if [[ -z "$x" || -z "$m" || -z "$L" || -z "$k" ]]; then err 'Missing mandatory parameters.'; fi
if [[ -z $s ]]; then s=20; fi
if [[ -z $S ]]; then S=2; fi
if [[ $S -gt $s ]]; then err 'Option -S must be lower or equal to -s.'; fi
}
err () {
if [[ $DEBUG = 'true' && -n "$1" ]]; then
echo '[logistic] There was an error: '"$1"
fi; exit 1
}
############################################################
# The logistic function, f(x), has the following parameters:
#
# x: value of a variable X
# m = x0: midpoint of X
# L: max value of f(x)
# k: growth rate
#
# And is given by the equation
#
# f(x) = L / {1 + e^[-k * (x - m)]},
#
# in which 'e' is Euler's constant.
#
# MORE INFO: https://en.wikipedia.org/wiki/logistic_function
############################################################
logistic () {
local EQUATION; local OUTPUT
EQUATION="output=($L)/(1+e(-($k)*(($x)-($m))))"
if ! OUTPUT=$(echo "scale=$s;$EQUATION;scale=$S;output/1" | bc -lq 2>/dev/null); then
err 'Unable to eval the equation.'
fi
echo "$OUTPUT"; return 0
}
usage () {
printf "\nUsage:\n %s -x VALUE -m VALUE -L VALUE -k VALUE [OPTIONS]\n" "$0"
printf "\n Logistic function:\n"
echo ' -x real* value of a variable X'
echo ' -m real* m = x0: Midpoint of X'
echo ' -L real* max value of f(x)'
echo ' -k real* growth rate'
printf "\n * acceptable inputs:\n"
echo ' integers: ..., -1, 0, 1, ...'
echo ' fractions: ..., -1/2, -.25, 0, .25, 1/2, ...'
echo ' positive exps: ..., "-(2^2)", 0, 2^2, ...'
echo ' natural exps: "e(VALUE)"'
echo ' natural logs: "l(VALUE)"'
echo ' square roots: "sqrt(VALUE)"'
echo ' sines: "s(VALUE)"'
echo ' cosines: "c(VALUE)"'
echo ' arctangents: "a(VALUE)"'
printf "\n Options:\n"
echo ' -d enable debug messages.'
echo ' -h show this help message.'
echo ' -s int # of decimals to compute f(x). default: 20.'
echo ' -S int # of decimals in the output. default: 2.'
printf "\n Example:\n"
echo ' logistic-function -x .5 -m 0 -L 1 -k 1 -s 10 -S 4'
printf "\nAuthor: cgomesu\nRepo: https://github.com/cgomesu/bash-logistic-function\n"
}
validate_integer () {
if [[ -z "$1" || ! "$1" =~ ^[0-9]+$ ]]; then return 1; else return 0; fi
}
validate_parameter () {
local REGEX_VALUE; local REGEX_MATLIB
REGEX_VALUE="\-{0,1}([0-9]+|[0-9]+(\.|\/\-{0,1}|\^)[0-9]+|\.[0-9]+)"
REGEX_MATLIB="\-{0,1}(e|l|sqrt|s|c|a)\($REGEX_VALUE\)"
if [[ -z "$1" || ! "$1" =~ ^($REGEX_VALUE|$REGEX_MATLIB)$ ]]; then
return 1
else
return 0
fi
}
while getopts 'hdx:m:L:k:s:S:' OPT; do
case ${OPT} in
h)
usage; exit 0
;;
d)
DEBUG='true'
;;
x)
x="$OPTARG"
if ! validate_parameter "$x"; then err 'Invalid value for -x.'; fi
;;
m)
m="$OPTARG"
if ! validate_parameter "$m"; then err 'Invalid value for -m.'; fi
;;
L)
L="$OPTARG"
if ! validate_parameter "$L"; then err 'Invalid value for -L.'; fi
;;
k)
k="$OPTARG"
if ! validate_parameter "$k"; then err 'Invalid value for -k.'; fi
;;
s)
s="$OPTARG"
if ! validate_integer "$s"; then err 'Invalid value for -s.'; fi
;;
S)
S="$OPTARG"
if ! validate_integer "$S"; then err 'Invalid value for -S.'; fi
;;
\?)
printf "\n!! INVALID OPTION !!\n"; usage; exit 1
;;
esac
done
if ! command 2>/dev/null; then
err 'Running command returns an error.'
elif [[ -z $(command -v bc 2>/dev/null) ]]; then
err 'The GNU basic calculator (bc) is not installed or cannot be found in this users PATH.'
fi
defaults
logistic
exit 0