Skip to content

Commit a120b25

Browse files
committed
Revert "Add basic matrix functionality (#178)"
This reverts commit 4cdc799.
1 parent 73d62bb commit a120b25

File tree

6 files changed

+2
-215
lines changed

6 files changed

+2
-215
lines changed

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111
- Bijection class for known bijection.
1212
- An implementation of the Simion and Schmidt bijection.
13-
- Matrix object
14-
- Function: Create Perm from a Matrix
15-
- Function: Create Matrix from Perm
1613
- Enumeration strategy to check whether a class has finitely many simple permutations
1714

1815
### Changed

README.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,6 @@ There are numerous practical methods available:
159159
[(1, 2)]
160160
>>> p.major_index()
161161
2
162-
>>> Perm((2,3,1,0)).matrix_repr()
163-
Matrix(4, {(0, 2): 1, (1, 3): 1, (2, 1): 1, (3, 0): 1})
164-
>>> Perm.from_matrix(Matrix(4, {(0, 2): 1, (1, 3): 1, (2, 1): 1, (3, 0): 1}))
165-
Perm((2, 3, 1, 0))
166-
>>> print(Matrix(3, {(0, 0): 0, (1, 0): 0, (2, 0): 1, (1, 1): 1, (2, 1): 0, (0, 2): 1, (2, 2): 0}))
167-
|0|0|1|
168-
|0|1|0|
169-
|1|0|0|
170-
<BLANKLINE>
171162
172163
Creating a perm class
173164
#####################

permuta/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from .misc.math import Matrix
21
from .patterns import BivincularPatt, CovincularPatt, MeshPatt, Perm, VincularPatt
32
from .perm_sets.permset import Av, Basis, MeshBasis
43

@@ -13,5 +12,4 @@
1312
"BivincularPatt",
1413
"CovincularPatt",
1514
"VincularPatt",
16-
"Matrix",
1715
]

permuta/misc/math.py

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from itertools import chain
2-
from typing import Dict, Iterable, List, Optional, Tuple
3-
4-
51
def is_prime(n: int) -> bool:
62
"""Primality test using 6k+-1 optimization."""
73
if n <= 3:
@@ -14,147 +10,3 @@ def is_prime(n: int) -> bool:
1410
return False
1511
i += 6
1612
return True
17-
18-
19-
class Matrix:
20-
"""
21-
Creates a square matrix of the dimensions (size x size).
22-
Arguments:
23-
`size:` The size of the `n x n` matrix.
24-
`binary:` If set to True then all calculations are done over GF(2).
25-
`elements:` The function from pos to value at pos. `f(pos) = value`.
26-
"""
27-
28-
def __init__(
29-
self,
30-
size: int,
31-
elements: Optional[Dict[Tuple[int, int], int]] = None,
32-
binary: bool = True,
33-
) -> None:
34-
if elements:
35-
self.elements: Dict[Tuple[int, int], int] = {
36-
key: (1 if binary and val else val) for key, val in elements.items()
37-
}
38-
else:
39-
self.elements = {}
40-
self.binary = binary
41-
self._size = size
42-
43-
def __getitem__(self, pos) -> int:
44-
"""Get value in row, col specified"""
45-
return self.elements[pos] if pos in self.elements else 0
46-
47-
def __setitem__(self, pos, value) -> None:
48-
"""Sets position in the matrix to val"""
49-
assert 0 <= pos[0] < len(self)
50-
assert 0 <= pos[1] < len(self)
51-
self.elements[pos] = value
52-
53-
def __add__(self, other: "Matrix") -> "Matrix":
54-
"""Adds two matrices together. Both classes have to be binary or not binary"""
55-
if not isinstance(other, Matrix):
56-
return NotImplemented
57-
if self.binary != other.binary:
58-
return NotImplemented
59-
assert len(self) == len(other)
60-
new_mat = Matrix(size=len(self))
61-
funcs = [self.elements.items(), other.elements.items()]
62-
if self.binary:
63-
for idx, val in chain(*funcs):
64-
new_mat[idx] ^= val
65-
else:
66-
for idx, val in chain(*funcs):
67-
new_mat[idx] += val
68-
return new_mat
69-
70-
def __mul__(self, other: "Matrix") -> "Matrix":
71-
if not isinstance(other, Matrix):
72-
return NotImplemented
73-
if self.binary != other.binary:
74-
return NotImplemented
75-
assert len(self) == len(other)
76-
mat = Matrix(size=len(self), elements={})
77-
for row in range(len(self)):
78-
for col in range(len(self)):
79-
for k in range(len(self)):
80-
mat[(row, col)] += self[(row, k)] * other[(k, col)]
81-
return mat
82-
83-
def __len__(self) -> int:
84-
return self._size
85-
86-
def __str__(self) -> str:
87-
string = ""
88-
for row in range(len(self)):
89-
for col in range(len(self)):
90-
cell = self[row, col]
91-
string += f"|{cell}"
92-
string += "|\n"
93-
return string
94-
95-
def __repr__(self) -> str:
96-
return f"Matrix({len(self)}, {self.elements})"
97-
98-
def __eq__(self, other: object) -> bool:
99-
"""Returns True if both matrices are equal."""
100-
if not isinstance(other, Matrix):
101-
return NotImplemented
102-
if self.binary != other.binary:
103-
return NotImplemented
104-
if len(self) != len(other):
105-
return False
106-
for row in range(len(self)):
107-
for col in range(len(self)):
108-
if self[row, col] != other[row, col]:
109-
return False
110-
return True
111-
112-
def test_sum(self) -> int:
113-
"""Sums up all the values in the matrix."""
114-
return sum(self.elements.values())
115-
116-
def hamming_sums(self, row: bool = True) -> List[int]:
117-
"""Gives the hamming sums for either the rows or columns"""
118-
key_idx = 0 if row else 1
119-
out = []
120-
for idx in range(len(self)):
121-
out.append(0)
122-
for key, val in self.elements.items():
123-
if key[key_idx] == idx:
124-
out[idx] += val
125-
return out
126-
127-
def row_labels(self) -> Tuple[int, ...]:
128-
"""
129-
Looks at each row as a binary number and returns the binary value for each.
130-
"""
131-
out_list: List[int] = []
132-
for row_idx in range(len(self)):
133-
num = 0
134-
for key, val in self.elements.items():
135-
if key[0] == row_idx and val:
136-
num += 2 ** (key[1])
137-
out_list.append(num)
138-
return tuple(sorted(out_list))
139-
140-
def apply_perm(self, perm: Iterable[int], row: bool = True) -> "Matrix":
141-
"""Returns a new matrix which has the given permutation
142-
applied on either the rows or columns of the matrix"""
143-
new_func: Dict[Tuple[int, int], int] = {}
144-
for n_idx, p_idx in enumerate(perm):
145-
for idx in range(len(self)):
146-
if row:
147-
pos = (p_idx, idx)
148-
else:
149-
pos = (idx, p_idx)
150-
151-
if pos in self.elements:
152-
if row:
153-
new_func[n_idx, idx] = self[pos]
154-
else:
155-
new_func[idx, n_idx] = self[pos]
156-
157-
return Matrix(len(self), elements=new_func)
158-
159-
def __hash__(self) -> int:
160-
return hash(str(self))

