Created
June 12, 2019 05:14
-
-
Save terjehaukaas/8cd743977f4e435e1733e0ea8ab34c1d to your computer and use it in GitHub Desktop.
conjugateGradientSearchDirection()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------ | |
# 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