Skip to content

Instantly share code, notes, and snippets.

@tswedish
Last active May 26, 2020 20:39
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 tswedish/d6fa88d4d139a85d944e16f56654d8d0 to your computer and use it in GitHub Desktop.
Save tswedish/d6fa88d4d139a85d944e16f56654d8d0 to your computer and use it in GitHub Desktop.
def super_complicated_function(x):
return x * sin(x+6.)**2. / 2. - 2. / 2.**x
# symbolic derivative by running the above in mathematica (wolfram alpha)
def d_super_complicated_function(x):
return 2**(1 - x) * math.log(2) + 0.5 * sin(x + 6)**2 + x * math.sin(x + 6) * math.cos(x + 6)
for x in [-1., 0.5, 3., 2.]:
print('Function Input: {}'.format(x))
print('Function Value: {}'.format(super_complicated_function(x)))
print('Function Symbolic Derivative: {}'.format(d_super_complicated_function(x)))
print(super_complicated_function(Dual(x, 1.)))
print('-'*32)
'''
# Printed Results
Function Input: -1.0
Function Value: -4.459767882269113
Function Symbolic Derivative: 3.504367159953579
< Dual value: -4.459767882269113, derivative: 3.504367159953579 >
--------------------------------
Function Input: 0.5
Function Value: -1.4026444100543694
Function Symbolic Derivative: 1.1084382073126584
< Dual value: -1.4026444100543694, derivative: 1.1084382073126582 >
--------------------------------
Function Input: 3.0
Function Value: 0.004762468816939924
Function Symbolic Derivative: -0.8682732520785479
< Dual value: 0.004762468816939924, derivative: -0.8682732520785477 >
--------------------------------
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment