Skip to content
Snippets Groups Projects
Commit 26d62541 authored by iliya's avatar iliya
Browse files

feat: dual number lib working with multivariable functions, able to compute gradients

parent f92aa41e
No related branches found
No related tags found
No related merge requests found
...@@ -30,23 +30,57 @@ import numpy as np ...@@ -30,23 +30,57 @@ import numpy as np
# g(x) = x*x*exp( ln( x*x*x*x + 2 ) * sin(x) + 1 ) # g(x) = x*x*exp( ln( x*x*x*x + 2 ) * sin(x) + 1 )
def g(x: Dual_Number) -> float: # def g(x: Dual_Number) -> float:
return (x * x * Dual_Number.exp(Dual_Number.log(x * x * x * x + 2) * Dual_Number.sin(x) + 1)).d # return (x * x * Dual_Number.exp(Dual_Number.log(x * x * x * x + 2) * Dual_Number.sin(x) + 1)).d
def h(x: Dual_Number) -> float: # def h(x: Dual_Number) -> float:
return g(2 * x - 0) # return g(2 * x - 0)
# def dgdx(x: float) -> float: # def dgdx(x: float) -> float:
# return g(Dual_Number(x, 1)) # return g(Dual_Number(x, 1))
def dhdx(x: float) -> float: # def dhdx(x: float) -> float:
return h(Dual_Number(x, 1)) # return h(Dual_Number(x, 1))
def P(x: Dual_Number, y: Dual_Number) -> float:
return (3 * (x * x) * y + 2 * x * (y * y) - x * y + 2 * x - 1).d
def delPdelx(x: float, y: float) -> float:
return P(Dual_Number(x, 1), y)
def delPdely(x: float, y: float) -> float:
return P(x, Dual_Number(y, 1))
def Z(x: Dual_Number, y: Dual_Number, z: Dual_Number) -> float:
return (x * y * z + x * x + y * y).d
def delZdelx(x: float, y: float, z: float) -> float:
return Z(Dual_Number(x, 1), y, z)
def delZdely(x: float, y: float, z: float) -> float:
return Z(x, Dual_Number(y, 1), z)
def delZdelz(x: float, y: float, z: float) -> float:
return Z(x, y, Dual_Number(z, 1))
if __name__ == "__main__": if __name__ == "__main__":
print(delPdelx(1, 1))
print(delPdely(1, 1))
print()
print(delZdelx(0, 1, -1))
print(delZdely(0, 1, -1))
print(delZdelz(0, 1, -1))
# Example for f(x) # Example for f(x)
# start = -6 # start = -6
# stop = -1 # stop = -1
...@@ -59,8 +93,8 @@ if __name__ == "__main__": ...@@ -59,8 +93,8 @@ if __name__ == "__main__":
# start = -5 # start = -5
# stop = 9 # stop = 9
start = -1 # start = -1
stop = -0.8 # stop = -0.8
# start = 3.5 # start = 3.5
# stop = 5.5 # stop = 5.5
...@@ -79,5 +113,5 @@ if __name__ == "__main__": ...@@ -79,5 +113,5 @@ if __name__ == "__main__":
# print(f"Iter bisection = {iter_bisection(start, stop, dgdx, False)}") # print(f"Iter bisection = {iter_bisection(start, stop, dgdx, False)}")
# #
# print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dhdx, False)}") # print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dhdx, False)}")
print("----------------------------------------------") # print("----------------------------------------------")
print(f"Iter bisection = {iter_bisection(start, stop, dhdx, True)}") # print(f"Iter bisection = {iter_bisection(start, stop, dhdx, True)}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment