My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

qr_solve.cpp 40KB

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