Skip to content

Commit

Permalink
Merge pull request #24 from argriffing/master
Browse files Browse the repository at this point in the history
a new test and corresponding bugfix for gammaln, and commented-out ipopt stuff
  • Loading branch information
b45ch1 committed Dec 9, 2012
2 parents cfa4bec + 2f30563 commit a14ee75
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
8 changes: 7 additions & 1 deletion algopy/tracer/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ def f(x1, x2):
else:
x_list = [x]

utpm_x_list = [algopy.UTPM(numpy.asarray(xi).reshape((1,1) + numpy.shape(xi))) for xi in x_list]
utpm_x_list = []
for xi in x_list:
element = numpy.asarray(xi).reshape((1,1) + numpy.shape(xi))
utpm_x_list.append(algopy.UTPM(element))

self.pushforward(utpm_x_list)

Expand Down Expand Up @@ -1017,6 +1020,9 @@ def __rdiv__(self, lhs):
lhs = self.__class__.totype(lhs)
return lhs/self

__truediv__ = __div__
__rtruediv__ = __rdiv__


# #########################################################
# numpy functions
Expand Down
13 changes: 13 additions & 0 deletions algopy/utpm/tests/test_utpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,19 @@ def test_gammaln(self):
b = UTPM.gammaln(x + 1) - UTPM.log(x)
assert_allclose(a.data, b.data)

def test_gammaln_pullback(self):
D,P = 2,1

# forward
x = UTPM(numpy.random.random((D,P)))
y = UTPM.gammaln(x)

# reverse
ybar = UTPM(numpy.random.random((D,P)))
xbar = UTPM.pb_gammaln(ybar, x, y)

assert_array_almost_equal(ybar.data[0]*y.data[1], xbar.data[0]*x.data[1])

def test_hyperu(self):
D,P,N,M = 5,1,3,3

Expand Down
2 changes: 1 addition & 1 deletion algopy/utpm/utpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def pb_gammaln(cls, ybar, x, y, out=None):
xbar, = out

cls._pb_gammaln(ybar.data, x.data, y.data, out = xbar.data)
return out
return xbar

@classmethod
def erf(cls, x):
Expand Down
39 changes: 39 additions & 0 deletions documentation/sphinx/examples/minimization/minhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
This is a helper module for the minimization examples.
"""

#FIXME: the pyipopt stuff has been commented out for now;
#FIXME: update this code to use a better python ipopt interface when available
# https://github.com/xuy/pyipopt
# http://gitview.danfis.cz/pipopt
# https://bitbucket.org/amitibo
# https://github.com/casadi/casadi

import functools

import numpy
import scipy.optimize
import algopy
import numdifftools
#import pyipopt

# Suppress log spam from pyipopt.
# But ipopt itself will stil spam...
#pyipopt.set_loglevel(0)


def eval_grad(f, theta):
Expand All @@ -18,6 +30,8 @@ def eval_hess(f, theta):
theta = algopy.UTPM.init_hessian(theta)
return algopy.UTPM.extract_hessian(len(theta), f(theta))



def show_local_curvature(f, g, h, x0):
print 'point:'
print x0
Expand Down Expand Up @@ -169,6 +183,31 @@ def do_searches(f, g, h, x0):
print results
print

#print 'strategy:', 'ipopt'
#print 'options:', 'default'
#print 'gradient:', 'autodiff'
#print 'hessian:', 'autodiff'
#results = pyipopt.fmin_unconstrained(
#f,
#x0,
#fprime=g,
#fhess=h,
#)
#print results
#print

#print 'strategy:', 'ipopt'
#print 'options:', 'default'
#print 'gradient:', 'autodiff'
#print 'hessian:', 'finite differences'
#results = pyipopt.fmin_unconstrained(
#f,
#x0,
#fprime=g,
#)
#print results
#print


def show_minimization_results(f, target_in, easy_init_in, hard_init_in):
"""
Expand Down

0 comments on commit a14ee75

Please sign in to comment.