コード例 #1
0
 def test_sub(self):
     first = Polynom({'ggg': 14.5, 'x': 2.539, '': 77})
     second = Polynom({'ggg': (-1j + 1), 'yz': -4, 'ab': 4, '': -77})
     self.assertTrue(
         compare_with_epsilon(
             Polynom({
                 'ggg': (13.5 + 1j),
                 'yz': 4,
                 'ab': -4,
                 '': 154,
                 'x': 2.539
             }), first - second))
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'ggg': 14.5,
             'x': 2.539,
             '': 50.7
         }), first - +26.3))
     self.assertTrue(compare_with_epsilon(0, second - second))
     self.assertTrue(
         compare_with_epsilon(
             0.5, second - Polynom({
                 'ggg': (-1j + 1),
                 'yz': -4,
                 'ab': 4
             }) - -77.5))
コード例 #2
0
ファイル: main.py プロジェクト: anzue/labs_magister_1
def Euclid_test():
    f1 = Polynom(f_values, p)
    f2 = Polynom((1, 1), p)

    d, a, b = ext_Euclid(f1, f2)

    print((a * f1 + b * f2).coefs)
    print(d.coefs)
コード例 #3
0
 def test_pow(self):
     polynom_ = Polynom({'x': 1, '': -2})
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xx': 1,
             'x': -4,
             '': 4
         }), polynom_**2.0))
     with self.assertRaises(ArithmeticError):
         polynom_**(-1)
     with self.assertRaises(TypeError):
         polynom_**0.5
コード例 #4
0
 def test_mul(self):
     first = Polynom({'x': 5, 'y': -4})
     second = Polynom({'x': -2, 'y': 6})
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xx': -10,
             'xy': 38,
             'yy': -24
         }), first * second))
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'x': -10,
             'y': 8
         }), -2 * first))
     self.assertTrue(compare_with_epsilon(0, second * 0))
コード例 #5
0
ファイル: main.py プロジェクト: anzue/labs_magister_1
def get_invertable(low, up):
    found = False
    res = None
    while not found:
        try:
            res = np.random.randint(low, up + 1, size=Polynom.N)
            Polynom.CUR_MOD = p
            inv1 = invert_mine(Polynom(res))
            Polynom.CUR_MOD = q
            inv2 = invert_mine(Polynom(res))
            found = True
        except:
            print("Not good, not good at all")
            found = False
    return res
コード例 #6
0
 def test_truediv(self):
     polynom_ = Polynom({'xy': 2, 'z': 0.676})
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xy': 1,
             'z': 0.338
         }), polynom_ / 2))
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xy': 4,
             'z': 1.352
         }), polynom_ / 0.5))
     with self.assertRaises(ArithmeticError):
         polynom_ / Polynom({'x': 25})
     with self.assertRaises(ZeroDivisionError):
         polynom_ / 0
コード例 #7
0
ファイル: main.py プロジェクト: anzue/labs_magister_1
def test_NTRU():
    Polynom.CUR_MOD = p
    f = Polynom(f_values)
    fp = invert_mine(f)

    Polynom.CUR_MOD = q
    f = Polynom(f_values)
    fq = invert_mine(f)

    g = Polynom(g_values)

    h = Polynom(p * fq.coefs) * g  # PUBLIC
    h.apply_mod()

    print("fp : ", fp)
    print("fq : ", fq)
    print("h  : ", h)

    print("\nEncryption")

    m = Polynom(m_values)
    r = Polynom(r_values)

    print("m  : ", m)
    print("r  : ", r)

    e = r * h + m  # MESSAGE
    e.apply_mod()
    print(e)

    print("\nDecryption")

    f = Polynom(f_values)
    a = f * e
    a.apply_halfmod()
    print("a : ", a)

    Polynom.CUR_MOD = p
    b = a
    b.apply_halfmod()

    print("b  : ", b)

    c = fp * f * m
    c.apply_halfmod()

    print("c : ", c)
コード例 #8
0
 def test_add_polynom(self):
     first = Polynom({'xyx': 2, 'z': 0, '': 2.539})
     second = Polynom({'xxy': 5j, 'z': -4, 'abcc': 4})
     third = Polynom({'cabc': -2, 'z': 4, '': -2.539})
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xxy': (2 + 5j),
             'abcc': 2
         }), first + second + third))
     self.assertTrue(
         compare_with_epsilon(
             Polynom({
                 'xxy': (2 + 5j),
                 '': 2.539,
                 'z': -4,
                 'abcc': 4
             }), first + second))
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xxy': 2,
             'abcc': -2,
             'z': 4
         }), first + third))
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xxy': 5j,
             'abcc': 2,
             '': -2.539
         }), second + third))
コード例 #9
0
 def get_polynom(self, expression):
     operands = []
     polynom = None
     for i in range(len(expression)):
         if (self.is_operand(expression[i]) or 'j' in expression[i]
                 or re.match(FLOAT_RE, expression[i])):
             if (re.search(NOT_DIGIT, expression[i]) and
                     '.' not in expression[i] and 'j' not in expression[i]):
                 polynom = Polynom({expression[i]: 1})
             else:
                 types = [int, float, complex]
                 for type_ in types:
                     try:
                         polynom = Polynom({'': type_(expression[i])})
                     except ValueError:
                         continue
             if polynom:
                 operands.append(polynom)
         if self.is_operation(expression[i]):
             operands[-2:] = [
                 self.operations[expression[i]](*operands[-2:])]
     return operands[-1]
コード例 #10
0
 def test_add_other(self):
     polynom_ = Polynom({'xyx': 2, 'z': 0, '': 2.539})
     self.assertTrue(
         compare_with_epsilon(Polynom({
             'xyx': 2,
             'z': 0,
             '': (2.539 + 5j)
         }), polynom_ + 5j))
     self.assertTrue(
         compare_with_epsilon(
             Polynom({
                 'xyx': 2,
                 'z': 0,
                 '': (18.039 + 5j)
             }), polynom_ + 15.5))
     self.assertTrue(
         compare_with_epsilon(
             Polynom({
                 'xyx': 2,
                 'z': 0,
                 '': (-1.961 + 5j)
             }), polynom_ + -20))
     with self.assertRaises(TypeError):
         polynom_ + 5 * 'asdfgh'
コード例 #11
0
 def test_get_polynom(self):
     parser = Parser("xyz + xyz".replace(' ', ''))
     self.assertTrue(compare_with_epsilon(Polynom({'xyz': 2}), parser()))
     parser = Parser("(x-2)(x-2)")
     self.assertTrue(compare_with_epsilon(
         Polynom({'xx': 1, 'x': -4, '': 4}), parser()))
     parser = Parser("(x + y)++(x)".replace(' ', ''))
     self.assertTrue(compare_with_epsilon(
         Polynom({'x': 2, 'y': 1}), parser()))
     parser = Parser("x^2 + 2x^3 + x^2 -4x+4".replace(' ', ''))
     self.assertTrue(compare_with_epsilon(Polynom(
         {'xx': 2, 'xxx': 2, 'x': -4, '': 4}), parser()))
     parser = Parser("j")
     self.assertTrue(compare_with_epsilon(Polynom({'': 1j}), parser()))
     parser = Parser("-2j + 5xu".replace(' ', ''))
     self.assertTrue(compare_with_epsilon(
         Polynom({'': -2j, 'xu': 5}), parser()))
     parser = Parser(" -x^2^3".replace(' ', ''))
     self.assertTrue(compare_with_epsilon(
         Polynom({'xxxxxxxx': -1}), parser()))
     parser = Parser("x/(0x + 1)".replace(' ', ''))
     self.assertTrue(compare_with_epsilon(Polynom({'x': 1}), parser()))
     parser = Parser("jjx")
     self.assertTrue(compare_with_epsilon(Polynom({'x': -1}), parser()))
コード例 #12
0
    def get_polynomials(self):
        M = np.zeros((self.n - 1, self.n - 1))
        M[0][0] = 2 * (self.h[0] + self.h[1])
        M[0][1] = self.h[1]
        M[self.n - 2, self.n - 3] = self.h[self.dots_count - 3]
        M[self.n - 2,
          self.n - 2] = 2 * (self.h[self.n - 2] + self.h[self.n - 1])
        for i in range(2, self.n - 1):
            M[i - 1][i - 2] = self.h[i - 1]
            M[i - 1][i - 1] = 2 * (self.h[i - 1] + self.h[i])
            M[i - 1][i] = self.h[i]

        v = np.zeros(self.n - 1)
        for i in range(1, self.n):
            v[i - 1] = 3.0 * (
                (self.dots[i + 1][1] - self.dots[i][1]) / self.h[i] -
                (self.dots[i][1] - self.dots[i - 1][1]) / self.h[i - 1])

        result = np.linalg.solve(M, v)
        self.c[0] = 0.0
        for i in range(0, self.n - 1):
            self.c[i + 1] = result[i]

        for i in range(self.n):
            self.a[i] = self.dots[i][1]

        for i in range(self.n - 1):
            self.d[i] = (self.c[i + 1] - self.c[i]) / (3.0 * self.h[i])
            self.b[i] = (self.dots[i+1][1] - self.dots[i][1]) / self.h[i] - \
                        self.h[i] / 3.0 * (self.c[i+1] + 2.0 * self.c[i])
        self.d[self.n - 1] = -self.c[self.n - 1] / (3.0 * self.h[self.n - 1])
        self.b[self.n-1] = (self.dots[self.n][1] - self.dots[self.n-1][1]) / self.h[self.n-1] - \
                           self.h[self.n-1] / 3.0 * 2.0 * self.c[self.n-1]

        self.polynomials = []
        for i in range(self.n):
            self.polynomials.append(
                Polynom(self.a[i], self.b[i], self.c[i], self.d[i],
                        self.dots[i][0]))
