exceptions Matrix

This commit is contained in:
Didictateur 2023-10-23 13:11:12 +02:00
parent d974eef680
commit 9aaebd4b34

View file

@ -4,12 +4,25 @@ I = complex(0, 1)
class Matrix: class Matrix:
def __init__(self, l: list[list[complex]]=[]) -> None: def __init__(self, l: list[list[complex]]=[]) -> None:
if type(l) is not list:
raise ValueError(f"l was expected to be list[list[complex]], no {type(l)}")
for u_ in l:
if type(u_) is not list:
raise ValueError(f"l was expected to be list[list[complex]]. A {type(u_)} has been found")
for l_ in l:
for x in l_:
if type(x) not in {int, float, complex}:
raise ValueError(f"l was expected to be list[list[complex]]. A {type(x)} has been found")
self.__m = l self.__m = l
if l == []: if l == []:
self.__m = [[0, 0], [0, 0]] self.__m = [[0, 0], [0, 0]]
self.__size = (len(self.__m), len(self.__m[0])) self.__size = (len(self.__m), len(self.__m[0]))
def __mul__(self, __value: "Matrix") -> "Matrix": def __mul__(self, __value: "Matrix") -> "Matrix":
if type(__value) is not Matrix:
raise ValueError(f"cannot multiply Matrix with {type(__value)}")
l: list[list[complex]] = [] l: list[list[complex]] = []
for i in range(self.__size[0]*__value.__size[0]): for i in range(self.__size[0]*__value.__size[0]):
l.append([0]*(self.__size[1]*__value.__size[1])) l.append([0]*(self.__size[1]*__value.__size[1]))
@ -24,6 +37,11 @@ class Matrix:
return Matrix(l) return Matrix(l)
def __apply(self, x: list[complex]) -> list[complex]: def __apply(self, x: list[complex]) -> list[complex]:
if type(x) is not list:
raise TypeError(f"can apply a Matrix to a list[complex, not to a {type(x)}]")
for y in x:
if type(y) not in {int, float, complex}:
TypeError(f"can apply a Matrix to a ")
assert(self.__size[1] == len(x)) assert(self.__size[1] == len(x))
y: list[complex] = [0]*self.__size[0] y: list[complex] = [0]*self.__size[0]
@ -88,6 +106,9 @@ class Matrix:
@staticmethod @staticmethod
def Rx(phi: float) -> "Matrix": def Rx(phi: float) -> "Matrix":
if type(phi) not in {int, float}:
TypeError(f"an angle must be integer or float, not {type(phi)}")
c = math.cos(phi/2) c = math.cos(phi/2)
s = math.sin(phi/2) s = math.sin(phi/2)
return Matrix([ return Matrix([
@ -97,6 +118,9 @@ class Matrix:
@staticmethod @staticmethod
def Ry(phi: float) -> "Matrix": def Ry(phi: float) -> "Matrix":
if type(phi) not in {int, float}:
TypeError(f"an angle must be integer or float, not {type(phi)}")
c = math.cos(phi/2) c = math.cos(phi/2)
s = math.sin(phi/2) s = math.sin(phi/2)
return Matrix([ return Matrix([
@ -106,6 +130,9 @@ class Matrix:
@staticmethod @staticmethod
def Rz(phi: float) -> "Matrix": def Rz(phi: float) -> "Matrix":
if type(phi) not in {int, float}:
TypeError(f"an angle must be integer or float, not {type(phi)}")
s = math.e**(I*phi/2) s = math.e**(I*phi/2)
return Matrix([ return Matrix([
[1/s, 0], [1/s, 0],
@ -114,6 +141,9 @@ class Matrix:
@staticmethod @staticmethod
def R1(phi: float) -> "Matrix": def R1(phi: float) -> "Matrix":
if type(phi) not in {int, float}:
TypeError(f"an angle must be integer or float, not {type(phi)}")
s = math.e**(I*phi) s = math.e**(I*phi)
return Matrix([ return Matrix([
[1, 0], [1, 0],
@ -139,8 +169,21 @@ class Matrix:
]) ])
@staticmethod @staticmethod
def Cu(u: "Matrix") -> "Matrix": def Cu(u: list[list[complex]]) -> "Matrix":
assert(u.size == (2, 2)) if type(u) is not list:
raise ValueError(f"u was expected to be list[list[complex]], not {type(u)}")
for u_ in u:
if type(u_) is not list:
raise ValueError(f"u was expected to be list[list[complex]]. A {type(u_)} has been found")
for l in u:
for x in l:
if type(x) not in {int, float, complex}:
raise ValueError(f"u was expected to be list[list[complex]]. A {type(x)} has been found")
if len(u) != 2 or len(u[0]) != 2:
if len(u) == 0:
ValueError(f"the size of the matrix was expected to be (2, 2). A matrix of size (0, .) has been given")
ValueError(f"the size of the matrix was expected to be (2, 2). A matrix of size ({len(u)}, {u[0]}) has been given")
l = [ l = [
[1, 0, 0, 0], [1, 0, 0, 0],
[0, 1, 0, 0], [0, 1, 0, 0],
@ -148,6 +191,8 @@ class Matrix:
[0, 0, u[1][0], u[1][1]] [0, 0, u[1][0], u[1][1]]
] ]
return Matrix(l)
if __name__=="__main__": if __name__=="__main__":
m1 = Matrix([[1]]) m1 = Matrix([[1]])
m2 = Matrix([[1, 1], [0, 1]]) m2 = Matrix([[1, 1], [0, 1]])