My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

qr_solve.cpp 40KB

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