permuta/patterns/perm.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727

2828
from permuta.misc import HTMLViewer
29-
from permuta.misc.math import Matrix, is_prime
29+
from permuta.misc.math import is_prime
3030

3131
from .patt import Patt
3232

@@ -82,19 +82,6 @@ def _to_standard(cls, iterable: Tuple) -> "Perm":
8282
standardize = to_standard
8383
from_iterable = to_standard
8484

85-
@classmethod
86-
def from_matrix(cls, matrix: "Matrix") -> "Perm":
87-
"""Returns the perm corresponding to the matrix given."""
88-
if matrix.test_sum() != len(matrix):
89-
raise ValueError("Incorrect amount of numbers in permutation matrix")
90-
perm_list: List[Optional[int]] = [None for _ in range(len(matrix))]
91-
for (row, col), val in matrix.elements.items():
92-
if val:
93-
perm_list[row] = col
94-
if None in perm_list:
95-
raise ValueError("Some rows/cols are empty.")
96-
return cls.to_standard(perm_list)
97-
9885
@classmethod
9986
def from_integer(cls, integer: int) -> "Perm":
10087
"""Return the perm corresponding to the integer given. The permutation
@@ -3000,12 +2987,6 @@ def show(self, scale: float = 1.0) -> None:
30002987
enlarged with scale parameter"""
30012988
HTMLViewer.open_svg(self.to_svg(image_scale=scale))
30022989

3003-
def matrix_repr(self):
3004-
"""Returns the matrix representation of the perm"""
3005-
return Matrix(
3006-
len(self), elements={(idx, val): 1 for idx, val in enumerate(self)}
3007-
)
3008-
30092990
def __call__(self, value: int) -> int:
30102991
assert 0 <= value < len(self)
30112992
return self[value]

tests/patterns/test_perm.py

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

66
import pytest
77

8-
from permuta import Matrix, MeshPatt, Perm
8+
from permuta import MeshPatt, Perm
99

1010

1111
def test_from_iterable_validated():
@@ -3682,35 +3682,3 @@ def test_count_foreminima():
36823682
assert Perm((3, 1, 2, 0)).count_foreminima() == 1
36833683
assert Perm((3, 2, 0, 1)).count_foreminima() == 1
36843684
assert Perm((3, 2, 1, 0)).count_foreminima() == 0
3685-
3686-
3687-
def test_matrix_class():
3688-
assert Matrix(0) == Matrix(0)
3689-
matrix = Matrix(size=3, elements={(0, 0): 1, (1, 1): 1, (2, 2): 1})
3690-
assert Perm((0, 1, 2)) == Perm.from_matrix(matrix)
3691-
matrix2 = Matrix(size=3)
3692-
matrix2[0, 0] = 1
3693-
matrix2[1, 1] = 1
3694-
matrix2[2, 2] = 1
3695-
assert matrix == matrix2
3696-
assert matrix + matrix2 == Matrix(size=3)
3697-
pmat = Matrix(
3698-
4,
3699-
elements={
3700-
(3, 3): 1,
3701-
(2, 1): 1,
3702-
(1, 0): 1,
3703-
(0, 2): 1,
3704-
},
3705-
)
3706-
perm = Perm.from_matrix(pmat)
3707-
assert perm == Perm((2, 0, 1, 3))
3708-
assert pmat == Perm((2, 0, 1, 3)).matrix_repr()
3709-
perm = Perm((5, 3, 4, 1, 2, 0))
3710-
pmat = perm.matrix_repr()
3711-
assert pmat == Matrix(
3712-
6, {(0, 5): 1, (1, 3): 1, (2, 4): 1, (3, 1): 1, (4, 2): 1, (5, 0): 1}
3713-
)
3714-
pmat = Perm((3, 1, 0, 2)).matrix_repr()
3715-
pmat = pmat.apply_perm(Perm((2, 1, 3, 0)))
3716-
assert pmat == Matrix(4, {(0, 0): 1, (1, 1): 1, (2, 2): 1, (3, 3): 1})

0 commit comments

Comments
 (0)