My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

qr_solve.cpp 38KB

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