<jupyter_start><jupyter_text>Partial Derivatives and Symbolic Computation $$f(x, y)=\frac{1}{3^{-x^2-y^2}+1}$$$$\frac{\partial f(x, y)}{\partial x}=\frac{2x\ln \left(3\right)\cdot \:3^{-x^2-y^2}}{\left(3^{-x^2-y^2}+1\right)^2}$$$$\frac{\partial f(x, y)}{\partial y}=\frac{2y\ln \left(3\right)\cdot \:3^{-y^2-x^2}}{\left(3^{-x^2-y^2}+1\right)^2}$$<jupyter_code>def f(x, y):
    return 1/(3**(-(x**2)-(y**2)) + 1)

def fpx(x, y): 
    den = 2 * x * np.log(3) * 3**(-x**2-y**2)
    num = (3**(-x**2-y**2)+1)**2
    return den/num

def fpy(x, y):
    den = 2 * y * np.log(3) * 3**(-x**2-y**2)
    num = (3**(-x**2-y**2)+1)**2
    return den/num

# Derivatives here are calculated with Symbolab. It's the same result as with sympy.
a, b = symbols('x, y') # Variable 'a' will now represent 'x' and 'b' will represent 'y'.

f(a, b) # Our cost function.
diff(f(a,b),a) # Differentiates f(x,y) with respect to 'x'.
diff(f(a,b),b) # Differentiates f(x,y) with respect to 'y'.
print(diff(f(a,b),a))
# Cost can be calculated with both sympy and directly.
# In this function we have to differentiate with respect to both 'x' and 'y'.
cost, cost2, dfx, dfy = (
    f(1.8,1.0),
    f(a,b).evalf(subs={a:1.8,b:1.0}),
    diff(f(a,b),a).evalf(subs={a:1.8,b:1.0}), # Differentiates f(x,y) with respect to 'x'.
    diff(f(a,b),b).evalf(subs={a:1.8,b:1.0}) # Differentiates f(x,y) with respect to 'y'.
)
cost, cost2, dfx, dfy
print("Value of f(x,y) at (x=1.8, y=1.0):\t", cost)
print("Value of df(x,y)/dx at (x=1.8, y=1.0):\t", dfx)
print("Value of df(x,y)/dy at (x=1.8, y=1.0):\t", dfy)<jupyter_output>Value of f(x,y) at (x=1.8, y=1.0):	 0.9906047940325824
Value of df(x,y)/dx at (x=1.8, y=1.0):	 0.0368089716197505
Value of df(x,y)/dy at (x=1.8, y=1.0):	 0.0204494286776392
{'fraction_non_alphanumeric': 0.26267806267806265, 'fraction_numerical': 0.08148148148148149, 'mean_word_length': 4.0315186246418335, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '477460', 'license': 'MIT', 'n_tokens_mistral': 892, 'n_tokens_neox': 832, 'n_words': 182, 'path': 'Section_04/Example_04_(05-08)/06-SymPy_derivatives.ipynb', 'repo_name': 'ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp', 'size': '1755'}