Gradient descent: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
Line 376: Line 376:
{{out}}
{{out}}
<pre>'#(0.10760797905122492 -1.2232993981966753)</pre>
<pre>'#(0.10760797905122492 -1.2232993981966753)</pre>

=={{header|REXX}}==
<lang rexx>/*REXX pgm searches for minimum values of the bi─variate function (AKA steepest descent)*/
numeric digits length( e() ) % 2
tolerance= 0.0000006
alpha= 0.1
x.0= 0.1; x.1= -1; n= 2
say center(' testing for the steepest descent method ', 79, "═")
call steepestD
say 'The minimum is at: x[0]=' format(x.0,,4) " x[1]=" format(x.1,,4)
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
/*──────────────────────────────────────────────────────────────────────────────────────*/
steepestD: h= tolerance
g0= gx()
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
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*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
═══════════════════ testing for the steepest descent method ═══════════════════
The minimum is at: x[0]= 0.1062 x[1]= -1.2264
</pre>


=={{header|Scala}}==
=={{header|Scala}}==