matrices.py 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. from ..libmp.backend import xrange
  2. import warnings
  3. # TODO: interpret list as vectors (for multiplication)
  4. rowsep = '\n'
  5. colsep = ' '
  6. class _matrix(object):
  7. """
  8. Numerical matrix.
  9. Specify the dimensions or the data as a nested list.
  10. Elements default to zero.
  11. Use a flat list to create a column vector easily.
  12. The datatype of the context (mpf for mp, mpi for iv, and float for fp) is used to store the data.
  13. Creating matrices
  14. -----------------
  15. Matrices in mpmath are implemented using dictionaries. Only non-zero values
  16. are stored, so it is cheap to represent sparse matrices.
  17. The most basic way to create one is to use the ``matrix`` class directly.
  18. You can create an empty matrix specifying the dimensions:
  19. >>> from mpmath import *
  20. >>> mp.dps = 15
  21. >>> matrix(2)
  22. matrix(
  23. [['0.0', '0.0'],
  24. ['0.0', '0.0']])
  25. >>> matrix(2, 3)
  26. matrix(
  27. [['0.0', '0.0', '0.0'],
  28. ['0.0', '0.0', '0.0']])
  29. Calling ``matrix`` with one dimension will create a square matrix.
  30. To access the dimensions of a matrix, use the ``rows`` or ``cols`` keyword:
  31. >>> A = matrix(3, 2)
  32. >>> A
  33. matrix(
  34. [['0.0', '0.0'],
  35. ['0.0', '0.0'],
  36. ['0.0', '0.0']])
  37. >>> A.rows
  38. 3
  39. >>> A.cols
  40. 2
  41. You can also change the dimension of an existing matrix. This will set the
  42. new elements to 0. If the new dimension is smaller than before, the
  43. concerning elements are discarded:
  44. >>> A.rows = 2
  45. >>> A
  46. matrix(
  47. [['0.0', '0.0'],
  48. ['0.0', '0.0']])
  49. Internally ``mpmathify`` is used every time an element is set. This
  50. is done using the syntax A[row,column], counting from 0:
  51. >>> A = matrix(2)
  52. >>> A[1,1] = 1 + 1j
  53. >>> A
  54. matrix(
  55. [['0.0', '0.0'],
  56. ['0.0', mpc(real='1.0', imag='1.0')]])
  57. A more comfortable way to create a matrix lets you use nested lists:
  58. >>> matrix([[1, 2], [3, 4]])
  59. matrix(
  60. [['1.0', '2.0'],
  61. ['3.0', '4.0']])
  62. Convenient advanced functions are available for creating various standard
  63. matrices, see ``zeros``, ``ones``, ``diag``, ``eye``, ``randmatrix`` and
  64. ``hilbert``.
  65. Vectors
  66. .......
  67. Vectors may also be represented by the ``matrix`` class (with rows = 1 or cols = 1).
  68. For vectors there are some things which make life easier. A column vector can
  69. be created using a flat list, a row vectors using an almost flat nested list::
  70. >>> matrix([1, 2, 3])
  71. matrix(
  72. [['1.0'],
  73. ['2.0'],
  74. ['3.0']])
  75. >>> matrix([[1, 2, 3]])
  76. matrix(
  77. [['1.0', '2.0', '3.0']])
  78. Optionally vectors can be accessed like lists, using only a single index::
  79. >>> x = matrix([1, 2, 3])
  80. >>> x[1]
  81. mpf('2.0')
  82. >>> x[1,0]
  83. mpf('2.0')
  84. Other
  85. .....
  86. Like you probably expected, matrices can be printed::
  87. >>> print randmatrix(3) # doctest:+SKIP
  88. [ 0.782963853573023 0.802057689719883 0.427895717335467]
  89. [0.0541876859348597 0.708243266653103 0.615134039977379]
  90. [ 0.856151514955773 0.544759264818486 0.686210904770947]
  91. Use ``nstr`` or ``nprint`` to specify the number of digits to print::
  92. >>> nprint(randmatrix(5), 3) # doctest:+SKIP
  93. [2.07e-1 1.66e-1 5.06e-1 1.89e-1 8.29e-1]
  94. [6.62e-1 6.55e-1 4.47e-1 4.82e-1 2.06e-2]
  95. [4.33e-1 7.75e-1 6.93e-2 2.86e-1 5.71e-1]
  96. [1.01e-1 2.53e-1 6.13e-1 3.32e-1 2.59e-1]
  97. [1.56e-1 7.27e-2 6.05e-1 6.67e-2 2.79e-1]
  98. As matrices are mutable, you will need to copy them sometimes::
  99. >>> A = matrix(2)
  100. >>> A
  101. matrix(
  102. [['0.0', '0.0'],
  103. ['0.0', '0.0']])
  104. >>> B = A.copy()
  105. >>> B[0,0] = 1
  106. >>> B
  107. matrix(
  108. [['1.0', '0.0'],
  109. ['0.0', '0.0']])
  110. >>> A
  111. matrix(
  112. [['0.0', '0.0'],
  113. ['0.0', '0.0']])
  114. Finally, it is possible to convert a matrix to a nested list. This is very useful,
  115. as most Python libraries involving matrices or arrays (namely NumPy or SymPy)
  116. support this format::
  117. >>> B.tolist()
  118. [[mpf('1.0'), mpf('0.0')], [mpf('0.0'), mpf('0.0')]]
  119. Matrix operations
  120. -----------------
  121. You can add and subtract matrices of compatible dimensions::
  122. >>> A = matrix([[1, 2], [3, 4]])
  123. >>> B = matrix([[-2, 4], [5, 9]])
  124. >>> A + B
  125. matrix(
  126. [['-1.0', '6.0'],
  127. ['8.0', '13.0']])
  128. >>> A - B
  129. matrix(
  130. [['3.0', '-2.0'],
  131. ['-2.0', '-5.0']])
  132. >>> A + ones(3) # doctest:+ELLIPSIS
  133. Traceback (most recent call last):
  134. ...
  135. ValueError: incompatible dimensions for addition
  136. It is possible to multiply or add matrices and scalars. In the latter case the
  137. operation will be done element-wise::
  138. >>> A * 2
  139. matrix(
  140. [['2.0', '4.0'],
  141. ['6.0', '8.0']])
  142. >>> A / 4
  143. matrix(
  144. [['0.25', '0.5'],
  145. ['0.75', '1.0']])
  146. >>> A - 1
  147. matrix(
  148. [['0.0', '1.0'],
  149. ['2.0', '3.0']])
  150. Of course you can perform matrix multiplication, if the dimensions are
  151. compatible, using ``@`` (for Python >= 3.5) or ``*``. For clarity, ``@`` is
  152. recommended (`PEP 465 <https://www.python.org/dev/peps/pep-0465/>`), because
  153. the meaning of ``*`` is different in many other Python libraries such as NumPy.
  154. >>> A @ B # doctest:+SKIP
  155. matrix(
  156. [['8.0', '22.0'],
  157. ['14.0', '48.0']])
  158. >>> A * B # same as A @ B
  159. matrix(
  160. [['8.0', '22.0'],
  161. ['14.0', '48.0']])
  162. >>> matrix([[1, 2, 3]]) * matrix([[-6], [7], [-2]])
  163. matrix(
  164. [['2.0']])
  165. ..
  166. COMMENT: TODO: the above "doctest:+SKIP" may be removed as soon as we
  167. have dropped support for Python 3.4 and below.
  168. You can raise powers of square matrices::
  169. >>> A**2
  170. matrix(
  171. [['7.0', '10.0'],
  172. ['15.0', '22.0']])
  173. Negative powers will calculate the inverse::
  174. >>> A**-1
  175. matrix(
  176. [['-2.0', '1.0'],
  177. ['1.5', '-0.5']])
  178. >>> A * A**-1
  179. matrix(
  180. [['1.0', '1.0842021724855e-19'],
  181. ['-2.16840434497101e-19', '1.0']])
  182. Matrix transposition is straightforward::
  183. >>> A = ones(2, 3)
  184. >>> A
  185. matrix(
  186. [['1.0', '1.0', '1.0'],
  187. ['1.0', '1.0', '1.0']])
  188. >>> A.T
  189. matrix(
  190. [['1.0', '1.0'],
  191. ['1.0', '1.0'],
  192. ['1.0', '1.0']])
  193. Norms
  194. .....
  195. Sometimes you need to know how "large" a matrix or vector is. Due to their
  196. multidimensional nature it's not possible to compare them, but there are
  197. several functions to map a matrix or a vector to a positive real number, the
  198. so called norms.
  199. For vectors the p-norm is intended, usually the 1-, the 2- and the oo-norm are
  200. used.
  201. >>> x = matrix([-10, 2, 100])
  202. >>> norm(x, 1)
  203. mpf('112.0')
  204. >>> norm(x, 2)
  205. mpf('100.5186549850325')
  206. >>> norm(x, inf)
  207. mpf('100.0')
  208. Please note that the 2-norm is the most used one, though it is more expensive
  209. to calculate than the 1- or oo-norm.
  210. It is possible to generalize some vector norms to matrix norm::
  211. >>> A = matrix([[1, -1000], [100, 50]])
  212. >>> mnorm(A, 1)
  213. mpf('1050.0')
  214. >>> mnorm(A, inf)
  215. mpf('1001.0')
  216. >>> mnorm(A, 'F')
  217. mpf('1006.2310867787777')
  218. The last norm (the "Frobenius-norm") is an approximation for the 2-norm, which
  219. is hard to calculate and not available. The Frobenius-norm lacks some
  220. mathematical properties you might expect from a norm.
  221. """
  222. def __init__(self, *args, **kwargs):
  223. self.__data = {}
  224. # LU decompostion cache, this is useful when solving the same system
  225. # multiple times, when calculating the inverse and when calculating the
  226. # determinant
  227. self._LU = None
  228. if "force_type" in kwargs:
  229. warnings.warn("The force_type argument was removed, it did not work"
  230. " properly anyway. If you want to force floating-point or"
  231. " interval computations, use the respective methods from `fp`"
  232. " or `mp` instead, e.g., `fp.matrix()` or `iv.matrix()`."
  233. " If you want to truncate values to integer, use .apply(int) instead.")
  234. if isinstance(args[0], (list, tuple)):
  235. if isinstance(args[0][0], (list, tuple)):
  236. # interpret nested list as matrix
  237. A = args[0]
  238. self.__rows = len(A)
  239. self.__cols = len(A[0])
  240. for i, row in enumerate(A):
  241. for j, a in enumerate(row):
  242. # note: this will call __setitem__ which will call self.ctx.convert() to convert the datatype.
  243. self[i, j] = a
  244. else:
  245. # interpret list as row vector
  246. v = args[0]
  247. self.__rows = len(v)
  248. self.__cols = 1
  249. for i, e in enumerate(v):
  250. self[i, 0] = e
  251. elif isinstance(args[0], int):
  252. # create empty matrix of given dimensions
  253. if len(args) == 1:
  254. self.__rows = self.__cols = args[0]
  255. else:
  256. if not isinstance(args[1], int):
  257. raise TypeError("expected int")
  258. self.__rows = args[0]
  259. self.__cols = args[1]
  260. elif isinstance(args[0], _matrix):
  261. A = args[0]
  262. self.__rows = A._matrix__rows
  263. self.__cols = A._matrix__cols
  264. for i in xrange(A.__rows):
  265. for j in xrange(A.__cols):
  266. self[i, j] = A[i, j]
  267. elif hasattr(args[0], 'tolist'):
  268. A = self.ctx.matrix(args[0].tolist())
  269. self.__data = A._matrix__data
  270. self.__rows = A._matrix__rows
  271. self.__cols = A._matrix__cols
  272. else:
  273. raise TypeError('could not interpret given arguments')
  274. def apply(self, f):
  275. """
  276. Return a copy of self with the function `f` applied elementwise.
  277. """
  278. new = self.ctx.matrix(self.__rows, self.__cols)
  279. for i in xrange(self.__rows):
  280. for j in xrange(self.__cols):
  281. new[i,j] = f(self[i,j])
  282. return new
  283. def __nstr__(self, n=None, **kwargs):
  284. # Build table of string representations of the elements
  285. res = []
  286. # Track per-column max lengths for pretty alignment
  287. maxlen = [0] * self.cols
  288. for i in range(self.rows):
  289. res.append([])
  290. for j in range(self.cols):
  291. if n:
  292. string = self.ctx.nstr(self[i,j], n, **kwargs)
  293. else:
  294. string = str(self[i,j])
  295. res[-1].append(string)
  296. maxlen[j] = max(len(string), maxlen[j])
  297. # Patch strings together
  298. for i, row in enumerate(res):
  299. for j, elem in enumerate(row):
  300. # Pad each element up to maxlen so the columns line up
  301. row[j] = elem.rjust(maxlen[j])
  302. res[i] = "[" + colsep.join(row) + "]"
  303. return rowsep.join(res)
  304. def __str__(self):
  305. return self.__nstr__()
  306. def _toliststr(self, avoid_type=False):
  307. """
  308. Create a list string from a matrix.
  309. If avoid_type: avoid multiple 'mpf's.
  310. """
  311. # XXX: should be something like self.ctx._types
  312. typ = self.ctx.mpf
  313. s = '['
  314. for i in xrange(self.__rows):
  315. s += '['
  316. for j in xrange(self.__cols):
  317. if not avoid_type or not isinstance(self[i,j], typ):
  318. a = repr(self[i,j])
  319. else:
  320. a = "'" + str(self[i,j]) + "'"
  321. s += a + ', '
  322. s = s[:-2]
  323. s += '],\n '
  324. s = s[:-3]
  325. s += ']'
  326. return s
  327. def tolist(self):
  328. """
  329. Convert the matrix to a nested list.
  330. """
  331. return [[self[i,j] for j in range(self.__cols)] for i in range(self.__rows)]
  332. def __repr__(self):
  333. if self.ctx.pretty:
  334. return self.__str__()
  335. s = 'matrix(\n'
  336. s += self._toliststr(avoid_type=True) + ')'
  337. return s
  338. def __get_element(self, key):
  339. '''
  340. Fast extraction of the i,j element from the matrix
  341. This function is for private use only because is unsafe:
  342. 1. Does not check on the value of key it expects key to be a integer tuple (i,j)
  343. 2. Does not check bounds
  344. '''
  345. if key in self.__data:
  346. return self.__data[key]
  347. else:
  348. return self.ctx.zero
  349. def __set_element(self, key, value):
  350. '''
  351. Fast assignment of the i,j element in the matrix
  352. This function is unsafe:
  353. 1. Does not check on the value of key it expects key to be a integer tuple (i,j)
  354. 2. Does not check bounds
  355. 3. Does not check the value type
  356. 4. Does not reset the LU cache
  357. '''
  358. if value: # only store non-zeros
  359. self.__data[key] = value
  360. elif key in self.__data:
  361. del self.__data[key]
  362. def __getitem__(self, key):
  363. '''
  364. Getitem function for mp matrix class with slice index enabled
  365. it allows the following assingments
  366. scalar to a slice of the matrix
  367. B = A[:,2:6]
  368. '''
  369. # Convert vector to matrix indexing
  370. if isinstance(key, int) or isinstance(key,slice):
  371. # only sufficent for vectors
  372. if self.__rows == 1:
  373. key = (0, key)
  374. elif self.__cols == 1:
  375. key = (key, 0)
  376. else:
  377. raise IndexError('insufficient indices for matrix')
  378. if isinstance(key[0],slice) or isinstance(key[1],slice):
  379. #Rows
  380. if isinstance(key[0],slice):
  381. #Check bounds
  382. if (key[0].start is None or key[0].start >= 0) and \
  383. (key[0].stop is None or key[0].stop <= self.__rows+1):
  384. # Generate indices
  385. rows = xrange(*key[0].indices(self.__rows))
  386. else:
  387. raise IndexError('Row index out of bounds')
  388. else:
  389. # Single row
  390. rows = [key[0]]
  391. # Columns
  392. if isinstance(key[1],slice):
  393. # Check bounds
  394. if (key[1].start is None or key[1].start >= 0) and \
  395. (key[1].stop is None or key[1].stop <= self.__cols+1):
  396. # Generate indices
  397. columns = xrange(*key[1].indices(self.__cols))
  398. else:
  399. raise IndexError('Column index out of bounds')
  400. else:
  401. # Single column
  402. columns = [key[1]]
  403. # Create matrix slice
  404. m = self.ctx.matrix(len(rows),len(columns))
  405. # Assign elements to the output matrix
  406. for i,x in enumerate(rows):
  407. for j,y in enumerate(columns):
  408. m.__set_element((i,j),self.__get_element((x,y)))
  409. return m
  410. else:
  411. # single element extraction
  412. if key[0] >= self.__rows or key[1] >= self.__cols:
  413. raise IndexError('matrix index out of range')
  414. if key in self.__data:
  415. return self.__data[key]
  416. else:
  417. return self.ctx.zero
  418. def __setitem__(self, key, value):
  419. # setitem function for mp matrix class with slice index enabled
  420. # it allows the following assingments
  421. # scalar to a slice of the matrix
  422. # A[:,2:6] = 2.5
  423. # submatrix to matrix (the value matrix should be the same size as the slice size)
  424. # A[3,:] = B where A is n x m and B is n x 1
  425. # Convert vector to matrix indexing
  426. if isinstance(key, int) or isinstance(key,slice):
  427. # only sufficent for vectors
  428. if self.__rows == 1:
  429. key = (0, key)
  430. elif self.__cols == 1:
  431. key = (key, 0)
  432. else:
  433. raise IndexError('insufficient indices for matrix')
  434. # Slice indexing
  435. if isinstance(key[0],slice) or isinstance(key[1],slice):
  436. # Rows
  437. if isinstance(key[0],slice):
  438. # Check bounds
  439. if (key[0].start is None or key[0].start >= 0) and \
  440. (key[0].stop is None or key[0].stop <= self.__rows+1):
  441. # generate row indices
  442. rows = xrange(*key[0].indices(self.__rows))
  443. else:
  444. raise IndexError('Row index out of bounds')
  445. else:
  446. # Single row
  447. rows = [key[0]]
  448. # Columns
  449. if isinstance(key[1],slice):
  450. # Check bounds
  451. if (key[1].start is None or key[1].start >= 0) and \
  452. (key[1].stop is None or key[1].stop <= self.__cols+1):
  453. # Generate column indices
  454. columns = xrange(*key[1].indices(self.__cols))
  455. else:
  456. raise IndexError('Column index out of bounds')
  457. else:
  458. # Single column
  459. columns = [key[1]]
  460. # Assign slice with a scalar
  461. if isinstance(value,self.ctx.matrix):
  462. # Assign elements to matrix if input and output dimensions match
  463. if len(rows) == value.rows and len(columns) == value.cols:
  464. for i,x in enumerate(rows):
  465. for j,y in enumerate(columns):
  466. self.__set_element((x,y), value.__get_element((i,j)))
  467. else:
  468. raise ValueError('Dimensions do not match')
  469. else:
  470. # Assign slice with scalars
  471. value = self.ctx.convert(value)
  472. for i in rows:
  473. for j in columns:
  474. self.__set_element((i,j), value)
  475. else:
  476. # Single element assingment
  477. # Check bounds
  478. if key[0] >= self.__rows or key[1] >= self.__cols:
  479. raise IndexError('matrix index out of range')
  480. # Convert and store value
  481. value = self.ctx.convert(value)
  482. if value: # only store non-zeros
  483. self.__data[key] = value
  484. elif key in self.__data:
  485. del self.__data[key]
  486. if self._LU:
  487. self._LU = None
  488. return
  489. def __iter__(self):
  490. for i in xrange(self.__rows):
  491. for j in xrange(self.__cols):
  492. yield self[i,j]
  493. def __mul__(self, other):
  494. if isinstance(other, self.ctx.matrix):
  495. # dot multiplication TODO: use Strassen's method?
  496. if self.__cols != other.__rows:
  497. raise ValueError('dimensions not compatible for multiplication')
  498. new = self.ctx.matrix(self.__rows, other.__cols)
  499. for i in xrange(self.__rows):
  500. for j in xrange(other.__cols):
  501. new[i, j] = self.ctx.fdot((self[i,k], other[k,j])
  502. for k in xrange(other.__rows))
  503. return new
  504. else:
  505. # try scalar multiplication
  506. new = self.ctx.matrix(self.__rows, self.__cols)
  507. for i in xrange(self.__rows):
  508. for j in xrange(self.__cols):
  509. new[i, j] = other * self[i, j]
  510. return new
  511. def __matmul__(self, other):
  512. return self.__mul__(other)
  513. def __rmul__(self, other):
  514. # assume other is scalar and thus commutative
  515. if isinstance(other, self.ctx.matrix):
  516. raise TypeError("other should not be type of ctx.matrix")
  517. return self.__mul__(other)
  518. def __pow__(self, other):
  519. # avoid cyclic import problems
  520. #from linalg import inverse
  521. if not isinstance(other, int):
  522. raise ValueError('only integer exponents are supported')
  523. if not self.__rows == self.__cols:
  524. raise ValueError('only powers of square matrices are defined')
  525. n = other
  526. if n == 0:
  527. return self.ctx.eye(self.__rows)
  528. if n < 0:
  529. n = -n
  530. neg = True
  531. else:
  532. neg = False
  533. i = n
  534. y = 1
  535. z = self.copy()
  536. while i != 0:
  537. if i % 2 == 1:
  538. y = y * z
  539. z = z*z
  540. i = i // 2
  541. if neg:
  542. y = self.ctx.inverse(y)
  543. return y
  544. def __div__(self, other):
  545. # assume other is scalar and do element-wise divison
  546. assert not isinstance(other, self.ctx.matrix)
  547. new = self.ctx.matrix(self.__rows, self.__cols)
  548. for i in xrange(self.__rows):
  549. for j in xrange(self.__cols):
  550. new[i,j] = self[i,j] / other
  551. return new
  552. __truediv__ = __div__
  553. def __add__(self, other):
  554. if isinstance(other, self.ctx.matrix):
  555. if not (self.__rows == other.__rows and self.__cols == other.__cols):
  556. raise ValueError('incompatible dimensions for addition')
  557. new = self.ctx.matrix(self.__rows, self.__cols)
  558. for i in xrange(self.__rows):
  559. for j in xrange(self.__cols):
  560. new[i,j] = self[i,j] + other[i,j]
  561. return new
  562. else:
  563. # assume other is scalar and add element-wise
  564. new = self.ctx.matrix(self.__rows, self.__cols)
  565. for i in xrange(self.__rows):
  566. for j in xrange(self.__cols):
  567. new[i,j] += self[i,j] + other
  568. return new
  569. def __radd__(self, other):
  570. return self.__add__(other)
  571. def __sub__(self, other):
  572. if isinstance(other, self.ctx.matrix) and not (self.__rows == other.__rows
  573. and self.__cols == other.__cols):
  574. raise ValueError('incompatible dimensions for subtraction')
  575. return self.__add__(other * (-1))
  576. def __pos__(self):
  577. """
  578. +M returns a copy of M, rounded to current working precision.
  579. """
  580. return (+1) * self
  581. def __neg__(self):
  582. return (-1) * self
  583. def __rsub__(self, other):
  584. return -self + other
  585. def __eq__(self, other):
  586. return self.__rows == other.__rows and self.__cols == other.__cols \
  587. and self.__data == other.__data
  588. def __len__(self):
  589. if self.rows == 1:
  590. return self.cols
  591. elif self.cols == 1:
  592. return self.rows
  593. else:
  594. return self.rows # do it like numpy
  595. def __getrows(self):
  596. return self.__rows
  597. def __setrows(self, value):
  598. for key in self.__data.copy():
  599. if key[0] >= value:
  600. del self.__data[key]
  601. self.__rows = value
  602. rows = property(__getrows, __setrows, doc='number of rows')
  603. def __getcols(self):
  604. return self.__cols
  605. def __setcols(self, value):
  606. for key in self.__data.copy():
  607. if key[1] >= value:
  608. del self.__data[key]
  609. self.__cols = value
  610. cols = property(__getcols, __setcols, doc='number of columns')
  611. def transpose(self):
  612. new = self.ctx.matrix(self.__cols, self.__rows)
  613. for i in xrange(self.__rows):
  614. for j in xrange(self.__cols):
  615. new[j,i] = self[i,j]
  616. return new
  617. T = property(transpose)
  618. def conjugate(self):
  619. return self.apply(self.ctx.conj)
  620. def transpose_conj(self):
  621. return self.conjugate().transpose()
  622. H = property(transpose_conj)
  623. def copy(self):
  624. new = self.ctx.matrix(self.__rows, self.__cols)
  625. new.__data = self.__data.copy()
  626. return new
  627. __copy__ = copy
  628. def column(self, n):
  629. m = self.ctx.matrix(self.rows, 1)
  630. for i in range(self.rows):
  631. m[i] = self[i,n]
  632. return m
  633. class MatrixMethods(object):
  634. def __init__(ctx):
  635. # XXX: subclass
  636. ctx.matrix = type('matrix', (_matrix,), {})
  637. ctx.matrix.ctx = ctx
  638. ctx.matrix.convert = ctx.convert
  639. def eye(ctx, n, **kwargs):
  640. """
  641. Create square identity matrix n x n.
  642. """
  643. A = ctx.matrix(n, **kwargs)
  644. for i in xrange(n):
  645. A[i,i] = 1
  646. return A
  647. def diag(ctx, diagonal, **kwargs):
  648. """
  649. Create square diagonal matrix using given list.
  650. Example:
  651. >>> from mpmath import diag, mp
  652. >>> mp.pretty = False
  653. >>> diag([1, 2, 3])
  654. matrix(
  655. [['1.0', '0.0', '0.0'],
  656. ['0.0', '2.0', '0.0'],
  657. ['0.0', '0.0', '3.0']])
  658. """
  659. A = ctx.matrix(len(diagonal), **kwargs)
  660. for i in xrange(len(diagonal)):
  661. A[i,i] = diagonal[i]
  662. return A
  663. def zeros(ctx, *args, **kwargs):
  664. """
  665. Create matrix m x n filled with zeros.
  666. One given dimension will create square matrix n x n.
  667. Example:
  668. >>> from mpmath import zeros, mp
  669. >>> mp.pretty = False
  670. >>> zeros(2)
  671. matrix(
  672. [['0.0', '0.0'],
  673. ['0.0', '0.0']])
  674. """
  675. if len(args) == 1:
  676. m = n = args[0]
  677. elif len(args) == 2:
  678. m = args[0]
  679. n = args[1]
  680. else:
  681. raise TypeError('zeros expected at most 2 arguments, got %i' % len(args))
  682. A = ctx.matrix(m, n, **kwargs)
  683. for i in xrange(m):
  684. for j in xrange(n):
  685. A[i,j] = 0
  686. return A
  687. def ones(ctx, *args, **kwargs):
  688. """
  689. Create matrix m x n filled with ones.
  690. One given dimension will create square matrix n x n.
  691. Example:
  692. >>> from mpmath import ones, mp
  693. >>> mp.pretty = False
  694. >>> ones(2)
  695. matrix(
  696. [['1.0', '1.0'],
  697. ['1.0', '1.0']])
  698. """
  699. if len(args) == 1:
  700. m = n = args[0]
  701. elif len(args) == 2:
  702. m = args[0]
  703. n = args[1]
  704. else:
  705. raise TypeError('ones expected at most 2 arguments, got %i' % len(args))
  706. A = ctx.matrix(m, n, **kwargs)
  707. for i in xrange(m):
  708. for j in xrange(n):
  709. A[i,j] = 1
  710. return A
  711. def hilbert(ctx, m, n=None):
  712. """
  713. Create (pseudo) hilbert matrix m x n.
  714. One given dimension will create hilbert matrix n x n.
  715. The matrix is very ill-conditioned and symmetric, positive definite if
  716. square.
  717. """
  718. if n is None:
  719. n = m
  720. A = ctx.matrix(m, n)
  721. for i in xrange(m):
  722. for j in xrange(n):
  723. A[i,j] = ctx.one / (i + j + 1)
  724. return A
  725. def randmatrix(ctx, m, n=None, min=0, max=1, **kwargs):
  726. """
  727. Create a random m x n matrix.
  728. All values are >= min and <max.
  729. n defaults to m.
  730. Example:
  731. >>> from mpmath import randmatrix
  732. >>> randmatrix(2) # doctest:+SKIP
  733. matrix(
  734. [['0.53491598236191806', '0.57195669543302752'],
  735. ['0.85589992269513615', '0.82444367501382143']])
  736. """
  737. if not n:
  738. n = m
  739. A = ctx.matrix(m, n, **kwargs)
  740. for i in xrange(m):
  741. for j in xrange(n):
  742. A[i,j] = ctx.rand() * (max - min) + min
  743. return A
  744. def swap_row(ctx, A, i, j):
  745. """
  746. Swap row i with row j.
  747. """
  748. if i == j:
  749. return
  750. if isinstance(A, ctx.matrix):
  751. for k in xrange(A.cols):
  752. A[i,k], A[j,k] = A[j,k], A[i,k]
  753. elif isinstance(A, list):
  754. A[i], A[j] = A[j], A[i]
  755. else:
  756. raise TypeError('could not interpret type')
  757. def extend(ctx, A, b):
  758. """
  759. Extend matrix A with column b and return result.
  760. """
  761. if not isinstance(A, ctx.matrix):
  762. raise TypeError("A should be a type of ctx.matrix")
  763. if A.rows != len(b):
  764. raise ValueError("Value should be equal to len(b)")
  765. A = A.copy()
  766. A.cols += 1
  767. for i in xrange(A.rows):
  768. A[i, A.cols-1] = b[i]
  769. return A
  770. def norm(ctx, x, p=2):
  771. r"""
  772. Gives the entrywise `p`-norm of an iterable *x*, i.e. the vector norm
  773. `\left(\sum_k |x_k|^p\right)^{1/p}`, for any given `1 \le p \le \infty`.
  774. Special cases:
  775. If *x* is not iterable, this just returns ``absmax(x)``.
  776. ``p=1`` gives the sum of absolute values.
  777. ``p=2`` is the standard Euclidean vector norm.
  778. ``p=inf`` gives the magnitude of the largest element.
  779. For *x* a matrix, ``p=2`` is the Frobenius norm.
  780. For operator matrix norms, use :func:`~mpmath.mnorm` instead.
  781. You can use the string 'inf' as well as float('inf') or mpf('inf')
  782. to specify the infinity norm.
  783. **Examples**
  784. >>> from mpmath import *
  785. >>> mp.dps = 15; mp.pretty = False
  786. >>> x = matrix([-10, 2, 100])
  787. >>> norm(x, 1)
  788. mpf('112.0')
  789. >>> norm(x, 2)
  790. mpf('100.5186549850325')
  791. >>> norm(x, inf)
  792. mpf('100.0')
  793. """
  794. try:
  795. iter(x)
  796. except TypeError:
  797. return ctx.absmax(x)
  798. if type(p) is not int:
  799. p = ctx.convert(p)
  800. if p == ctx.inf:
  801. return max(ctx.absmax(i) for i in x)
  802. elif p == 1:
  803. return ctx.fsum(x, absolute=1)
  804. elif p == 2:
  805. return ctx.sqrt(ctx.fsum(x, absolute=1, squared=1))
  806. elif p > 1:
  807. return ctx.nthroot(ctx.fsum(abs(i)**p for i in x), p)
  808. else:
  809. raise ValueError('p has to be >= 1')
  810. def mnorm(ctx, A, p=1):
  811. r"""
  812. Gives the matrix (operator) `p`-norm of A. Currently ``p=1`` and ``p=inf``
  813. are supported:
  814. ``p=1`` gives the 1-norm (maximal column sum)
  815. ``p=inf`` gives the `\infty`-norm (maximal row sum).
  816. You can use the string 'inf' as well as float('inf') or mpf('inf')
  817. ``p=2`` (not implemented) for a square matrix is the usual spectral
  818. matrix norm, i.e. the largest singular value.
  819. ``p='f'`` (or 'F', 'fro', 'Frobenius, 'frobenius') gives the
  820. Frobenius norm, which is the elementwise 2-norm. The Frobenius norm is an
  821. approximation of the spectral norm and satisfies
  822. .. math ::
  823. \frac{1}{\sqrt{\mathrm{rank}(A)}} \|A\|_F \le \|A\|_2 \le \|A\|_F
  824. The Frobenius norm lacks some mathematical properties that might
  825. be expected of a norm.
  826. For general elementwise `p`-norms, use :func:`~mpmath.norm` instead.
  827. **Examples**
  828. >>> from mpmath import *
  829. >>> mp.dps = 15; mp.pretty = False
  830. >>> A = matrix([[1, -1000], [100, 50]])
  831. >>> mnorm(A, 1)
  832. mpf('1050.0')
  833. >>> mnorm(A, inf)
  834. mpf('1001.0')
  835. >>> mnorm(A, 'F')
  836. mpf('1006.2310867787777')
  837. """
  838. A = ctx.matrix(A)
  839. if type(p) is not int:
  840. if type(p) is str and 'frobenius'.startswith(p.lower()):
  841. return ctx.norm(A, 2)
  842. p = ctx.convert(p)
  843. m, n = A.rows, A.cols
  844. if p == 1:
  845. return max(ctx.fsum((A[i,j] for i in xrange(m)), absolute=1) for j in xrange(n))
  846. elif p == ctx.inf:
  847. return max(ctx.fsum((A[i,j] for j in xrange(n)), absolute=1) for i in xrange(m))
  848. else:
  849. raise NotImplementedError("matrix p-norm for arbitrary p")
  850. if __name__ == '__main__':
  851. import doctest
  852. doctest.testmod()