-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Attila Oláh
committed
May 29, 2020
1 parent
9ab5562
commit efc6bfd
Showing
5 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Module int_p implements a polynomial wint integer terms and coefficients.""" | ||
from typing import Iterable, List, Tuple | ||
|
||
from poly.int_t import IntT | ||
|
||
|
||
class IntP(List[IntT]): | ||
"""A polynomial with int terms and coefficients. | ||
This type implements a sparse representation, i.e. only non-zero terms are | ||
stored. | ||
""" | ||
|
||
def __init__(self, *args: Tuple[int, Iterable[int]]) -> None: | ||
"""Initialise with a list of constants and terms.""" | ||
for const, ind in args: | ||
self.append(IntT(const, ind)) | ||
|
||
def __add__(self, other: 'IntP') -> 'IntP': | ||
"""Add two polynomials together.""" | ||
if not isinstance(other, type(self)): | ||
return NotImplemented | ||
ret = self.__class__() | ||
ret.extend(self) | ||
ret.extend(other) | ||
return ret._compact() | ||
|
||
def __repr__(self) -> str: | ||
terms: List[str] = [] | ||
|
||
for term in self: | ||
if not term.const: | ||
# Exclude "+ 0" terms. | ||
continue | ||
if term.const > 0: | ||
terms += "+" | ||
else: | ||
terms += "-" | ||
term.const *= -1 | ||
terms.append(repr(term)) | ||
|
||
return ' '.join(terms[1:]) | ||
|
||
def _compact(self): | ||
"""Merges terms with the same indeterminates.""" | ||
self.sort(reverse=True) | ||
|
||
ret = self.__class__() | ||
for term in self: | ||
size = len(ret) | ||
if not size: | ||
ret.append(term) | ||
continue | ||
if ret[size-1].ind_eq(term): | ||
ret[size-1].const += term.const | ||
continue | ||
print('XXX: {} > {}'.format(ret, term)) | ||
ret.append(term) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Tests for module int_p.""" | ||
import unittest | ||
|
||
from poly import int_p | ||
|
||
|
||
class TestIntP(unittest.TestCase): | ||
"""Tests the int_p.IntP class.""" | ||
|
||
def test_add(self): | ||
"""Tests the add method.""" | ||
self.assertEqual( | ||
repr(int_p.IntP( | ||
(2, (1,)), | ||
) + int_p.IntP( | ||
(2, (1,)), | ||
)), | ||
'4x' | ||
) | ||
self.assertEqual( | ||
repr(int_p.IntP( | ||
(1, (0, 1)), | ||
(1, (1, 0)), | ||
(2, (1, 1)), | ||
) + int_p.IntP( | ||
(2, (1, 1)), | ||
)), | ||
'4xy + x + y' | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters