$\vec{x} = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}$
x = (1, 2, 3)
x
(1, 2, 3)
$\bf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}$
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
A
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$\bf{A} * \vec{x} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} * \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} = \begin{pmatrix} 14 \\ 32 \\ 50 \end{pmatrix}$
A * x
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[5], line 1 ----> 1 A * x TypeError: can't multiply sequence by non-int of type 'tuple'
class Vector:
"""A one-dimensional vector from linear algebra."""
dummy_variable = "I am a vector"
def dummy_method(self):
"""A dummy method for illustration purposes."""
return self.dummy_variable
type(Vector)
type
Vector
__main__.Vector
Vector.dummy_variable
'I am a vector'
Vector.dummy_method
<function __main__.Vector.dummy_method(self)>
Vector.dummy_method()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[16], line 1 ----> 1 Vector.dummy_method() TypeError: Vector.dummy_method() missing 1 required positional argument: 'self'
class Vector:
"""A one-dimensional vector from linear algebra.
All entries are converted to floats.
"""
def __init__(self, data):
"""Create a new vector.
Args:
data (sequence): the vector's entries
Raises:
ValueError: if no entries are provided
"""
self._entries = list(float(x) for x in data)
if len(self._entries) == 0:
raise ValueError("a vector must have at least one entry")
v = Vector([1, 2, 3])
type(v)
__main__.Vector
v
<__main__.Vector at 0x7f9b9416d760>
Vector()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[22], line 1 ----> 1 Vector() TypeError: Vector.__init__() missing 1 required positional argument: 'data'
Vector(1, 2, 3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[23], line 1 ----> 1 Vector(1, 2, 3) TypeError: Vector.__init__() takes 2 positional arguments but 4 were given
Vector([])
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[24], line 1 ----> 1 Vector([]) Cell In[17], line 18, in Vector.__init__(self, data) 16 self._entries = list(float(x) for x in data) 17 if len(self._entries) == 0: ---> 18 raise ValueError("a vector must have at least one entry") ValueError: a vector must have at least one entry
v._entries # by convention not allowed
[1.0, 2.0, 3.0]
x = 1, 2, 3
x
(1, 2, 3)
(1, 2, 3)
(1, 2, 3)
class Vector:
def __init__(self, data):
self._entries = list(float(x) for x in data)
# ...
def __repr__(self):
args = ", ".join(repr(x) for x in self._entries)
return f"Vector(({args}))"
def __str__(self):
first, last = self._entries[0], self._entries[-1]
n_entries = len(self._entries)
return f"Vector({first!r}, ..., {last!r})[{n_entries:d}]"
v = Vector([1, 2, 3])
v
Vector((1.0, 2.0, 3.0))
repr(v)
'Vector((1.0, 2.0, 3.0))'
str(v)
'Vector(1.0, ..., 3.0)[3]'
print(v)
Vector(1.0, ..., 3.0)[3]
Matrix
Class¶class Matrix:
def __init__(self, data):
self._entries = list(list(float(x) for x in r) for r in data)
for row in self._entries[1:]:
if len(row) != len(self._entries[0]):
raise ValueError("rows must have the same number of entries")
if len(self._entries) == 0:
raise ValueError("a matrix must have at least one entry")
def __repr__(self):
args = ", ".join("(" + ", ".join(repr(c) for c in r) + ",)" for r in self._entries)
return f"Matrix(({args}))"
def __str__(self):
first, last = self._entries[0][0], self._entries[-1][-1]
m, n = len(self._entries), len(self._entries[0])
return f"Matrix(({first!r}, ...), ..., (..., {last!r}))[{m:d}x{n:d}]"
m = Matrix([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
type(m)
__main__.Matrix
m
Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))
print(m)
Matrix((1.0, ...), ..., (..., 9.0))[3x3]
Matrix([(1, 2, 3), (4, 5)])
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[46], line 1 ----> 1 Matrix([(1, 2, 3), (4, 5)]) Cell In[36], line 7, in Matrix.__init__(self, data) 5 for row in self._entries[1:]: 6 if len(row) != len(self._entries[0]): ----> 7 raise ValueError("rows must have the same number of entries") 8 if len(self._entries) == 0: 9 raise ValueError("a matrix must have at least one entry") ValueError: rows must have the same number of entries
class Matrix:
def __init__(self, data):
self._entries = list(list(float(x) for x in r) for r in data)
# ...
def __repr__(self):
args = ", ".join("(" + ", ".join(repr(c) for c in r) + ",)" for r in self._entries)
return f"Matrix(({args}))"
def transpose(self):
return Matrix(zip(*self._entries))
m = Matrix([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
m
Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))
m.transpose()
Matrix(((1.0, 4.0, 7.0,), (2.0, 5.0, 8.0,), (3.0, 6.0, 9.0,)))
n = m.transpose().transpose()
n
Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))
m is n
False
m == n
False
class Matrix:
def __init__(self, data):
self._entries = list(list(float(x) for x in r) for r in data)
# ...
def __repr__(self):
args = ", ".join("(" + ", ".join(repr(c) for c in r) + ",)" for r in self._entries)
return f"Matrix(({args}))"
def transpose(self):
return Matrix(zip(*self._entries))
@classmethod
def from_columns(cls, data):
return cls(data).transpose()
m = Matrix.from_columns([(1, 4, 7), (2, 5, 8), (3, 6, 9)])
m
Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))
class Matrix:
def __init__(self, data):
self._entries = list(list(float(x) for x in r) for r in data)
for row in self._entries[1:]:
if len(row) != self.n_cols:
raise ValueError("rows must have the same number of entries")
if self.n_rows == 0:
raise ValueError("a matrix must have at least one entry")
def __repr__(self):
args = ", ".join("(" + ", ".join(repr(c) for c in r) + ",)" for r in self._entries)
return f"Matrix(({args}))"
@property
def n_rows(self):
return len(self._entries)
@property
def n_cols(self):
return len(self._entries[0])
m = Matrix([(1, 2, 3), (4, 5, 6)])
m.n_rows, m.n_cols
(2, 3)
m.n_rows = 3
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[61], line 1 ----> 1 m.n_rows = 3 AttributeError: property 'n_rows' of 'Matrix' object has no setter