Gradient descent: Difference between revisions

m
→‎{{header|REXX}}: added whitespace, aligned statements, added a comment.
(→‎{{header|zkl}}: added code)
m (→‎{{header|REXX}}: added whitespace, aligned statements, added a comment.)
Line 388:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gx: return (x.0-1)**2 * exp(- (x.1**2) ) + x.1 * (x.1 + 2) * exp(-2 * x.0**2)
gy: return (y.0-1)**2 * exp(- (y.1**2) ) + y.1 * (y.1 + 2) * exp(-2 * y.0**2)
/*──────────────────────────────────────────────────────────────────────────────────────*/
gradG: do j=0 for n; y.j= x.j /*copy X ──► Y */
end /*j*/
g0= gx()
do j=0 for n; y.j= y.j + h
z.j= (gy() - g0) / h
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 403:
call gradG
delG= 0
do j=0 for n; delG= delG + z.j**2
end /*j*/
delG= sqrt(delG)
b= alpha / delG
 
do while delG>tolerance
do j=0 for n; x.j= x.j - b*z.j
end /*j*/
h= h / 2
call gradG
delG= 0
do j=0 for n; delG= delG + z.j**2
end /*j*/
delG= sqrt(delG)
if delG=0 then return
b= alpha / delG
g1= gx()
if g1>g0 then alpha= alpha / 2
else g0= g1
end /*while*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
e: e= 2.7182818284590452353602874713526624977572470936999595749669676277240766303 || ,
535475945713821785; return e
/*──────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure; parse arg x; ix= x%1; if abs(x-ix)>.5 then ix= ix + sign(x); x= x - ix
z=1; _=1; w=z; do j=1; _= _*x/j; z= (z+_)/1; if z==w then leave; w= z; end
if z\==0 then z= z * e() ** ix; return z/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d= digits(); numeric digits; h= d+6
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ %2
do j=0 while h>9; m.j=h; h= h % 2 + 1; end /*j*/