"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mpmath/matrices/matrices.py" between
mpmath-0.18.tar.gz and mpmath-0.19.tar.gz

About: mpmath is a Python library for arbitrary-precision floating-point arithmetic.

matrices.py  (mpmath-0.18):matrices.py  (mpmath-0.19)
skipping to change at line 73 skipping to change at line 73
['0.0', '0.0']]) ['0.0', '0.0']])
Internally ``mpmathify`` is used every time an element is set. This Internally ``mpmathify`` is used every time an element is set. This
is done using the syntax A[row,column], counting from 0: is done using the syntax A[row,column], counting from 0:
>>> A = matrix(2) >>> A = matrix(2)
>>> A[1,1] = 1 + 1j >>> A[1,1] = 1 + 1j
>>> A >>> A
matrix( matrix(
[['0.0', '0.0'], [['0.0', '0.0'],
['0.0', '(1.0 + 1.0j)']]) ['0.0', mpc(real='1.0', imag='1.0')]])
You can use the keyword ``force_type`` to change the function which is You can use the keyword ``force_type`` to change the function which is
called on every new element: called on every new element:
>>> matrix(2, 5, force_type=int) >>> matrix(2, 5, force_type=int) # doctest: +SKIP
matrix( matrix(
[[0, 0, 0, 0, 0], [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]) [0, 0, 0, 0, 0]])
A more comfortable way to create a matrix lets you use nested lists: A more comfortable way to create a matrix lets you use nested lists:
>>> matrix([[1, 2], [3, 4]]) >>> matrix([[1, 2], [3, 4]])
matrix( matrix(
[['1.0', '2.0'], [['1.0', '2.0'],
['3.0', '4.0']]) ['3.0', '4.0']])
If you want to preserve the type of the elements you can use If you want to preserve the type of the elements you can use
``force_type=None``: ``force_type=None``:
>>> matrix([[1, 2.5], [1j, mpf(2)]], force_type=None) >>> matrix([[1, 2.5], [1j, mpf(2)]], force_type=None)
matrix( matrix(
[[1, 2.5], [['1.0', '2.5'],
[1j, '2.0']]) [mpc(real='0.0', imag='1.0'), '2.0']])
Convenient advanced functions are available for creating various standard Convenient advanced functions are available for creating various standard
matrices, see ``zeros``, ``ones``, ``diag``, ``eye``, ``randmatrix`` and matrices, see ``zeros``, ``ones``, ``diag``, ``eye``, ``randmatrix`` and
``hilbert``. ``hilbert``.
Vectors Vectors
....... .......
Vectors may also be represented by the ``matrix`` class (with rows = 1 or co ls = 1). Vectors may also be represented by the ``matrix`` class (with rows = 1 or co ls = 1).
For vectors there are some things which make life easier. A column vector ca n For vectors there are some things which make life easier. A column vector ca n
skipping to change at line 292 skipping to change at line 292
mathematical properties you might expect from a norm. mathematical properties you might expect from a norm.
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.__data = {} self.__data = {}
# LU decompostion cache, this is useful when solving the same system # LU decompostion cache, this is useful when solving the same system
# multiple times, when calculating the inverse and when calculating the # multiple times, when calculating the inverse and when calculating the
# determinant # determinant
self._LU = None self._LU = None
convert = kwargs.get('force_type', self.ctx.convert) convert = kwargs.get('force_type', self.ctx.convert)
if not convert:
convert = lambda x: x
if isinstance(args[0], (list, tuple)): if isinstance(args[0], (list, tuple)):
if isinstance(args[0][0], (list, tuple)): if isinstance(args[0][0], (list, tuple)):
# interpret nested list as matrix # interpret nested list as matrix
A = args[0] A = args[0]
self.__rows = len(A) self.__rows = len(A)
self.__cols = len(A[0]) self.__cols = len(A[0])
for i, row in enumerate(A): for i, row in enumerate(A):
for j, a in enumerate(row): for j, a in enumerate(row):
self[i, j] = convert(a) self[i, j] = convert(a)
else: else:
skipping to change at line 313 skipping to change at line 315
v = args[0] v = args[0]
self.__rows = len(v) self.__rows = len(v)
self.__cols = 1 self.__cols = 1
for i, e in enumerate(v): for i, e in enumerate(v):
self[i, 0] = e self[i, 0] = e
elif isinstance(args[0], int): elif isinstance(args[0], int):
# create empty matrix of given dimensions # create empty matrix of given dimensions
if len(args) == 1: if len(args) == 1:
self.__rows = self.__cols = args[0] self.__rows = self.__cols = args[0]
else: else:
assert isinstance(args[1], int), 'expected int' if not isinstance(args[1], int):
raise TypeError("expected int")
self.__rows = args[0] self.__rows = args[0]
self.__cols = args[1] self.__cols = args[1]
elif isinstance(args[0], _matrix): elif isinstance(args[0], _matrix):
A = args[0].copy() A = args[0].copy()
self.__data = A._matrix__data self.__data = A._matrix__data
self.__rows = A._matrix__rows self.__rows = A._matrix__rows
self.__cols = A._matrix__cols self.__cols = A._matrix__cols
convert = kwargs.get('force_type', self.ctx.convert)
for i in xrange(A.__rows): for i in xrange(A.__rows):
for j in xrange(A.__cols): for j in xrange(A.__cols):
A[i,j] = convert(A[i,j]) A[i,j] = convert(A[i,j])
elif hasattr(args[0], 'tolist'): elif hasattr(args[0], 'tolist'):
A = self.ctx.matrix(args[0].tolist()) A = self.ctx.matrix(args[0].tolist())
self.__data = A._matrix__data self.__data = A._matrix__data
self.__rows = A._matrix__rows self.__rows = A._matrix__rows
self.__cols = A._matrix__cols self.__cols = A._matrix__cols
else: else:
raise TypeError('could not interpret given arguments') raise TypeError('could not interpret given arguments')
skipping to change at line 593 skipping to change at line 595
else: else:
# try scalar multiplication # try scalar multiplication
new = self.ctx.matrix(self.__rows, self.__cols) new = self.ctx.matrix(self.__rows, self.__cols)
for i in xrange(self.__rows): for i in xrange(self.__rows):
for j in xrange(self.__cols): for j in xrange(self.__cols):
new[i, j] = other * self[i, j] new[i, j] = other * self[i, j]
return new return new
def __rmul__(self, other): def __rmul__(self, other):
# assume other is scalar and thus commutative # assume other is scalar and thus commutative
assert not isinstance(other, self.ctx.matrix) if isinstance(other, self.ctx.matrix):
raise TypeError("other should not be type of ctx.matrix")
return self.__mul__(other) return self.__mul__(other)
def __pow__(self, other): def __pow__(self, other):
# avoid cyclic import problems # avoid cyclic import problems
#from linalg import inverse #from linalg import inverse
if not isinstance(other, int): if not isinstance(other, int):
raise ValueError('only integer exponents are supported') raise ValueError('only integer exponents are supported')
if not self.__rows == self.__cols: if not self.__rows == self.__cols:
raise ValueError('only powers of square matrices are defined') raise ValueError('only powers of square matrices are defined')
n = other n = other
skipping to change at line 873 skipping to change at line 876
A[i,k], A[j,k] = A[j,k], A[i,k] A[i,k], A[j,k] = A[j,k], A[i,k]
elif isinstance(A, list): elif isinstance(A, list):
A[i], A[j] = A[j], A[i] A[i], A[j] = A[j], A[i]
else: else:
raise TypeError('could not interpret type') raise TypeError('could not interpret type')
def extend(ctx, A, b): def extend(ctx, A, b):
""" """
Extend matrix A with column b and return result. Extend matrix A with column b and return result.
""" """
assert isinstance(A, ctx.matrix) if not isinstance(A, ctx.matrix):
assert A.rows == len(b) raise TypeError("A should be a type of ctx.matrix")
if A.rows != len(b):
raise ValueError("Value should be equal to len(b)")
A = A.copy() A = A.copy()
A.cols += 1 A.cols += 1
for i in xrange(A.rows): for i in xrange(A.rows):
A[i, A.cols-1] = b[i] A[i, A.cols-1] = b[i]
return A return A
def norm(ctx, x, p=2): def norm(ctx, x, p=2):
r""" r"""
Gives the entrywise `p`-norm of an iterable *x*, i.e. the vector norm Gives the entrywise `p`-norm of an iterable *x*, i.e. the vector norm
`\left(\sum_k |x_k|^p\right)^{1/p}`, for any given `1 \le p \le \infty`. `\left(\sum_k |x_k|^p\right)^{1/p}`, for any given `1 \le p \le \infty`.
 End of changes. 8 change blocks. 
9 lines changed or deleted 14 lines changed or added

Home  |  About  |  All  |  Newest  |  Fossies Dox  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTPS