Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab0 1.3 Горлов И.В. #62

Open
wants to merge 7 commits into
base: lab0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lab0/__pycache__/basic_math.cpython-311.pyc
Binary file not shown.
73 changes: 65 additions & 8 deletions lab0/basic_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,70 @@
import scipy as sc


def matrix_multiplication(matrix_a, matrix_b):
def matrix_multiplication(matrix1, matrix2):
"""
Задание 1. Функция для перемножения матриц с помощью списков и циклов.
Вернуть нужно матрицу в формате списка.
"""
# put your code here
pass

m = len(matrix1)
n = len(matrix1[0])
n_2 = len(matrix2)
p = len(matrix2[0])


if (n != n_2):
raise ValueError()

c = [[0 for x in range(p)] for y in range(m)]

for i in range(m):
for k in range(p):
temp_sum = 0
for j in range(n):
temp_sum += matrix1[i][j]*matrix2[j][k]
c[i][k] = temp_sum

return c

def functions(a_1, a_2):
def functions(coeffs1, coeffs2):
"""
Задание 2. На вход поступает две строки, содержащие коэффициенты двух функций.
Необходимо найти точки экстремума функции и определить, есть ли у функций общие решения.
Вернуть нужно координаты найденных решения списком, если они есть. None, если их бесконечно много.
"""
# put your code here
pass
eq1 = [int(x) for x in coeffs1.split()]
eq2 = [int(x) for x in coeffs2.split()]

if (eq1 == eq2):
return

def f(x):
return eq1[0] * x**2 + eq1[1] * x + eq1[2]


a = (eq1[0]-eq2[0])
b = (eq1[1]-eq2[1])
c = (eq1[2]-eq2[2])


if a != 0:
discriminant = b**2 - 4*a*c

if discriminant < 0:
return []

x1 = (-b + discriminant**0.5) / (2*a)
x2 = (-b - discriminant**0.5) / (2*a)

if x1 != x2:
return [(x1, f(x1)), (x2, f(x2))]
return [(x1, f(x1))]
elif a == 0 and b != 0:
x = -c / b
return [(x, f(x))]
else:
return []


def skew(x):
Expand All @@ -27,7 +74,12 @@ def skew(x):
Необходимо вернуть значение коэффициента асимметрии, округленное до 2 знаков после запятой.
"""
# put your code here
pass
x_e = sum(x)/len(x)
m_2 = (sum([(x_i - x_e)**2 for x_i in x])/len(x))**0.5
m_3 = sum([(x_i - x_e)**3 for x_i in x])/len(x)
A_3 = m_3/(m_2**3)

return round(A_3, 2)


def kurtosis(x):
Expand All @@ -36,4 +88,9 @@ def kurtosis(x):
Необходимо вернуть значение коэффициента эксцесса, округленное до 2 знаков после запятой.
"""
# put your code here
pass
x_e = sum(x)/len(x)
m_2 = (sum([(x_i - x_e)**2 for x_i in x])/len(x))**0.5
m_4 = sum([(x_i - x_e)**4 for x_i in x])/len(x)
E_4 = m_4/(m_2**4) - 3

return round(E_4, 2)
172 changes: 86 additions & 86 deletions lab0/test_basic_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,89 @@


class MATHTestCase(unittest.TestCase):
def test_matrix_multiplication(self):
a1 = [[1, 2, 3],
[4, 5, 6]]
b1 = [[7, 8],
[9, 10],
[11, 12]]
self.assertEqual(basic_math.matrix_multiplication(a1, b1), [[58, 64],[139, 154]])

a2 = [[1, 2],
[3, 4]]
b2 = [[1, 2]]
with self.assertRaises(ValueError):
basic_math.matrix_multiplication(a2, b2)

a3 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
b3 = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
self.assertEqual(basic_math.matrix_multiplication(a3, b3), a3)

a4 = [[1, 2, 3],
[4, 5, 6]]
b4 =[[0, 0],
[0, 0],
[0, 0]]
self.assertEqual(basic_math.matrix_multiplication(a4, b4), [[0, 0], [0, 0]])

a5 = [[1, 2, 3]]
b5 = [[4],
[5],
[6]]
self.assertEqual(basic_math.matrix_multiplication(a5, b5), [[32]])

a6 = [[3]]
b6 = [[4]]
self.assertEqual(basic_math.matrix_multiplication(a6, b6), [[12]])


def test_functions(self):
coeffs1 = "1 0 -4"
coeffs2 = "1 -2 0"
self.assertEqual(basic_math.functions(coeffs1, coeffs2), [(2, 0)])

coeffs3 = "1 0 4"
coeffs4 = "1 0 1"
self.assertEqual(basic_math.functions(coeffs3, coeffs4), [])

coeffs5 = "1 2 1"
coeffs6 = "1 2 1"
self.assertIsNone(basic_math.functions(coeffs5, coeffs6))

coeffs7 = "1 2 3"
coeffs8 = "1 2 1"
self.assertEqual(basic_math.functions(coeffs7, coeffs8), [])

