PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/example/fsl/logistic.forth

https://github.com/crcx/retroforth
Forth | 63 lines | 48 code | 15 blank | 0 comment | 1 complexity | 10b5846cbb0d7969a334faa6d4cf7300 MD5 | raw file
Possible License(s): 0BSD, MIT
  1. \ logistic The Logistic function and its first derivative
  2. \ logistic = Exp( c + a x ) / (1 + Exp( c + a x ) )
  3. \ d_logistic = a Exp( c + a x ) / (1 + Exp( c + a x ) )^2
  4. \ Forth Scientific Library Algorithm #4
  5. \ This code conforms with ANS requiring:
  6. \ 1. The Floating-Point word set
  7. \
  8. \ (c) Copyright 1994 Everett F. Carter. Permission is granted by the
  9. \ author to use this software for any application provided this
  10. \ copyright notice is preserved.
  11. cr .( Logistic V1.2 17 October 1994 EFC )
  12. : logistic ( --, f: x a c -- z )
  13. FROT FROT
  14. F* F+
  15. FEXP
  16. FDUP 1.0e0 F+
  17. F/
  18. ;
  19. : d_logistic ( -- , f: x a c -- z )
  20. FSWAP FROT
  21. FOVER F* FROT F+
  22. FEXP
  23. FDUP 1.0e0 F+ FDUP F*
  24. F/ F*
  25. ;
  26. \ Examples % 1.0 % 1.0 % 0.0 logistic f. 0.731059
  27. \ % 3.2 % 1.5 % 0.2 logistic f. 0.993307
  28. \ % 3.2 % 1.5 % 0.2 d_logistic f. 0.00997209
  29. # The Code
  30. ~~~
  31. :logistic (-,f:xac-z)
  32. f:rot f:rot
  33. f:* f:+
  34. f:E f:swap f:power
  35. f:dup .1.0e0 f:+
  36. f:/ ;
  37. :d_logistic (-,f:xac-z)
  38. f:swap f:rot
  39. f:over f:* f:rot f:+
  40. f:E f:swap f:power
  41. f:dup .1.0e0 f:+ f:dup f:*
  42. f:/ f:* ;
  43. ~~~
  44. # Tests
  45. ```
  46. .1.0 .1.0 .0.0 logistic f:put nl
  47. .3.2 .1.5 .0.2 logistic f:put nl
  48. ```