My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

qr_solve.cpp 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. #include "qr_solve.h"
  2. #if ENABLED(AUTO_BED_LEVELING_GRID)
  3. #include <stdlib.h>
  4. #include <math.h>
  5. //# include "r8lib.h"
  6. int i4_min ( int i1, int i2 )
  7. /******************************************************************************/
  8. /*
  9. Purpose:
  10. I4_MIN returns the smaller of two I4's.
  11. Licensing:
  12. This code is distributed under the GNU LGPL license.
  13. Modified:
  14. 29 August 2006
  15. Author:
  16. John Burkardt
  17. Parameters:
  18. Input, int I1, I2, two integers to be compared.
  19. Output, int I4_MIN, the smaller of I1 and I2.
  20. */
  21. {
  22. int value;
  23. if ( i1 < i2 )
  24. {
  25. value = i1;
  26. }
  27. else
  28. {
  29. value = i2;
  30. }
  31. return value;
  32. }
  33. double r8_epsilon ( void )
  34. /******************************************************************************/
  35. /*
  36. Purpose:
  37. R8_EPSILON returns the R8 round off unit.
  38. Discussion:
  39. R8_EPSILON is a number R which is a power of 2 with the property that,
  40. to the precision of the computer's arithmetic,
  41. 1 < 1 + R
  42. but
  43. 1 = ( 1 + R / 2 )
  44. Licensing:
  45. This code is distributed under the GNU LGPL license.
  46. Modified:
  47. 01 September 2012
  48. Author:
  49. John Burkardt
  50. Parameters:
  51. Output, double R8_EPSILON, the R8 round-off unit.
  52. */
  53. {
  54. const double value = 2.220446049250313E-016;
  55. return value;
  56. }
  57. double r8_max ( double x, double y )
  58. /******************************************************************************/
  59. /*
  60. Purpose:
  61. R8_MAX returns the maximum of two R8's.
  62. Licensing:
  63. This code is distributed under the GNU LGPL license.
  64. Modified:
  65. 07 May 2006
  66. Author:
  67. John Burkardt
  68. Parameters:
  69. Input, double X, Y, the quantities to compare.
  70. Output, double R8_MAX, the maximum of X and Y.
  71. */
  72. {
  73. double value;
  74. if ( y < x )
  75. {
  76. value = x;
  77. }
  78. else
  79. {
  80. value = y;
  81. }
  82. return value;
  83. }
  84. double r8_abs ( double x )
  85. /******************************************************************************/
  86. /*
  87. Purpose:
  88. R8_ABS returns the absolute value of an R8.
  89. Licensing:
  90. This code is distributed under the GNU LGPL license.
  91. Modified:
  92. 07 May 2006
  93. Author:
  94. John Burkardt
  95. Parameters:
  96. Input, double X, the quantity whose absolute value is desired.
  97. Output, double R8_ABS, the absolute value of X.
  98. */
  99. {
  100. double value;
  101. if ( 0.0 <= x )
  102. {
  103. value = + x;
  104. }
  105. else
  106. {
  107. value = - x;
  108. }
  109. return value;
  110. }
  111. double r8_sign ( double x )
  112. /******************************************************************************/
  113. /*
  114. Purpose:
  115. R8_SIGN returns the sign of an R8.
  116. Licensing:
  117. This code is distributed under the GNU LGPL license.
  118. Modified:
  119. 08 May 2006
  120. Author:
  121. John Burkardt
  122. Parameters:
  123. Input, double X, the number whose sign is desired.
  124. Output, double R8_SIGN, the sign of X.
  125. */
  126. {
  127. double value;
  128. if ( x < 0.0 )
  129. {
  130. value = - 1.0;
  131. }
  132. else
  133. {
  134. value = + 1.0;
  135. }
  136. return value;
  137. }
  138. double r8mat_amax ( int m, int n, double a[] )
  139. /******************************************************************************/
  140. /*
  141. Purpose:
  142. R8MAT_AMAX returns the maximum absolute value entry of an R8MAT.
  143. Discussion:
  144. An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
  145. in column-major order.
  146. Licensing:
  147. This code is distributed under the GNU LGPL license.
  148. Modified:
  149. 07 September 2012
  150. Author:
  151. John Burkardt
  152. Parameters:
  153. Input, int M, the number of rows in A.
  154. Input, int N, the number of columns in A.
  155. Input, double A[M*N], the M by N matrix.
  156. Output, double R8MAT_AMAX, the maximum absolute value entry of A.
  157. */
  158. {
  159. int i;
  160. int j;
  161. double value;
  162. value = r8_abs ( a[0+0*m] );
  163. for ( j = 0; j < n; j++ )
  164. {
  165. for ( i = 0; i < m; i++ )
  166. {
  167. if ( value < r8_abs ( a[i+j*m] ) )
  168. {
  169. value = r8_abs ( a[i+j*m] );
  170. }
  171. }
  172. }
  173. return value;
  174. }
  175. void r8mat_copy( double a2[], int m, int n, double a1[] )
  176. /******************************************************************************/
  177. /*
  178. Purpose:
  179. R8MAT_COPY_NEW copies one R8MAT to a "new" R8MAT.
  180. Discussion:
  181. An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
  182. in column-major order.
  183. Licensing:
  184. This code is distributed under the GNU LGPL license.
  185. Modified:
  186. 26 July 2008
  187. Author:
  188. John Burkardt
  189. Parameters:
  190. Input, int M, N, the number of rows and columns.
  191. Input, double A1[M*N], the matrix to be copied.
  192. Output, double R8MAT_COPY_NEW[M*N], the copy of A1.
  193. */
  194. {
  195. int i;
  196. int j;
  197. for ( j = 0; j < n; j++ )
  198. {
  199. for ( i = 0; i < m; i++ )
  200. {
  201. a2[i+j*m] = a1[i+j*m];
  202. }
  203. }
  204. }
  205. /******************************************************************************/
  206. void daxpy ( int n, double da, double dx[], int incx, double dy[], int incy )
  207. /******************************************************************************/
  208. /*
  209. Purpose:
  210. DAXPY computes constant times a vector plus a vector.
  211. Discussion:
  212. This routine uses unrolled loops for increments equal to one.
  213. Licensing:
  214. This code is distributed under the GNU LGPL license.
  215. Modified:
  216. 30 March 2007
  217. Author:
  218. C version by John Burkardt
  219. Reference:
  220. Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
  221. LINPACK User's Guide,
  222. SIAM, 1979.
  223. Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
  224. Basic Linear Algebra Subprograms for Fortran Usage,
  225. Algorithm 539,
  226. ACM Transactions on Mathematical Software,
  227. Volume 5, Number 3, September 1979, pages 308-323.
  228. Parameters:
  229. Input, int N, the number of elements in DX and DY.
  230. Input, double DA, the multiplier of DX.
  231. Input, double DX[*], the first vector.
  232. Input, int INCX, the increment between successive entries of DX.
  233. Input/output, double DY[*], the second vector.
  234. On output, DY[*] has been replaced by DY[*] + DA * DX[*].
  235. Input, int INCY, the increment between successive entries of DY.
  236. */
  237. {
  238. int i;
  239. int ix;
  240. int iy;
  241. int m;
  242. if ( n <= 0 )
  243. {
  244. return;
  245. }
  246. if ( da == 0.0 )
  247. {
  248. return;
  249. }
  250. /*
  251. Code for unequal increments or equal increments
  252. not equal to 1.
  253. */
  254. if ( incx != 1 || incy != 1 )
  255. {
  256. if ( 0 <= incx )
  257. {
  258. ix = 0;
  259. }
  260. else
  261. {
  262. ix = ( - n + 1 ) * incx;
  263. }
  264. if ( 0 <= incy )
  265. {
  266. iy = 0;
  267. }
  268. else
  269. {
  270. iy = ( - n + 1 ) * incy;
  271. }
  272. for ( i = 0; i < n; i++ )
  273. {
  274. dy[iy] = dy[iy] + da * dx[ix];
  275. ix = ix + incx;
  276. iy = iy + incy;
  277. }
  278. }
  279. /*
  280. Code for both increments equal to 1.
  281. */
  282. else
  283. {
  284. m = n % 4;
  285. for ( i = 0; i < m; i++ )
  286. {
  287. dy[i] = dy[i] + da * dx[i];
  288. }
  289. for ( i = m; i < n; i = i + 4 )
  290. {
  291. dy[i ] = dy[i ] + da * dx[i ];
  292. dy[i+1] = dy[i+1] + da * dx[i+1];
  293. dy[i+2] = dy[i+2] + da * dx[i+2];
  294. dy[i+3] = dy[i+3] + da * dx[i+3];
  295. }
  296. }
  297. return;
  298. }
  299. /******************************************************************************/
  300. double ddot ( int n, double dx[], int incx, double dy[], int incy )
  301. /******************************************************************************/
  302. /*
  303. Purpose:
  304. DDOT forms the dot product of two vectors.
  305. Discussion:
  306. This routine uses unrolled loops for increments equal to one.
  307. Licensing:
  308. This code is distributed under the GNU LGPL license.
  309. Modified:
  310. 30 March 2007
  311. Author:
  312. C version by John Burkardt
  313. Reference:
  314. Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
  315. LINPACK User's Guide,
  316. SIAM, 1979.
  317. Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
  318. Basic Linear Algebra Subprograms for Fortran Usage,
  319. Algorithm 539,
  320. ACM Transactions on Mathematical Software,
  321. Volume 5, Number 3, September 1979, pages 308-323.
  322. Parameters:
  323. Input, int N, the number of entries in the vectors.
  324. Input, double DX[*], the first vector.
  325. Input, int INCX, the increment between successive entries in DX.
  326. Input, double DY[*], the second vector.
  327. Input, int INCY, the increment between successive entries in DY.
  328. Output, double DDOT, the sum of the product of the corresponding
  329. entries of DX and DY.
  330. */
  331. {
  332. double dtemp;
  333. int i;
  334. int ix;
  335. int iy;
  336. int m;
  337. dtemp = 0.0;
  338. if ( n <= 0 )
  339. {
  340. return dtemp;
  341. }
  342. /*
  343. Code for unequal increments or equal increments
  344. not equal to 1.
  345. */
  346. if ( incx != 1 || incy != 1 )
  347. {
  348. if ( 0 <= incx )
  349. {
  350. ix = 0;
  351. }
  352. else
  353. {
  354. ix = ( - n + 1 ) * incx;
  355. }
  356. if ( 0 <= incy )
  357. {
  358. iy = 0;
  359. }
  360. else
  361. {
  362. iy = ( - n + 1 ) * incy;
  363. }
  364. for ( i = 0; i < n; i++ )
  365. {
  366. dtemp = dtemp + dx[ix] * dy[iy];
  367. ix = ix + incx;
  368. iy = iy + incy;
  369. }
  370. }
  371. /*
  372. Code for both increments equal to 1.
  373. */
  374. else
  375. {
  376. m = n % 5;
  377. for ( i = 0; i < m; i++ )
  378. {
  379. dtemp = dtemp + dx[i] * dy[i];
  380. }
  381. for ( i = m; i < n; i = i + 5 )
  382. {
  383. dtemp = dtemp + dx[i ] * dy[i ]
  384. + dx[i+1] * dy[i+1]
  385. + dx[i+2] * dy[i+2]
  386. + dx[i+3] * dy[i+3]
  387. + dx[i+4] * dy[i+4];
  388. }
  389. }
  390. return dtemp;
  391. }
  392. /******************************************************************************/
  393. double dnrm2 ( int n, double x[], int incx )
  394. /******************************************************************************/
  395. /*
  396. Purpose:
  397. DNRM2 returns the euclidean norm of a vector.
  398. Discussion:
  399. DNRM2 ( X ) = sqrt ( X' * X )
  400. Licensing:
  401. This code is distributed under the GNU LGPL license.
  402. Modified:
  403. 30 March 2007
  404. Author:
  405. C version by John Burkardt
  406. Reference:
  407. Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
  408. LINPACK User's Guide,
  409. SIAM, 1979.
  410. Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
  411. Basic Linear Algebra Subprograms for Fortran Usage,
  412. Algorithm 539,
  413. ACM Transactions on Mathematical Software,
  414. Volume 5, Number 3, September 1979, pages 308-323.
  415. Parameters:
  416. Input, int N, the number of entries in the vector.
  417. Input, double X[*], the vector whose norm is to be computed.
  418. Input, int INCX, the increment between successive entries of X.
  419. Output, double DNRM2, the Euclidean norm of X.
  420. */
  421. {
  422. double absxi;
  423. int i;
  424. int ix;
  425. double norm;
  426. double scale;
  427. double ssq;
  428. if ( n < 1 || incx < 1 )
  429. {
  430. norm = 0.0;
  431. }
  432. else if ( n == 1 )
  433. {
  434. norm = r8_abs ( x[0] );
  435. }
  436. else
  437. {
  438. scale = 0.0;
  439. ssq = 1.0;
  440. ix = 0;
  441. for ( i = 0; i < n; i++ )
  442. {
  443. if ( x[ix] != 0.0 )
  444. {
  445. absxi = r8_abs ( x[ix] );
  446. if ( scale < absxi )
  447. {
  448. ssq = 1.0 + ssq * ( scale / absxi ) * ( scale / absxi );
  449. scale = absxi;
  450. }
  451. else
  452. {
  453. ssq = ssq + ( absxi / scale ) * ( absxi / scale );
  454. }
  455. }
  456. ix = ix + incx;
  457. }
  458. norm = scale * sqrt ( ssq );
  459. }
  460. return norm;
  461. }
  462. /******************************************************************************/
  463. void dqrank ( double a[], int lda, int m, int n, double tol, int *kr,
  464. int jpvt[], double qraux[] )
  465. /******************************************************************************/
  466. /*
  467. Purpose:
  468. DQRANK computes the QR factorization of a rectangular matrix.
  469. Discussion:
  470. This routine is used in conjunction with DQRLSS to solve
  471. overdetermined, underdetermined and singular linear systems
  472. in a least squares sense.
  473. DQRANK uses the LINPACK subroutine DQRDC to compute the QR
  474. factorization, with column pivoting, of an M by N matrix A.
  475. The numerical rank is determined using the tolerance TOL.
  476. Note that on output, ABS ( A(1,1) ) / ABS ( A(KR,KR) ) is an estimate
  477. of the condition number of the matrix of independent columns,
  478. and of R. This estimate will be <= 1/TOL.
  479. Licensing:
  480. This code is distributed under the GNU LGPL license.
  481. Modified:
  482. 21 April 2012
  483. Author:
  484. C version by John Burkardt.
  485. Reference:
  486. Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
  487. LINPACK User's Guide,
  488. SIAM, 1979,
  489. ISBN13: 978-0-898711-72-1,
  490. LC: QA214.L56.
  491. Parameters:
  492. Input/output, double A[LDA*N]. On input, the matrix whose
  493. decomposition is to be computed. On output, the information from DQRDC.
  494. The triangular matrix R of the QR factorization is contained in the
  495. upper triangle and information needed to recover the orthogonal
  496. matrix Q is stored below the diagonal in A and in the vector QRAUX.
  497. Input, int LDA, the leading dimension of A, which must
  498. be at least M.
  499. Input, int M, the number of rows of A.
  500. Input, int N, the number of columns of A.
  501. Input, double TOL, a relative tolerance used to determine the
  502. numerical rank. The problem should be scaled so that all the elements
  503. of A have roughly the same absolute accuracy, EPS. Then a reasonable
  504. value for TOL is roughly EPS divided by the magnitude of the largest
  505. element.
  506. Output, int *KR, the numerical rank.
  507. Output, int JPVT[N], the pivot information from DQRDC.
  508. Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
  509. independent to within the tolerance TOL and the remaining columns
  510. are linearly dependent.
  511. Output, double QRAUX[N], will contain extra information defining
  512. the QR factorization.
  513. */
  514. {
  515. int i;
  516. int j;
  517. int job;
  518. int k;
  519. double work[n];
  520. for ( i = 0; i < n; i++ )
  521. {
  522. jpvt[i] = 0;
  523. }
  524. job = 1;
  525. dqrdc ( a, lda, m, n, qraux, jpvt, work, job );
  526. *kr = 0;
  527. k = i4_min ( m, n );
  528. for ( j = 0; j < k; j++ )
  529. {
  530. if ( r8_abs ( a[j+j*lda] ) <= tol * r8_abs ( a[0+0*lda] ) )
  531. {
  532. return;
  533. }
  534. *kr = j + 1;
  535. }
  536. return;
  537. }
  538. /******************************************************************************/
  539. void dqrdc ( double a[], int lda, int n, int p, double qraux[], int jpvt[],
  540. double work[], int job )
  541. /******************************************************************************/
  542. /*
  543. Purpose:
  544. DQRDC computes the QR factorization of a real rectangular matrix.
  545. Discussion:
  546. DQRDC uses Householder transformations.
  547. Column pivoting based on the 2-norms of the reduced columns may be
  548. performed at the user's option.
  549. Licensing:
  550. This code is distributed under the GNU LGPL license.
  551. Modified:
  552. 07 June 2005
  553. Author:
  554. C version by John Burkardt.
  555. Reference:
  556. Jack Dongarra, Cleve Moler, Jim Bunch and Pete Stewart,
  557. LINPACK User's Guide,
  558. SIAM, (Society for Industrial and Applied Mathematics),
  559. 3600 University City Science Center,
  560. Philadelphia, PA, 19104-2688.
  561. ISBN 0-89871-172-X
  562. Parameters:
  563. Input/output, double A(LDA,P). On input, the N by P matrix
  564. whose decomposition is to be computed. On output, A contains in
  565. its upper triangle the upper triangular matrix R of the QR
  566. factorization. Below its diagonal A contains information from
  567. which the orthogonal part of the decomposition can be recovered.
  568. Note that if pivoting has been requested, the decomposition is not that
  569. of the original matrix A but that of A with its columns permuted
  570. as described by JPVT.
  571. Input, int LDA, the leading dimension of the array A. LDA must
  572. be at least N.
  573. Input, int N, the number of rows of the matrix A.
  574. Input, int P, the number of columns of the matrix A.
  575. Output, double QRAUX[P], contains further information required
  576. to recover the orthogonal part of the decomposition.
  577. Input/output, integer JPVT[P]. On input, JPVT contains integers that
  578. control the selection of the pivot columns. The K-th column A(*,K) of A
  579. is placed in one of three classes according to the value of JPVT(K).
  580. > 0, then A(K) is an initial column.
  581. = 0, then A(K) is a free column.
  582. < 0, then A(K) is a final column.
  583. Before the decomposition is computed, initial columns are moved to
  584. the beginning of the array A and final columns to the end. Both
  585. initial and final columns are frozen in place during the computation
  586. and only free columns are moved. At the K-th stage of the
  587. reduction, if A(*,K) is occupied by a free column it is interchanged
  588. with the free column of largest reduced norm. JPVT is not referenced
  589. if JOB == 0. On output, JPVT(K) contains the index of the column of the
  590. original matrix that has been interchanged into the K-th column, if
  591. pivoting was requested.
  592. Workspace, double WORK[P]. WORK is not referenced if JOB == 0.
  593. Input, int JOB, initiates column pivoting.
  594. 0, no pivoting is done.
  595. nonzero, pivoting is done.
  596. */
  597. {
  598. int j;
  599. int jp;
  600. int l;
  601. int lup;
  602. int maxj;
  603. double maxnrm;
  604. double nrmxl;
  605. int pl;
  606. int pu;
  607. int swapj;
  608. double t;
  609. double tt;
  610. pl = 1;
  611. pu = 0;
  612. /*
  613. If pivoting is requested, rearrange the columns.
  614. */
  615. if ( job != 0 )
  616. {
  617. for ( j = 1; j <= p; j++ )
  618. {
  619. swapj = ( 0 < jpvt[j-1] );
  620. if ( jpvt[j-1] < 0 )
  621. {
  622. jpvt[j-1] = -j;
  623. }
  624. else
  625. {
  626. jpvt[j-1] = j;
  627. }
  628. if ( swapj )
  629. {
  630. if ( j != pl )
  631. {
  632. dswap ( n, a+0+(pl-1)*lda, 1, a+0+(j-1), 1 );
  633. }
  634. jpvt[j-1] = jpvt[pl-1];
  635. jpvt[pl-1] = j;
  636. pl = pl + 1;
  637. }
  638. }
  639. pu = p;
  640. for ( j = p; 1 <= j; j-- )
  641. {
  642. if ( jpvt[j-1] < 0 )
  643. {
  644. jpvt[j-1] = -jpvt[j-1];
  645. if ( j != pu )
  646. {
  647. dswap ( n, a+0+(pu-1)*lda, 1, a+0+(j-1)*lda, 1 );
  648. jp = jpvt[pu-1];
  649. jpvt[pu-1] = jpvt[j-1];
  650. jpvt[j-1] = jp;
  651. }
  652. pu = pu - 1;
  653. }
  654. }
  655. }
  656. /*
  657. Compute the norms of the free columns.
  658. */
  659. for ( j = pl; j <= pu; j++ )
  660. {
  661. qraux[j-1] = dnrm2 ( n, a+0+(j-1)*lda, 1 );
  662. }
  663. for ( j = pl; j <= pu; j++ )
  664. {
  665. work[j-1] = qraux[j-1];
  666. }
  667. /*
  668. Perform the Householder reduction of A.
  669. */
  670. lup = i4_min ( n, p );
  671. for ( l = 1; l <= lup; l++ )
  672. {
  673. /*
  674. Bring the column of largest norm into the pivot position.
  675. */
  676. if ( pl <= l && l < pu )
  677. {
  678. maxnrm = 0.0;
  679. maxj = l;
  680. for ( j = l; j <= pu; j++ )
  681. {
  682. if ( maxnrm < qraux[j-1] )
  683. {
  684. maxnrm = qraux[j-1];
  685. maxj = j;
  686. }
  687. }
  688. if ( maxj != l )
  689. {
  690. dswap ( n, a+0+(l-1)*lda, 1, a+0+(maxj-1)*lda, 1 );
  691. qraux[maxj-1] = qraux[l-1];
  692. work[maxj-1] = work[l-1];
  693. jp = jpvt[maxj-1];
  694. jpvt[maxj-1] = jpvt[l-1];
  695. jpvt[l-1] = jp;
  696. }
  697. }
  698. /*
  699. Compute the Householder transformation for column L.
  700. */
  701. qraux[l-1] = 0.0;
  702. if ( l != n )
  703. {
  704. nrmxl = dnrm2 ( n-l+1, a+l-1+(l-1)*lda, 1 );
  705. if ( nrmxl != 0.0 )
  706. {
  707. if ( a[l-1+(l-1)*lda] != 0.0 )
  708. {
  709. nrmxl = nrmxl * r8_sign ( a[l-1+(l-1)*lda] );
  710. }
  711. dscal ( n-l+1, 1.0 / nrmxl, a+l-1+(l-1)*lda, 1 );
  712. a[l-1+(l-1)*lda] = 1.0 + a[l-1+(l-1)*lda];
  713. /*
  714. Apply the transformation to the remaining columns, updating the norms.
  715. */
  716. for ( j = l + 1; j <= p; j++ )
  717. {
  718. t = -ddot ( n-l+1, a+l-1+(l-1)*lda, 1, a+l-1+(j-1)*lda, 1 )
  719. / a[l-1+(l-1)*lda];
  720. daxpy ( n-l+1, t, a+l-1+(l-1)*lda, 1, a+l-1+(j-1)*lda, 1 );
  721. if ( pl <= j && j <= pu )
  722. {
  723. if ( qraux[j-1] != 0.0 )
  724. {
  725. tt = 1.0 - pow ( r8_abs ( a[l-1+(j-1)*lda] ) / qraux[j-1], 2 );
  726. tt = r8_max ( tt, 0.0 );
  727. t = tt;
  728. tt = 1.0 + 0.05 * tt * pow ( qraux[j-1] / work[j-1], 2 );
  729. if ( tt != 1.0 )
  730. {
  731. qraux[j-1] = qraux[j-1] * sqrt ( t );
  732. }
  733. else
  734. {
  735. qraux[j-1] = dnrm2 ( n-l, a+l+(j-1)*lda, 1 );
  736. work[j-1] = qraux[j-1];
  737. }
  738. }
  739. }
  740. }
  741. /*
  742. Save the transformation.
  743. */
  744. qraux[l-1] = a[l-1+(l-1)*lda];
  745. a[l-1+(l-1)*lda] = -nrmxl;
  746. }
  747. }
  748. }
  749. return;
  750. }
  751. /******************************************************************************/
  752. int dqrls ( double a[], int lda, int m, int n, double tol, int *kr, double b[],
  753. double x[], double rsd[], int jpvt[], double qraux[], int itask )
  754. /******************************************************************************/
  755. /*
  756. Purpose:
  757. DQRLS factors and solves a linear system in the least squares sense.
  758. Discussion:
  759. The linear system may be overdetermined, underdetermined or singular.
  760. The solution is obtained using a QR factorization of the
  761. coefficient matrix.
  762. DQRLS can be efficiently used to solve several least squares
  763. problems with the same matrix A. The first system is solved
  764. with ITASK = 1. The subsequent systems are solved with
  765. ITASK = 2, to avoid the recomputation of the matrix factors.
  766. The parameters KR, JPVT, and QRAUX must not be modified
  767. between calls to DQRLS.
  768. DQRLS is used to solve in a least squares sense
  769. overdetermined, underdetermined and singular linear systems.
  770. The system is A*X approximates B where A is M by N.
  771. B is a given M-vector, and X is the N-vector to be computed.
  772. A solution X is found which minimimzes the sum of squares (2-norm)
  773. of the residual, A*X - B.
  774. The numerical rank of A is determined using the tolerance TOL.
  775. DQRLS uses the LINPACK subroutine DQRDC to compute the QR
  776. factorization, with column pivoting, of an M by N matrix A.
  777. Licensing:
  778. This code is distributed under the GNU LGPL license.
  779. Modified:
  780. 10 September 2012
  781. Author:
  782. C version by John Burkardt.
  783. Reference:
  784. David Kahaner, Cleve Moler, Steven Nash,
  785. Numerical Methods and Software,
  786. Prentice Hall, 1989,
  787. ISBN: 0-13-627258-4,
  788. LC: TA345.K34.
  789. Parameters:
  790. Input/output, double A[LDA*N], an M by N matrix.
  791. On input, the matrix whose decomposition is to be computed.
  792. In a least squares data fitting problem, A(I,J) is the
  793. value of the J-th basis (model) function at the I-th data point.
  794. On output, A contains the output from DQRDC. The triangular matrix R
  795. of the QR factorization is contained in the upper triangle and
  796. information needed to recover the orthogonal matrix Q is stored
  797. below the diagonal in A and in the vector QRAUX.
  798. Input, int LDA, the leading dimension of A.
  799. Input, int M, the number of rows of A.
  800. Input, int N, the number of columns of A.
  801. Input, double TOL, a relative tolerance used to determine the
  802. numerical rank. The problem should be scaled so that all the elements
  803. of A have roughly the same absolute accuracy EPS. Then a reasonable
  804. value for TOL is roughly EPS divided by the magnitude of the largest
  805. element.
  806. Output, int *KR, the numerical rank.
  807. Input, double B[M], the right hand side of the linear system.
  808. Output, double X[N], a least squares solution to the linear
  809. system.
  810. Output, double RSD[M], the residual, B - A*X. RSD may
  811. overwrite B.
  812. Workspace, int JPVT[N], required if ITASK = 1.
  813. Columns JPVT(1), ..., JPVT(KR) of the original matrix are linearly
  814. independent to within the tolerance TOL and the remaining columns
  815. are linearly dependent. ABS ( A(1,1) ) / ABS ( A(KR,KR) ) is an estimate
  816. of the condition number of the matrix of independent columns,
  817. and of R. This estimate will be <= 1/TOL.
  818. Workspace, double QRAUX[N], required if ITASK = 1.
  819. Input, int ITASK.
  820. 1, DQRLS factors the matrix A and solves the least squares problem.
  821. 2, DQRLS assumes that the matrix A was factored with an earlier
  822. call to DQRLS, and only solves the least squares problem.
  823. Output, int DQRLS, error code.
  824. 0: no error
  825. -1: LDA < M (fatal error)
  826. -2: N < 1 (fatal error)
  827. -3: ITASK < 1 (fatal error)
  828. */
  829. {
  830. int ind;
  831. if ( lda < m )
  832. {
  833. /*fprintf ( stderr, "\n" );
  834. fprintf ( stderr, "DQRLS - Fatal error!\n" );
  835. fprintf ( stderr, " LDA < M.\n" );*/
  836. ind = -1;
  837. return ind;
  838. }
  839. if ( n <= 0 )
  840. {
  841. /*fprintf ( stderr, "\n" );
  842. fprintf ( stderr, "DQRLS - Fatal error!\n" );
  843. fprintf ( stderr, " N <= 0.\n" );*/
  844. ind = -2;
  845. return ind;
  846. }
  847. if ( itask < 1 )
  848. {
  849. /*fprintf ( stderr, "\n" );
  850. fprintf ( stderr, "DQRLS - Fatal error!\n" );
  851. fprintf ( stderr, " ITASK < 1.\n" );*/
  852. ind = -3;
  853. return ind;
  854. }
  855. ind = 0;
  856. /*
  857. Factor the matrix.
  858. */
  859. if ( itask == 1 )
  860. {
  861. dqrank ( a, lda, m, n, tol, kr, jpvt, qraux );
  862. }
  863. /*
  864. Solve the least-squares problem.
  865. */
  866. dqrlss ( a, lda, m, n, *kr, b, x, rsd, jpvt, qraux );
  867. return ind;
  868. }
  869. /******************************************************************************/
  870. void dqrlss ( double a[], int lda, int m, int n, int kr, double b[], double x[],
  871. double rsd[], int jpvt[], double qraux[] )
  872. /******************************************************************************/
  873. /*
  874. Purpose:
  875. DQRLSS solves a linear system in a least squares sense.
  876. Discussion:
  877. DQRLSS must be preceded by a call to DQRANK.
  878. The system is to be solved is
  879. A * X = B
  880. where
  881. A is an M by N matrix with rank KR, as determined by DQRANK,
  882. B is a given M-vector,
  883. X is the N-vector to be computed.
  884. A solution X, with at most KR nonzero components, is found which
  885. minimizes the 2-norm of the residual (A*X-B).
  886. Once the matrix A has been formed, DQRANK should be
  887. called once to decompose it. Then, for each right hand
  888. side B, DQRLSS should be called once to obtain the
  889. solution and residual.
  890. Licensing:
  891. This code is distributed under the GNU LGPL license.
  892. Modified:
  893. 10 September 2012
  894. Author:
  895. C version by John Burkardt
  896. Parameters:
  897. Input, double A[LDA*N], the QR factorization information
  898. from DQRANK. The triangular matrix R of the QR factorization is
  899. contained in the upper triangle and information needed to recover
  900. the orthogonal matrix Q is stored below the diagonal in A and in
  901. the vector QRAUX.
  902. Input, int LDA, the leading dimension of A, which must
  903. be at least M.
  904. Input, int M, the number of rows of A.
  905. Input, int N, the number of columns of A.
  906. Input, int KR, the rank of the matrix, as estimated by DQRANK.
  907. Input, double B[M], the right hand side of the linear system.
  908. Output, double X[N], a least squares solution to the
  909. linear system.
  910. Output, double RSD[M], the residual, B - A*X. RSD may
  911. overwrite B.
  912. Input, int JPVT[N], the pivot information from DQRANK.
  913. Columns JPVT[0], ..., JPVT[KR-1] of the original matrix are linearly
  914. independent to within the tolerance TOL and the remaining columns
  915. are linearly dependent.
  916. Input, double QRAUX[N], auxiliary information from DQRANK
  917. defining the QR factorization.
  918. */
  919. {
  920. int i;
  921. int info;
  922. int j;
  923. int job;
  924. int k;
  925. double t;
  926. if ( kr != 0 )
  927. {
  928. job = 110;
  929. info = dqrsl ( a, lda, m, kr, qraux, b, rsd, rsd, x, rsd, rsd, job );
  930. }
  931. for ( i = 0; i < n; i++ )
  932. {
  933. jpvt[i] = - jpvt[i];
  934. }
  935. for ( i = kr; i < n; i++ )
  936. {
  937. x[i] = 0.0;
  938. }
  939. for ( j = 1; j <= n; j++ )
  940. {
  941. if ( jpvt[j-1] <= 0 )
  942. {
  943. k = - jpvt[j-1];
  944. jpvt[j-1] = k;
  945. while ( k != j )
  946. {
  947. t = x[j-1];
  948. x[j-1] = x[k-1];
  949. x[k-1] = t;
  950. jpvt[k-1] = -jpvt[k-1];
  951. k = jpvt[k-1];
  952. }
  953. }
  954. }
  955. return;
  956. }
  957. /******************************************************************************/
  958. int dqrsl ( double a[], int lda, int n, int k, double qraux[], double y[],
  959. double qy[], double qty[], double b[], double rsd[], double ab[], int job )
  960. /******************************************************************************/
  961. /*
  962. Purpose:
  963. DQRSL computes transformations, projections, and least squares solutions.
  964. Discussion:
  965. DQRSL requires the output of DQRDC.
  966. For K <= min(N,P), let AK be the matrix
  967. AK = ( A(JPVT[0]), A(JPVT(2)), ..., A(JPVT(K)) )
  968. formed from columns JPVT[0], ..., JPVT(K) of the original
  969. N by P matrix A that was input to DQRDC. If no pivoting was
  970. done, AK consists of the first K columns of A in their
  971. original order. DQRDC produces a factored orthogonal matrix Q
  972. and an upper triangular matrix R such that
  973. AK = Q * (R)
  974. (0)
  975. This information is contained in coded form in the arrays
  976. A and QRAUX.
  977. The parameters QY, QTY, B, RSD, and AB are not referenced
  978. if their computation is not requested and in this case
  979. can be replaced by dummy variables in the calling program.
  980. To save storage, the user may in some cases use the same
  981. array for different parameters in the calling sequence. A
  982. frequently occurring example is when one wishes to compute
  983. any of B, RSD, or AB and does not need Y or QTY. In this
  984. case one may identify Y, QTY, and one of B, RSD, or AB, while
  985. providing separate arrays for anything else that is to be
  986. computed.
  987. Thus the calling sequence
  988. dqrsl ( a, lda, n, k, qraux, y, dum, y, b, y, dum, 110, info )
  989. will result in the computation of B and RSD, with RSD
  990. overwriting Y. More generally, each item in the following
  991. list contains groups of permissible identifications for
  992. a single calling sequence.
  993. 1. (Y,QTY,B) (RSD) (AB) (QY)
  994. 2. (Y,QTY,RSD) (B) (AB) (QY)
  995. 3. (Y,QTY,AB) (B) (RSD) (QY)
  996. 4. (Y,QY) (QTY,B) (RSD) (AB)
  997. 5. (Y,QY) (QTY,RSD) (B) (AB)
  998. 6. (Y,QY) (QTY,AB) (B) (RSD)
  999. In any group the value returned in the array allocated to
  1000. the group corresponds to the last member of the group.
  1001. Licensing:
  1002. This code is distributed under the GNU LGPL license.
  1003. Modified:
  1004. 07 June 2005
  1005. Author:
  1006. C version by John Burkardt.
  1007. Reference:
  1008. Jack Dongarra, Cleve Moler, Jim Bunch and Pete Stewart,
  1009. LINPACK User's Guide,
  1010. SIAM, (Society for Industrial and Applied Mathematics),
  1011. 3600 University City Science Center,
  1012. Philadelphia, PA, 19104-2688.
  1013. ISBN 0-89871-172-X
  1014. Parameters:
  1015. Input, double A[LDA*P], contains the output of DQRDC.
  1016. Input, int LDA, the leading dimension of the array A.
  1017. Input, int N, the number of rows of the matrix AK. It must
  1018. have the same value as N in DQRDC.
  1019. Input, int K, the number of columns of the matrix AK. K
  1020. must not be greater than min(N,P), where P is the same as in the
  1021. calling sequence to DQRDC.
  1022. Input, double QRAUX[P], the auxiliary output from DQRDC.
  1023. Input, double Y[N], a vector to be manipulated by DQRSL.
  1024. Output, double QY[N], contains Q * Y, if requested.
  1025. Output, double QTY[N], contains Q' * Y, if requested.
  1026. Output, double B[K], the solution of the least squares problem
  1027. minimize norm2 ( Y - AK * B),
  1028. if its computation has been requested. Note that if pivoting was
  1029. requested in DQRDC, the J-th component of B will be associated with
  1030. column JPVT(J) of the original matrix A that was input into DQRDC.
  1031. Output, double RSD[N], the least squares residual Y - AK * B,
  1032. if its computation has been requested. RSD is also the orthogonal
  1033. projection of Y onto the orthogonal complement of the column space
  1034. of AK.
  1035. Output, double AB[N], the least squares approximation Ak * B,
  1036. if its computation has been requested. AB is also the orthogonal
  1037. projection of Y onto the column space of A.
  1038. Input, integer JOB, specifies what is to be computed. JOB has
  1039. the decimal expansion ABCDE, with the following meaning:
  1040. if A != 0, compute QY.
  1041. if B != 0, compute QTY.
  1042. if C != 0, compute QTY and B.
  1043. if D != 0, compute QTY and RSD.
  1044. if E != 0, compute QTY and AB.
  1045. Note that a request to compute B, RSD, or AB automatically triggers
  1046. the computation of QTY, for which an array must be provided in the
  1047. calling sequence.
  1048. Output, int DQRSL, is zero unless the computation of B has
  1049. been requested and R is exactly singular. In this case, INFO is the
  1050. index of the first zero diagonal element of R, and B is left unaltered.
  1051. */
  1052. {
  1053. int cab;
  1054. int cb;
  1055. int cqty;
  1056. int cqy;
  1057. int cr;
  1058. int i;
  1059. int info;
  1060. int j;
  1061. int jj;
  1062. int ju;
  1063. double t;
  1064. double temp;
  1065. /*
  1066. Set INFO flag.
  1067. */
  1068. info = 0;
  1069. /*
  1070. Determine what is to be computed.
  1071. */
  1072. cqy = ( job / 10000 != 0 );
  1073. cqty = ( ( job % 10000 ) != 0 );
  1074. cb = ( ( job % 1000 ) / 100 != 0 );
  1075. cr = ( ( job % 100 ) / 10 != 0 );
  1076. cab = ( ( job % 10 ) != 0 );
  1077. ju = i4_min ( k, n-1 );
  1078. /*
  1079. Special action when N = 1.
  1080. */
  1081. if ( ju == 0 )
  1082. {
  1083. if ( cqy )
  1084. {
  1085. qy[0] = y[0];
  1086. }
  1087. if ( cqty )
  1088. {
  1089. qty[0] = y[0];
  1090. }
  1091. if ( cab )
  1092. {
  1093. ab[0] = y[0];
  1094. }
  1095. if ( cb )
  1096. {
  1097. if ( a[0+0*lda] == 0.0 )
  1098. {
  1099. info = 1;
  1100. }
  1101. else
  1102. {
  1103. b[0] = y[0] / a[0+0*lda];
  1104. }
  1105. }
  1106. if ( cr )
  1107. {
  1108. rsd[0] = 0.0;
  1109. }
  1110. return info;
  1111. }
  1112. /*
  1113. Set up to compute QY or QTY.
  1114. */
  1115. if ( cqy )
  1116. {
  1117. for ( i = 1; i <= n; i++ )
  1118. {
  1119. qy[i-1] = y[i-1];
  1120. }
  1121. }
  1122. if ( cqty )
  1123. {
  1124. for ( i = 1; i <= n; i++ )
  1125. {
  1126. qty[i-1] = y[i-1];
  1127. }
  1128. }
  1129. /*
  1130. Compute QY.
  1131. */
  1132. if ( cqy )
  1133. {
  1134. for ( jj = 1; jj <= ju; jj++ )
  1135. {
  1136. j = ju - jj + 1;
  1137. if ( qraux[j-1] != 0.0 )
  1138. {
  1139. temp = a[j-1+(j-1)*lda];
  1140. a[j-1+(j-1)*lda] = qraux[j-1];
  1141. t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, qy+j-1, 1 ) / a[j-1+(j-1)*lda];
  1142. daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, qy+j-1, 1 );
  1143. a[j-1+(j-1)*lda] = temp;
  1144. }
  1145. }
  1146. }
  1147. /*
  1148. Compute Q'*Y.
  1149. */
  1150. if ( cqty )
  1151. {
  1152. for ( j = 1; j <= ju; j++ )
  1153. {
  1154. if ( qraux[j-1] != 0.0 )
  1155. {
  1156. temp = a[j-1+(j-1)*lda];
  1157. a[j-1+(j-1)*lda] = qraux[j-1];
  1158. t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, qty+j-1, 1 ) / a[j-1+(j-1)*lda];
  1159. daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, qty+j-1, 1 );
  1160. a[j-1+(j-1)*lda] = temp;
  1161. }
  1162. }
  1163. }
  1164. /*
  1165. Set up to compute B, RSD, or AB.
  1166. */
  1167. if ( cb )
  1168. {
  1169. for ( i = 1; i <= k; i++ )
  1170. {
  1171. b[i-1] = qty[i-1];
  1172. }
  1173. }
  1174. if ( cab )
  1175. {
  1176. for ( i = 1; i <= k; i++ )
  1177. {
  1178. ab[i-1] = qty[i-1];
  1179. }
  1180. }
  1181. if ( cr && k < n )
  1182. {
  1183. for ( i = k+1; i <= n; i++ )
  1184. {
  1185. rsd[i-1] = qty[i-1];
  1186. }
  1187. }
  1188. if ( cab && k+1 <= n )
  1189. {
  1190. for ( i = k+1; i <= n; i++ )
  1191. {
  1192. ab[i-1] = 0.0;
  1193. }
  1194. }
  1195. if ( cr )
  1196. {
  1197. for ( i = 1; i <= k; i++ )
  1198. {
  1199. rsd[i-1] = 0.0;
  1200. }
  1201. }
  1202. /*
  1203. Compute B.
  1204. */
  1205. if ( cb )
  1206. {
  1207. for ( jj = 1; jj <= k; jj++ )
  1208. {
  1209. j = k - jj + 1;
  1210. if ( a[j-1+(j-1)*lda] == 0.0 )
  1211. {
  1212. info = j;
  1213. break;
  1214. }
  1215. b[j-1] = b[j-1] / a[j-1+(j-1)*lda];
  1216. if ( j != 1 )
  1217. {
  1218. t = -b[j-1];
  1219. daxpy ( j-1, t, a+0+(j-1)*lda, 1, b, 1 );
  1220. }
  1221. }
  1222. }
  1223. /*
  1224. Compute RSD or AB as required.
  1225. */
  1226. if ( cr || cab )
  1227. {
  1228. for ( jj = 1; jj <= ju; jj++ )
  1229. {
  1230. j = ju - jj + 1;
  1231. if ( qraux[j-1] != 0.0 )
  1232. {
  1233. temp = a[j-1+(j-1)*lda];
  1234. a[j-1+(j-1)*lda] = qraux[j-1];
  1235. if ( cr )
  1236. {
  1237. t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, rsd+j-1, 1 )
  1238. / a[j-1+(j-1)*lda];
  1239. daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, rsd+j-1, 1 );
  1240. }
  1241. if ( cab )
  1242. {
  1243. t = -ddot ( n-j+1, a+j-1+(j-1)*lda, 1, ab+j-1, 1 )
  1244. / a[j-1+(j-1)*lda];
  1245. daxpy ( n-j+1, t, a+j-1+(j-1)*lda, 1, ab+j-1, 1 );
  1246. }
  1247. a[j-1+(j-1)*lda] = temp;
  1248. }
  1249. }
  1250. }
  1251. return info;
  1252. }
  1253. /******************************************************************************/
  1254. /******************************************************************************/
  1255. void dscal ( int n, double sa, double x[], int incx )
  1256. /******************************************************************************/
  1257. /*
  1258. Purpose:
  1259. DSCAL scales a vector by a constant.
  1260. Licensing:
  1261. This code is distributed under the GNU LGPL license.
  1262. Modified:
  1263. 30 March 2007
  1264. Author:
  1265. C version by John Burkardt
  1266. Reference:
  1267. Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
  1268. LINPACK User's Guide,
  1269. SIAM, 1979.
  1270. Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
  1271. Basic Linear Algebra Subprograms for Fortran Usage,
  1272. Algorithm 539,
  1273. ACM Transactions on Mathematical Software,
  1274. Volume 5, Number 3, September 1979, pages 308-323.
  1275. Parameters:
  1276. Input, int N, the number of entries in the vector.
  1277. Input, double SA, the multiplier.
  1278. Input/output, double X[*], the vector to be scaled.
  1279. Input, int INCX, the increment between successive entries of X.
  1280. */
  1281. {
  1282. int i;
  1283. int ix;
  1284. int m;
  1285. if ( n <= 0 )
  1286. {
  1287. }
  1288. else if ( incx == 1 )
  1289. {
  1290. m = n % 5;
  1291. for ( i = 0; i < m; i++ )
  1292. {
  1293. x[i] = sa * x[i];
  1294. }
  1295. for ( i = m; i < n; i = i + 5 )
  1296. {
  1297. x[i] = sa * x[i];
  1298. x[i+1] = sa * x[i+1];
  1299. x[i+2] = sa * x[i+2];
  1300. x[i+3] = sa * x[i+3];
  1301. x[i+4] = sa * x[i+4];
  1302. }
  1303. }
  1304. else
  1305. {
  1306. if ( 0 <= incx )
  1307. {
  1308. ix = 0;
  1309. }
  1310. else
  1311. {
  1312. ix = ( - n + 1 ) * incx;
  1313. }
  1314. for ( i = 0; i < n; i++ )
  1315. {
  1316. x[ix] = sa * x[ix];
  1317. ix = ix + incx;
  1318. }
  1319. }
  1320. return;
  1321. }
  1322. /******************************************************************************/
  1323. void dswap ( int n, double x[], int incx, double y[], int incy )
  1324. /******************************************************************************/
  1325. /*
  1326. Purpose:
  1327. DSWAP interchanges two vectors.
  1328. Licensing:
  1329. This code is distributed under the GNU LGPL license.
  1330. Modified:
  1331. 30 March 2007
  1332. Author:
  1333. C version by John Burkardt
  1334. Reference:
  1335. Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
  1336. LINPACK User's Guide,
  1337. SIAM, 1979.
  1338. Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
  1339. Basic Linear Algebra Subprograms for Fortran Usage,
  1340. Algorithm 539,
  1341. ACM Transactions on Mathematical Software,
  1342. Volume 5, Number 3, September 1979, pages 308-323.
  1343. Parameters:
  1344. Input, int N, the number of entries in the vectors.
  1345. Input/output, double X[*], one of the vectors to swap.
  1346. Input, int INCX, the increment between successive entries of X.
  1347. Input/output, double Y[*], one of the vectors to swap.
  1348. Input, int INCY, the increment between successive elements of Y.
  1349. */
  1350. {
  1351. int i;
  1352. int ix;
  1353. int iy;
  1354. int m;
  1355. double temp;
  1356. if ( n <= 0 )
  1357. {
  1358. }
  1359. else if ( incx == 1 && incy == 1 )
  1360. {
  1361. m = n % 3;
  1362. for ( i = 0; i < m; i++ )
  1363. {
  1364. temp = x[i];
  1365. x[i] = y[i];
  1366. y[i] = temp;
  1367. }
  1368. for ( i = m; i < n; i = i + 3 )
  1369. {
  1370. temp = x[i];
  1371. x[i] = y[i];
  1372. y[i] = temp;
  1373. temp = x[i+1];
  1374. x[i+1] = y[i+1];
  1375. y[i+1] = temp;
  1376. temp = x[i+2];
  1377. x[i+2] = y[i+2];
  1378. y[i+2] = temp;
  1379. }
  1380. }
  1381. else
  1382. {
  1383. if ( 0 <= incx )
  1384. {
  1385. ix = 0;
  1386. }
  1387. else
  1388. {
  1389. ix = ( - n + 1 ) * incx;
  1390. }
  1391. if ( 0 <= incy )
  1392. {
  1393. iy = 0;
  1394. }
  1395. else
  1396. {
  1397. iy = ( - n + 1 ) * incy;
  1398. }
  1399. for ( i = 0; i < n; i++ )
  1400. {
  1401. temp = x[ix];
  1402. x[ix] = y[iy];
  1403. y[iy] = temp;
  1404. ix = ix + incx;
  1405. iy = iy + incy;
  1406. }
  1407. }
  1408. return;
  1409. }
  1410. /******************************************************************************/
  1411. /******************************************************************************/
  1412. void qr_solve ( double x[], int m, int n, double a[], double b[] )
  1413. /******************************************************************************/
  1414. /*
  1415. Purpose:
  1416. QR_SOLVE solves a linear system in the least squares sense.
  1417. Discussion:
  1418. If the matrix A has full column rank, then the solution X should be the
  1419. unique vector that minimizes the Euclidean norm of the residual.
  1420. If the matrix A does not have full column rank, then the solution is
  1421. not unique; the vector X will minimize the residual norm, but so will
  1422. various other vectors.
  1423. Licensing:
  1424. This code is distributed under the GNU LGPL license.
  1425. Modified:
  1426. 11 September 2012
  1427. Author:
  1428. John Burkardt
  1429. Reference:
  1430. David Kahaner, Cleve Moler, Steven Nash,
  1431. Numerical Methods and Software,
  1432. Prentice Hall, 1989,
  1433. ISBN: 0-13-627258-4,
  1434. LC: TA345.K34.
  1435. Parameters:
  1436. Input, int M, the number of rows of A.
  1437. Input, int N, the number of columns of A.
  1438. Input, double A[M*N], the matrix.
  1439. Input, double B[M], the right hand side.
  1440. Output, double QR_SOLVE[N], the least squares solution.
  1441. */
  1442. {
  1443. double a_qr[n*m];
  1444. int ind;
  1445. int itask;
  1446. int jpvt[n];
  1447. int kr;
  1448. int lda;
  1449. double qraux[n];
  1450. double r[m];
  1451. double tol;
  1452. r8mat_copy( a_qr, m, n, a );
  1453. lda = m;
  1454. tol = r8_epsilon ( ) / r8mat_amax ( m, n, a_qr );
  1455. itask = 1;
  1456. ind = dqrls ( a_qr, lda, m, n, tol, &kr, b, x, r, jpvt, qraux, itask );
  1457. }
  1458. /******************************************************************************/
  1459. #endif