laguerre.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. """
  2. ==================================================
  3. Laguerre Series (:mod:`numpy.polynomial.laguerre`)
  4. ==================================================
  5. This module provides a number of objects (mostly functions) useful for
  6. dealing with Laguerre series, including a `Laguerre` class that
  7. encapsulates the usual arithmetic operations. (General information
  8. on how this module represents and works with such polynomials is in the
  9. docstring for its "parent" sub-package, `numpy.polynomial`).
  10. Classes
  11. -------
  12. .. autosummary::
  13. :toctree: generated/
  14. Laguerre
  15. Constants
  16. ---------
  17. .. autosummary::
  18. :toctree: generated/
  19. lagdomain
  20. lagzero
  21. lagone
  22. lagx
  23. Arithmetic
  24. ----------
  25. .. autosummary::
  26. :toctree: generated/
  27. lagadd
  28. lagsub
  29. lagmulx
  30. lagmul
  31. lagdiv
  32. lagpow
  33. lagval
  34. lagval2d
  35. lagval3d
  36. laggrid2d
  37. laggrid3d
  38. Calculus
  39. --------
  40. .. autosummary::
  41. :toctree: generated/
  42. lagder
  43. lagint
  44. Misc Functions
  45. --------------
  46. .. autosummary::
  47. :toctree: generated/
  48. lagfromroots
  49. lagroots
  50. lagvander
  51. lagvander2d
  52. lagvander3d
  53. laggauss
  54. lagweight
  55. lagcompanion
  56. lagfit
  57. lagtrim
  58. lagline
  59. lag2poly
  60. poly2lag
  61. See also
  62. --------
  63. `numpy.polynomial`
  64. """
  65. import numpy as np
  66. import numpy.linalg as la
  67. from numpy.core.multiarray import normalize_axis_index
  68. from . import polyutils as pu
  69. from ._polybase import ABCPolyBase
  70. __all__ = [
  71. 'lagzero', 'lagone', 'lagx', 'lagdomain', 'lagline', 'lagadd',
  72. 'lagsub', 'lagmulx', 'lagmul', 'lagdiv', 'lagpow', 'lagval', 'lagder',
  73. 'lagint', 'lag2poly', 'poly2lag', 'lagfromroots', 'lagvander',
  74. 'lagfit', 'lagtrim', 'lagroots', 'Laguerre', 'lagval2d', 'lagval3d',
  75. 'laggrid2d', 'laggrid3d', 'lagvander2d', 'lagvander3d', 'lagcompanion',
  76. 'laggauss', 'lagweight']
  77. lagtrim = pu.trimcoef
  78. def poly2lag(pol):
  79. """
  80. poly2lag(pol)
  81. Convert a polynomial to a Laguerre series.
  82. Convert an array representing the coefficients of a polynomial (relative
  83. to the "standard" basis) ordered from lowest degree to highest, to an
  84. array of the coefficients of the equivalent Laguerre series, ordered
  85. from lowest to highest degree.
  86. Parameters
  87. ----------
  88. pol : array_like
  89. 1-D array containing the polynomial coefficients
  90. Returns
  91. -------
  92. c : ndarray
  93. 1-D array containing the coefficients of the equivalent Laguerre
  94. series.
  95. See Also
  96. --------
  97. lag2poly
  98. Notes
  99. -----
  100. The easy way to do conversions between polynomial basis sets
  101. is to use the convert method of a class instance.
  102. Examples
  103. --------
  104. >>> from numpy.polynomial.laguerre import poly2lag
  105. >>> poly2lag(np.arange(4))
  106. array([ 23., -63., 58., -18.])
  107. """
  108. [pol] = pu.as_series([pol])
  109. res = 0
  110. for p in pol[::-1]:
  111. res = lagadd(lagmulx(res), p)
  112. return res
  113. def lag2poly(c):
  114. """
  115. Convert a Laguerre series to a polynomial.
  116. Convert an array representing the coefficients of a Laguerre series,
  117. ordered from lowest degree to highest, to an array of the coefficients
  118. of the equivalent polynomial (relative to the "standard" basis) ordered
  119. from lowest to highest degree.
  120. Parameters
  121. ----------
  122. c : array_like
  123. 1-D array containing the Laguerre series coefficients, ordered
  124. from lowest order term to highest.
  125. Returns
  126. -------
  127. pol : ndarray
  128. 1-D array containing the coefficients of the equivalent polynomial
  129. (relative to the "standard" basis) ordered from lowest order term
  130. to highest.
  131. See Also
  132. --------
  133. poly2lag
  134. Notes
  135. -----
  136. The easy way to do conversions between polynomial basis sets
  137. is to use the convert method of a class instance.
  138. Examples
  139. --------
  140. >>> from numpy.polynomial.laguerre import lag2poly
  141. >>> lag2poly([ 23., -63., 58., -18.])
  142. array([0., 1., 2., 3.])
  143. """
  144. from .polynomial import polyadd, polysub, polymulx
  145. [c] = pu.as_series([c])
  146. n = len(c)
  147. if n == 1:
  148. return c
  149. else:
  150. c0 = c[-2]
  151. c1 = c[-1]
  152. # i is the current degree of c1
  153. for i in range(n - 1, 1, -1):
  154. tmp = c0
  155. c0 = polysub(c[i - 2], (c1*(i - 1))/i)
  156. c1 = polyadd(tmp, polysub((2*i - 1)*c1, polymulx(c1))/i)
  157. return polyadd(c0, polysub(c1, polymulx(c1)))
  158. #
  159. # These are constant arrays are of integer type so as to be compatible
  160. # with the widest range of other types, such as Decimal.
  161. #
  162. # Laguerre
  163. lagdomain = np.array([0, 1])
  164. # Laguerre coefficients representing zero.
  165. lagzero = np.array([0])
  166. # Laguerre coefficients representing one.
  167. lagone = np.array([1])
  168. # Laguerre coefficients representing the identity x.
  169. lagx = np.array([1, -1])
  170. def lagline(off, scl):
  171. """
  172. Laguerre series whose graph is a straight line.
  173. Parameters
  174. ----------
  175. off, scl : scalars
  176. The specified line is given by ``off + scl*x``.
  177. Returns
  178. -------
  179. y : ndarray
  180. This module's representation of the Laguerre series for
  181. ``off + scl*x``.
  182. See Also
  183. --------
  184. numpy.polynomial.polynomial.polyline
  185. numpy.polynomial.chebyshev.chebline
  186. numpy.polynomial.legendre.legline
  187. numpy.polynomial.hermite.hermline
  188. numpy.polynomial.hermite_e.hermeline
  189. Examples
  190. --------
  191. >>> from numpy.polynomial.laguerre import lagline, lagval
  192. >>> lagval(0,lagline(3, 2))
  193. 3.0
  194. >>> lagval(1,lagline(3, 2))
  195. 5.0
  196. """
  197. if scl != 0:
  198. return np.array([off + scl, -scl])
  199. else:
  200. return np.array([off])
  201. def lagfromroots(roots):
  202. """
  203. Generate a Laguerre series with given roots.
  204. The function returns the coefficients of the polynomial
  205. .. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
  206. in Laguerre form, where the `r_n` are the roots specified in `roots`.
  207. If a zero has multiplicity n, then it must appear in `roots` n times.
  208. For instance, if 2 is a root of multiplicity three and 3 is a root of
  209. multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The
  210. roots can appear in any order.
  211. If the returned coefficients are `c`, then
  212. .. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x)
  213. The coefficient of the last term is not generally 1 for monic
  214. polynomials in Laguerre form.
  215. Parameters
  216. ----------
  217. roots : array_like
  218. Sequence containing the roots.
  219. Returns
  220. -------
  221. out : ndarray
  222. 1-D array of coefficients. If all roots are real then `out` is a
  223. real array, if some of the roots are complex, then `out` is complex
  224. even if all the coefficients in the result are real (see Examples
  225. below).
  226. See Also
  227. --------
  228. numpy.polynomial.polynomial.polyfromroots
  229. numpy.polynomial.legendre.legfromroots
  230. numpy.polynomial.chebyshev.chebfromroots
  231. numpy.polynomial.hermite.hermfromroots
  232. numpy.polynomial.hermite_e.hermefromroots
  233. Examples
  234. --------
  235. >>> from numpy.polynomial.laguerre import lagfromroots, lagval
  236. >>> coef = lagfromroots((-1, 0, 1))
  237. >>> lagval((-1, 0, 1), coef)
  238. array([0., 0., 0.])
  239. >>> coef = lagfromroots((-1j, 1j))
  240. >>> lagval((-1j, 1j), coef)
  241. array([0.+0.j, 0.+0.j])
  242. """
  243. return pu._fromroots(lagline, lagmul, roots)
  244. def lagadd(c1, c2):
  245. """
  246. Add one Laguerre series to another.
  247. Returns the sum of two Laguerre series `c1` + `c2`. The arguments
  248. are sequences of coefficients ordered from lowest order term to
  249. highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
  250. Parameters
  251. ----------
  252. c1, c2 : array_like
  253. 1-D arrays of Laguerre series coefficients ordered from low to
  254. high.
  255. Returns
  256. -------
  257. out : ndarray
  258. Array representing the Laguerre series of their sum.
  259. See Also
  260. --------
  261. lagsub, lagmulx, lagmul, lagdiv, lagpow
  262. Notes
  263. -----
  264. Unlike multiplication, division, etc., the sum of two Laguerre series
  265. is a Laguerre series (without having to "reproject" the result onto
  266. the basis set) so addition, just like that of "standard" polynomials,
  267. is simply "component-wise."
  268. Examples
  269. --------
  270. >>> from numpy.polynomial.laguerre import lagadd
  271. >>> lagadd([1, 2, 3], [1, 2, 3, 4])
  272. array([2., 4., 6., 4.])
  273. """
  274. return pu._add(c1, c2)
  275. def lagsub(c1, c2):
  276. """
  277. Subtract one Laguerre series from another.
  278. Returns the difference of two Laguerre series `c1` - `c2`. The
  279. sequences of coefficients are from lowest order term to highest, i.e.,
  280. [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
  281. Parameters
  282. ----------
  283. c1, c2 : array_like
  284. 1-D arrays of Laguerre series coefficients ordered from low to
  285. high.
  286. Returns
  287. -------
  288. out : ndarray
  289. Of Laguerre series coefficients representing their difference.
  290. See Also
  291. --------
  292. lagadd, lagmulx, lagmul, lagdiv, lagpow
  293. Notes
  294. -----
  295. Unlike multiplication, division, etc., the difference of two Laguerre
  296. series is a Laguerre series (without having to "reproject" the result
  297. onto the basis set) so subtraction, just like that of "standard"
  298. polynomials, is simply "component-wise."
  299. Examples
  300. --------
  301. >>> from numpy.polynomial.laguerre import lagsub
  302. >>> lagsub([1, 2, 3, 4], [1, 2, 3])
  303. array([0., 0., 0., 4.])
  304. """
  305. return pu._sub(c1, c2)
  306. def lagmulx(c):
  307. """Multiply a Laguerre series by x.
  308. Multiply the Laguerre series `c` by x, where x is the independent
  309. variable.
  310. Parameters
  311. ----------
  312. c : array_like
  313. 1-D array of Laguerre series coefficients ordered from low to
  314. high.
  315. Returns
  316. -------
  317. out : ndarray
  318. Array representing the result of the multiplication.
  319. See Also
  320. --------
  321. lagadd, lagsub, lagmul, lagdiv, lagpow
  322. Notes
  323. -----
  324. The multiplication uses the recursion relationship for Laguerre
  325. polynomials in the form
  326. .. math::
  327. xP_i(x) = (-(i + 1)*P_{i + 1}(x) + (2i + 1)P_{i}(x) - iP_{i - 1}(x))
  328. Examples
  329. --------
  330. >>> from numpy.polynomial.laguerre import lagmulx
  331. >>> lagmulx([1, 2, 3])
  332. array([-1., -1., 11., -9.])
  333. """
  334. # c is a trimmed copy
  335. [c] = pu.as_series([c])
  336. # The zero series needs special treatment
  337. if len(c) == 1 and c[0] == 0:
  338. return c
  339. prd = np.empty(len(c) + 1, dtype=c.dtype)
  340. prd[0] = c[0]
  341. prd[1] = -c[0]
  342. for i in range(1, len(c)):
  343. prd[i + 1] = -c[i]*(i + 1)
  344. prd[i] += c[i]*(2*i + 1)
  345. prd[i - 1] -= c[i]*i
  346. return prd
  347. def lagmul(c1, c2):
  348. """
  349. Multiply one Laguerre series by another.
  350. Returns the product of two Laguerre series `c1` * `c2`. The arguments
  351. are sequences of coefficients, from lowest order "term" to highest,
  352. e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
  353. Parameters
  354. ----------
  355. c1, c2 : array_like
  356. 1-D arrays of Laguerre series coefficients ordered from low to
  357. high.
  358. Returns
  359. -------
  360. out : ndarray
  361. Of Laguerre series coefficients representing their product.
  362. See Also
  363. --------
  364. lagadd, lagsub, lagmulx, lagdiv, lagpow
  365. Notes
  366. -----
  367. In general, the (polynomial) product of two C-series results in terms
  368. that are not in the Laguerre polynomial basis set. Thus, to express
  369. the product as a Laguerre series, it is necessary to "reproject" the
  370. product onto said basis set, which may produce "unintuitive" (but
  371. correct) results; see Examples section below.
  372. Examples
  373. --------
  374. >>> from numpy.polynomial.laguerre import lagmul
  375. >>> lagmul([1, 2, 3], [0, 1, 2])
  376. array([ 8., -13., 38., -51., 36.])
  377. """
  378. # s1, s2 are trimmed copies
  379. [c1, c2] = pu.as_series([c1, c2])
  380. if len(c1) > len(c2):
  381. c = c2
  382. xs = c1
  383. else:
  384. c = c1
  385. xs = c2
  386. if len(c) == 1:
  387. c0 = c[0]*xs
  388. c1 = 0
  389. elif len(c) == 2:
  390. c0 = c[0]*xs
  391. c1 = c[1]*xs
  392. else:
  393. nd = len(c)
  394. c0 = c[-2]*xs
  395. c1 = c[-1]*xs
  396. for i in range(3, len(c) + 1):
  397. tmp = c0
  398. nd = nd - 1
  399. c0 = lagsub(c[-i]*xs, (c1*(nd - 1))/nd)
  400. c1 = lagadd(tmp, lagsub((2*nd - 1)*c1, lagmulx(c1))/nd)
  401. return lagadd(c0, lagsub(c1, lagmulx(c1)))
  402. def lagdiv(c1, c2):
  403. """
  404. Divide one Laguerre series by another.
  405. Returns the quotient-with-remainder of two Laguerre series
  406. `c1` / `c2`. The arguments are sequences of coefficients from lowest
  407. order "term" to highest, e.g., [1,2,3] represents the series
  408. ``P_0 + 2*P_1 + 3*P_2``.
  409. Parameters
  410. ----------
  411. c1, c2 : array_like
  412. 1-D arrays of Laguerre series coefficients ordered from low to
  413. high.
  414. Returns
  415. -------
  416. [quo, rem] : ndarrays
  417. Of Laguerre series coefficients representing the quotient and
  418. remainder.
  419. See Also
  420. --------
  421. lagadd, lagsub, lagmulx, lagmul, lagpow
  422. Notes
  423. -----
  424. In general, the (polynomial) division of one Laguerre series by another
  425. results in quotient and remainder terms that are not in the Laguerre
  426. polynomial basis set. Thus, to express these results as a Laguerre
  427. series, it is necessary to "reproject" the results onto the Laguerre
  428. basis set, which may produce "unintuitive" (but correct) results; see
  429. Examples section below.
  430. Examples
  431. --------
  432. >>> from numpy.polynomial.laguerre import lagdiv
  433. >>> lagdiv([ 8., -13., 38., -51., 36.], [0, 1, 2])
  434. (array([1., 2., 3.]), array([0.]))
  435. >>> lagdiv([ 9., -12., 38., -51., 36.], [0, 1, 2])
  436. (array([1., 2., 3.]), array([1., 1.]))
  437. """
  438. return pu._div(lagmul, c1, c2)
  439. def lagpow(c, pow, maxpower=16):
  440. """Raise a Laguerre series to a power.
  441. Returns the Laguerre series `c` raised to the power `pow`. The
  442. argument `c` is a sequence of coefficients ordered from low to high.
  443. i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
  444. Parameters
  445. ----------
  446. c : array_like
  447. 1-D array of Laguerre series coefficients ordered from low to
  448. high.
  449. pow : integer
  450. Power to which the series will be raised
  451. maxpower : integer, optional
  452. Maximum power allowed. This is mainly to limit growth of the series
  453. to unmanageable size. Default is 16
  454. Returns
  455. -------
  456. coef : ndarray
  457. Laguerre series of power.
  458. See Also
  459. --------
  460. lagadd, lagsub, lagmulx, lagmul, lagdiv
  461. Examples
  462. --------
  463. >>> from numpy.polynomial.laguerre import lagpow
  464. >>> lagpow([1, 2, 3], 2)
  465. array([ 14., -16., 56., -72., 54.])
  466. """
  467. return pu._pow(lagmul, c, pow, maxpower)
  468. def lagder(c, m=1, scl=1, axis=0):
  469. """
  470. Differentiate a Laguerre series.
  471. Returns the Laguerre series coefficients `c` differentiated `m` times
  472. along `axis`. At each iteration the result is multiplied by `scl` (the
  473. scaling factor is for use in a linear change of variable). The argument
  474. `c` is an array of coefficients from low to high degree along each
  475. axis, e.g., [1,2,3] represents the series ``1*L_0 + 2*L_1 + 3*L_2``
  476. while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) +
  477. 2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is
  478. ``y``.
  479. Parameters
  480. ----------
  481. c : array_like
  482. Array of Laguerre series coefficients. If `c` is multidimensional
  483. the different axis correspond to different variables with the
  484. degree in each axis given by the corresponding index.
  485. m : int, optional
  486. Number of derivatives taken, must be non-negative. (Default: 1)
  487. scl : scalar, optional
  488. Each differentiation is multiplied by `scl`. The end result is
  489. multiplication by ``scl**m``. This is for use in a linear change of
  490. variable. (Default: 1)
  491. axis : int, optional
  492. Axis over which the derivative is taken. (Default: 0).
  493. .. versionadded:: 1.7.0
  494. Returns
  495. -------
  496. der : ndarray
  497. Laguerre series of the derivative.
  498. See Also
  499. --------
  500. lagint
  501. Notes
  502. -----
  503. In general, the result of differentiating a Laguerre series does not
  504. resemble the same operation on a power series. Thus the result of this
  505. function may be "unintuitive," albeit correct; see Examples section
  506. below.
  507. Examples
  508. --------
  509. >>> from numpy.polynomial.laguerre import lagder
  510. >>> lagder([ 1., 1., 1., -3.])
  511. array([1., 2., 3.])
  512. >>> lagder([ 1., 0., 0., -4., 3.], m=2)
  513. array([1., 2., 3.])
  514. """
  515. c = np.array(c, ndmin=1, copy=True)
  516. if c.dtype.char in '?bBhHiIlLqQpP':
  517. c = c.astype(np.double)
  518. cnt = pu._deprecate_as_int(m, "the order of derivation")
  519. iaxis = pu._deprecate_as_int(axis, "the axis")
  520. if cnt < 0:
  521. raise ValueError("The order of derivation must be non-negative")
  522. iaxis = normalize_axis_index(iaxis, c.ndim)
  523. if cnt == 0:
  524. return c
  525. c = np.moveaxis(c, iaxis, 0)
  526. n = len(c)
  527. if cnt >= n:
  528. c = c[:1]*0
  529. else:
  530. for i in range(cnt):
  531. n = n - 1
  532. c *= scl
  533. der = np.empty((n,) + c.shape[1:], dtype=c.dtype)
  534. for j in range(n, 1, -1):
  535. der[j - 1] = -c[j]
  536. c[j - 1] += c[j]
  537. der[0] = -c[1]
  538. c = der
  539. c = np.moveaxis(c, 0, iaxis)
  540. return c
  541. def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
  542. """
  543. Integrate a Laguerre series.
  544. Returns the Laguerre series coefficients `c` integrated `m` times from
  545. `lbnd` along `axis`. At each iteration the resulting series is
  546. **multiplied** by `scl` and an integration constant, `k`, is added.
  547. The scaling factor is for use in a linear change of variable. ("Buyer
  548. beware": note that, depending on what one is doing, one may want `scl`
  549. to be the reciprocal of what one might expect; for more information,
  550. see the Notes section below.) The argument `c` is an array of
  551. coefficients from low to high degree along each axis, e.g., [1,2,3]
  552. represents the series ``L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]]
  553. represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) +
  554. 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
  555. Parameters
  556. ----------
  557. c : array_like
  558. Array of Laguerre series coefficients. If `c` is multidimensional
  559. the different axis correspond to different variables with the
  560. degree in each axis given by the corresponding index.
  561. m : int, optional
  562. Order of integration, must be positive. (Default: 1)
  563. k : {[], list, scalar}, optional
  564. Integration constant(s). The value of the first integral at
  565. ``lbnd`` is the first value in the list, the value of the second
  566. integral at ``lbnd`` is the second value, etc. If ``k == []`` (the
  567. default), all constants are set to zero. If ``m == 1``, a single
  568. scalar can be given instead of a list.
  569. lbnd : scalar, optional
  570. The lower bound of the integral. (Default: 0)
  571. scl : scalar, optional
  572. Following each integration the result is *multiplied* by `scl`
  573. before the integration constant is added. (Default: 1)
  574. axis : int, optional
  575. Axis over which the integral is taken. (Default: 0).
  576. .. versionadded:: 1.7.0
  577. Returns
  578. -------
  579. S : ndarray
  580. Laguerre series coefficients of the integral.
  581. Raises
  582. ------
  583. ValueError
  584. If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or
  585. ``np.ndim(scl) != 0``.
  586. See Also
  587. --------
  588. lagder
  589. Notes
  590. -----
  591. Note that the result of each integration is *multiplied* by `scl`.
  592. Why is this important to note? Say one is making a linear change of
  593. variable :math:`u = ax + b` in an integral relative to `x`. Then
  594. :math:`dx = du/a`, so one will need to set `scl` equal to
  595. :math:`1/a` - perhaps not what one would have first thought.
  596. Also note that, in general, the result of integrating a C-series needs
  597. to be "reprojected" onto the C-series basis set. Thus, typically,
  598. the result of this function is "unintuitive," albeit correct; see
  599. Examples section below.
  600. Examples
  601. --------
  602. >>> from numpy.polynomial.laguerre import lagint
  603. >>> lagint([1,2,3])
  604. array([ 1., 1., 1., -3.])
  605. >>> lagint([1,2,3], m=2)
  606. array([ 1., 0., 0., -4., 3.])
  607. >>> lagint([1,2,3], k=1)
  608. array([ 2., 1., 1., -3.])
  609. >>> lagint([1,2,3], lbnd=-1)
  610. array([11.5, 1. , 1. , -3. ])
  611. >>> lagint([1,2], m=2, k=[1,2], lbnd=-1)
  612. array([ 11.16666667, -5. , -3. , 2. ]) # may vary
  613. """
  614. c = np.array(c, ndmin=1, copy=True)
  615. if c.dtype.char in '?bBhHiIlLqQpP':
  616. c = c.astype(np.double)
  617. if not np.iterable(k):
  618. k = [k]
  619. cnt = pu._deprecate_as_int(m, "the order of integration")
  620. iaxis = pu._deprecate_as_int(axis, "the axis")
  621. if cnt < 0:
  622. raise ValueError("The order of integration must be non-negative")
  623. if len(k) > cnt:
  624. raise ValueError("Too many integration constants")
  625. if np.ndim(lbnd) != 0:
  626. raise ValueError("lbnd must be a scalar.")
  627. if np.ndim(scl) != 0:
  628. raise ValueError("scl must be a scalar.")
  629. iaxis = normalize_axis_index(iaxis, c.ndim)
  630. if cnt == 0:
  631. return c
  632. c = np.moveaxis(c, iaxis, 0)
  633. k = list(k) + [0]*(cnt - len(k))
  634. for i in range(cnt):
  635. n = len(c)
  636. c *= scl
  637. if n == 1 and np.all(c[0] == 0):
  638. c[0] += k[i]
  639. else:
  640. tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype)
  641. tmp[0] = c[0]
  642. tmp[1] = -c[0]
  643. for j in range(1, n):
  644. tmp[j] += c[j]
  645. tmp[j + 1] = -c[j]
  646. tmp[0] += k[i] - lagval(lbnd, tmp)
  647. c = tmp
  648. c = np.moveaxis(c, 0, iaxis)
  649. return c
  650. def lagval(x, c, tensor=True):
  651. """
  652. Evaluate a Laguerre series at points x.
  653. If `c` is of length `n + 1`, this function returns the value:
  654. .. math:: p(x) = c_0 * L_0(x) + c_1 * L_1(x) + ... + c_n * L_n(x)
  655. The parameter `x` is converted to an array only if it is a tuple or a
  656. list, otherwise it is treated as a scalar. In either case, either `x`
  657. or its elements must support multiplication and addition both with
  658. themselves and with the elements of `c`.
  659. If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If
  660. `c` is multidimensional, then the shape of the result depends on the
  661. value of `tensor`. If `tensor` is true the shape will be c.shape[1:] +
  662. x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that
  663. scalars have shape (,).
  664. Trailing zeros in the coefficients will be used in the evaluation, so
  665. they should be avoided if efficiency is a concern.
  666. Parameters
  667. ----------
  668. x : array_like, compatible object
  669. If `x` is a list or tuple, it is converted to an ndarray, otherwise
  670. it is left unchanged and treated as a scalar. In either case, `x`
  671. or its elements must support addition and multiplication with
  672. with themselves and with the elements of `c`.
  673. c : array_like
  674. Array of coefficients ordered so that the coefficients for terms of
  675. degree n are contained in c[n]. If `c` is multidimensional the
  676. remaining indices enumerate multiple polynomials. In the two
  677. dimensional case the coefficients may be thought of as stored in
  678. the columns of `c`.
  679. tensor : boolean, optional
  680. If True, the shape of the coefficient array is extended with ones
  681. on the right, one for each dimension of `x`. Scalars have dimension 0
  682. for this action. The result is that every column of coefficients in
  683. `c` is evaluated for every element of `x`. If False, `x` is broadcast
  684. over the columns of `c` for the evaluation. This keyword is useful
  685. when `c` is multidimensional. The default value is True.
  686. .. versionadded:: 1.7.0
  687. Returns
  688. -------
  689. values : ndarray, algebra_like
  690. The shape of the return value is described above.
  691. See Also
  692. --------
  693. lagval2d, laggrid2d, lagval3d, laggrid3d
  694. Notes
  695. -----
  696. The evaluation uses Clenshaw recursion, aka synthetic division.
  697. Examples
  698. --------
  699. >>> from numpy.polynomial.laguerre import lagval
  700. >>> coef = [1,2,3]
  701. >>> lagval(1, coef)
  702. -0.5
  703. >>> lagval([[1,2],[3,4]], coef)
  704. array([[-0.5, -4. ],
  705. [-4.5, -2. ]])
  706. """
  707. c = np.array(c, ndmin=1, copy=False)
  708. if c.dtype.char in '?bBhHiIlLqQpP':
  709. c = c.astype(np.double)
  710. if isinstance(x, (tuple, list)):
  711. x = np.asarray(x)
  712. if isinstance(x, np.ndarray) and tensor:
  713. c = c.reshape(c.shape + (1,)*x.ndim)
  714. if len(c) == 1:
  715. c0 = c[0]
  716. c1 = 0
  717. elif len(c) == 2:
  718. c0 = c[0]
  719. c1 = c[1]
  720. else:
  721. nd = len(c)
  722. c0 = c[-2]
  723. c1 = c[-1]
  724. for i in range(3, len(c) + 1):
  725. tmp = c0
  726. nd = nd - 1
  727. c0 = c[-i] - (c1*(nd - 1))/nd
  728. c1 = tmp + (c1*((2*nd - 1) - x))/nd
  729. return c0 + c1*(1 - x)
  730. def lagval2d(x, y, c):
  731. """
  732. Evaluate a 2-D Laguerre series at points (x, y).
  733. This function returns the values:
  734. .. math:: p(x,y) = \\sum_{i,j} c_{i,j} * L_i(x) * L_j(y)
  735. The parameters `x` and `y` are converted to arrays only if they are
  736. tuples or a lists, otherwise they are treated as a scalars and they
  737. must have the same shape after conversion. In either case, either `x`
  738. and `y` or their elements must support multiplication and addition both
  739. with themselves and with the elements of `c`.
  740. If `c` is a 1-D array a one is implicitly appended to its shape to make
  741. it 2-D. The shape of the result will be c.shape[2:] + x.shape.
  742. Parameters
  743. ----------
  744. x, y : array_like, compatible objects
  745. The two dimensional series is evaluated at the points `(x, y)`,
  746. where `x` and `y` must have the same shape. If `x` or `y` is a list
  747. or tuple, it is first converted to an ndarray, otherwise it is left
  748. unchanged and if it isn't an ndarray it is treated as a scalar.
  749. c : array_like
  750. Array of coefficients ordered so that the coefficient of the term
  751. of multi-degree i,j is contained in ``c[i,j]``. If `c` has
  752. dimension greater than two the remaining indices enumerate multiple
  753. sets of coefficients.
  754. Returns
  755. -------
  756. values : ndarray, compatible object
  757. The values of the two dimensional polynomial at points formed with
  758. pairs of corresponding values from `x` and `y`.
  759. See Also
  760. --------
  761. lagval, laggrid2d, lagval3d, laggrid3d
  762. Notes
  763. -----
  764. .. versionadded:: 1.7.0
  765. """
  766. return pu._valnd(lagval, c, x, y)
  767. def laggrid2d(x, y, c):
  768. """
  769. Evaluate a 2-D Laguerre series on the Cartesian product of x and y.
  770. This function returns the values:
  771. .. math:: p(a,b) = \\sum_{i,j} c_{i,j} * L_i(a) * L_j(b)
  772. where the points `(a, b)` consist of all pairs formed by taking
  773. `a` from `x` and `b` from `y`. The resulting points form a grid with
  774. `x` in the first dimension and `y` in the second.
  775. The parameters `x` and `y` are converted to arrays only if they are
  776. tuples or a lists, otherwise they are treated as a scalars. In either
  777. case, either `x` and `y` or their elements must support multiplication
  778. and addition both with themselves and with the elements of `c`.
  779. If `c` has fewer than two dimensions, ones are implicitly appended to
  780. its shape to make it 2-D. The shape of the result will be c.shape[2:] +
  781. x.shape + y.shape.
  782. Parameters
  783. ----------
  784. x, y : array_like, compatible objects
  785. The two dimensional series is evaluated at the points in the
  786. Cartesian product of `x` and `y`. If `x` or `y` is a list or
  787. tuple, it is first converted to an ndarray, otherwise it is left
  788. unchanged and, if it isn't an ndarray, it is treated as a scalar.
  789. c : array_like
  790. Array of coefficients ordered so that the coefficient of the term of
  791. multi-degree i,j is contained in `c[i,j]`. If `c` has dimension
  792. greater than two the remaining indices enumerate multiple sets of
  793. coefficients.
  794. Returns
  795. -------
  796. values : ndarray, compatible object
  797. The values of the two dimensional Chebyshev series at points in the
  798. Cartesian product of `x` and `y`.
  799. See Also
  800. --------
  801. lagval, lagval2d, lagval3d, laggrid3d
  802. Notes
  803. -----
  804. .. versionadded:: 1.7.0
  805. """
  806. return pu._gridnd(lagval, c, x, y)
  807. def lagval3d(x, y, z, c):
  808. """
  809. Evaluate a 3-D Laguerre series at points (x, y, z).
  810. This function returns the values:
  811. .. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * L_i(x) * L_j(y) * L_k(z)
  812. The parameters `x`, `y`, and `z` are converted to arrays only if
  813. they are tuples or a lists, otherwise they are treated as a scalars and
  814. they must have the same shape after conversion. In either case, either
  815. `x`, `y`, and `z` or their elements must support multiplication and
  816. addition both with themselves and with the elements of `c`.
  817. If `c` has fewer than 3 dimensions, ones are implicitly appended to its
  818. shape to make it 3-D. The shape of the result will be c.shape[3:] +
  819. x.shape.
  820. Parameters
  821. ----------
  822. x, y, z : array_like, compatible object
  823. The three dimensional series is evaluated at the points
  824. `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If
  825. any of `x`, `y`, or `z` is a list or tuple, it is first converted
  826. to an ndarray, otherwise it is left unchanged and if it isn't an
  827. ndarray it is treated as a scalar.
  828. c : array_like
  829. Array of coefficients ordered so that the coefficient of the term of
  830. multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension
  831. greater than 3 the remaining indices enumerate multiple sets of
  832. coefficients.
  833. Returns
  834. -------
  835. values : ndarray, compatible object
  836. The values of the multidimension polynomial on points formed with
  837. triples of corresponding values from `x`, `y`, and `z`.
  838. See Also
  839. --------
  840. lagval, lagval2d, laggrid2d, laggrid3d
  841. Notes
  842. -----
  843. .. versionadded:: 1.7.0
  844. """
  845. return pu._valnd(lagval, c, x, y, z)
  846. def laggrid3d(x, y, z, c):
  847. """
  848. Evaluate a 3-D Laguerre series on the Cartesian product of x, y, and z.
  849. This function returns the values:
  850. .. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * L_i(a) * L_j(b) * L_k(c)
  851. where the points `(a, b, c)` consist of all triples formed by taking
  852. `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form
  853. a grid with `x` in the first dimension, `y` in the second, and `z` in
  854. the third.
  855. The parameters `x`, `y`, and `z` are converted to arrays only if they
  856. are tuples or a lists, otherwise they are treated as a scalars. In
  857. either case, either `x`, `y`, and `z` or their elements must support
  858. multiplication and addition both with themselves and with the elements
  859. of `c`.
  860. If `c` has fewer than three dimensions, ones are implicitly appended to
  861. its shape to make it 3-D. The shape of the result will be c.shape[3:] +
  862. x.shape + y.shape + z.shape.
  863. Parameters
  864. ----------
  865. x, y, z : array_like, compatible objects
  866. The three dimensional series is evaluated at the points in the
  867. Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a
  868. list or tuple, it is first converted to an ndarray, otherwise it is
  869. left unchanged and, if it isn't an ndarray, it is treated as a
  870. scalar.
  871. c : array_like
  872. Array of coefficients ordered so that the coefficients for terms of
  873. degree i,j are contained in ``c[i,j]``. If `c` has dimension
  874. greater than two the remaining indices enumerate multiple sets of
  875. coefficients.
  876. Returns
  877. -------
  878. values : ndarray, compatible object
  879. The values of the two dimensional polynomial at points in the Cartesian
  880. product of `x` and `y`.
  881. See Also
  882. --------
  883. lagval, lagval2d, laggrid2d, lagval3d
  884. Notes
  885. -----
  886. .. versionadded:: 1.7.0
  887. """
  888. return pu._gridnd(lagval, c, x, y, z)
  889. def lagvander(x, deg):
  890. """Pseudo-Vandermonde matrix of given degree.
  891. Returns the pseudo-Vandermonde matrix of degree `deg` and sample points
  892. `x`. The pseudo-Vandermonde matrix is defined by
  893. .. math:: V[..., i] = L_i(x)
  894. where `0 <= i <= deg`. The leading indices of `V` index the elements of
  895. `x` and the last index is the degree of the Laguerre polynomial.
  896. If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the
  897. array ``V = lagvander(x, n)``, then ``np.dot(V, c)`` and
  898. ``lagval(x, c)`` are the same up to roundoff. This equivalence is
  899. useful both for least squares fitting and for the evaluation of a large
  900. number of Laguerre series of the same degree and sample points.
  901. Parameters
  902. ----------
  903. x : array_like
  904. Array of points. The dtype is converted to float64 or complex128
  905. depending on whether any of the elements are complex. If `x` is
  906. scalar it is converted to a 1-D array.
  907. deg : int
  908. Degree of the resulting matrix.
  909. Returns
  910. -------
  911. vander : ndarray
  912. The pseudo-Vandermonde matrix. The shape of the returned matrix is
  913. ``x.shape + (deg + 1,)``, where The last index is the degree of the
  914. corresponding Laguerre polynomial. The dtype will be the same as
  915. the converted `x`.
  916. Examples
  917. --------
  918. >>> from numpy.polynomial.laguerre import lagvander
  919. >>> x = np.array([0, 1, 2])
  920. >>> lagvander(x, 3)
  921. array([[ 1. , 1. , 1. , 1. ],
  922. [ 1. , 0. , -0.5 , -0.66666667],
  923. [ 1. , -1. , -1. , -0.33333333]])
  924. """
  925. ideg = pu._deprecate_as_int(deg, "deg")
  926. if ideg < 0:
  927. raise ValueError("deg must be non-negative")
  928. x = np.array(x, copy=False, ndmin=1) + 0.0
  929. dims = (ideg + 1,) + x.shape
  930. dtyp = x.dtype
  931. v = np.empty(dims, dtype=dtyp)
  932. v[0] = x*0 + 1
  933. if ideg > 0:
  934. v[1] = 1 - x
  935. for i in range(2, ideg + 1):
  936. v[i] = (v[i-1]*(2*i - 1 - x) - v[i-2]*(i - 1))/i
  937. return np.moveaxis(v, 0, -1)
  938. def lagvander2d(x, y, deg):
  939. """Pseudo-Vandermonde matrix of given degrees.
  940. Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
  941. points `(x, y)`. The pseudo-Vandermonde matrix is defined by
  942. .. math:: V[..., (deg[1] + 1)*i + j] = L_i(x) * L_j(y),
  943. where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of
  944. `V` index the points `(x, y)` and the last index encodes the degrees of
  945. the Laguerre polynomials.
  946. If ``V = lagvander2d(x, y, [xdeg, ydeg])``, then the columns of `V`
  947. correspond to the elements of a 2-D coefficient array `c` of shape
  948. (xdeg + 1, ydeg + 1) in the order
  949. .. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
  950. and ``np.dot(V, c.flat)`` and ``lagval2d(x, y, c)`` will be the same
  951. up to roundoff. This equivalence is useful both for least squares
  952. fitting and for the evaluation of a large number of 2-D Laguerre
  953. series of the same degrees and sample points.
  954. Parameters
  955. ----------
  956. x, y : array_like
  957. Arrays of point coordinates, all of the same shape. The dtypes
  958. will be converted to either float64 or complex128 depending on
  959. whether any of the elements are complex. Scalars are converted to
  960. 1-D arrays.
  961. deg : list of ints
  962. List of maximum degrees of the form [x_deg, y_deg].
  963. Returns
  964. -------
  965. vander2d : ndarray
  966. The shape of the returned matrix is ``x.shape + (order,)``, where
  967. :math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same
  968. as the converted `x` and `y`.
  969. See Also
  970. --------
  971. lagvander, lagvander3d, lagval2d, lagval3d
  972. Notes
  973. -----
  974. .. versionadded:: 1.7.0
  975. """
  976. return pu._vander_nd_flat((lagvander, lagvander), (x, y), deg)
  977. def lagvander3d(x, y, z, deg):
  978. """Pseudo-Vandermonde matrix of given degrees.
  979. Returns the pseudo-Vandermonde matrix of degrees `deg` and sample
  980. points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`,
  981. then The pseudo-Vandermonde matrix is defined by
  982. .. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = L_i(x)*L_j(y)*L_k(z),
  983. where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading
  984. indices of `V` index the points `(x, y, z)` and the last index encodes
  985. the degrees of the Laguerre polynomials.
  986. If ``V = lagvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns
  987. of `V` correspond to the elements of a 3-D coefficient array `c` of
  988. shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
  989. .. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
  990. and ``np.dot(V, c.flat)`` and ``lagval3d(x, y, z, c)`` will be the
  991. same up to roundoff. This equivalence is useful both for least squares
  992. fitting and for the evaluation of a large number of 3-D Laguerre
  993. series of the same degrees and sample points.
  994. Parameters
  995. ----------
  996. x, y, z : array_like
  997. Arrays of point coordinates, all of the same shape. The dtypes will
  998. be converted to either float64 or complex128 depending on whether
  999. any of the elements are complex. Scalars are converted to 1-D
  1000. arrays.
  1001. deg : list of ints
  1002. List of maximum degrees of the form [x_deg, y_deg, z_deg].
  1003. Returns
  1004. -------
  1005. vander3d : ndarray
  1006. The shape of the returned matrix is ``x.shape + (order,)``, where
  1007. :math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will
  1008. be the same as the converted `x`, `y`, and `z`.
  1009. See Also
  1010. --------
  1011. lagvander, lagvander3d, lagval2d, lagval3d
  1012. Notes
  1013. -----
  1014. .. versionadded:: 1.7.0
  1015. """
  1016. return pu._vander_nd_flat((lagvander, lagvander, lagvander), (x, y, z), deg)
  1017. def lagfit(x, y, deg, rcond=None, full=False, w=None):
  1018. """
  1019. Least squares fit of Laguerre series to data.
  1020. Return the coefficients of a Laguerre series of degree `deg` that is the
  1021. least squares fit to the data values `y` given at points `x`. If `y` is
  1022. 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple
  1023. fits are done, one for each column of `y`, and the resulting
  1024. coefficients are stored in the corresponding columns of a 2-D return.
  1025. The fitted polynomial(s) are in the form
  1026. .. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x),
  1027. where `n` is `deg`.
  1028. Parameters
  1029. ----------
  1030. x : array_like, shape (M,)
  1031. x-coordinates of the M sample points ``(x[i], y[i])``.
  1032. y : array_like, shape (M,) or (M, K)
  1033. y-coordinates of the sample points. Several data sets of sample
  1034. points sharing the same x-coordinates can be fitted at once by
  1035. passing in a 2D-array that contains one dataset per column.
  1036. deg : int or 1-D array_like
  1037. Degree(s) of the fitting polynomials. If `deg` is a single integer
  1038. all terms up to and including the `deg`'th term are included in the
  1039. fit. For NumPy versions >= 1.11.0 a list of integers specifying the
  1040. degrees of the terms to include may be used instead.
  1041. rcond : float, optional
  1042. Relative condition number of the fit. Singular values smaller than
  1043. this relative to the largest singular value will be ignored. The
  1044. default value is len(x)*eps, where eps is the relative precision of
  1045. the float type, about 2e-16 in most cases.
  1046. full : bool, optional
  1047. Switch determining nature of return value. When it is False (the
  1048. default) just the coefficients are returned, when True diagnostic
  1049. information from the singular value decomposition is also returned.
  1050. w : array_like, shape (`M`,), optional
  1051. Weights. If not None, the contribution of each point
  1052. ``(x[i],y[i])`` to the fit is weighted by ``w[i]``. Ideally the
  1053. weights are chosen so that the errors of the products ``w[i]*y[i]``
  1054. all have the same variance. The default value is None.
  1055. Returns
  1056. -------
  1057. coef : ndarray, shape (M,) or (M, K)
  1058. Laguerre coefficients ordered from low to high. If `y` was 2-D,
  1059. the coefficients for the data in column k of `y` are in column
  1060. `k`.
  1061. [residuals, rank, singular_values, rcond] : list
  1062. These values are only returned if `full` = True
  1063. resid -- sum of squared residuals of the least squares fit
  1064. rank -- the numerical rank of the scaled Vandermonde matrix
  1065. sv -- singular values of the scaled Vandermonde matrix
  1066. rcond -- value of `rcond`.
  1067. For more details, see `numpy.linalg.lstsq`.
  1068. Warns
  1069. -----
  1070. RankWarning
  1071. The rank of the coefficient matrix in the least-squares fit is
  1072. deficient. The warning is only raised if `full` = False. The
  1073. warnings can be turned off by
  1074. >>> import warnings
  1075. >>> warnings.simplefilter('ignore', np.RankWarning)
  1076. See Also
  1077. --------
  1078. numpy.polynomial.polynomial.polyfit
  1079. numpy.polynomial.legendre.legfit
  1080. numpy.polynomial.chebyshev.chebfit
  1081. numpy.polynomial.hermite.hermfit
  1082. numpy.polynomial.hermite_e.hermefit
  1083. lagval : Evaluates a Laguerre series.
  1084. lagvander : pseudo Vandermonde matrix of Laguerre series.
  1085. lagweight : Laguerre weight function.
  1086. numpy.linalg.lstsq : Computes a least-squares fit from the matrix.
  1087. scipy.interpolate.UnivariateSpline : Computes spline fits.
  1088. Notes
  1089. -----
  1090. The solution is the coefficients of the Laguerre series `p` that
  1091. minimizes the sum of the weighted squared errors
  1092. .. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
  1093. where the :math:`w_j` are the weights. This problem is solved by
  1094. setting up as the (typically) overdetermined matrix equation
  1095. .. math:: V(x) * c = w * y,
  1096. where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the
  1097. coefficients to be solved for, `w` are the weights, and `y` are the
  1098. observed values. This equation is then solved using the singular value
  1099. decomposition of `V`.
  1100. If some of the singular values of `V` are so small that they are
  1101. neglected, then a `RankWarning` will be issued. This means that the
  1102. coefficient values may be poorly determined. Using a lower order fit
  1103. will usually get rid of the warning. The `rcond` parameter can also be
  1104. set to a value smaller than its default, but the resulting fit may be
  1105. spurious and have large contributions from roundoff error.
  1106. Fits using Laguerre series are probably most useful when the data can
  1107. be approximated by ``sqrt(w(x)) * p(x)``, where `w(x)` is the Laguerre
  1108. weight. In that case the weight ``sqrt(w(x[i]))`` should be used
  1109. together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is
  1110. available as `lagweight`.
  1111. References
  1112. ----------
  1113. .. [1] Wikipedia, "Curve fitting",
  1114. https://en.wikipedia.org/wiki/Curve_fitting
  1115. Examples
  1116. --------
  1117. >>> from numpy.polynomial.laguerre import lagfit, lagval
  1118. >>> x = np.linspace(0, 10)
  1119. >>> err = np.random.randn(len(x))/10
  1120. >>> y = lagval(x, [1, 2, 3]) + err
  1121. >>> lagfit(x, y, 2)
  1122. array([ 0.96971004, 2.00193749, 3.00288744]) # may vary
  1123. """
  1124. return pu._fit(lagvander, x, y, deg, rcond, full, w)
  1125. def lagcompanion(c):
  1126. """
  1127. Return the companion matrix of c.
  1128. The usual companion matrix of the Laguerre polynomials is already
  1129. symmetric when `c` is a basis Laguerre polynomial, so no scaling is
  1130. applied.
  1131. Parameters
  1132. ----------
  1133. c : array_like
  1134. 1-D array of Laguerre series coefficients ordered from low to high
  1135. degree.
  1136. Returns
  1137. -------
  1138. mat : ndarray
  1139. Companion matrix of dimensions (deg, deg).
  1140. Notes
  1141. -----
  1142. .. versionadded:: 1.7.0
  1143. """
  1144. # c is a trimmed copy
  1145. [c] = pu.as_series([c])
  1146. if len(c) < 2:
  1147. raise ValueError('Series must have maximum degree of at least 1.')
  1148. if len(c) == 2:
  1149. return np.array([[1 + c[0]/c[1]]])
  1150. n = len(c) - 1
  1151. mat = np.zeros((n, n), dtype=c.dtype)
  1152. top = mat.reshape(-1)[1::n+1]
  1153. mid = mat.reshape(-1)[0::n+1]
  1154. bot = mat.reshape(-1)[n::n+1]
  1155. top[...] = -np.arange(1, n)
  1156. mid[...] = 2.*np.arange(n) + 1.
  1157. bot[...] = top
  1158. mat[:, -1] += (c[:-1]/c[-1])*n
  1159. return mat
  1160. def lagroots(c):
  1161. """
  1162. Compute the roots of a Laguerre series.
  1163. Return the roots (a.k.a. "zeros") of the polynomial
  1164. .. math:: p(x) = \\sum_i c[i] * L_i(x).
  1165. Parameters
  1166. ----------
  1167. c : 1-D array_like
  1168. 1-D array of coefficients.
  1169. Returns
  1170. -------
  1171. out : ndarray
  1172. Array of the roots of the series. If all the roots are real,
  1173. then `out` is also real, otherwise it is complex.
  1174. See Also
  1175. --------
  1176. numpy.polynomial.polynomial.polyroots
  1177. numpy.polynomial.legendre.legroots
  1178. numpy.polynomial.chebyshev.chebroots
  1179. numpy.polynomial.hermite.hermroots
  1180. numpy.polynomial.hermite_e.hermeroots
  1181. Notes
  1182. -----
  1183. The root estimates are obtained as the eigenvalues of the companion
  1184. matrix, Roots far from the origin of the complex plane may have large
  1185. errors due to the numerical instability of the series for such
  1186. values. Roots with multiplicity greater than 1 will also show larger
  1187. errors as the value of the series near such points is relatively
  1188. insensitive to errors in the roots. Isolated roots near the origin can
  1189. be improved by a few iterations of Newton's method.
  1190. The Laguerre series basis polynomials aren't powers of `x` so the
  1191. results of this function may seem unintuitive.
  1192. Examples
  1193. --------
  1194. >>> from numpy.polynomial.laguerre import lagroots, lagfromroots
  1195. >>> coef = lagfromroots([0, 1, 2])
  1196. >>> coef
  1197. array([ 2., -8., 12., -6.])
  1198. >>> lagroots(coef)
  1199. array([-4.4408921e-16, 1.0000000e+00, 2.0000000e+00])
  1200. """
  1201. # c is a trimmed copy
  1202. [c] = pu.as_series([c])
  1203. if len(c) <= 1:
  1204. return np.array([], dtype=c.dtype)
  1205. if len(c) == 2:
  1206. return np.array([1 + c[0]/c[1]])
  1207. # rotated companion matrix reduces error
  1208. m = lagcompanion(c)[::-1,::-1]
  1209. r = la.eigvals(m)
  1210. r.sort()
  1211. return r
  1212. def laggauss(deg):
  1213. """
  1214. Gauss-Laguerre quadrature.
  1215. Computes the sample points and weights for Gauss-Laguerre quadrature.
  1216. These sample points and weights will correctly integrate polynomials of
  1217. degree :math:`2*deg - 1` or less over the interval :math:`[0, \\inf]`
  1218. with the weight function :math:`f(x) = \\exp(-x)`.
  1219. Parameters
  1220. ----------
  1221. deg : int
  1222. Number of sample points and weights. It must be >= 1.
  1223. Returns
  1224. -------
  1225. x : ndarray
  1226. 1-D ndarray containing the sample points.
  1227. y : ndarray
  1228. 1-D ndarray containing the weights.
  1229. Notes
  1230. -----
  1231. .. versionadded:: 1.7.0
  1232. The results have only been tested up to degree 100 higher degrees may
  1233. be problematic. The weights are determined by using the fact that
  1234. .. math:: w_k = c / (L'_n(x_k) * L_{n-1}(x_k))
  1235. where :math:`c` is a constant independent of :math:`k` and :math:`x_k`
  1236. is the k'th root of :math:`L_n`, and then scaling the results to get
  1237. the right value when integrating 1.
  1238. """
  1239. ideg = pu._deprecate_as_int(deg, "deg")
  1240. if ideg <= 0:
  1241. raise ValueError("deg must be a positive integer")
  1242. # first approximation of roots. We use the fact that the companion
  1243. # matrix is symmetric in this case in order to obtain better zeros.
  1244. c = np.array([0]*deg + [1])
  1245. m = lagcompanion(c)
  1246. x = la.eigvalsh(m)
  1247. # improve roots by one application of Newton
  1248. dy = lagval(x, c)
  1249. df = lagval(x, lagder(c))
  1250. x -= dy/df
  1251. # compute the weights. We scale the factor to avoid possible numerical
  1252. # overflow.
  1253. fm = lagval(x, c[1:])
  1254. fm /= np.abs(fm).max()
  1255. df /= np.abs(df).max()
  1256. w = 1/(fm * df)
  1257. # scale w to get the right value, 1 in this case
  1258. w /= w.sum()
  1259. return x, w
  1260. def lagweight(x):
  1261. """Weight function of the Laguerre polynomials.
  1262. The weight function is :math:`exp(-x)` and the interval of integration
  1263. is :math:`[0, \\inf]`. The Laguerre polynomials are orthogonal, but not
  1264. normalized, with respect to this weight function.
  1265. Parameters
  1266. ----------
  1267. x : array_like
  1268. Values at which the weight function will be computed.
  1269. Returns
  1270. -------
  1271. w : ndarray
  1272. The weight function at `x`.
  1273. Notes
  1274. -----
  1275. .. versionadded:: 1.7.0
  1276. """
  1277. w = np.exp(-x)
  1278. return w
  1279. #
  1280. # Laguerre series class
  1281. #
  1282. class Laguerre(ABCPolyBase):
  1283. """A Laguerre series class.
  1284. The Laguerre class provides the standard Python numerical methods
  1285. '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the
  1286. attributes and methods listed in the `ABCPolyBase` documentation.
  1287. Parameters
  1288. ----------
  1289. coef : array_like
  1290. Laguerre coefficients in order of increasing degree, i.e,
  1291. ``(1, 2, 3)`` gives ``1*L_0(x) + 2*L_1(X) + 3*L_2(x)``.
  1292. domain : (2,) array_like, optional
  1293. Domain to use. The interval ``[domain[0], domain[1]]`` is mapped
  1294. to the interval ``[window[0], window[1]]`` by shifting and scaling.
  1295. The default value is [0, 1].
  1296. window : (2,) array_like, optional
  1297. Window, see `domain` for its use. The default value is [0, 1].
  1298. .. versionadded:: 1.6.0
  1299. """
  1300. # Virtual Functions
  1301. _add = staticmethod(lagadd)
  1302. _sub = staticmethod(lagsub)
  1303. _mul = staticmethod(lagmul)
  1304. _div = staticmethod(lagdiv)
  1305. _pow = staticmethod(lagpow)
  1306. _val = staticmethod(lagval)
  1307. _int = staticmethod(lagint)
  1308. _der = staticmethod(lagder)
  1309. _fit = staticmethod(lagfit)
  1310. _line = staticmethod(lagline)
  1311. _roots = staticmethod(lagroots)
  1312. _fromroots = staticmethod(lagfromroots)
  1313. # Virtual properties
  1314. domain = np.array(lagdomain)
  1315. window = np.array(lagdomain)
  1316. basis_name = 'L'