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 39KB

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