test_special_matrices.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. """Tests for functions in special_matrices.py."""
  2. from __future__ import division, print_function, absolute_import
  3. import numpy as np
  4. from numpy import arange, add, array, eye, copy, sqrt
  5. from numpy.testing import (assert_equal, assert_array_equal,
  6. assert_array_almost_equal, assert_allclose)
  7. from pytest import raises as assert_raises
  8. from scipy._lib.six import xrange
  9. from scipy import fftpack
  10. from scipy.special import comb
  11. from scipy.linalg import (toeplitz, hankel, circulant, hadamard, leslie,
  12. companion, tri, triu, tril, kron, block_diag,
  13. helmert, hilbert, invhilbert, pascal, invpascal, dft)
  14. from numpy.linalg import cond
  15. def get_mat(n):
  16. data = arange(n)
  17. data = add.outer(data,data)
  18. return data
  19. class TestTri(object):
  20. def test_basic(self):
  21. assert_equal(tri(4),array([[1,0,0,0],
  22. [1,1,0,0],
  23. [1,1,1,0],
  24. [1,1,1,1]]))
  25. assert_equal(tri(4,dtype='f'),array([[1,0,0,0],
  26. [1,1,0,0],
  27. [1,1,1,0],
  28. [1,1,1,1]],'f'))
  29. def test_diag(self):
  30. assert_equal(tri(4,k=1),array([[1,1,0,0],
  31. [1,1,1,0],
  32. [1,1,1,1],
  33. [1,1,1,1]]))
  34. assert_equal(tri(4,k=-1),array([[0,0,0,0],
  35. [1,0,0,0],
  36. [1,1,0,0],
  37. [1,1,1,0]]))
  38. def test_2d(self):
  39. assert_equal(tri(4,3),array([[1,0,0],
  40. [1,1,0],
  41. [1,1,1],
  42. [1,1,1]]))
  43. assert_equal(tri(3,4),array([[1,0,0,0],
  44. [1,1,0,0],
  45. [1,1,1,0]]))
  46. def test_diag2d(self):
  47. assert_equal(tri(3,4,k=2),array([[1,1,1,0],
  48. [1,1,1,1],
  49. [1,1,1,1]]))
  50. assert_equal(tri(4,3,k=-2),array([[0,0,0],
  51. [0,0,0],
  52. [1,0,0],
  53. [1,1,0]]))
  54. class TestTril(object):
  55. def test_basic(self):
  56. a = (100*get_mat(5)).astype('l')
  57. b = a.copy()
  58. for k in range(5):
  59. for l in range(k+1,5):
  60. b[k,l] = 0
  61. assert_equal(tril(a),b)
  62. def test_diag(self):
  63. a = (100*get_mat(5)).astype('f')
  64. b = a.copy()
  65. for k in range(5):
  66. for l in range(k+3,5):
  67. b[k,l] = 0
  68. assert_equal(tril(a,k=2),b)
  69. b = a.copy()
  70. for k in range(5):
  71. for l in range(max((k-1,0)),5):
  72. b[k,l] = 0
  73. assert_equal(tril(a,k=-2),b)
  74. class TestTriu(object):
  75. def test_basic(self):
  76. a = (100*get_mat(5)).astype('l')
  77. b = a.copy()
  78. for k in range(5):
  79. for l in range(k+1,5):
  80. b[l,k] = 0
  81. assert_equal(triu(a),b)
  82. def test_diag(self):
  83. a = (100*get_mat(5)).astype('f')
  84. b = a.copy()
  85. for k in range(5):
  86. for l in range(max((k-1,0)),5):
  87. b[l,k] = 0
  88. assert_equal(triu(a,k=2),b)
  89. b = a.copy()
  90. for k in range(5):
  91. for l in range(k+3,5):
  92. b[l,k] = 0
  93. assert_equal(triu(a,k=-2),b)
  94. class TestToeplitz(object):
  95. def test_basic(self):
  96. y = toeplitz([1,2,3])
  97. assert_array_equal(y,[[1,2,3],[2,1,2],[3,2,1]])
  98. y = toeplitz([1,2,3],[1,4,5])
  99. assert_array_equal(y,[[1,4,5],[2,1,4],[3,2,1]])
  100. def test_complex_01(self):
  101. data = (1.0 + arange(3.0)) * (1.0 + 1.0j)
  102. x = copy(data)
  103. t = toeplitz(x)
  104. # Calling toeplitz should not change x.
  105. assert_array_equal(x, data)
  106. # According to the docstring, x should be the first column of t.
  107. col0 = t[:,0]
  108. assert_array_equal(col0, data)
  109. assert_array_equal(t[0,1:], data[1:].conj())
  110. def test_scalar_00(self):
  111. """Scalar arguments still produce a 2D array."""
  112. t = toeplitz(10)
  113. assert_array_equal(t, [[10]])
  114. t = toeplitz(10, 20)
  115. assert_array_equal(t, [[10]])
  116. def test_scalar_01(self):
  117. c = array([1,2,3])
  118. t = toeplitz(c, 1)
  119. assert_array_equal(t, [[1],[2],[3]])
  120. def test_scalar_02(self):
  121. c = array([1,2,3])
  122. t = toeplitz(c, array(1))
  123. assert_array_equal(t, [[1],[2],[3]])
  124. def test_scalar_03(self):
  125. c = array([1,2,3])
  126. t = toeplitz(c, array([1]))
  127. assert_array_equal(t, [[1],[2],[3]])
  128. def test_scalar_04(self):
  129. r = array([10,2,3])
  130. t = toeplitz(1, r)
  131. assert_array_equal(t, [[1,2,3]])
  132. class TestHankel(object):
  133. def test_basic(self):
  134. y = hankel([1,2,3])
  135. assert_array_equal(y, [[1,2,3], [2,3,0], [3,0,0]])
  136. y = hankel([1,2,3], [3,4,5])
  137. assert_array_equal(y, [[1,2,3], [2,3,4], [3,4,5]])
  138. class TestCirculant(object):
  139. def test_basic(self):
  140. y = circulant([1,2,3])
  141. assert_array_equal(y, [[1,3,2], [2,1,3], [3,2,1]])
  142. class TestHadamard(object):
  143. def test_basic(self):
  144. y = hadamard(1)
  145. assert_array_equal(y, [[1]])
  146. y = hadamard(2, dtype=float)
  147. assert_array_equal(y, [[1.0, 1.0], [1.0, -1.0]])
  148. y = hadamard(4)
  149. assert_array_equal(y, [[1,1,1,1], [1,-1,1,-1], [1,1,-1,-1], [1,-1,-1,1]])
  150. assert_raises(ValueError, hadamard, 0)
  151. assert_raises(ValueError, hadamard, 5)
  152. class TestLeslie(object):
  153. def test_bad_shapes(self):
  154. assert_raises(ValueError, leslie, [[1,1],[2,2]], [3,4,5])
  155. assert_raises(ValueError, leslie, [3,4,5], [[1,1],[2,2]])
  156. assert_raises(ValueError, leslie, [1,2], [1,2])
  157. assert_raises(ValueError, leslie, [1], [])
  158. def test_basic(self):
  159. a = leslie([1, 2, 3], [0.25, 0.5])
  160. expected = array([
  161. [1.0, 2.0, 3.0],
  162. [0.25, 0.0, 0.0],
  163. [0.0, 0.5, 0.0]])
  164. assert_array_equal(a, expected)
  165. class TestCompanion(object):
  166. def test_bad_shapes(self):
  167. assert_raises(ValueError, companion, [[1,1],[2,2]])
  168. assert_raises(ValueError, companion, [0,4,5])
  169. assert_raises(ValueError, companion, [1])
  170. assert_raises(ValueError, companion, [])
  171. def test_basic(self):
  172. c = companion([1, 2, 3])
  173. expected = array([
  174. [-2.0, -3.0],
  175. [1.0, 0.0]])
  176. assert_array_equal(c, expected)
  177. c = companion([2.0, 5.0, -10.0])
  178. expected = array([
  179. [-2.5, 5.0],
  180. [1.0, 0.0]])
  181. assert_array_equal(c, expected)
  182. class TestBlockDiag:
  183. def test_basic(self):
  184. x = block_diag(eye(2), [[1,2], [3,4], [5,6]], [[1, 2, 3]])
  185. assert_array_equal(x, [[1, 0, 0, 0, 0, 0, 0],
  186. [0, 1, 0, 0, 0, 0, 0],
  187. [0, 0, 1, 2, 0, 0, 0],
  188. [0, 0, 3, 4, 0, 0, 0],
  189. [0, 0, 5, 6, 0, 0, 0],
  190. [0, 0, 0, 0, 1, 2, 3]])
  191. def test_dtype(self):
  192. x = block_diag([[1.5]])
  193. assert_equal(x.dtype, float)
  194. x = block_diag([[True]])
  195. assert_equal(x.dtype, bool)
  196. def test_mixed_dtypes(self):
  197. actual = block_diag([[1]], [[1j]])
  198. desired = np.array([[1, 0], [0, 1j]])
  199. assert_array_equal(actual, desired)
  200. def test_scalar_and_1d_args(self):
  201. a = block_diag(1)
  202. assert_equal(a.shape, (1,1))
  203. assert_array_equal(a, [[1]])
  204. a = block_diag([2,3], 4)
  205. assert_array_equal(a, [[2, 3, 0], [0, 0, 4]])
  206. def test_bad_arg(self):
  207. assert_raises(ValueError, block_diag, [[[1]]])
  208. def test_no_args(self):
  209. a = block_diag()
  210. assert_equal(a.ndim, 2)
  211. assert_equal(a.nbytes, 0)
  212. def test_empty_matrix_arg(self):
  213. # regression test for gh-4596: check the shape of the result
  214. # for empty matrix inputs. Empty matrices are no longer ignored
  215. # (gh-4908) it is viewed as a shape (1, 0) matrix.
  216. a = block_diag([[1, 0], [0, 1]],
  217. [],
  218. [[2, 3], [4, 5], [6, 7]])
  219. assert_array_equal(a, [[1, 0, 0, 0],
  220. [0, 1, 0, 0],
  221. [0, 0, 0, 0],
  222. [0, 0, 2, 3],
  223. [0, 0, 4, 5],
  224. [0, 0, 6, 7]])
  225. def test_zerosized_matrix_arg(self):
  226. # test for gh-4908: check the shape of the result for
  227. # zero-sized matrix inputs, i.e. matrices with shape (0,n) or (n,0).
  228. # note that [[]] takes shape (1,0)
  229. a = block_diag([[1, 0], [0, 1]],
  230. [[]],
  231. [[2, 3], [4, 5], [6, 7]],
  232. np.zeros([0,2],dtype='int32'))
  233. assert_array_equal(a, [[1, 0, 0, 0, 0, 0],
  234. [0, 1, 0, 0, 0, 0],
  235. [0, 0, 0, 0, 0, 0],
  236. [0, 0, 2, 3, 0, 0],
  237. [0, 0, 4, 5, 0, 0],
  238. [0, 0, 6, 7, 0, 0]])
  239. class TestKron:
  240. def test_basic(self):
  241. a = kron(array([[1, 2], [3, 4]]), array([[1, 1, 1]]))
  242. assert_array_equal(a, array([[1, 1, 1, 2, 2, 2],
  243. [3, 3, 3, 4, 4, 4]]))
  244. m1 = array([[1, 2], [3, 4]])
  245. m2 = array([[10], [11]])
  246. a = kron(m1, m2)
  247. expected = array([[10, 20],
  248. [11, 22],
  249. [30, 40],
  250. [33, 44]])
  251. assert_array_equal(a, expected)
  252. class TestHelmert(object):
  253. def test_orthogonality(self):
  254. for n in range(1, 7):
  255. H = helmert(n, full=True)
  256. Id = np.eye(n)
  257. assert_allclose(H.dot(H.T), Id, atol=1e-12)
  258. assert_allclose(H.T.dot(H), Id, atol=1e-12)
  259. def test_subspace(self):
  260. for n in range(2, 7):
  261. H_full = helmert(n, full=True)
  262. H_partial = helmert(n)
  263. for U in H_full[1:, :].T, H_partial.T:
  264. C = np.eye(n) - np.ones((n, n)) / n
  265. assert_allclose(U.dot(U.T), C)
  266. assert_allclose(U.T.dot(U), np.eye(n-1), atol=1e-12)
  267. class TestHilbert(object):
  268. def test_basic(self):
  269. h3 = array([[1.0, 1/2., 1/3.],
  270. [1/2., 1/3., 1/4.],
  271. [1/3., 1/4., 1/5.]])
  272. assert_array_almost_equal(hilbert(3), h3)
  273. assert_array_equal(hilbert(1), [[1.0]])
  274. h0 = hilbert(0)
  275. assert_equal(h0.shape, (0,0))
  276. class TestInvHilbert(object):
  277. def test_basic(self):
  278. invh1 = array([[1]])
  279. assert_array_equal(invhilbert(1, exact=True), invh1)
  280. assert_array_equal(invhilbert(1), invh1)
  281. invh2 = array([[4, -6],
  282. [-6, 12]])
  283. assert_array_equal(invhilbert(2, exact=True), invh2)
  284. assert_array_almost_equal(invhilbert(2), invh2)
  285. invh3 = array([[9, -36, 30],
  286. [-36, 192, -180],
  287. [30, -180, 180]])
  288. assert_array_equal(invhilbert(3, exact=True), invh3)
  289. assert_array_almost_equal(invhilbert(3), invh3)
  290. invh4 = array([[16, -120, 240, -140],
  291. [-120, 1200, -2700, 1680],
  292. [240, -2700, 6480, -4200],
  293. [-140, 1680, -4200, 2800]])
  294. assert_array_equal(invhilbert(4, exact=True), invh4)
  295. assert_array_almost_equal(invhilbert(4), invh4)
  296. invh5 = array([[25, -300, 1050, -1400, 630],
  297. [-300, 4800, -18900, 26880, -12600],
  298. [1050, -18900, 79380, -117600, 56700],
  299. [-1400, 26880, -117600, 179200, -88200],
  300. [630, -12600, 56700, -88200, 44100]])
  301. assert_array_equal(invhilbert(5, exact=True), invh5)
  302. assert_array_almost_equal(invhilbert(5), invh5)
  303. invh17 = array([
  304. [289, -41616, 1976760, -46124400, 629598060, -5540462928,
  305. 33374693352, -143034400080, 446982500250, -1033026222800,
  306. 1774926873720, -2258997839280, 2099709530100, -1384423866000,
  307. 613101997800, -163493866080, 19835652870],
  308. [-41616, 7990272, -426980160, 10627061760, -151103534400, 1367702848512,
  309. -8410422724704, 36616806420480, -115857864064800, 270465047424000,
  310. -468580694662080, 600545887119360, -561522320049600, 372133135180800,
  311. -165537539406000, 44316454993920, -5395297580640],
  312. [1976760, -426980160, 24337869120, -630981792000, 9228108708000,
  313. -85267724461920, 532660105897920, -2348052711713280, 7504429831470000,
  314. -17664748409880000, 30818191841236800, -39732544853164800,
  315. 37341234283298400, -24857330514030000, 11100752642520000,
  316. -2982128117299200, 364182586693200],
  317. [-46124400, 10627061760, -630981792000, 16826181120000,
  318. -251209625940000, 2358021022156800, -14914482965141760,
  319. 66409571644416000, -214015221119700000, 507295338950400000,
  320. -890303319857952000, 1153715376477081600, -1089119333262870000,
  321. 727848632044800000, -326170262829600000, 87894302404608000,
  322. -10763618673376800],
  323. [629598060, -151103534400, 9228108708000,
  324. -251209625940000, 3810012660090000, -36210360321495360,
  325. 231343968720664800, -1038687206500944000, 3370739732635275000,
  326. -8037460526495400000, 14178080368737885600, -18454939322943942000,
  327. 17489975175339030000, -11728977435138600000, 5272370630081100000,
  328. -1424711708039692800, 174908803442373000],
  329. [-5540462928, 1367702848512, -85267724461920, 2358021022156800,
  330. -36210360321495360, 347619459086355456, -2239409617216035264,
  331. 10124803292907663360, -33052510749726468000, 79217210949138662400,
  332. -140362995650505067440, 183420385176741672960, -174433352415381259200,
  333. 117339159519533952000, -52892422160973595200, 14328529177999196160,
  334. -1763080738699119840],
  335. [33374693352, -8410422724704, 532660105897920,
  336. -14914482965141760, 231343968720664800, -2239409617216035264,
  337. 14527452132196331328, -66072377044391477760, 216799987176909536400,
  338. -521925895055522958000, 928414062734059661760, -1217424500995626443520,
  339. 1161358898976091015200, -783401860847777371200, 354015418167362952000,
  340. -96120549902411274240, 11851820521255194480],
  341. [-143034400080, 36616806420480, -2348052711713280, 66409571644416000,
  342. -1038687206500944000, 10124803292907663360, -66072377044391477760,
  343. 302045152202932469760, -995510145200094810000, 2405996923185123840000,
  344. -4294704507885446054400, 5649058909023744614400,
  345. -5403874060541811254400, 3654352703663101440000,
  346. -1655137020003255360000, 450325202737117593600, -55630994283442749600],
  347. [446982500250, -115857864064800, 7504429831470000, -214015221119700000,
  348. 3370739732635275000, -33052510749726468000, 216799987176909536400,
  349. -995510145200094810000, 3293967392206196062500,
  350. -7988661659013106500000, 14303908928401362270000,
  351. -18866974090684772052000, 18093328327706957325000,
  352. -12263364009096700500000, 5565847995255512250000,
  353. -1517208935002984080000, 187754605706619279900],
  354. [-1033026222800, 270465047424000, -17664748409880000,
  355. 507295338950400000, -8037460526495400000, 79217210949138662400,
  356. -521925895055522958000, 2405996923185123840000,
  357. -7988661659013106500000, 19434404971634224000000,
  358. -34894474126569249192000, 46141453390504792320000,
  359. -44349976506971935800000, 30121928988527376000000,
  360. -13697025107665828500000, 3740200989399948902400,
  361. -463591619028689580000],
  362. [1774926873720, -468580694662080,
  363. 30818191841236800, -890303319857952000, 14178080368737885600,
  364. -140362995650505067440, 928414062734059661760, -4294704507885446054400,
  365. 14303908928401362270000, -34894474126569249192000,
  366. 62810053427824648545600, -83243376594051600326400,
  367. 80177044485212743068000, -54558343880470209780000,
  368. 24851882355348879230400, -6797096028813368678400, 843736746632215035600],
  369. [-2258997839280, 600545887119360, -39732544853164800,
  370. 1153715376477081600, -18454939322943942000, 183420385176741672960,
  371. -1217424500995626443520, 5649058909023744614400,
  372. -18866974090684772052000, 46141453390504792320000,
  373. -83243376594051600326400, 110552468520163390156800,
  374. -106681852579497947388000, 72720410752415168870400,
  375. -33177973900974346080000, 9087761081682520473600,
  376. -1129631016152221783200],
  377. [2099709530100, -561522320049600, 37341234283298400,
  378. -1089119333262870000, 17489975175339030000, -174433352415381259200,
  379. 1161358898976091015200, -5403874060541811254400,
  380. 18093328327706957325000, -44349976506971935800000,
  381. 80177044485212743068000, -106681852579497947388000,
  382. 103125790826848015808400, -70409051543137015800000,
  383. 32171029219823375700000, -8824053728865840192000,
  384. 1098252376814660067000],
  385. [-1384423866000, 372133135180800,
  386. -24857330514030000, 727848632044800000, -11728977435138600000,
  387. 117339159519533952000, -783401860847777371200, 3654352703663101440000,
  388. -12263364009096700500000, 30121928988527376000000,
  389. -54558343880470209780000, 72720410752415168870400,
  390. -70409051543137015800000, 48142941226076592000000,
  391. -22027500987368499000000, 6049545098753157120000,
  392. -753830033789944188000],
  393. [613101997800, -165537539406000,
  394. 11100752642520000, -326170262829600000, 5272370630081100000,
  395. -52892422160973595200, 354015418167362952000, -1655137020003255360000,
  396. 5565847995255512250000, -13697025107665828500000,
  397. 24851882355348879230400, -33177973900974346080000,
  398. 32171029219823375700000, -22027500987368499000000,
  399. 10091416708498869000000, -2774765838662800128000, 346146444087219270000],
  400. [-163493866080, 44316454993920, -2982128117299200, 87894302404608000,
  401. -1424711708039692800, 14328529177999196160, -96120549902411274240,
  402. 450325202737117593600, -1517208935002984080000, 3740200989399948902400,
  403. -6797096028813368678400, 9087761081682520473600,
  404. -8824053728865840192000, 6049545098753157120000,
  405. -2774765838662800128000, 763806510427609497600, -95382575704033754400],
  406. [19835652870, -5395297580640, 364182586693200, -10763618673376800,
  407. 174908803442373000, -1763080738699119840, 11851820521255194480,
  408. -55630994283442749600, 187754605706619279900, -463591619028689580000,
  409. 843736746632215035600, -1129631016152221783200, 1098252376814660067000,
  410. -753830033789944188000, 346146444087219270000, -95382575704033754400,
  411. 11922821963004219300]
  412. ])
  413. assert_array_equal(invhilbert(17, exact=True), invh17)
  414. assert_allclose(invhilbert(17), invh17.astype(float), rtol=1e-12)
  415. def test_inverse(self):
  416. for n in xrange(1, 10):
  417. a = hilbert(n)
  418. b = invhilbert(n)
  419. # The Hilbert matrix is increasingly badly conditioned,
  420. # so take that into account in the test
  421. c = cond(a)
  422. assert_allclose(a.dot(b), eye(n), atol=1e-15*c, rtol=1e-15*c)
  423. class TestPascal(object):
  424. cases = [
  425. (1, array([[1]]), array([[1]])),
  426. (2, array([[1, 1],
  427. [1, 2]]),
  428. array([[1, 0],
  429. [1, 1]])),
  430. (3, array([[1, 1, 1],
  431. [1, 2, 3],
  432. [1, 3, 6]]),
  433. array([[1, 0, 0],
  434. [1, 1, 0],
  435. [1, 2, 1]])),
  436. (4, array([[1, 1, 1, 1],
  437. [1, 2, 3, 4],
  438. [1, 3, 6, 10],
  439. [1, 4, 10, 20]]),
  440. array([[1, 0, 0, 0],
  441. [1, 1, 0, 0],
  442. [1, 2, 1, 0],
  443. [1, 3, 3, 1]])),
  444. ]
  445. def check_case(self, n, sym, low):
  446. assert_array_equal(pascal(n), sym)
  447. assert_array_equal(pascal(n, kind='lower'), low)
  448. assert_array_equal(pascal(n, kind='upper'), low.T)
  449. assert_array_almost_equal(pascal(n, exact=False), sym)
  450. assert_array_almost_equal(pascal(n, exact=False, kind='lower'), low)
  451. assert_array_almost_equal(pascal(n, exact=False, kind='upper'), low.T)
  452. def test_cases(self):
  453. for n, sym, low in self.cases:
  454. self.check_case(n, sym, low)
  455. def test_big(self):
  456. p = pascal(50)
  457. assert_equal(p[-1, -1], comb(98, 49, exact=True))
  458. def test_threshold(self):
  459. # Regression test. An early version of `pascal` returned an
  460. # array of type np.uint64 for n=35, but that data type is too small
  461. # to hold p[-1, -1]. The second assert_equal below would fail
  462. # because p[-1, -1] overflowed.
  463. p = pascal(34)
  464. assert_equal(2*p.item(-1, -2), p.item(-1, -1), err_msg="n = 34")
  465. p = pascal(35)
  466. assert_equal(2*p.item(-1, -2), p.item(-1, -1), err_msg="n = 35")
  467. def test_invpascal():
  468. def check_invpascal(n, kind, exact):
  469. ip = invpascal(n, kind=kind, exact=exact)
  470. p = pascal(n, kind=kind, exact=exact)
  471. # Matrix-multiply ip and p, and check that we get the identity matrix.
  472. # We can't use the simple expression e = ip.dot(p), because when
  473. # n < 35 and exact is True, p.dtype is np.uint64 and ip.dtype is
  474. # np.int64. The product of those dtypes is np.float64, which loses
  475. # precision when n is greater than 18. Instead we'll cast both to
  476. # object arrays, and then multiply.
  477. e = ip.astype(object).dot(p.astype(object))
  478. assert_array_equal(e, eye(n), err_msg="n=%d kind=%r exact=%r" %
  479. (n, kind, exact))
  480. kinds = ['symmetric', 'lower', 'upper']
  481. ns = [1, 2, 5, 18]
  482. for n in ns:
  483. for kind in kinds:
  484. for exact in [True, False]:
  485. check_invpascal(n, kind, exact)
  486. ns = [19, 34, 35, 50]
  487. for n in ns:
  488. for kind in kinds:
  489. check_invpascal(n, kind, True)
  490. def test_dft():
  491. m = dft(2)
  492. expected = array([[1.0, 1.0], [1.0, -1.0]])
  493. assert_array_almost_equal(m, expected)
  494. m = dft(2, scale='n')
  495. assert_array_almost_equal(m, expected/2.0)
  496. m = dft(2, scale='sqrtn')
  497. assert_array_almost_equal(m, expected/sqrt(2.0))
  498. x = array([0, 1, 2, 3, 4, 5, 0, 1])
  499. m = dft(8)
  500. mx = m.dot(x)
  501. fx = fftpack.fft(x)
  502. assert_array_almost_equal(mx, fx)