Sujet : Re: Differentiable Forth
De : melahi_ahmed (at) *nospam* yahoo.fr (ahmed)
Groupes : comp.lang.forthDate : 26. Jul 2024, 15:07:03
Autres entêtes
Organisation : novaBBS
Message-ID : <168e154c647beaf323c56041756a6cf3@www.novabbs.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Rocksolid Light
In fact, dl^ has too many use cases.
in the last post I've given a definition for dl^, it works for
calculating the values (real part)
but it fails to get the eps part (that gives the derivative).
Here a new definition that corrects this problem:
: is_even_integer? ( f: a --) ( -- f)
fdup f>s dup s>f f=
swap 2 mod 0= and
;
: dl^ ( dl: a b -- a^b) \ a^b = exp(b*ln(a))
dldup dl>rl is_even_integer? 0=
dlover dl>rl f0< and if
dldrop dldrop
cr ." Can't be defined for Real numbers!"
exit
then
dldup dl>rl f0= if dldrop dldrop 1e 0e exit then
dldup dl>eps f0=
dlover f0= and
f0< and
dldup dl>rl is_even_integer? and
if
dlswap
dlnegate
dlln dl* dlexp
fdrop 0e
exit
then
dlover f0=
f0< and
dldup dl>rl is_even_integer? and
if
dlover
dlnegate
dlln dl* dlexp
dlswap dl>rl fln fnip
exit
then
dlover f0<>
f0< and
dldup dl>rl is_even_integer? and
if
dlswap
dlnegate
dlln dl* dlexp
exit
then
dlover dl>rl f0<> if dlswap dlln dl* dlexp exit then
dldrop dldrop 0e 0e
;
And here, some examples:
-1e f>dl 2e f>dl dl^ dl. 1. + eps 0. ok
-1e f>dl_d 2e f>dl dl^ dl. 1. + eps -2. ok
-1e f>dl 2e f>dl_d dl^ dl. 1. + eps NaN ok
-1e f>dl 2.2e f>dl dl^
Can't be defined for Real numbers! ok
-1.2e f>dl 2e f>dl dl^ dl. 1.44 + eps 0. ok
-1.2e f>dl_d 2e f>dl dl^ dl. 1.44 + eps -2.4 ok
-1.2e f>dl 2e f>dl_d dl^ dl. 1.44 + eps NaN ok
-1.2e f>dl 2.2e f>dl_d dl^
Can't be defined for Real numbers! ok
-1.2e f>dl -2.2e f>dl dl^
Can't be defined for Real numbers! ok
-1.2e f>dl -2e f>dl dl^ dl. 0.694444444444445 + eps 0. ok
-1.2e f>dl_d -2e f>dl dl^ dl. 0.694444444444445 + eps 1.15740740740741 ok
-1.2e f>dl -2e f>dl_d dl^ dl. 0.694444444444445 + eps NaN ok
-1.2e f>dl 0e f>dl_d dl^ dl. 1. + eps 0. ok
-1.2e f>dl 0e f>dl dl^ dl. 1. + eps 0. ok
-1.2e f>dl_d 0e f>dl dl^ dl. 1. + eps 0. ok
0e f>dl_d 0e f>dl dl^ dl. 1. + eps 0. ok
It is ok for the real part and the eps part.
It is not optimized.
Ahmed