My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qr_solve.cpp 40KB

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