aboutsummaryrefslogtreecommitdiff
path: root/src/scalar.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalar.py')
-rw-r--r--src/scalar.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/scalar.py b/src/scalar.py
new file mode 100644
index 0000000..a67b7ae
--- /dev/null
+++ b/src/scalar.py
@@ -0,0 +1,20 @@
+class Scalar:
+ def __init__(self, data, _children=(), _op='', label='') -> None:
+ self.label = label
+
+ self.data = float(data)
+ self.grad = 0.0
+
+ self._prev = set(_children)
+ self._op = _op
+
+ def __repr__(self) -> str:
+ return f'Scalar({self.data})'
+
+ def __add__(self, y):
+ result = self.data + y.data
+ return Scalar(result, (self, y), _op='+')
+
+ def __mul__(self, y):
+ result = self.data * y.data
+ return Scalar(result, (self, y), _op='*')