15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SciPy Reference Guide, Release 0.8.dev<br />

except:<br />

sign = 1.0<br />

x = d[0]<br />

y = d[1]<br />

return sign*(2*x*y + 2*x - x**2 - 2*y**2)<br />

def testfunc_deriv(d,*args):<br />

""" This is the derivative of testfunc, returning a numpy array<br />

representing df/dx and df/dy<br />

"""<br />

try:<br />

sign = args[0]<br />

except:<br />

sign = 1.0<br />

x = d[0]<br />

y = d[1]<br />

dfdx = sign*(-2*x + 2*y + 2)<br />

dfdy = sign*(2*x - 4*y)<br />

return array([ dfdx, dfdy ],float)<br />

from time import time<br />

print ’\n\n’<br />

print "Unbounded optimization. Derivatives approximated."<br />

t0 = time()<br />

x = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,), iprint=2, full_output=1)<br />

print "Elapsed time:", 1000*(time()-t0), "ms"<br />

print "Results",x<br />

print "\n\n"<br />

print "Unbounded optimization. Derivatives provided."<br />

t0 = time()<br />

x = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,), iprint=2, full_output=1)<br />

print "Elapsed time:", 1000*(time()-t0), "ms"<br />

print "Results",x<br />

print "\n\n"<br />

print "Bound optimization. Derivatives approximated."<br />

t0 = time()<br />

x = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,),<br />

eqcons=[lambda x, y: x[0]-x[1] ], iprint=2, full_output=1)<br />

print "Elapsed time:", 1000*(time()-t0), "ms"<br />

print "Results",x<br />

print "\n\n"<br />

print "Bound optimization (equality constraints). Derivatives provided."<br />

t0 = time()<br />

x = fmin_slsqp(testfunc, [-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),<br />

eqcons=[lambda x, y: x[0]-x[1] ], iprint=2, full_output=1)<br />

print "Elapsed time:", 1000*(time()-t0), "ms"<br />

print "Results",x<br />

print "\n\n"<br />

print "Bound optimization (equality and inequality constraints)."<br />

print "Derivatives provided."<br />

22 Chapter 1. SciPy Tutorial

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!