coeffs9 = "0 2 -1"
coeffs10 = "1 -4 4"
self.assertEqual(basic_math.functions(coeffs9, coeffs10), [(1, 1), (5, 9)])


def test_skew(self):
x1 = [2,3,5,7,8]
x2 = [2,3,2,5,7,2,2,8]
self.assertEqual(basic_math.skew(x1), round(scipy.stats.skew(x1), 2))
self.assertEqual(basic_math.skew(x2), round(scipy.stats.skew(x2), 2))

random.seed(100)
random_floats = [random.random() for _ in range(10000)]
random_integers = [random.randint(1, 99) for _ in range(10000)]
self.assertEqual(basic_math.skew(random_floats), round(scipy.stats.skew(random_floats), 2))
self.assertEqual(basic_math.skew(random_integers), round(scipy.stats.skew(random_integers), 2))


def test_kurtosis(self):
x1 = [2,3,5,7,8]
x2 = [2,3,2,5,7,2,2,8]
self.assertEqual(basic_math.kurtosis(x1), round(scipy.stats.kurtosis(x1), 2))
self.assertEqual(basic_math.kurtosis(x2), round(scipy.stats.kurtosis(x2), 2))

random.seed(100)
random_floats = [random.random() for _ in range(10000)]
random_integers = [random.randint(1, 99) for _ in range(10000)]
self.assertEqual(basic_math.kurtosis(random_floats), round(scipy.stats.kurtosis(random_floats), 2))
self.assertEqual(basic_math.kurtosis(random_integers), round(scipy.stats.kurtosis(random_integers), 2))
def test_matrix_multiplication(self):
a1 = [[1, 2, 3],
[4, 5, 6]]
b1 = [[7, 8],
[9, 10],
[11, 12]]
self.assertEqual(basic_math.matrix_multiplication(a1, b1), [[58, 64],[139, 154]])

a2 = [[1, 2],
[3, 4]]
b2 = [[1, 2]]
with self.assertRaises(ValueError):
basic_math.matrix_multiplication(a2, b2)
a3 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
b3 = [[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
self.assertEqual(basic_math.matrix_multiplication(a3, b3), a3)

a4 = [[1, 2, 3],
[4, 5, 6]]
b4 =[[0, 0],
[0, 0],
[0, 0]]
self.assertEqual(basic_math.matrix_multiplication(a4, b4), [[0, 0], [0, 0]])
a5 = [[1, 2, 3]]
b5 = [[4],
[5],
[6]]
self.assertEqual(basic_math.matrix_multiplication(a5, b5), [[32]])

a6 = [[3]]
b6 = [[4]]
self.assertEqual(basic_math.matrix_multiplication(a6, b6), [[12]])


def test_functions(self):
coeffs1 = "1 0 -4"
coeffs2 = "1 -2 0"
self.assertEqual(basic_math.functions(coeffs1, coeffs2), [(2, 0)])

coeffs3 = "1 0 4"
coeffs4 = "1 0 1"
self.assertEqual(basic_math.functions(coeffs3, coeffs4), [])

coeffs5 = "1 2 1"
coeffs6 = "1 2 1"
self.assertIsNone(basic_math.functions(coeffs5, coeffs6))

coeffs7 = "1 2 3"
coeffs8 = "1 2 1"
self.assertEqual(basic_math.functions(coeffs7, coeffs8), [])

coeffs9 = "0 2 -1"
coeffs10 = "1 -4 4"
self.assertEqual(basic_math.functions(coeffs9, coeffs10), [(1, 1), (5, 9)])


def test_skew(self):
x1 = [2,3,5,7,8]
x2 = [2,3,2,5,7,2,2,8]
self.assertEqual(basic_math.skew(x1), round(scipy.stats.skew(x1), 2))
self.assertEqual(basic_math.skew(x2), round(scipy.stats.skew(x2), 2))

random.seed(100)
random_floats = [random.random() for _ in range(10000)]
random_integers = [random.randint(1, 99) for _ in range(10000)]
self.assertEqual(basic_math.skew(random_floats), round(scipy.stats.skew(random_floats), 2))
self.assertEqual(basic_math.skew(random_integers), round(scipy.stats.skew(random_integers), 2))


def test_kurtosis(self):
x1 = [2,3,5,7,8]
x2 = [2,3,2,5,7,2,2,8]
self.assertEqual(basic_math.kurtosis(x1), round(scipy.stats.kurtosis(x1), 2))
self.assertEqual(basic_math.kurtosis(x2), round(scipy.stats.kurtosis(x2), 2))

random.seed(100)
random_floats = [random.random() for _ in range(10000)]
random_integers = [random.randint(1, 99) for _ in range(10000)]
self.assertEqual(basic_math.kurtosis(random_floats), round(scipy.stats.kurtosis(random_floats), 2))
self.assertEqual(basic_math.kurtosis(random_integers), round(scipy.stats.kurtosis(random_integers), 2))
Loading