-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfns.c
283 lines (276 loc) · 17.8 KB
/
cfns.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* cfns.c: Low-level C functions: *
* Action potential functions */
#include "cfns.h"
#define MAX_I 1000 /* Maximum index of lookup table, exclusive. */
#define MAX_T 9.0 /* Maximum input before saturation is assumed, exclusive. */
/* Lookup table for efficiently computing values along the *
* logistic curve using linear interpolation. */
static double sig_table[] = {
0.500000000000, 0.502249984813, 0.504499878504, 0.506749589967,
0.508999028126, 0.511248101947, 0.513496720456, 0.515744792754,
0.517992228029, 0.520238935572, 0.522484824792, 0.524729805230,
0.526973786575, 0.529216678675, 0.531458391556, 0.533698835433,
0.535937920724, 0.538175558069, 0.540411658337, 0.542646132645,
0.544878892374, 0.547109849174, 0.549338914988, 0.551566002060,
0.553791022949, 0.556013890545, 0.558234518078, 0.560452819137,
0.562668707680, 0.564882098044, 0.567092904965, 0.569301043586,
0.571506429468, 0.573708978607, 0.575908607447, 0.578105232884,
0.580298772289, 0.582489143512, 0.584676264895, 0.586860055289,
0.589040434059, 0.591217321096, 0.593390636833, 0.595560302250,
0.597726238890, 0.599888368864, 0.602046614867, 0.604200900183,
0.606351148700, 0.608497284916, 0.610639233949, 0.612776921550,
0.614910274107, 0.617039218659, 0.619163682899, 0.621283595191,
0.623398884570, 0.625509480754, 0.627615314153, 0.629716315876,
0.631812417736, 0.633903552262, 0.635989652703, 0.638070653035,
0.640146487969, 0.642217092957, 0.644282404200, 0.646342358649,
0.648396894018, 0.650445948783, 0.652489462193, 0.654527374271,
0.656559625822, 0.658586158437, 0.660606914494, 0.662621837170,
0.664630870438, 0.666633959073, 0.668631048660, 0.670622085589,
0.672607017068, 0.674585791117, 0.676558356579, 0.678524663117,
0.680484661217, 0.682438302193, 0.684385538188, 0.686326322175,
0.688260607959, 0.690188350177, 0.692109504302, 0.694024026643,
0.695931874344, 0.697833005387, 0.699727378592, 0.701614953615,
0.703495690952, 0.705369551933, 0.707236498730, 0.709096494346,
0.710949502625, 0.712795488243, 0.714634416711, 0.716466254372,
0.718290968400, 0.720108526800, 0.721918898404, 0.723722052869,
0.725517960677, 0.727306593130, 0.729087922349, 0.730861921274,
0.732628563655, 0.734387824054, 0.736139677842, 0.737884101192,
0.739621071079, 0.741350565276, 0.743072562350, 0.744787041656,
0.746493983338, 0.748193368319, 0.749885178302, 0.751569395764,
0.753246003949, 0.754914986868, 0.756576329289, 0.758230016738,
0.759876035491, 0.761514372567, 0.763145015727, 0.764767953467,
0.766383175011, 0.767990670310, 0.769590430031, 0.771182445556,
0.772766708973, 0.774343213074, 0.775911951343, 0.777472917959,
0.779026107780, 0.780571516345, 0.782109139864, 0.783638975211,
0.785161019922, 0.786675272184, 0.788181730830, 0.789680395335,
0.791171265805, 0.792654342975, 0.794129628199, 0.795597123445,
0.797056831289, 0.798508754905, 0.799952898062, 0.801389265115,
0.802817860998, 0.804238691220, 0.805651761854, 0.807057079532,
0.808454651439, 0.809844485303, 0.811226589392, 0.812600972504,
0.813967643962, 0.815326613603, 0.816677891777, 0.818021489335,
0.819357417623, 0.820685688477, 0.822006314214, 0.823319307624,
0.824624681966, 0.825922450958, 0.827212628772, 0.828495230025,
0.829770269772, 0.831037763504, 0.832297727131, 0.833550176986,
0.834795129809, 0.836032602747, 0.837262613340, 0.838485179522,
0.839700319607, 0.840908052286, 0.842108396619, 0.843301372029,
0.844486998293, 0.845665295539, 0.846836284235, 0.847999985184,
0.849156419519, 0.850305608693, 0.851447574476, 0.852582338946,
0.853709924483, 0.854830353761, 0.855943649746, 0.857049835684,
0.858148935100, 0.859240971785, 0.860325969797, 0.861403953451,
0.862474947311, 0.863538976187, 0.864596065130, 0.865646239420,
0.866689524567, 0.867725946299, 0.868755530561, 0.869778303507,
0.870794291493, 0.871803521072, 0.872806018990, 0.873801812179,
0.874790927751, 0.875773392992, 0.876749235359, 0.877718482471,
0.878681162108, 0.879637302201, 0.880586930830, 0.881530076218,
0.882466766723, 0.883397030839, 0.884320897185, 0.885238394503,
0.886149551653, 0.887054397606, 0.887952961443, 0.888845272345,
0.889731359594, 0.890611252564, 0.891484980718, 0.892352573605,
0.893214060851, 0.894069472160, 0.894918837307, 0.895762186133,
0.896599548542, 0.897430954496, 0.898256434011, 0.899076017156,
0.899889734042, 0.900697614825, 0.901499689699, 0.902295988892,
0.903086542662, 0.903871381295, 0.904650535101, 0.905424034408,
0.906191909560, 0.906954190915, 0.907710908840, 0.908462093707,
0.909207775889, 0.909947985761, 0.910682753693, 0.911412110045,
0.912136085171, 0.912854709407, 0.913568013075, 0.914276026478,
0.914978779894, 0.915676303579, 0.916368627756, 0.917055782623,
0.917737798340, 0.918414705031, 0.919086532785, 0.919753311644,
0.920415071610, 0.921071842637, 0.921723654630, 0.922370537444,
0.923012520878, 0.923649634678, 0.924281908530, 0.924909372061,
0.925532054834, 0.926149986349, 0.926763196038, 0.927371713266,
0.927975567327, 0.928574787442, 0.929169402759, 0.929759442347,
0.930344935201, 0.930925910233, 0.931502396276, 0.932074422078,
0.932642016304, 0.933205207533, 0.933764024253, 0.934318494867,
0.934868647685, 0.935414510925, 0.935956112710, 0.936493481072,
0.937026643943, 0.937555629159, 0.938080464457, 0.938601177474,
0.939117795747, 0.939630346708, 0.940138857688, 0.940643355913,
0.941143868503, 0.941640422471, 0.942133044724, 0.942621762059,
0.943106601165, 0.943587588620, 0.944064750891, 0.944538114332,
0.945007705188, 0.945473549585, 0.945935673539, 0.946394102950,
0.946848863602, 0.947299981162, 0.947747481183, 0.948191389096,
0.948631730219, 0.949068529747, 0.949501812759, 0.949931604212,
0.950357928946, 0.950780811676, 0.951200277001, 0.951616349395,
0.952029053212, 0.952438412683, 0.952844451917, 0.953247194900,
0.953646665497, 0.954042887445, 0.954435884363, 0.954825679741,
0.955212296950, 0.955595759233, 0.955976089711, 0.956353311379,
0.956727447108, 0.957098519643, 0.957466551607, 0.957831565496,
0.958193583681, 0.958552628408, 0.958908721800, 0.959261885851,
0.959612142433, 0.959959513292, 0.960304020049, 0.960645684201,
0.960984527117, 0.961320570046, 0.961653834107, 0.961984340298,
0.962312109491, 0.962637162435, 0.962959519752, 0.963279201943,
0.963596229382, 0.963910622322, 0.964222400892, 0.964531585095,
0.964838194815, 0.965142249809, 0.965443769714, 0.965742774043,
0.966039282189, 0.966333313420, 0.966624886886, 0.966914021611,
0.967200736503, 0.967485050345, 0.967766981803, 0.968046549420,
0.968323771621, 0.968598666711, 0.968871252875, 0.969141548181,
0.969409570578, 0.969675337896, 0.969938867848, 0.970200178031,
0.970459285922, 0.970716208884, 0.970970964164, 0.971223568891,
0.971474040082, 0.971722394637, 0.971968649342, 0.972212820868,
0.972454925775, 0.972694980507, 0.972933001398, 0.973169004667,
0.973403006423, 0.973635022664, 0.973865069276, 0.974093162034,
0.974319316606, 0.974543548548, 0.974765873307, 0.974986306223,
0.975204862526, 0.975421557341, 0.975636405685, 0.975849422466,
0.976060622490, 0.976270020455, 0.976477630954, 0.976683468477,
0.976887547408, 0.977089882029, 0.977290486517, 0.977489374950,
0.977686561301, 0.977882059442, 0.978075883145, 0.978268046082,
0.978458561822, 0.978647443839, 0.978834705505, 0.979020360094,
0.979204420784, 0.979386900653, 0.979567812686, 0.979747169768,
0.979924984689, 0.980101270147, 0.980276038741, 0.980449302979,
0.980621075274, 0.980791367947, 0.980960193224, 0.981127563242,
0.981293490046, 0.981457985588, 0.981621061731, 0.981782730249,
0.981943002826, 0.982101891056, 0.982259406446, 0.982415560416,
0.982570364296, 0.982723829333, 0.982875966684, 0.983026787424,
0.983176302540, 0.983324522936, 0.983471459432, 0.983617122764,
0.983761523584, 0.983904672463, 0.984046579890, 0.984187256271,
0.984326711934, 0.984464957123, 0.984602002005, 0.984737856666,
0.984872531114, 0.985006035277, 0.985138379008, 0.985269572080,
0.985399624191, 0.985528544961, 0.985656343937, 0.985783030586,
0.985908614305, 0.986033104414, 0.986156510161, 0.986278840717,
0.986400105185, 0.986520312593, 0.986639471897, 0.986757591982,
0.986874681663, 0.986990749683, 0.987105804718, 0.987219855370,
0.987332910177, 0.987444977605, 0.987556066054, 0.987666183855,
0.987775339274, 0.987883540508, 0.987990795690, 0.988097112886,
0.988202500097, 0.988306965261, 0.988410516249, 0.988513160870,
0.988614906870, 0.988715761931, 0.988815733673, 0.988914829653,
0.989013057369, 0.989110424257, 0.989206937690, 0.989302604983,
0.989397433392, 0.989491430111, 0.989584602278, 0.989676956970,
0.989768501209, 0.989859241956, 0.989949186117, 0.990038340540,
0.990126712018, 0.990214307287, 0.990301133028, 0.990387195867,
0.990472502376, 0.990557059070, 0.990640872414, 0.990723948816,
0.990806294633, 0.990887916169, 0.990968819676, 0.991049011353,
0.991128497349, 0.991207283760, 0.991285376635, 0.991362781967,
0.991439505705, 0.991515553745, 0.991590931934, 0.991665646071,
0.991739701907, 0.991813105144, 0.991885861437, 0.991957976393,
0.992029455573, 0.992100304491, 0.992170528614, 0.992240133364,
0.992309124118, 0.992377506208, 0.992445284918, 0.992512465492,
0.992579053127, 0.992645052978, 0.992710470156, 0.992775309727,
0.992839576718, 0.992903276110, 0.992966412845, 0.993028991821,
0.993091017895, 0.993152495885, 0.993213430565, 0.993273826671,
0.993333688898, 0.993393021901, 0.993451830297, 0.993510118662,
0.993567891533, 0.993625153411, 0.993681908756, 0.993738161993,
0.993793917505, 0.993849179642, 0.993903952716, 0.993958241000,
0.994012048733, 0.994065380117, 0.994118239319, 0.994170630469,
0.994222557662, 0.994274024959, 0.994325036387, 0.994375595935,
0.994425707563, 0.994475375192, 0.994524602714, 0.994573393984,
0.994621752826, 0.994669683031, 0.994717188358, 0.994764272532,
0.994810939247, 0.994857192167, 0.994903034921, 0.994948471110,
0.994993504302, 0.995038138037, 0.995082375821, 0.995126221132,
0.995169677418, 0.995212748096, 0.995255436556, 0.995297746157,
0.995339680229, 0.995381242075, 0.995422434966, 0.995463262148,
0.995503726839, 0.995543832227, 0.995583581475, 0.995622977715,
0.995662024057, 0.995700723580, 0.995739079338, 0.995777094359,
0.995814771643, 0.995852114166, 0.995889124878, 0.995925806701,
0.995962162536, 0.995998195255, 0.996033907707, 0.996069302716,
0.996104383080, 0.996139151576, 0.996173610954, 0.996207763942,
0.996241613241, 0.996275161534, 0.996308411476, 0.996341365701,
0.996374026819, 0.996406397419, 0.996438480065, 0.996470277302,
0.996501791649, 0.996533025607, 0.996563981652, 0.996594662240,
0.996625069806, 0.996655206762, 0.996685075501, 0.996714678393,
0.996744017789, 0.996773096020, 0.996801915395, 0.996830478204,
0.996858786715, 0.996886843179, 0.996914649827, 0.996942208867,
0.996969522493, 0.996996592875, 0.997023422167, 0.997050012504,
0.997076366001, 0.997102484755, 0.997128370844, 0.997154026331,
0.997179453256, 0.997204653645, 0.997229629505, 0.997254382825,
0.997278915577, 0.997303229716, 0.997327327179, 0.997351209887,
0.997374879743, 0.997398338635, 0.997421588433, 0.997444630991,
0.997467468147, 0.997490101722, 0.997512533522, 0.997534765338,
0.997556798942, 0.997578636094, 0.997600278537, 0.997621727998,
0.997642986190, 0.997664054811, 0.997684935542, 0.997705630053,
0.997726139996, 0.997746467009, 0.997766612717, 0.997786578730,
0.997806366643, 0.997825978039, 0.997845414484, 0.997864677533,
0.997883768727, 0.997902689591, 0.997921441640, 0.997940026372,
0.997958445275, 0.997976699823, 0.997994791476, 0.998012721682,
0.998030491875, 0.998048103479, 0.998065557902, 0.998082856543,
0.998100000786, 0.998116992004, 0.998133831559, 0.998150520797,
0.998167061058, 0.998183453664, 0.998199699930, 0.998215801158,
0.998231758637, 0.998247573647, 0.998263247454, 0.998278781316,
0.998294176477, 0.998309434172, 0.998324555624, 0.998339542045,
0.998354394637, 0.998369114592, 0.998383703090, 0.998398161302,
0.998412490386, 0.998426691493, 0.998440765762, 0.998454714322,
0.998468538293, 0.998482238784, 0.998495816895, 0.998509273715,
0.998522610326, 0.998535827797, 0.998548927189, 0.998561909555,
0.998574775937, 0.998587527369, 0.998600164873, 0.998612689466,
0.998625102153, 0.998637403932, 0.998649595790, 0.998661678708,
0.998673653656, 0.998685521596, 0.998697283482, 0.998708940260,
0.998720492866, 0.998731942228, 0.998743289267, 0.998754534896,
0.998765680018, 0.998776725529, 0.998787672318, 0.998798521264,
0.998809273241, 0.998819929112, 0.998830489735, 0.998840955959,
0.998851328626, 0.998861608571, 0.998871796619, 0.998881893592,
0.998891900301, 0.998901817552, 0.998911646142, 0.998921386863,
0.998931040498, 0.998940607824, 0.998950089611, 0.998959486622,
0.998968799614, 0.998978029337, 0.998987176533, 0.998996241939,
0.999005226285, 0.999014130294, 0.999022954683, 0.999031700162,
0.999040367437, 0.999048957204, 0.999057470156, 0.999065906978,
0.999074268350, 0.999082554946, 0.999090767432, 0.999098906471,
0.999106972718, 0.999114966823, 0.999122889430, 0.999130741177,
0.999138522697, 0.999146234618, 0.999153877560, 0.999161452140,
0.999168958967, 0.999176398648, 0.999183771781, 0.999191078961,
0.999198320776, 0.999205497812, 0.999212610646, 0.999219659851,
0.999226645996, 0.999233569645, 0.999240431354, 0.999247231679,
0.999253971166, 0.999260650360, 0.999267269799, 0.999273830017,
0.999280331542, 0.999286774900, 0.999293160610, 0.999299489187,
0.999305761141, 0.999311976979, 0.999318137201, 0.999324242305,
0.999330292784, 0.999336289124, 0.999342231811, 0.999348121323,
0.999353958136, 0.999359742720, 0.999365475543, 0.999371157067,
0.999376787750, 0.999382368047, 0.999387898408, 0.999393379279,
0.999398811104, 0.999404194319, 0.999409529361, 0.999414816658,
0.999420056638, 0.999425249725, 0.999430396336, 0.999435496889,
0.999440551793, 0.999445561458, 0.999450526288, 0.999455446683,
0.999460323042, 0.999465155757, 0.999469945218, 0.999474691814,
0.999479395926, 0.999484057935, 0.999488678216, 0.999493257144,
0.999497795088, 0.999502292415, 0.999506749487, 0.999511166664,
0.999515544304, 0.999519882759, 0.999524182381, 0.999528443516,
0.999532666509, 0.999536851701, 0.999540999430, 0.999545110030,
0.999549183835, 0.999553221172, 0.999557222369, 0.999561187748,
0.999565117630, 0.999569012332, 0.999572872169, 0.999576697452,
0.999580488492, 0.999584245593, 0.999587969061, 0.999591659194,
0.999595316293, 0.999598940651, 0.999602532563, 0.999606092318,
0.999609620204, 0.999613116506, 0.999616581506, 0.999620015485,
0.999623418720, 0.999626791487, 0.999630134057, 0.999633446701,
0.999636729687, 0.999639983279, 0.999643207742, 0.999646403335,
0.999649570317, 0.999652708944, 0.999655819469, 0.999658902145,
0.999661957219, 0.999664984940, 0.999667985551, 0.999670959296,
0.999673906415, 0.999676827146, 0.999679721725, 0.999682590386,
0.999685433361, 0.999688250880, 0.999691043172, 0.999693810460,
0.999696552970, 0.999699270923, 0.999701964538, 0.999704634035,
0.999707279627, 0.999709901530, 0.999712499955, 0.999715075113,
0.999717627211, 0.999720156456, 0.999722663053, 0.999725147204,
0.999727609110, 0.999730048970, 0.999732466982, 0.999734863341,
0.999737238241, 0.999739591874, 0.999741924430, 0.999744236098,
0.999746527065, 0.999748797516, 0.999751047635, 0.999753277603,
0.999755487602, 0.999757677809, 0.999759848403, 0.999761999558,
0.999764131449, 0.999766244248, 0.999768338126, 0.999770413252,
0.999772469794, 0.999774507919, 0.999776527791, 0.999778529574,
0.999780513430, 0.999782479519, 0.999784428000, 0.999786359030,
0.999788272767, 0.999790169365, 0.999792048977, 0.999793911755,
0.999795757851, 0.999797587413, 0.999799400589, 0.999801197527,
0.999802978371, 0.999804743265, 0.999806492353, 0.999808225776,
0.999809943673, 0.999811646185, 0.999813333449, 0.999815005601,
0.999816662777, 0.999818305111, 0.999819932735, 0.999821545782,
0.999823144381, 0.999824728663, 0.999826298755, 0.999827854784,
0.999829396877, 0.999830925158, 0.999832439751, 0.999833940778,
0.999835428361, 0.999836902620, 0.999838363674, 0.999839811643,
0.999841246642, 0.999842668788, 0.999844078196, 0.999845474981,
0.999846859254, 0.999848231129, 0.999849590716, 0.999850938125,
0.999852273466, 0.999853596846, 0.999854908373, 0.999856208152,
0.999857496289, 0.999858772888, 0.999860038052, 0.999861291884,
0.999862534485, 0.999863765957, 0.999864986397, 0.999866195906,
0.999867394581, 0.999868582519, 0.999869759817, 0.999870926569,
0.999872082870, 0.999873228814, 0.999874364493, 0.999875489999,
};
/* Auxiliary function for computing the logistic function with positive t. */
static double sig_aux(double t) {
double i = t * (double)MAX_I / MAX_T;
unsigned int i0 = (unsigned int)i;
if (i0 >= MAX_I - 1)
return 0.9999; /* saturated output */
double s0 = sig_table[i0];
return s0 + (sig_table[i0 + 1] - s0) * (i - (double)i0);
}
/* _sig: logistic function. */
double _sig(double t) {
return t >= 0.0 ? sig_aux(t) : 1.0 - sig_aux(-t);
}
/* _bin: unit step function. */
double _bin(double t) {
return t > 0.0 ? 1.0 : 0.0;
}