Gradient descent: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Now uses new core library method.)
Line 1,104: Line 1,104:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Math
<lang ecmascript>import "/fmt" for Fmt
import "/fmt" for Fmt


// Function for which minimum is to be found.
// Function for which minimum is to be found.
var g = Fn.new { |x|
var g = Fn.new { |x|
return (x[0]-1)*(x[0]-1)*
return (x[0]-1)*(x[0]-1)*
Math.exp(-x[1]*x[1]) + x[1]*(x[1]+2)*
(-x[1]*x[1]).exp + x[1]*(x[1]+2)*
Math.exp(-2*x[0]*x[0])
(-2*x[0]*x[0]).exp
}
}


Line 1,120: Line 1,118:
var x = p[0]
var x = p[0]
var y = p[1]
var y = p[1]
return [2*(x-1)*Math.exp(-y*y) - 4*x*Math.exp(-2*x*x)*y*(y+2),
return [2*(x-1)*(-y*y).exp - 4*x*(-2*x*x).exp*y*(y+2),
-2*(x-1)*(x-1)*y*Math.exp(-y*y) + Math.exp(-2*x*x)*(y+2) + Math.exp(-2*x*x)*y]
-2*(x-1)*(x-1)*y*(-y*y).exp + (-2*x*x).exp*(y+2) + (-2*x*x).exp*y]
}
}