コード例 #13
0
 def test_derivative_3(self):
     poly = Polynom("175")
     self.assertEqual(str(poly.derivative()), "0")
コード例 #14
0
 def test_derivative_2(self):
     poly = Polynom("x")
     self.assertEqual(str(poly.derivative()), "1")
コード例 #15
0
ファイル: py_polynom_test.py プロジェクト: L-Q-K/C4TAdHW
t.coefficient = 3
t.degree = 1
assert (t.__str__() == "3x")
assert (t.evaluate(2) == 6)

t.coefficient = 4
t.degree = 2
assert (t.__str__() == "4x^2")
assert (t.evaluate(3) == 36)

t.coefficient = 1
t.degree = 2
assert (t.__str__() == "x^2")
assert (t.evaluate(3) == 9)

p = Polynom()
assert (hasattr(p, "head"))
assert (hasattr(p, "add"))
assert (hasattr(p, "evaluate"))

assert (p.__str__() == "")
assert (p.evaluate(100) == 0)

p.add(4, 3)
assert (p.__str__() == "4x^3")
assert (p.evaluate(2) == 32)

p.add(3, 2)
assert (p.__str__() == "4x^3+3x^2")
assert (p.evaluate(2) == 32 + 12)
コード例 #16
0
 def test_two_solutions(self):
     """ Resolve X^2 - X - 12 = 0 """
     p = Polynom(1, -1, -12)
     self.assertEqual(str(p), 'X^2 - X - 12')
     self.assertEqual(p.get_sol(), [-3, 4])
コード例 #17
0
 def test_one_solution(self):
     """ Resolve X^2 = 0 """
     p = Polynom(1, 0, 0)
     self.assertEqual(str(p), 'X^2')
     self.assertEqual(p.get_sol(), [0])
コード例 #18
0
 def test_none_solution(self):
     """ Resolve X^2 + X + 1 = 0 """
     p = Polynom(1, 1, 1)
     self.assertEqual(str(p), 'X^2 + X + 1')
     self.assertEqual(p.get_sol(), [])
コード例 #19
0
 def test_a_zero(self):
     """ Coefficient a should not be 0 """
     with self.assertRaises(AssertionError):
         p = Polynom(0, 1, 2)
コード例 #20
0
def main():
    p = Polynom(argv[1])
    p.print_derivative()
コード例 #21
0
 def test_derivative_4(self):
     poly = Polynom("x^2")
     self.assertEqual(str(poly.derivative()), "2*x")
コード例 #22
0
 def test_check_new_polynom(self):
     polynom1 = Polynom({'xz': 2, '': 3.5})
     polynom2 = Polynom({'xz': -2, '': -2.5})
     self.assertTrue(compare_with_epsilon(1, polynom1 + polynom2))
コード例 #23
0
 def test_compare_with_epsilon(self):
     polynom1 = Polynom({'xy': 2, '': 6})
     polynom2 = Polynom({'xy': 2, '': -2.5})
     self.assertTrue(compare_with_epsilon(1, polynom1 - polynom2, 10))
コード例 #24
0
 def setUp(self):
     self.poly_1 = Polynom("3x^5 + 2x^5 + 4x^3 + 5 + 2")
コード例 #25
0
ファイル: power.py プロジェクト: mxtm/cmps-200-fall-2018
from polynom import Polynom


def power1(p, d):
    # A fun iterative version of raising a polynomial to a power.
    result = p
    # We use d - 2 instead of d - 1 because we are initializing our result to p already
    for i in range(d - 2):
        # Multiply result variable by itself!
        result *= result
    return result


def power2(p, d):
    # A more fun recursive version of raising a polynomial to a power.
    if d == 1:
        # A polynomial raised to power 1 is itself
        return p
    else:
        # Otherwise, get the polynomial to the desired power / 2
        x = power2(p, d / 2)
        # Multiply this by itself to get the desired result
        return x * x


p1 = Polynom([(1, 0), (1, 1)])
print(p1)
print(power1(p1, 4))
print(power2(p1, 4))