Skip to content

Commit ab7198d

Browse files
(1) add negation and subtraction, (2) update readme
1 parent 6d710ce commit ab7198d

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,78 @@
1-
# abel
1+
# Vector
2+
3+
```python
4+
from abel.linalg.vector import Vector
5+
6+
a, b = Vector([1, 2]), Vector([3, 4])
7+
c, d = Vector([1, 2, 3]), Vector([4, 5, 6])
8+
9+
assert a.shape == b.shape == (1, 2)
10+
assert c.shape == d.shape == (1, 3)
11+
```
12+
13+
#### Addition
14+
15+
```python
16+
assert a + a == Vector([2, 4])
17+
assert a + b == Vector([4, 6])
18+
assert b + b == Vector([6, 8])
19+
```
20+
21+
#### Subtraction
22+
23+
```python
24+
assert a - b == Vector([-2, -2])
25+
assert b - a == Vector([2, 2])
26+
```
27+
28+
#### Scaling
29+
30+
```python
31+
assert a * 5 == Vector([5, 10])
32+
assert 5 * a == Vector([5, 10])
33+
```
34+
35+
#### Dot (inner) product
36+
37+
```python
38+
assert a @ b == 11
39+
assert a @ a == 5
40+
```
41+
42+
#### Norm (length)
43+
44+
```python
45+
assert a.norm() - 2.236 < 0.001
46+
assert b.norm() - 5 < 0.001
47+
```
48+
49+
#### Angle
50+
51+
```python
52+
assert Vector.angle(a, a) < 0.001
53+
assert Vector.angle(a, b) - 0.1799 < 0.001
54+
assert Vector.angle(a, b) == Vector.angle(b, a)
55+
```
56+
57+
#### Vector projection
58+
59+
```python
60+
assert Vector.proj(a, a) == a
61+
assert Vector.proj(a, b) == Vector([1.32, 1.76])
62+
assert Vector.proj(b, a) == Vector([2.2, 4.4])
63+
```
64+
65+
#### Scalar projection
66+
67+
```python
68+
assert Vector.scalproj(a, b) - 4.919 < 0.01
69+
assert Vector.scalproj(b, a) - 2.2 < 0.01
70+
assert Vector.scalproj(a, a) - 2.236 < 0.01
71+
assert Vector.scalproj(b, b) - 5 < 0.1
72+
```
73+
74+
#### Cross product
75+
76+
```python
77+
assert Vector.cross(c, d) == Vector([-3, 6, -3])
78+
```

abel/linalg/test_vector.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ def test_add():
1313
assert a + b == Vector([4, 6])
1414
assert b + b == Vector([6, 8])
1515

16+
def test_neg():
17+
assert -a == -1 * a
18+
19+
def test_sub():
20+
assert a - b == a + (-b) == Vector([-2, -2])
21+
assert b - a == b + (-a) == Vector([2, 2])
22+
1623
def test_scal_mult():
1724
assert a * 5 == Vector([5, 10])
1825
assert 5 * a == Vector([5, 10])

abel/linalg/vector.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ def __repr__(self):
1717
def __eq__(self, other):
1818
return self._contents == other._contents
1919

20+
def __neg__(self):
21+
return self * -1
22+
2023
def __add__(self, B):
2124
"""Vector addition."""
2225
xs, ys = self._contents, B._contents
2326
return Vector([x + ys[i] for i, x in enumerate(xs)])
2427

28+
def __sub__(self, B):
29+
"""Vector subtraction."""
30+
return self + -B
31+
2532
def __mul__(self, k):
2633
"""Scalar multiplication."""
2734
return Vector([x * k for x in self._contents])

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='abel',
8-
version='0.0.2',
8+
version='0.0.3',
99
author='Hunan Rostomyan',
1010
author_email='hunan131@gmail.com',
1111
description='Machine learning components',

0 commit comments

Comments
 (0)