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

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