lapack.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. """
  2. Low-level LAPACK functions (:mod:`scipy.linalg.lapack`)
  3. =======================================================
  4. This module contains low-level functions from the LAPACK library.
  5. .. versionadded:: 0.12.0
  6. .. note::
  7. The common ``overwrite_<>`` option in many routines, allows the
  8. input arrays to be overwritten to avoid extra memory allocation.
  9. However this requires the array to satisfy two conditions
  10. which are memory order and the data type to match exactly the
  11. order and the type expected by the routine.
  12. As an example, if you pass a double precision float array to any
  13. ``S....`` routine which expects single precision arguments, f2py
  14. will create an intermediate array to match the argument types and
  15. overwriting will be performed on that intermediate array.
  16. Similarly, if a C-contiguous array is passed, f2py will pass a
  17. FORTRAN-contiguous array internally. Please make sure that these
  18. details are satisfied. More information can be found in the f2py
  19. documentation.
  20. .. warning::
  21. These functions do little to no error checking.
  22. It is possible to cause crashes by mis-using them,
  23. so prefer using the higher-level routines in `scipy.linalg`.
  24. Finding functions
  25. -----------------
  26. .. autosummary::
  27. :toctree: generated/
  28. get_lapack_funcs
  29. All functions
  30. -------------
  31. .. autosummary::
  32. :toctree: generated/
  33. sgbcon
  34. dgbcon
  35. cgbcon
  36. zgbcon
  37. sgbsv
  38. dgbsv
  39. cgbsv
  40. zgbsv
  41. sgbtrf
  42. dgbtrf
  43. cgbtrf
  44. zgbtrf
  45. sgbtrs
  46. dgbtrs
  47. cgbtrs
  48. zgbtrs
  49. sgebal
  50. dgebal
  51. cgebal
  52. zgebal
  53. sgecon
  54. dgecon
  55. cgecon
  56. zgecon
  57. sgeequ
  58. dgeequ
  59. cgeequ
  60. zgeequ
  61. sgeequb
  62. dgeequb
  63. cgeequb
  64. zgeequb
  65. sgees
  66. dgees
  67. cgees
  68. zgees
  69. sgeev
  70. dgeev
  71. cgeev
  72. zgeev
  73. sgeev_lwork
  74. dgeev_lwork
  75. cgeev_lwork
  76. zgeev_lwork
  77. sgehrd
  78. dgehrd
  79. cgehrd
  80. zgehrd
  81. sgehrd_lwork
  82. dgehrd_lwork
  83. cgehrd_lwork
  84. zgehrd_lwork
  85. sgejsv
  86. dgejsv
  87. sgels
  88. dgels
  89. cgels
  90. zgels
  91. sgels_lwork
  92. dgels_lwork
  93. cgels_lwork
  94. zgels_lwork
  95. sgelsd
  96. dgelsd
  97. cgelsd
  98. zgelsd
  99. sgelsd_lwork
  100. dgelsd_lwork
  101. cgelsd_lwork
  102. zgelsd_lwork
  103. sgelss
  104. dgelss
  105. cgelss
  106. zgelss
  107. sgelss_lwork
  108. dgelss_lwork
  109. cgelss_lwork
  110. zgelss_lwork
  111. sgelsy
  112. dgelsy
  113. cgelsy
  114. zgelsy
  115. sgelsy_lwork
  116. dgelsy_lwork
  117. cgelsy_lwork
  118. zgelsy_lwork
  119. sgeqp3
  120. dgeqp3
  121. cgeqp3
  122. zgeqp3
  123. sgeqrf
  124. dgeqrf
  125. cgeqrf
  126. zgeqrf
  127. sgeqrf_lwork
  128. dgeqrf_lwork
  129. cgeqrf_lwork
  130. zgeqrf_lwork
  131. sgeqrfp
  132. dgeqrfp
  133. cgeqrfp
  134. zgeqrfp
  135. sgeqrfp_lwork
  136. dgeqrfp_lwork
  137. cgeqrfp_lwork
  138. zgeqrfp_lwork
  139. sgerqf
  140. dgerqf
  141. cgerqf
  142. zgerqf
  143. sgesdd
  144. dgesdd
  145. cgesdd
  146. zgesdd
  147. sgesdd_lwork
  148. dgesdd_lwork
  149. cgesdd_lwork
  150. zgesdd_lwork
  151. sgesv
  152. dgesv
  153. cgesv
  154. zgesv
  155. sgesvd
  156. dgesvd
  157. cgesvd
  158. zgesvd
  159. sgesvd_lwork
  160. dgesvd_lwork
  161. cgesvd_lwork
  162. zgesvd_lwork
  163. sgesvx
  164. dgesvx
  165. cgesvx
  166. zgesvx
  167. sgetrf
  168. dgetrf
  169. cgetrf
  170. zgetrf
  171. sgetc2
  172. dgetc2
  173. cgetc2
  174. zgetc2
  175. sgetri
  176. dgetri
  177. cgetri
  178. zgetri
  179. sgetri_lwork
  180. dgetri_lwork
  181. cgetri_lwork
  182. zgetri_lwork
  183. sgetrs
  184. dgetrs
  185. cgetrs
  186. zgetrs
  187. sgesc2
  188. dgesc2
  189. cgesc2
  190. zgesc2
  191. sgges
  192. dgges
  193. cgges
  194. zgges
  195. sggev
  196. dggev
  197. cggev
  198. zggev
  199. sgglse
  200. dgglse
  201. cgglse
  202. zgglse
  203. sgglse_lwork
  204. dgglse_lwork
  205. cgglse_lwork
  206. zgglse_lwork
  207. sgtsv
  208. dgtsv
  209. cgtsv
  210. zgtsv
  211. sgtsvx
  212. dgtsvx
  213. cgtsvx
  214. zgtsvx
  215. chbevd
  216. zhbevd
  217. chbevx
  218. zhbevx
  219. checon
  220. zhecon
  221. cheequb
  222. zheequb
  223. cheev
  224. zheev
  225. cheev_lwork
  226. zheev_lwork
  227. cheevd
  228. zheevd
  229. cheevd_lwork
  230. zheevd_lwork
  231. cheevr
  232. zheevr
  233. cheevr_lwork
  234. zheevr_lwork
  235. cheevx
  236. zheevx
  237. cheevx_lwork
  238. zheevx_lwork
  239. chegst
  240. zhegst
  241. chegv
  242. zhegv
  243. chegv_lwork
  244. zhegv_lwork
  245. chegvd
  246. zhegvd
  247. chegvx
  248. zhegvx
  249. chegvx_lwork
  250. zhegvx_lwork
  251. chesv
  252. zhesv
  253. chesv_lwork
  254. zhesv_lwork
  255. chesvx
  256. zhesvx
  257. chesvx_lwork
  258. zhesvx_lwork
  259. chetrd
  260. zhetrd
  261. chetrd_lwork
  262. zhetrd_lwork
  263. chetrf
  264. zhetrf
  265. chetrf_lwork
  266. zhetrf_lwork
  267. chetri
  268. zhetri
  269. chetrs
  270. zhetrs
  271. chfrk
  272. zhfrk
  273. slamch
  274. dlamch
  275. slangb
  276. dlangb
  277. clangb
  278. zlangb
  279. slange
  280. dlange
  281. clange
  282. zlange
  283. slantr
  284. dlantr
  285. clantr
  286. zlantr
  287. slarf
  288. dlarf
  289. clarf
  290. zlarf
  291. slarfg
  292. dlarfg
  293. clarfg
  294. zlarfg
  295. slartg
  296. dlartg
  297. clartg
  298. zlartg
  299. slasd4
  300. dlasd4
  301. slaswp
  302. dlaswp
  303. claswp
  304. zlaswp
  305. slauum
  306. dlauum
  307. clauum
  308. zlauum
  309. sorcsd
  310. dorcsd
  311. sorcsd_lwork
  312. dorcsd_lwork
  313. sorghr
  314. dorghr
  315. sorghr_lwork
  316. dorghr_lwork
  317. sorgqr
  318. dorgqr
  319. sorgrq
  320. dorgrq
  321. sormqr
  322. dormqr
  323. sormrz
  324. dormrz
  325. sormrz_lwork
  326. dormrz_lwork
  327. spbsv
  328. dpbsv
  329. cpbsv
  330. zpbsv
  331. spbtrf
  332. dpbtrf
  333. cpbtrf
  334. zpbtrf
  335. spbtrs
  336. dpbtrs
  337. cpbtrs
  338. zpbtrs
  339. spftrf
  340. dpftrf
  341. cpftrf
  342. zpftrf
  343. spftri
  344. dpftri
  345. cpftri
  346. zpftri
  347. spftrs
  348. dpftrs
  349. cpftrs
  350. zpftrs
  351. spocon
  352. dpocon
  353. cpocon
  354. zpocon
  355. spstrf
  356. dpstrf
  357. cpstrf
  358. zpstrf
  359. spstf2
  360. dpstf2
  361. cpstf2
  362. zpstf2
  363. sposv
  364. dposv
  365. cposv
  366. zposv
  367. sposvx
  368. dposvx
  369. cposvx
  370. zposvx
  371. spotrf
  372. dpotrf
  373. cpotrf
  374. zpotrf
  375. spotri
  376. dpotri
  377. cpotri
  378. zpotri
  379. spotrs
  380. dpotrs
  381. cpotrs
  382. zpotrs
  383. sppcon
  384. dppcon
  385. cppcon
  386. zppcon
  387. sppsv
  388. dppsv
  389. cppsv
  390. zppsv
  391. spptrf
  392. dpptrf
  393. cpptrf
  394. zpptrf
  395. spptri
  396. dpptri
  397. cpptri
  398. zpptri
  399. spptrs
  400. dpptrs
  401. cpptrs
  402. zpptrs
  403. sptsv
  404. dptsv
  405. cptsv
  406. zptsv
  407. sptsvx
  408. dptsvx
  409. cptsvx
  410. zptsvx
  411. spttrf
  412. dpttrf
  413. cpttrf
  414. zpttrf
  415. spttrs
  416. dpttrs
  417. cpttrs
  418. zpttrs
  419. spteqr
  420. dpteqr
  421. cpteqr
  422. zpteqr
  423. crot
  424. zrot
  425. ssbev
  426. dsbev
  427. ssbevd
  428. dsbevd
  429. ssbevx
  430. dsbevx
  431. ssfrk
  432. dsfrk
  433. sstebz
  434. dstebz
  435. sstein
  436. dstein
  437. sstemr
  438. dstemr
  439. sstemr_lwork
  440. dstemr_lwork
  441. ssterf
  442. dsterf
  443. sstev
  444. dstev
  445. sstevd
  446. dstevd
  447. ssycon
  448. dsycon
  449. csycon
  450. zsycon
  451. ssyconv
  452. dsyconv
  453. csyconv
  454. zsyconv
  455. ssyequb
  456. dsyequb
  457. csyequb
  458. zsyequb
  459. ssyev
  460. dsyev
  461. ssyev_lwork
  462. dsyev_lwork
  463. ssyevd
  464. dsyevd
  465. ssyevd_lwork
  466. dsyevd_lwork
  467. ssyevr
  468. dsyevr
  469. ssyevr_lwork
  470. dsyevr_lwork
  471. ssyevx
  472. dsyevx
  473. ssyevx_lwork
  474. dsyevx_lwork
  475. ssygst
  476. dsygst
  477. ssygv
  478. dsygv
  479. ssygv_lwork
  480. dsygv_lwork
  481. ssygvd
  482. dsygvd
  483. ssygvx
  484. dsygvx
  485. ssygvx_lwork
  486. dsygvx_lwork
  487. ssysv
  488. dsysv
  489. csysv
  490. zsysv
  491. ssysv_lwork
  492. dsysv_lwork
  493. csysv_lwork
  494. zsysv_lwork
  495. ssysvx
  496. dsysvx
  497. csysvx
  498. zsysvx
  499. ssysvx_lwork
  500. dsysvx_lwork
  501. csysvx_lwork
  502. zsysvx_lwork
  503. ssytf2
  504. dsytf2
  505. csytf2
  506. zsytf2
  507. ssytrd
  508. dsytrd
  509. ssytrd_lwork
  510. dsytrd_lwork
  511. ssytrf
  512. dsytrf
  513. csytrf
  514. zsytrf
  515. ssytrf_lwork
  516. dsytrf_lwork
  517. csytrf_lwork
  518. zsytrf_lwork
  519. ssytri
  520. dsytri
  521. csytri
  522. zsytri
  523. ssytrs
  524. dsytrs
  525. csytrs
  526. zsytrs
  527. stbtrs
  528. dtbtrs
  529. ctbtrs
  530. ztbtrs
  531. stfsm
  532. dtfsm
  533. ctfsm
  534. ztfsm
  535. stfttp
  536. dtfttp
  537. ctfttp
  538. ztfttp
  539. stfttr
  540. dtfttr
  541. ctfttr
  542. ztfttr
  543. stgexc
  544. dtgexc
  545. ctgexc
  546. ztgexc
  547. stgsen
  548. dtgsen
  549. ctgsen
  550. ztgsen
  551. stgsen_lwork
  552. dtgsen_lwork
  553. ctgsen_lwork
  554. ztgsen_lwork
  555. stgsyl
  556. dtgsyl
  557. stpttf
  558. dtpttf
  559. ctpttf
  560. ztpttf
  561. stpttr
  562. dtpttr
  563. ctpttr
  564. ztpttr
  565. strcon
  566. dtrcon
  567. ctrcon
  568. ztrcon
  569. strexc
  570. dtrexc
  571. ctrexc
  572. ztrexc
  573. strsen
  574. dtrsen
  575. ctrsen
  576. ztrsen
  577. strsen_lwork
  578. dtrsen_lwork
  579. ctrsen_lwork
  580. ztrsen_lwork
  581. strsyl
  582. dtrsyl
  583. ctrsyl
  584. ztrsyl
  585. strtri
  586. dtrtri
  587. ctrtri
  588. ztrtri
  589. strtrs
  590. dtrtrs
  591. ctrtrs
  592. ztrtrs
  593. strttf
  594. dtrttf
  595. ctrttf
  596. ztrttf
  597. strttp
  598. dtrttp
  599. ctrttp
  600. ztrttp
  601. stzrzf
  602. dtzrzf
  603. ctzrzf
  604. ztzrzf
  605. stzrzf_lwork
  606. dtzrzf_lwork
  607. ctzrzf_lwork
  608. ztzrzf_lwork
  609. cunghr
  610. zunghr
  611. cunghr_lwork
  612. zunghr_lwork
  613. cungqr
  614. zungqr
  615. cungrq
  616. zungrq
  617. cunmqr
  618. zunmqr
  619. sgeqrt
  620. dgeqrt
  621. cgeqrt
  622. zgeqrt
  623. sgemqrt
  624. dgemqrt
  625. cgemqrt
  626. zgemqrt
  627. sgttrf
  628. dgttrf
  629. cgttrf
  630. zgttrf
  631. sgttrs
  632. dgttrs
  633. cgttrs
  634. zgttrs
  635. sgtcon
  636. dgtcon
  637. cgtcon
  638. zgtcon
  639. stpqrt
  640. dtpqrt
  641. ctpqrt
  642. ztpqrt
  643. stpmqrt
  644. dtpmqrt
  645. ctpmqrt
  646. ztpmqrt
  647. cuncsd
  648. zuncsd
  649. cuncsd_lwork
  650. zuncsd_lwork
  651. cunmrz
  652. zunmrz
  653. cunmrz_lwork
  654. zunmrz_lwork
  655. ilaver
  656. """
  657. #
  658. # Author: Pearu Peterson, March 2002
  659. #
  660. import numpy as np
  661. from .blas import (
  662. _get_funcs, _memoize_get_funcs,
  663. find_best_blas_type as find_best_lapack_type # to appease the name test
  664. )
  665. from scipy.linalg import _flapack
  666. from re import compile as regex_compile
  667. try:
  668. from scipy.linalg import _clapack
  669. except ImportError:
  670. _clapack = None
  671. from scipy.__config__ import CONFIG
  672. HAS_ILP64 = CONFIG['Build Dependencies']['lapack']['has ilp64']
  673. del CONFIG
  674. _flapack_64 = None
  675. if HAS_ILP64:
  676. from scipy.linalg import _flapack_64
  677. # Expose all functions (only flapack --- clapack is an implementation detail)
  678. empty_module = None
  679. from scipy.linalg._flapack import * # noqa: E402, F403
  680. del empty_module
  681. __all__ = ['get_lapack_funcs']
  682. # some convenience alias for complex functions
  683. _lapack_alias = {
  684. 'corghr': 'cunghr', 'zorghr': 'zunghr',
  685. 'corghr_lwork': 'cunghr_lwork', 'zorghr_lwork': 'zunghr_lwork',
  686. 'corgqr': 'cungqr', 'zorgqr': 'zungqr',
  687. 'cormqr': 'cunmqr', 'zormqr': 'zunmqr',
  688. 'corgrq': 'cungrq', 'zorgrq': 'zungrq',
  689. }
  690. # Place guards against docstring rendering issues with special characters
  691. p1 = regex_compile(r'with bounds (?P<b>.*?)( and (?P<s>.*?) storage){0,1}\n')
  692. p2 = regex_compile(r'Default: (?P<d>.*?)\n')
  693. def backtickrepl(m):
  694. if m.group('s'):
  695. return (f"with bounds ``{m.group('b')}`` with ``{m.group('s')}`` storage\n")
  696. else:
  697. return f"with bounds ``{m.group('b')}``\n"
  698. for routine in [ssyevr, dsyevr, cheevr, zheevr,
  699. ssyevx, dsyevx, cheevx, zheevx,
  700. ssygvd, dsygvd, chegvd, zhegvd]:
  701. if routine.__doc__:
  702. routine.__doc__ = p1.sub(backtickrepl, routine.__doc__)
  703. routine.__doc__ = p2.sub('Default ``\\1``\n', routine.__doc__)
  704. else:
  705. continue
  706. del regex_compile, p1, p2, backtickrepl
  707. @_memoize_get_funcs
  708. def get_lapack_funcs(names, arrays=(), dtype=None, ilp64=False):
  709. """Return available LAPACK function objects from names.
  710. Arrays are used to determine the optimal prefix of LAPACK routines.
  711. Parameters
  712. ----------
  713. names : str or sequence of str
  714. Name(s) of LAPACK functions without type prefix.
  715. arrays : sequence of ndarrays, optional
  716. Arrays can be given to determine optimal prefix of LAPACK
  717. routines. If not given, double-precision routines will be
  718. used, otherwise the most generic type in arrays will be used.
  719. dtype : str or dtype, optional
  720. Data-type specifier. Not used if `arrays` is non-empty.
  721. ilp64 : {True, False, 'preferred'}, optional
  722. Whether to return ILP64 routine variant.
  723. Choosing 'preferred' returns ILP64 routine if available, and
  724. otherwise the 32-bit routine. Default: False
  725. Returns
  726. -------
  727. funcs : list
  728. List containing the found function(s).
  729. Notes
  730. -----
  731. This routine automatically chooses between Fortran/C
  732. interfaces. Fortran code is used whenever possible for arrays with
  733. column major order. In all other cases, C code is preferred.
  734. In LAPACK, the naming convention is that all functions start with a
  735. type prefix, which depends on the type of the principal
  736. matrix. These can be one of {'s', 'd', 'c', 'z'} for the NumPy
  737. types {float32, float64, complex64, complex128} respectively, and
  738. are stored in attribute ``typecode`` of the returned functions.
  739. Examples
  740. --------
  741. Suppose we would like to use '?lange' routine which computes the selected
  742. norm of an array. We pass our array in order to get the correct 'lange'
  743. flavor.
  744. >>> import numpy as np
  745. >>> import scipy.linalg as LA
  746. >>> rng = np.random.default_rng()
  747. >>> a = rng.random((3,2))
  748. >>> x_lange = LA.get_lapack_funcs('lange', (a,))
  749. >>> x_lange.typecode
  750. 'd'
  751. >>> x_lange = LA.get_lapack_funcs('lange',(a*1j,))
  752. >>> x_lange.typecode
  753. 'z'
  754. Several LAPACK routines work best when its internal WORK array has
  755. the optimal size (big enough for fast computation and small enough to
  756. avoid waste of memory). This size is determined also by a dedicated query
  757. to the function which is often wrapped as a standalone function and
  758. commonly denoted as ``###_lwork``. Below is an example for ``?sysv``
  759. >>> a = rng.random((1000, 1000))
  760. >>> b = rng.random((1000, 1)) * 1j
  761. >>> # We pick up zsysv and zsysv_lwork due to b array
  762. ... xsysv, xlwork = LA.get_lapack_funcs(('sysv', 'sysv_lwork'), (a, b))
  763. >>> opt_lwork, _ = xlwork(a.shape[0]) # returns a complex for 'z' prefix
  764. >>> udut, ipiv, x, info = xsysv(a, b, lwork=int(opt_lwork.real))
  765. """
  766. if isinstance(ilp64, str):
  767. if ilp64 == 'preferred':
  768. ilp64 = HAS_ILP64
  769. else:
  770. raise ValueError("Invalid value for 'ilp64'")
  771. if not ilp64:
  772. return _get_funcs(names, arrays, dtype,
  773. "LAPACK", _flapack, _clapack,
  774. "flapack", "clapack", _lapack_alias,
  775. ilp64=False)
  776. else:
  777. if not HAS_ILP64:
  778. raise RuntimeError("LAPACK ILP64 routine requested, but Scipy "
  779. "compiled only with 32-bit BLAS")
  780. return _get_funcs(names, arrays, dtype,
  781. "LAPACK", _flapack_64, None,
  782. "flapack_64", None, _lapack_alias,
  783. ilp64=True)
  784. _int32_max = np.iinfo(np.int32).max
  785. _int64_max = np.iinfo(np.int64).max
  786. def _compute_lwork(routine, *args, **kwargs):
  787. """
  788. Round floating-point lwork returned by lapack to integer.
  789. Several LAPACK routines compute optimal values for LWORK, which
  790. they return in a floating-point variable. However, for large
  791. values of LWORK, single-precision floating point is not sufficient
  792. to hold the exact value --- some LAPACK versions (<= 3.5.0 at
  793. least) truncate the returned integer to single precision and in
  794. some cases this can be smaller than the required value.
  795. Examples
  796. --------
  797. >>> from scipy.linalg import lapack
  798. >>> n = 5000
  799. >>> s_r, s_lw = lapack.get_lapack_funcs(('sysvx', 'sysvx_lwork'))
  800. >>> lwork = lapack._compute_lwork(s_lw, n)
  801. >>> lwork
  802. 32000
  803. """
  804. dtype = getattr(routine, 'dtype', None)
  805. int_dtype = getattr(routine, 'int_dtype', None)
  806. ret = routine(*args, **kwargs)
  807. if ret[-1] != 0:
  808. raise ValueError(f"Internal work array size computation failed: {ret[-1]}")
  809. if len(ret) == 2:
  810. return _check_work_float(ret[0].real, dtype, int_dtype)
  811. else:
  812. return tuple(_check_work_float(x.real, dtype, int_dtype)
  813. for x in ret[:-1])
  814. def _check_work_float(value, dtype, int_dtype):
  815. """
  816. Convert LAPACK-returned work array size float to integer,
  817. carefully for single-precision types.
  818. """
  819. if dtype == np.float32 or dtype == np.complex64:
  820. # Single-precision routine -- take next fp value to work
  821. # around possible truncation in LAPACK code
  822. value = np.nextafter(value, np.inf, dtype=np.float32)
  823. value = int(value)
  824. if int_dtype.itemsize == 4:
  825. if value < 0 or value > _int32_max:
  826. raise ValueError("Too large work array required -- computation "
  827. "cannot be performed with standard 32-bit"
  828. " LAPACK.")
  829. elif int_dtype.itemsize == 8:
  830. if value < 0 or value > _int64_max:
  831. raise ValueError("Too large work array required -- computation"
  832. " cannot be performed with standard 64-bit"
  833. " LAPACK.")
  834. return value
  835. # The numpy facilities for type-casting checks are too slow for small sized
  836. # arrays and eat away the time budget for the checkups. Here we set a
  837. # precomputed dict container of the numpy.can_cast() table.
  838. # It can be used to determine quickly what a dtype can be cast to LAPACK
  839. # compatible types, i.e., 'float32, float64, complex64, complex128'.
  840. # Then it can be checked via "casting_dict[arr.dtype.char]"
  841. # TODO unify "normalization" functions below, see gh-24505
  842. _lapack_cast_dict = {x: ''.join([y for y in 'fdFD' if np.can_cast(x, y)])
  843. for x in np.typecodes['All']}
  844. def _normalize_lapack_dtype(a, overwrite_a):
  845. """Make sure an input array has a LAPACK-compatible dtype, cast and copy otherwise.
  846. """
  847. _, dtyp, _ = find_best_lapack_type((a,))
  848. needs_copy = dtyp.char != a.dtype.char # .char to tell 'g' from 'd' on arm
  849. if needs_copy:
  850. a = a.astype(dtyp) # makes a copy, free to scratch
  851. return a, overwrite_a or needs_copy
  852. def _normalize_lapack_dtype1(a, overwrite_a):
  853. if a.dtype.char not in 'fdFD':
  854. dtype_char = _lapack_cast_dict[a.dtype.char]
  855. if not dtype_char: # No casting possible
  856. raise TypeError(f'The dtype {a.dtype} cannot be cast '
  857. 'to float(32, 64) or complex(64, 128).')
  858. a = a.astype(dtype_char[0]) # makes a copy, free to scratch
  859. overwrite_a = True
  860. return a, overwrite_a