Skip to content

Instantly share code, notes, and snippets.

@kayhman
Last active November 9, 2020 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kayhman/71115860a1b3b0f6a0ce3e6de4d70fcd to your computer and use it in GitHub Desktop.
Save kayhman/71115860a1b3b0f6a0ce3e6de4d70fcd to your computer and use it in GitHub Desktop.
class DualFloat:
def __init__(self, x=0, x_p=0):
self.x = x
self.x_p = x_p
def __str__(self):
return "DF({0}, {1})".format(self.x, self.x_p)
def convert(self, x):
if isinstance(x, int) or isinstance(x, float):
return DualFloat(x, 0.0)
elif isinstance(x, DualFloat):
return x
raise Exception(f"Type {type(x)} not supported")
def __add__(self, other):
other = self.convert(other)
x = self.x + other.x
x_p = self.x_p + other.x_p
return DualFloat(x, x_p)
def __sub__(self, other):
other = self.convert(other)
x = self.x - other.x
x_p = self.x_p - other.x_p
return DualFloat(x, x_p)
def __mul__(self, other):
other = self.convert(other)
x = self.x * other.x
x_p = self.x_p * other.x + self.x * other.x_p
return DualFloat(x, x_p)
def __truediv__(self, other):
other = self.convert(other)
x = self.x / other.x
x_p = (self.x_p * other.x - self.x * other.x_p) / other.x**2
return DualFloat(x, x_p)
def __pow__(self, other):
if not (isinstance(other, int) or isinstance(other, float)):
raise Exception(f"Type {type(x)} not supported")
x = self.x**other
x_p = other * self.x_p * self.x**(other-1)
return DualFloat(x, x_p)
def __float__(self):
return float(self.x)
def value(self):
return self.x
def derivative(self):
return self.x_p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment