Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terjehaukaas/8cd743977f4e435e1733e0ea8ab34c1d to your computer and use it in GitHub Desktop.
Save terjehaukaas/8cd743977f4e435e1733e0ea8ab34c1d to your computer and use it in GitHub Desktop.
conjugateGradientSearchDirection()
# ------------------------------------------------------------------------
# The following function is implemented in Python by Professor Terje Haukaas
# at the University of British Columbia in Vancouver, Canada. It is made
# freely available online at terje.civil.ubc.ca together with notes,
# examples, and additional Python code. Please be cautious when using
# this code; it may contain bugs and comes without any form of warranty.
# ------------------------------------------------------------------------
def conjugateGradientSearchDirection(gradient, previousGradient):
# Calculate the suared norm of the gradients
thisGradientNormSquared = (np.linalg.norm(gradient))**2
previousGradientNormSquared = (np.linalg.norm(previousGradient))**2
steepest = np.multiply(gradient, -1.0)
if previousGradientNormSquared == 0.0:
return steepest
else:
return steepest + np.multiply(previousGradient, -thisGradientNormSquared / previousGradientNormSquared)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment