Skip to content

Commit

Permalink
Vector: (1) scalar division, (2) average, (3) collinearity, (4) linea…
Browse files Browse the repository at this point in the history
…r independence
  • Loading branch information
hunan-rostomyan committed Jul 2, 2018
1 parent ba8f04d commit 3d76186
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ assert b - a == Vector([2, 2])
```python
assert a * 5 == Vector([5, 10])
assert 5 * a == Vector([5, 10])
assert a / 2 == Vector([0.5, 1.0])
```

#### Dot (inner) product
Expand Down Expand Up @@ -75,4 +76,30 @@ assert Vector.scalproj(b, b) - 5 < 0.1

```python
assert Vector.cross(c, d) == Vector([-3, 6, -3])
```

#### Average (arithmetic mean)

```python
assert Vector.average(Vector([2, 1]), Vector([4, 2])) == Vector([3.0, 1.5])
```

#### Collinearity

Vectors are collinear iff one is a scalar multiple of the other.

```python
assert Vector.collinear(Vector([2, 1]), Vector([4, 2]))
assert Vector.collinear(Vector([-3, 4, 1]), Vector([-15, 20, 5]))
assert not Vector.collinear(Vector([0, 1]), Vector([1, 0]))
```

#### Linear independence

A set of vectors is linearly independent iff all vectors in it are pairwise non-collinear.

```python
assert Vector.linindep(Vector([0, 1]), Vector([1, 0]))
assert Vector.linindep(Vector([1, 1]), Vector([2, 1]))
assert not Vector.linindep(Vector([1, 2, 3]), Vector([0, 0, 1]), Vector([0, 0, 2]))
```
17 changes: 17 additions & 0 deletions abel/linalg/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def test_scal_mult():
assert a * 5 == Vector([5, 10])
assert 5 * a == Vector([5, 10])

def test_scal_div():
assert a / 2 == Vector([0.5, 1.0])

def test_matmul():
assert a @ b == 11
assert b @ a == a @ b
Expand Down Expand Up @@ -52,3 +55,17 @@ def test_scalproj():
def test_cross():
A, B = Vector([1, 2, 3]), Vector([4, 5, 6])
assert Vector.cross(A, B) == Vector([-3, 6, -3])

def test_average():
assert Vector.average(Vector([2, 1]), Vector([4, 2])) == Vector([3.0, 1.5])

def test_collinear():
assert Vector.collinear(Vector([2, 1]), Vector([4, 2]))
assert Vector.collinear(Vector([-3, 4, 1]), Vector([-15, 20, 5]))
assert not Vector.collinear(Vector([0, 1]), Vector([1, 0]))

def test_linindep():
assert Vector.linindep(Vector([0, 1]), Vector([1, 0]))
assert Vector.linindep(Vector([1, 1]), Vector([2, 1]))
assert not Vector.linindep(
Vector([1, 2, 3]), Vector([0, 0, 1]), Vector([0, 0, 2]))
29 changes: 29 additions & 0 deletions abel/linalg/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def __mul__(self, k):
def __rmul__(self, k):
return self * k

def __truediv__(self, k):
"""Scalar division."""
return self * (1 / k)

def __matmul__(self, B):
"""Dot product."""
xs, ys = self._contents, B._contents
Expand Down Expand Up @@ -70,3 +74,28 @@ def cross(A, B):
A_z * B_x - A_x * B_z,
A_x * B_y - A_y * B_x
])

@staticmethod
def average(A, B):
"""The average or arithmetic mean of A and B."""
return (A + B) / A.shape[1]

@staticmethod
def collinear(A, B):
"""Whether two vectors are collinear."""
def find_n(A_cont, B_cont):
for a, b in zip(A_cont, B_cont):
if a != 0:
return b / a
if b != 0:
return a / b
n = find_n(A._contents, B._contents)
return (n * A == B) or (n * B == A)

@staticmethod
def linindep(*vs):
for i, v in enumerate(vs):
for j in range(i + 1, len(vs)):
if Vector.collinear(vs[i], vs[j]):
return False
return True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name='abel',
version='0.0.5',
version='0.0.6',
author='Hunan Rostomyan',
author_email='[email protected]',
description='Machine learning components',
Expand Down

0 comments on commit 3d76186

Please sign in to comment.