lsei.f 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. *DECK LSEI
  2. SUBROUTINE LSEI (W, MDW, ME, MA, MG, N, PRGOPT, X, RNORME, RNORML,
  3. + MODE, WS, IP)
  4. C***BEGIN PROLOGUE LSEI
  5. C***PURPOSE Solve a linearly constrained least squares problem with
  6. C equality and inequality constraints, and optionally compute
  7. C a covariance matrix.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY K1A2A, D9
  10. C***TYPE SINGLE PRECISION (LSEI-S, DLSEI-D)
  11. C***KEYWORDS CONSTRAINED LEAST SQUARES, CURVE FITTING, DATA FITTING,
  12. C EQUALITY CONSTRAINTS, INEQUALITY CONSTRAINTS,
  13. C QUADRATIC PROGRAMMING
  14. C***AUTHOR Hanson, R. J., (SNLA)
  15. C Haskell, K. H., (SNLA)
  16. C***DESCRIPTION
  17. C
  18. C Abstract
  19. C
  20. C This subprogram solves a linearly constrained least squares
  21. C problem with both equality and inequality constraints, and, if the
  22. C user requests, obtains a covariance matrix of the solution
  23. C parameters.
  24. C
  25. C Suppose there are given matrices E, A and G of respective
  26. C dimensions ME by N, MA by N and MG by N, and vectors F, B and H of
  27. C respective lengths ME, MA and MG. This subroutine solves the
  28. C linearly constrained least squares problem
  29. C
  30. C EX = F, (E ME by N) (equations to be exactly
  31. C satisfied)
  32. C AX = B, (A MA by N) (equations to be
  33. C approximately satisfied,
  34. C least squares sense)
  35. C GX .GE. H,(G MG by N) (inequality constraints)
  36. C
  37. C The inequalities GX .GE. H mean that every component of the
  38. C product GX must be .GE. the corresponding component of H.
  39. C
  40. C In case the equality constraints cannot be satisfied, a
  41. C generalized inverse solution residual vector length is obtained
  42. C for F-EX. This is the minimal length possible for F-EX.
  43. C
  44. C Any values ME .GE. 0, MA .GE. 0, or MG .GE. 0 are permitted. The
  45. C rank of the matrix E is estimated during the computation. We call
  46. C this value KRANKE. It is an output parameter in IP(1) defined
  47. C below. Using a generalized inverse solution of EX=F, a reduced
  48. C least squares problem with inequality constraints is obtained.
  49. C The tolerances used in these tests for determining the rank
  50. C of E and the rank of the reduced least squares problem are
  51. C given in Sandia Tech. Rept. SAND-78-1290. They can be
  52. C modified by the user if new values are provided in
  53. C the option list of the array PRGOPT(*).
  54. C
  55. C The user must dimension all arrays appearing in the call list..
  56. C W(MDW,N+1),PRGOPT(*),X(N),WS(2*(ME+N)+K+(MG+2)*(N+7)),IP(MG+2*N+2)
  57. C where K=MAX(MA+MG,N). This allows for a solution of a range of
  58. C problems in the given working space. The dimension of WS(*)
  59. C given is a necessary overestimate. Once a particular problem
  60. C has been run, the output parameter IP(3) gives the actual
  61. C dimension required for that problem.
  62. C
  63. C The parameters for LSEI( ) are
  64. C
  65. C Input..
  66. C
  67. C W(*,*),MDW, The array W(*,*) is doubly subscripted with
  68. C ME,MA,MG,N first dimensioning parameter equal to MDW.
  69. C For this discussion let us call M = ME+MA+MG. Then
  70. C MDW must satisfy MDW .GE. M. The condition
  71. C MDW .LT. M is an error.
  72. C
  73. C The array W(*,*) contains the matrices and vectors
  74. C
  75. C (E F)
  76. C (A B)
  77. C (G H)
  78. C
  79. C in rows and columns 1,...,M and 1,...,N+1
  80. C respectively.
  81. C
  82. C The integers ME, MA, and MG are the
  83. C respective matrix row dimensions
  84. C of E, A and G. Each matrix has N columns.
  85. C
  86. C PRGOPT(*) This real-valued array is the option vector.
  87. C If the user is satisfied with the nominal
  88. C subprogram features set
  89. C
  90. C PRGOPT(1)=1 (or PRGOPT(1)=1.0)
  91. C
  92. C Otherwise PRGOPT(*) is a linked list consisting of
  93. C groups of data of the following form
  94. C
  95. C LINK
  96. C KEY
  97. C DATA SET
  98. C
  99. C The parameters LINK and KEY are each one word.
  100. C The DATA SET can be comprised of several words.
  101. C The number of items depends on the value of KEY.
  102. C The value of LINK points to the first
  103. C entry of the next group of data within
  104. C PRGOPT(*). The exception is when there are
  105. C no more options to change. In that
  106. C case, LINK=1 and the values KEY and DATA SET
  107. C are not referenced. The general layout of
  108. C PRGOPT(*) is as follows.
  109. C
  110. C ...PRGOPT(1) = LINK1 (link to first entry of next group)
  111. C . PRGOPT(2) = KEY1 (key to the option change)
  112. C . PRGOPT(3) = data value (data value for this change)
  113. C . .
  114. C . .
  115. C . .
  116. C ...PRGOPT(LINK1) = LINK2 (link to the first entry of
  117. C . next group)
  118. C . PRGOPT(LINK1+1) = KEY2 (key to the option change)
  119. C . PRGOPT(LINK1+2) = data value
  120. C ... .
  121. C . .
  122. C . .
  123. C ...PRGOPT(LINK) = 1 (no more options to change)
  124. C
  125. C Values of LINK that are nonpositive are errors.
  126. C A value of LINK .GT. NLINK=100000 is also an error.
  127. C This helps prevent using invalid but positive
  128. C values of LINK that will probably extend
  129. C beyond the program limits of PRGOPT(*).
  130. C Unrecognized values of KEY are ignored. The
  131. C order of the options is arbitrary and any number
  132. C of options can be changed with the following
  133. C restriction. To prevent cycling in the
  134. C processing of the option array, a count of the
  135. C number of options changed is maintained.
  136. C Whenever this count exceeds NOPT=1000, an error
  137. C message is printed and the subprogram returns.
  138. C
  139. C Options..
  140. C
  141. C KEY=1
  142. C Compute in W(*,*) the N by N
  143. C covariance matrix of the solution variables
  144. C as an output parameter. Nominally the
  145. C covariance matrix will not be computed.
  146. C (This requires no user input.)
  147. C The data set for this option is a single value.
  148. C It must be nonzero when the covariance matrix
  149. C is desired. If it is zero, the covariance
  150. C matrix is not computed. When the covariance matrix
  151. C is computed, the first dimensioning parameter
  152. C of the array W(*,*) must satisfy MDW .GE. MAX(M,N).
  153. C
  154. C KEY=10
  155. C Suppress scaling of the inverse of the
  156. C normal matrix by the scale factor RNORM**2/
  157. C MAX(1, no. of degrees of freedom). This option
  158. C only applies when the option for computing the
  159. C covariance matrix (KEY=1) is used. With KEY=1 and
  160. C KEY=10 used as options the unscaled inverse of the
  161. C normal matrix is returned in W(*,*).
  162. C The data set for this option is a single value.
  163. C When it is nonzero no scaling is done. When it is
  164. C zero scaling is done. The nominal case is to do
  165. C scaling so if option (KEY=1) is used alone, the
  166. C matrix will be scaled on output.
  167. C
  168. C KEY=2
  169. C Scale the nonzero columns of the
  170. C entire data matrix.
  171. C (E)
  172. C (A)
  173. C (G)
  174. C
  175. C to have length one. The data set for this
  176. C option is a single value. It must be
  177. C nonzero if unit length column scaling
  178. C is desired.
  179. C
  180. C KEY=3
  181. C Scale columns of the entire data matrix
  182. C (E)
  183. C (A)
  184. C (G)
  185. C
  186. C with a user-provided diagonal matrix.
  187. C The data set for this option consists
  188. C of the N diagonal scaling factors, one for
  189. C each matrix column.
  190. C
  191. C KEY=4
  192. C Change the rank determination tolerance for
  193. C the equality constraint equations from
  194. C the nominal value of SQRT(SRELPR). This quantity can
  195. C be no smaller than SRELPR, the arithmetic-
  196. C storage precision. The quantity SRELPR is the
  197. C largest positive number such that T=1.+SRELPR
  198. C satisfies T .EQ. 1. The quantity used
  199. C here is internally restricted to be at
  200. C least SRELPR. The data set for this option
  201. C is the new tolerance.
  202. C
  203. C KEY=5
  204. C Change the rank determination tolerance for
  205. C the reduced least squares equations from
  206. C the nominal value of SQRT(SRELPR). This quantity can
  207. C be no smaller than SRELPR, the arithmetic-
  208. C storage precision. The quantity used
  209. C here is internally restricted to be at
  210. C least SRELPR. The data set for this option
  211. C is the new tolerance.
  212. C
  213. C For example, suppose we want to change
  214. C the tolerance for the reduced least squares
  215. C problem, compute the covariance matrix of
  216. C the solution parameters, and provide
  217. C column scaling for the data matrix. For
  218. C these options the dimension of PRGOPT(*)
  219. C must be at least N+9. The Fortran statements
  220. C defining these options would be as follows:
  221. C
  222. C PRGOPT(1)=4 (link to entry 4 in PRGOPT(*))
  223. C PRGOPT(2)=1 (covariance matrix key)
  224. C PRGOPT(3)=1 (covariance matrix wanted)
  225. C
  226. C PRGOPT(4)=7 (link to entry 7 in PRGOPT(*))
  227. C PRGOPT(5)=5 (least squares equas. tolerance key)
  228. C PRGOPT(6)=... (new value of the tolerance)
  229. C
  230. C PRGOPT(7)=N+9 (link to entry N+9 in PRGOPT(*))
  231. C PRGOPT(8)=3 (user-provided column scaling key)
  232. C
  233. C CALL SCOPY (N, D, 1, PRGOPT(9), 1) (Copy the N
  234. C scaling factors from the user array D(*)
  235. C to PRGOPT(9)-PRGOPT(N+8))
  236. C
  237. C PRGOPT(N+9)=1 (no more options to change)
  238. C
  239. C The contents of PRGOPT(*) are not modified
  240. C by the subprogram.
  241. C The options for WNNLS( ) can also be included
  242. C in this array. The values of KEY recognized
  243. C by WNNLS( ) are 6, 7 and 8. Their functions
  244. C are documented in the usage instructions for
  245. C subroutine WNNLS( ). Normally these options
  246. C do not need to be modified when using LSEI( ).
  247. C
  248. C IP(1), The amounts of working storage actually
  249. C IP(2) allocated for the working arrays WS(*) and
  250. C IP(*), respectively. These quantities are
  251. C compared with the actual amounts of storage
  252. C needed by LSEI( ). Insufficient storage
  253. C allocated for either WS(*) or IP(*) is an
  254. C error. This feature was included in LSEI( )
  255. C because miscalculating the storage formulas
  256. C for WS(*) and IP(*) might very well lead to
  257. C subtle and hard-to-find execution errors.
  258. C
  259. C The length of WS(*) must be at least
  260. C
  261. C LW = 2*(ME+N)+K+(MG+2)*(N+7)
  262. C
  263. C where K = max(MA+MG,N)
  264. C This test will not be made if IP(1).LE.0.
  265. C
  266. C The length of IP(*) must be at least
  267. C
  268. C LIP = MG+2*N+2
  269. C This test will not be made if IP(2).LE.0.
  270. C
  271. C Output..
  272. C
  273. C X(*),RNORME, The array X(*) contains the solution parameters
  274. C RNORML if the integer output flag MODE = 0 or 1.
  275. C The definition of MODE is given directly below.
  276. C When MODE = 0 or 1, RNORME and RNORML
  277. C respectively contain the residual vector
  278. C Euclidean lengths of F - EX and B - AX. When
  279. C MODE=1 the equality constraint equations EX=F
  280. C are contradictory, so RNORME .NE. 0. The residual
  281. C vector F-EX has minimal Euclidean length. For
  282. C MODE .GE. 2, none of these parameters is defined.
  283. C
  284. C MODE Integer flag that indicates the subprogram
  285. C status after completion. If MODE .GE. 2, no
  286. C solution has been computed.
  287. C
  288. C MODE =
  289. C
  290. C 0 Both equality and inequality constraints
  291. C are compatible and have been satisfied.
  292. C
  293. C 1 Equality constraints are contradictory.
  294. C A generalized inverse solution of EX=F was used
  295. C to minimize the residual vector length F-EX.
  296. C In this sense, the solution is still meaningful.
  297. C
  298. C 2 Inequality constraints are contradictory.
  299. C
  300. C 3 Both equality and inequality constraints
  301. C are contradictory.
  302. C
  303. C The following interpretation of
  304. C MODE=1,2 or 3 must be made. The
  305. C sets consisting of all solutions
  306. C of the equality constraints EX=F
  307. C and all vectors satisfying GX .GE. H
  308. C have no points in common. (In
  309. C particular this does not say that
  310. C each individual set has no points
  311. C at all, although this could be the
  312. C case.)
  313. C
  314. C 4 Usage error occurred. The value
  315. C of MDW is .LT. ME+MA+MG, MDW is
  316. C .LT. N and a covariance matrix is
  317. C requested, or the option vector
  318. C PRGOPT(*) is not properly defined,
  319. C or the lengths of the working arrays
  320. C WS(*) and IP(*), when specified in
  321. C IP(1) and IP(2) respectively, are not
  322. C long enough.
  323. C
  324. C W(*,*) The array W(*,*) contains the N by N symmetric
  325. C covariance matrix of the solution parameters,
  326. C provided this was requested on input with
  327. C the option vector PRGOPT(*) and the output
  328. C flag is returned with MODE = 0 or 1.
  329. C
  330. C IP(*) The integer working array has three entries
  331. C that provide rank and working array length
  332. C information after completion.
  333. C
  334. C IP(1) = rank of equality constraint
  335. C matrix. Define this quantity
  336. C as KRANKE.
  337. C
  338. C IP(2) = rank of reduced least squares
  339. C problem.
  340. C
  341. C IP(3) = the amount of storage in the
  342. C working array WS(*) that was
  343. C actually used by the subprogram.
  344. C The formula given above for the length
  345. C of WS(*) is a necessary overestimate.
  346. C If exactly the same problem matrices
  347. C are used in subsequent executions,
  348. C the declared dimension of WS(*) can
  349. C be reduced to this output value.
  350. C User Designated
  351. C Working Arrays..
  352. C
  353. C WS(*),IP(*) These are respectively type real
  354. C and type integer working arrays.
  355. C Their required minimal lengths are
  356. C given above.
  357. C
  358. C***REFERENCES K. H. Haskell and R. J. Hanson, An algorithm for
  359. C linear least squares problems with equality and
  360. C nonnegativity constraints, Report SAND77-0552, Sandia
  361. C Laboratories, June 1978.
  362. C K. H. Haskell and R. J. Hanson, Selected algorithms for
  363. C the linearly constrained least squares problem - a
  364. C users guide, Report SAND78-1290, Sandia Laboratories,
  365. C August 1979.
  366. C K. H. Haskell and R. J. Hanson, An algorithm for
  367. C linear least squares problems with equality and
  368. C nonnegativity constraints, Mathematical Programming
  369. C 21 (1981), pp. 98-118.
  370. C R. J. Hanson and K. H. Haskell, Two algorithms for the
  371. C linearly constrained least squares problem, ACM
  372. C Transactions on Mathematical Software, September 1982.
  373. C***ROUTINES CALLED H12, LSI, R1MACH, SASUM, SAXPY, SCOPY, SDOT, SNRM2,
  374. C SSCAL, SSWAP, XERMSG
  375. C***REVISION HISTORY (YYMMDD)
  376. C 790701 DATE WRITTEN
  377. C 890531 Changed all specific intrinsics to generic. (WRB)
  378. C 890618 Completely restructured and extensively revised (WRB & RWC)
  379. C 890831 REVISION DATE from Version 3.2
  380. C 891214 Prologue converted to Version 4.0 format. (BAB)
  381. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  382. C 900510 Convert XERRWV calls to XERMSG calls. (RWC)
  383. C 920501 Reformatted the REFERENCES section. (WRB)
  384. C***END PROLOGUE LSEI
  385. INTEGER IP(3), MA, MDW, ME, MG, MODE, N
  386. REAL PRGOPT(*), RNORME, RNORML, W(MDW,*), WS(*), X(*)
  387. C
  388. EXTERNAL H12, LSI, R1MACH, SASUM, SAXPY, SCOPY, SDOT, SNRM2,
  389. * SSCAL, SSWAP, XERMSG
  390. REAL R1MACH, SASUM, SDOT, SNRM2
  391. C
  392. REAL ENORM, FNORM, GAM, RB, RN, RNMAX, SIZE, SN,
  393. * SNMAX, SRELPR, T, TAU, UJ, UP, VJ, XNORM, XNRME
  394. INTEGER I, IMAX, J, JP1, K, KEY, KRANKE, LAST, LCHK, LINK, M,
  395. * MAPKE1, MDEQC, MEND, MEP1, N1, N2, NEXT, NLINK, NOPT, NP1,
  396. * NTIMES
  397. LOGICAL COV, FIRST
  398. CHARACTER*8 XERN1, XERN2, XERN3, XERN4
  399. SAVE FIRST, SRELPR
  400. C
  401. DATA FIRST /.TRUE./
  402. C***FIRST EXECUTABLE STATEMENT LSEI
  403. C
  404. C Set the nominal tolerance used in the code for the equality
  405. C constraint equations.
  406. C
  407. IF (FIRST) SRELPR = R1MACH(4)
  408. FIRST = .FALSE.
  409. TAU = SQRT(SRELPR)
  410. C
  411. C Check that enough storage was allocated in WS(*) and IP(*).
  412. C
  413. MODE = 4
  414. IF (MIN(N,ME,MA,MG) .LT. 0) THEN
  415. WRITE (XERN1, '(I8)') N
  416. WRITE (XERN2, '(I8)') ME
  417. WRITE (XERN3, '(I8)') MA
  418. WRITE (XERN4, '(I8)') MG
  419. CALL XERMSG ('SLATEC', 'LSEI', 'ALL OF THE VARIABLES N, ME,' //
  420. * ' MA, MG MUST BE .GE. 0$$ENTERED ROUTINE WITH' //
  421. * '$$N = ' // XERN1 //
  422. * '$$ME = ' // XERN2 //
  423. * '$$MA = ' // XERN3 //
  424. * '$$MG = ' // XERN4, 2, 1)
  425. RETURN
  426. ENDIF
  427. C
  428. IF (IP(1).GT.0) THEN
  429. LCHK = 2*(ME+N) + MAX(MA+MG,N) + (MG+2)*(N+7)
  430. IF (IP(1).LT.LCHK) THEN
  431. WRITE (XERN1, '(I8)') LCHK
  432. CALL XERMSG ('SLATEC', 'LSEI', 'INSUFFICIENT STORAGE ' //
  433. * 'ALLOCATED FOR WS(*), NEED LW = ' // XERN1, 2, 1)
  434. RETURN
  435. ENDIF
  436. ENDIF
  437. C
  438. IF (IP(2).GT.0) THEN
  439. LCHK = MG + 2*N + 2
  440. IF (IP(2).LT.LCHK) THEN
  441. WRITE (XERN1, '(I8)') LCHK
  442. CALL XERMSG ('SLATEC', 'LSEI', 'INSUFFICIENT STORAGE ' //
  443. * 'ALLOCATED FOR IP(*), NEED LIP = ' // XERN1, 2, 1)
  444. RETURN
  445. ENDIF
  446. ENDIF
  447. C
  448. C Compute number of possible right multiplying Householder
  449. C transformations.
  450. C
  451. M = ME + MA + MG
  452. IF (N.LE.0 .OR. M.LE.0) THEN
  453. MODE = 0
  454. RNORME = 0
  455. RNORML = 0
  456. RETURN
  457. ENDIF
  458. C
  459. IF (MDW.LT.M) THEN
  460. CALL XERMSG ('SLATEC', 'LSEI', 'MDW.LT.ME+MA+MG IS AN ERROR',
  461. + 2, 1)
  462. RETURN
  463. ENDIF
  464. C
  465. NP1 = N + 1
  466. KRANKE = MIN(ME,N)
  467. N1 = 2*KRANKE + 1
  468. N2 = N1 + N
  469. C
  470. C Set nominal values.
  471. C
  472. C The nominal column scaling used in the code is
  473. C the identity scaling.
  474. C
  475. CALL SCOPY (N, 1.E0, 0, WS(N1), 1)
  476. C
  477. C No covariance matrix is nominally computed.
  478. C
  479. COV = .FALSE.
  480. C
  481. C Process option vector.
  482. C Define bound for number of options to change.
  483. C
  484. NOPT = 1000
  485. NTIMES = 0
  486. C
  487. C Define bound for positive values of LINK.
  488. C
  489. NLINK = 100000
  490. LAST = 1
  491. LINK = PRGOPT(1)
  492. IF (LINK.EQ.0 .OR. LINK.GT.NLINK) THEN
  493. CALL XERMSG ('SLATEC', 'LSEI',
  494. + 'THE OPTION VECTOR IS UNDEFINED', 2, 1)
  495. RETURN
  496. ENDIF
  497. C
  498. 100 IF (LINK.GT.1) THEN
  499. NTIMES = NTIMES + 1
  500. IF (NTIMES.GT.NOPT) THEN
  501. CALL XERMSG ('SLATEC', 'LSEI',
  502. + 'THE LINKS IN THE OPTION VECTOR ARE CYCLING.', 2, 1)
  503. RETURN
  504. ENDIF
  505. C
  506. KEY = PRGOPT(LAST+1)
  507. IF (KEY.EQ.1) THEN
  508. COV = PRGOPT(LAST+2) .NE. 0.E0
  509. ELSEIF (KEY.EQ.2 .AND. PRGOPT(LAST+2).NE.0.E0) THEN
  510. DO 110 J = 1,N
  511. T = SNRM2(M,W(1,J),1)
  512. IF (T.NE.0.E0) T = 1.E0/T
  513. WS(J+N1-1) = T
  514. 110 CONTINUE
  515. ELSEIF (KEY.EQ.3) THEN
  516. CALL SCOPY (N, PRGOPT(LAST+2), 1, WS(N1), 1)
  517. ELSEIF (KEY.EQ.4) THEN
  518. TAU = MAX(SRELPR,PRGOPT(LAST+2))
  519. ENDIF
  520. C
  521. NEXT = PRGOPT(LINK)
  522. IF (NEXT.LE.0 .OR. NEXT.GT.NLINK) THEN
  523. CALL XERMSG ('SLATEC', 'LSEI',
  524. + 'THE OPTION VECTOR IS UNDEFINED', 2, 1)
  525. RETURN
  526. ENDIF
  527. C
  528. LAST = LINK
  529. LINK = NEXT
  530. GO TO 100
  531. ENDIF
  532. C
  533. DO 120 J = 1,N
  534. CALL SSCAL (M, WS(N1+J-1), W(1,J), 1)
  535. 120 CONTINUE
  536. C
  537. IF (COV .AND. MDW.LT.N) THEN
  538. CALL XERMSG ('SLATEC', 'LSEI',
  539. + 'MDW .LT. N WHEN COV MATRIX NEEDED, IS AN ERROR', 2, 1)
  540. RETURN
  541. ENDIF
  542. C
  543. C Problem definition and option vector OK.
  544. C
  545. MODE = 0
  546. C
  547. C Compute norm of equality constraint matrix and right side.
  548. C
  549. ENORM = 0.E0
  550. DO 130 J = 1,N
  551. ENORM = MAX(ENORM,SASUM(ME,W(1,J),1))
  552. 130 CONTINUE
  553. C
  554. FNORM = SASUM(ME,W(1,NP1),1)
  555. SNMAX = 0.E0
  556. RNMAX = 0.E0
  557. DO 150 I = 1,KRANKE
  558. C
  559. C Compute maximum ratio of vector lengths. Partition is at
  560. C column I.
  561. C
  562. DO 140 K = I,ME
  563. SN = SDOT(N-I+1,W(K,I),MDW,W(K,I),MDW)
  564. RN = SDOT(I-1,W(K,1),MDW,W(K,1),MDW)
  565. IF (RN.EQ.0.E0 .AND. SN.GT.SNMAX) THEN
  566. SNMAX = SN
  567. IMAX = K
  568. ELSEIF (K.EQ.I .OR. SN*RNMAX.GT.RN*SNMAX) THEN
  569. SNMAX = SN
  570. RNMAX = RN
  571. IMAX = K
  572. ENDIF
  573. 140 CONTINUE
  574. C
  575. C Interchange rows if necessary.
  576. C
  577. IF (I.NE.IMAX) CALL SSWAP (NP1, W(I,1), MDW, W(IMAX,1), MDW)
  578. IF (SNMAX.GT.RNMAX*TAU**2) THEN
  579. C
  580. C Eliminate elements I+1,...,N in row I.
  581. C
  582. CALL H12 (1, I, I+1, N, W(I,1), MDW, WS(I), W(I+1,1), MDW,
  583. + 1, M-I)
  584. ELSE
  585. KRANKE = I - 1
  586. GO TO 160
  587. ENDIF
  588. 150 CONTINUE
  589. C
  590. C Save diagonal terms of lower trapezoidal matrix.
  591. C
  592. 160 CALL SCOPY (KRANKE, W, MDW+1, WS(KRANKE+1), 1)
  593. C
  594. C Use Householder transformation from left to achieve
  595. C KRANKE by KRANKE upper triangular form.
  596. C
  597. IF (KRANKE.LT.ME) THEN
  598. DO 170 K = KRANKE,1,-1
  599. C
  600. C Apply transformation to matrix cols. 1,...,K-1.
  601. C
  602. CALL H12 (1, K, KRANKE+1, ME, W(1,K), 1, UP, W, 1, MDW, K-1)
  603. C
  604. C Apply to rt side vector.
  605. C
  606. CALL H12 (2, K, KRANKE+1, ME, W(1,K), 1, UP, W(1,NP1), 1, 1,
  607. + 1)
  608. 170 CONTINUE
  609. ENDIF
  610. C
  611. C Solve for variables 1,...,KRANKE in new coordinates.
  612. C
  613. CALL SCOPY (KRANKE, W(1, NP1), 1, X, 1)
  614. DO 180 I = 1,KRANKE
  615. X(I) = (X(I)-SDOT(I-1,W(I,1),MDW,X,1))/W(I,I)
  616. 180 CONTINUE
  617. C
  618. C Compute residuals for reduced problem.
  619. C
  620. MEP1 = ME + 1
  621. RNORML = 0.E0
  622. DO 190 I = MEP1,M
  623. W(I,NP1) = W(I,NP1) - SDOT(KRANKE,W(I,1),MDW,X,1)
  624. SN = SDOT(KRANKE,W(I,1),MDW,W(I,1),MDW)
  625. RN = SDOT(N-KRANKE,W(I,KRANKE+1),MDW,W(I,KRANKE+1),MDW)
  626. IF (RN.LE.SN*TAU**2 .AND. KRANKE.LT.N)
  627. * CALL SCOPY (N-KRANKE, 0.E0, 0, W(I,KRANKE+1), MDW)
  628. 190 CONTINUE
  629. C
  630. C Compute equality constraint equations residual length.
  631. C
  632. RNORME = SNRM2(ME-KRANKE,W(KRANKE+1,NP1),1)
  633. C
  634. C Move reduced problem data upward if KRANKE.LT.ME.
  635. C
  636. IF (KRANKE.LT.ME) THEN
  637. DO 200 J = 1,NP1
  638. CALL SCOPY (M-ME, W(ME+1,J), 1, W(KRANKE+1,J), 1)
  639. 200 CONTINUE
  640. ENDIF
  641. C
  642. C Compute solution of reduced problem.
  643. C
  644. CALL LSI(W(KRANKE+1, KRANKE+1), MDW, MA, MG, N-KRANKE, PRGOPT,
  645. + X(KRANKE+1), RNORML, MODE, WS(N2), IP(2))
  646. C
  647. C Test for consistency of equality constraints.
  648. C
  649. IF (ME.GT.0) THEN
  650. MDEQC = 0
  651. XNRME = SASUM(KRANKE,W(1,NP1),1)
  652. IF (RNORME.GT.TAU*(ENORM*XNRME+FNORM)) MDEQC = 1
  653. MODE = MODE + MDEQC
  654. C
  655. C Check if solution to equality constraints satisfies inequality
  656. C constraints when there are no degrees of freedom left.
  657. C
  658. IF (KRANKE.EQ.N .AND. MG.GT.0) THEN
  659. XNORM = SASUM(N,X,1)
  660. MAPKE1 = MA + KRANKE + 1
  661. MEND = MA + KRANKE + MG
  662. DO 210 I = MAPKE1,MEND
  663. SIZE = SASUM(N,W(I,1),MDW)*XNORM + ABS(W(I,NP1))
  664. IF (W(I,NP1).GT.TAU*SIZE) THEN
  665. MODE = MODE + 2
  666. GO TO 290
  667. ENDIF
  668. 210 CONTINUE
  669. ENDIF
  670. ENDIF
  671. C
  672. C Replace diagonal terms of lower trapezoidal matrix.
  673. C
  674. IF (KRANKE.GT.0) THEN
  675. CALL SCOPY (KRANKE, WS(KRANKE+1), 1, W, MDW+1)
  676. C
  677. C Reapply transformation to put solution in original coordinates.
  678. C
  679. DO 220 I = KRANKE,1,-1
  680. CALL H12 (2, I, I+1, N, W(I,1), MDW, WS(I), X, 1, 1, 1)
  681. 220 CONTINUE
  682. C
  683. C Compute covariance matrix of equality constrained problem.
  684. C
  685. IF (COV) THEN
  686. DO 270 J = MIN(KRANKE,N-1),1,-1
  687. RB = WS(J)*W(J,J)
  688. IF (RB.NE.0.E0) RB = 1.E0/RB
  689. JP1 = J + 1
  690. DO 230 I = JP1,N
  691. W(I,J) = RB*SDOT(N-J,W(I,JP1),MDW,W(J,JP1),MDW)
  692. 230 CONTINUE
  693. C
  694. GAM = 0.5E0*RB*SDOT(N-J,W(JP1,J),1,W(J,JP1),MDW)
  695. CALL SAXPY (N-J, GAM, W(J,JP1), MDW, W(JP1,J), 1)
  696. DO 250 I = JP1,N
  697. DO 240 K = I,N
  698. W(I,K) = W(I,K) + W(J,I)*W(K,J) + W(I,J)*W(J,K)
  699. W(K,I) = W(I,K)
  700. 240 CONTINUE
  701. 250 CONTINUE
  702. UJ = WS(J)
  703. VJ = GAM*UJ
  704. W(J,J) = UJ*VJ + UJ*VJ
  705. DO 260 I = JP1,N
  706. W(J,I) = UJ*W(I,J) + VJ*W(J,I)
  707. 260 CONTINUE
  708. CALL SCOPY (N-J, W(J, JP1), MDW, W(JP1,J), 1)
  709. 270 CONTINUE
  710. ENDIF
  711. ENDIF
  712. C
  713. C Apply the scaling to the covariance matrix.
  714. C
  715. IF (COV) THEN
  716. DO 280 I = 1,N
  717. CALL SSCAL (N, WS(I+N1-1), W(I,1), MDW)
  718. CALL SSCAL (N, WS(I+N1-1), W(1,I), 1)
  719. 280 CONTINUE
  720. ENDIF
  721. C
  722. C Rescale solution vector.
  723. C
  724. 290 IF (MODE.LE.1) THEN
  725. DO 300 J = 1,N
  726. X(J) = X(J)*WS(N1+J-1)
  727. 300 CONTINUE
  728. ENDIF
  729. C
  730. IP(1) = KRANKE
  731. IP(3) = IP(3) + 2*KRANKE + N
  732. RETURN
  733. END