ContentsFoundations to FrontierBezhig

integration_code/starters/dual.py

All lab code. Download this file.

# Exercise scaffold. Reference implementation is in a separate directory.
"""W04: forward-mode automatic differentiation; each operation carries its tangent."""
from dataclasses import dataclass
import math

@dataclass(frozen=True)
class Dual:
    value: float
    tangent: float = 0.0

    def __post_init__(self):
        raise NotImplementedError('Implement this contract from the book.')

    @staticmethod
    def wrap(other):
        raise NotImplementedError('Implement this contract from the book.')

    def __add__(self, other):
        raise NotImplementedError('Implement this contract from the book.')
    __radd__ = __add__

    def __neg__(self):
        raise NotImplementedError('Implement this contract from the book.')

    def __sub__(self, other):
        raise NotImplementedError('Implement this contract from the book.')

    def __rsub__(self, other):
        raise NotImplementedError('Implement this contract from the book.')

    def __mul__(self, other):
        raise NotImplementedError('Implement this contract from the book.')
    __rmul__ = __mul__

    def __truediv__(self, other):
        raise NotImplementedError('Implement this contract from the book.')

    def __rtruediv__(self, other):
        raise NotImplementedError('Implement this contract from the book.')

def sin(x):
    raise NotImplementedError('Implement this contract from the book.')

def exp(x):
    raise NotImplementedError('Implement this contract from the book.')

def log(x):
    raise NotImplementedError('Implement this contract from the book.')