Xmipp  v3.23.11-Nereus
solvers.cpp
Go to the documentation of this file.
1 /*************************************************************************
2 Copyright (c) Sergey Bochkanov (ALGLIB project).
3 
4 >>> SOURCE LICENSE >>>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation (www.fsf.org); either version 2 of the
8 License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 A copy of the GNU General Public License is available at
16 http://www.fsf.org/licensing/licenses
17 >>> END OF LICENSE >>>
18 *************************************************************************/
19 #include "stdafx.h"
20 #include "solvers.h"
21 
22 // disable some irrelevant warnings
23 #if (AE_COMPILER==AE_MSVC)
24 #pragma warning(disable:4100)
25 #pragma warning(disable:4127)
26 #pragma warning(disable:4702)
27 #pragma warning(disable:4996)
28 #endif
29 using namespace std;
30 
32 //
33 // THIS SECTION CONTAINS IMPLEMENTATION OF C++ INTERFACE
34 //
36 namespace alglib
37 {
38 
39 
40 /*************************************************************************
41 
42 *************************************************************************/
43 _densesolverreport_owner::_densesolverreport_owner()
44 {
46  if( p_struct==NULL )
47  throw ap_error("ALGLIB: malloc error");
48  if( !alglib_impl::_densesolverreport_init(p_struct, NULL, ae_false) )
49  throw ap_error("ALGLIB: malloc error");
50 }
51 
52 _densesolverreport_owner::_densesolverreport_owner(const _densesolverreport_owner &rhs)
53 {
55  if( p_struct==NULL )
56  throw ap_error("ALGLIB: malloc error");
57  if( !alglib_impl::_densesolverreport_init_copy(p_struct, const_cast<alglib_impl::densesolverreport*>(rhs.p_struct), NULL, ae_false) )
58  throw ap_error("ALGLIB: malloc error");
59 }
60 
61 _densesolverreport_owner& _densesolverreport_owner::operator=(const _densesolverreport_owner &rhs)
62 {
63  if( this==&rhs )
64  return *this;
66  if( !alglib_impl::_densesolverreport_init_copy(p_struct, const_cast<alglib_impl::densesolverreport*>(rhs.p_struct), NULL, ae_false) )
67  throw ap_error("ALGLIB: malloc error");
68  return *this;
69 }
70 
71 _densesolverreport_owner::~_densesolverreport_owner()
72 {
74  ae_free(p_struct);
75 }
76 
77 alglib_impl::densesolverreport* _densesolverreport_owner::c_ptr()
78 {
79  return p_struct;
80 }
81 
82 alglib_impl::densesolverreport* _densesolverreport_owner::c_ptr() const
83 {
84  return const_cast<alglib_impl::densesolverreport*>(p_struct);
85 }
86 densesolverreport::densesolverreport() : _densesolverreport_owner() ,r1(p_struct->r1),rinf(p_struct->rinf)
87 {
88 }
89 
91 {
92 }
93 
95 {
96  if( this==&rhs )
97  return *this;
99  return *this;
100 }
101 
103 {
104 }
105 
106 
107 /*************************************************************************
108 
109 *************************************************************************/
111 {
113  if( p_struct==NULL )
114  throw ap_error("ALGLIB: malloc error");
116  throw ap_error("ALGLIB: malloc error");
117 }
118 
120 {
122  if( p_struct==NULL )
123  throw ap_error("ALGLIB: malloc error");
124  if( !alglib_impl::_densesolverlsreport_init_copy(p_struct, const_cast<alglib_impl::densesolverlsreport*>(rhs.p_struct), NULL, ae_false) )
125  throw ap_error("ALGLIB: malloc error");
126 }
127 
129 {
130  if( this==&rhs )
131  return *this;
133  if( !alglib_impl::_densesolverlsreport_init_copy(p_struct, const_cast<alglib_impl::densesolverlsreport*>(rhs.p_struct), NULL, ae_false) )
134  throw ap_error("ALGLIB: malloc error");
135  return *this;
136 }
137 
139 {
141  ae_free(p_struct);
142 }
143 
145 {
146  return p_struct;
147 }
148 
150 {
151  return const_cast<alglib_impl::densesolverlsreport*>(p_struct);
152 }
154 {
155 }
156 
158 {
159 }
160 
162 {
163  if( this==&rhs )
164  return *this;
166  return *this;
167 }
168 
170 {
171 }
172 
173 /*************************************************************************
174 Dense solver.
175 
176 This subroutine solves a system A*x=b, where A is NxN non-denegerate
177 real matrix, x and b are vectors.
178 
179 Algorithm features:
180 * automatic detection of degenerate cases
181 * condition number estimation
182 * iterative refinement
183 * O(N^3) complexity
184 
185 INPUT PARAMETERS
186  A - array[0..N-1,0..N-1], system matrix
187  N - size of A
188  B - array[0..N-1], right part
189 
190 OUTPUT PARAMETERS
191  Info - return code:
192  * -3 A is singular, or VERY close to singular.
193  X is filled by zeros in such cases.
194  * -1 N<=0 was passed
195  * 1 task is solved (but matrix A may be ill-conditioned,
196  check R1/RInf parameters for condition numbers).
197  Rep - solver report, see below for more info
198  X - array[0..N-1], it contains:
199  * solution of A*x=b if A is non-singular (well-conditioned
200  or ill-conditioned, but not very close to singular)
201  * zeros, if A is singular or VERY close to singular
202  (in this case Info=-3).
203 
204 SOLVER REPORT
205 
206 Subroutine sets following fields of the Rep structure:
207 * R1 reciprocal of condition number: 1/cond(A), 1-norm.
208 * RInf reciprocal of condition number: 1/cond(A), inf-norm.
209 
210  -- ALGLIB --
211  Copyright 27.01.2010 by Bochkanov Sergey
212 *************************************************************************/
214 {
215  alglib_impl::ae_state _alglib_env_state;
216  alglib_impl::ae_state_init(&_alglib_env_state);
217  try
218  {
219  alglib_impl::rmatrixsolve(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
220  alglib_impl::ae_state_clear(&_alglib_env_state);
221  return;
222  }
224  {
225  throw ap_error(_alglib_env_state.error_msg);
226  }
227 }
228 
229 /*************************************************************************
230 Dense solver.
231 
232 Similar to RMatrixSolve() but solves task with multiple right parts (where
233 b and x are NxM matrices).
234 
235 Algorithm features:
236 * automatic detection of degenerate cases
237 * condition number estimation
238 * optional iterative refinement
239 * O(N^3+M*N^2) complexity
240 
241 INPUT PARAMETERS
242  A - array[0..N-1,0..N-1], system matrix
243  N - size of A
244  B - array[0..N-1,0..M-1], right part
245  M - right part size
246  RFS - iterative refinement switch:
247  * True - refinement is used.
248  Less performance, more precision.
249  * False - refinement is not used.
250  More performance, less precision.
251 
252 OUTPUT PARAMETERS
253  Info - same as in RMatrixSolve
254  Rep - same as in RMatrixSolve
255  X - same as in RMatrixSolve
256 
257  -- ALGLIB --
258  Copyright 27.01.2010 by Bochkanov Sergey
259 *************************************************************************/
260 void rmatrixsolvem(const real_2d_array &a, const ae_int_t n, const real_2d_array &b, const ae_int_t m, const bool rfs, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
261 {
262  alglib_impl::ae_state _alglib_env_state;
263  alglib_impl::ae_state_init(&_alglib_env_state);
264  try
265  {
266  alglib_impl::rmatrixsolvem(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, rfs, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
267  alglib_impl::ae_state_clear(&_alglib_env_state);
268  return;
269  }
271  {
272  throw ap_error(_alglib_env_state.error_msg);
273  }
274 }
275 
276 /*************************************************************************
277 Dense solver.
278 
279 This subroutine solves a system A*X=B, where A is NxN non-denegerate
280 real matrix given by its LU decomposition, X and B are NxM real matrices.
281 
282 Algorithm features:
283 * automatic detection of degenerate cases
284 * O(N^2) complexity
285 * condition number estimation
286 
287 No iterative refinement is provided because exact form of original matrix
288 is not known to subroutine. Use RMatrixSolve or RMatrixMixedSolve if you
289 need iterative refinement.
290 
291 INPUT PARAMETERS
292  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
293  P - array[0..N-1], pivots array, RMatrixLU result
294  N - size of A
295  B - array[0..N-1], right part
296 
297 OUTPUT PARAMETERS
298  Info - same as in RMatrixSolve
299  Rep - same as in RMatrixSolve
300  X - same as in RMatrixSolve
301 
302  -- ALGLIB --
303  Copyright 27.01.2010 by Bochkanov Sergey
304 *************************************************************************/
306 {
307  alglib_impl::ae_state _alglib_env_state;
308  alglib_impl::ae_state_init(&_alglib_env_state);
309  try
310  {
311  alglib_impl::rmatrixlusolve(const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
312  alglib_impl::ae_state_clear(&_alglib_env_state);
313  return;
314  }
316  {
317  throw ap_error(_alglib_env_state.error_msg);
318  }
319 }
320 
321 /*************************************************************************
322 Dense solver.
323 
324 Similar to RMatrixLUSolve() but solves task with multiple right parts
325 (where b and x are NxM matrices).
326 
327 Algorithm features:
328 * automatic detection of degenerate cases
329 * O(M*N^2) complexity
330 * condition number estimation
331 
332 No iterative refinement is provided because exact form of original matrix
333 is not known to subroutine. Use RMatrixSolve or RMatrixMixedSolve if you
334 need iterative refinement.
335 
336 INPUT PARAMETERS
337  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
338  P - array[0..N-1], pivots array, RMatrixLU result
339  N - size of A
340  B - array[0..N-1,0..M-1], right part
341  M - right part size
342 
343 OUTPUT PARAMETERS
344  Info - same as in RMatrixSolve
345  Rep - same as in RMatrixSolve
346  X - same as in RMatrixSolve
347 
348  -- ALGLIB --
349  Copyright 27.01.2010 by Bochkanov Sergey
350 *************************************************************************/
352 {
353  alglib_impl::ae_state _alglib_env_state;
354  alglib_impl::ae_state_init(&_alglib_env_state);
355  try
356  {
357  alglib_impl::rmatrixlusolvem(const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
358  alglib_impl::ae_state_clear(&_alglib_env_state);
359  return;
360  }
362  {
363  throw ap_error(_alglib_env_state.error_msg);
364  }
365 }
366 
367 /*************************************************************************
368 Dense solver.
369 
370 This subroutine solves a system A*x=b, where BOTH ORIGINAL A AND ITS
371 LU DECOMPOSITION ARE KNOWN. You can use it if for some reasons you have
372 both A and its LU decomposition.
373 
374 Algorithm features:
375 * automatic detection of degenerate cases
376 * condition number estimation
377 * iterative refinement
378 * O(N^2) complexity
379 
380 INPUT PARAMETERS
381  A - array[0..N-1,0..N-1], system matrix
382  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
383  P - array[0..N-1], pivots array, RMatrixLU result
384  N - size of A
385  B - array[0..N-1], right part
386 
387 OUTPUT PARAMETERS
388  Info - same as in RMatrixSolveM
389  Rep - same as in RMatrixSolveM
390  X - same as in RMatrixSolveM
391 
392  -- ALGLIB --
393  Copyright 27.01.2010 by Bochkanov Sergey
394 *************************************************************************/
396 {
397  alglib_impl::ae_state _alglib_env_state;
398  alglib_impl::ae_state_init(&_alglib_env_state);
399  try
400  {
401  alglib_impl::rmatrixmixedsolve(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
402  alglib_impl::ae_state_clear(&_alglib_env_state);
403  return;
404  }
406  {
407  throw ap_error(_alglib_env_state.error_msg);
408  }
409 }
410 
411 /*************************************************************************
412 Dense solver.
413 
414 Similar to RMatrixMixedSolve() but solves task with multiple right parts
415 (where b and x are NxM matrices).
416 
417 Algorithm features:
418 * automatic detection of degenerate cases
419 * condition number estimation
420 * iterative refinement
421 * O(M*N^2) complexity
422 
423 INPUT PARAMETERS
424  A - array[0..N-1,0..N-1], system matrix
425  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
426  P - array[0..N-1], pivots array, RMatrixLU result
427  N - size of A
428  B - array[0..N-1,0..M-1], right part
429  M - right part size
430 
431 OUTPUT PARAMETERS
432  Info - same as in RMatrixSolveM
433  Rep - same as in RMatrixSolveM
434  X - same as in RMatrixSolveM
435 
436  -- ALGLIB --
437  Copyright 27.01.2010 by Bochkanov Sergey
438 *************************************************************************/
440 {
441  alglib_impl::ae_state _alglib_env_state;
442  alglib_impl::ae_state_init(&_alglib_env_state);
443  try
444  {
445  alglib_impl::rmatrixmixedsolvem(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
446  alglib_impl::ae_state_clear(&_alglib_env_state);
447  return;
448  }
450  {
451  throw ap_error(_alglib_env_state.error_msg);
452  }
453 }
454 
455 /*************************************************************************
456 Dense solver. Same as RMatrixSolveM(), but for complex matrices.
457 
458 Algorithm features:
459 * automatic detection of degenerate cases
460 * condition number estimation
461 * iterative refinement
462 * O(N^3+M*N^2) complexity
463 
464 INPUT PARAMETERS
465  A - array[0..N-1,0..N-1], system matrix
466  N - size of A
467  B - array[0..N-1,0..M-1], right part
468  M - right part size
469  RFS - iterative refinement switch:
470  * True - refinement is used.
471  Less performance, more precision.
472  * False - refinement is not used.
473  More performance, less precision.
474 
475 OUTPUT PARAMETERS
476  Info - same as in RMatrixSolve
477  Rep - same as in RMatrixSolve
478  X - same as in RMatrixSolve
479 
480  -- ALGLIB --
481  Copyright 27.01.2010 by Bochkanov Sergey
482 *************************************************************************/
483 void cmatrixsolvem(const complex_2d_array &a, const ae_int_t n, const complex_2d_array &b, const ae_int_t m, const bool rfs, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
484 {
485  alglib_impl::ae_state _alglib_env_state;
486  alglib_impl::ae_state_init(&_alglib_env_state);
487  try
488  {
489  alglib_impl::cmatrixsolvem(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, rfs, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
490  alglib_impl::ae_state_clear(&_alglib_env_state);
491  return;
492  }
494  {
495  throw ap_error(_alglib_env_state.error_msg);
496  }
497 }
498 
499 /*************************************************************************
500 Dense solver. Same as RMatrixSolve(), but for complex matrices.
501 
502 Algorithm features:
503 * automatic detection of degenerate cases
504 * condition number estimation
505 * iterative refinement
506 * O(N^3) complexity
507 
508 INPUT PARAMETERS
509  A - array[0..N-1,0..N-1], system matrix
510  N - size of A
511  B - array[0..N-1], right part
512 
513 OUTPUT PARAMETERS
514  Info - same as in RMatrixSolve
515  Rep - same as in RMatrixSolve
516  X - same as in RMatrixSolve
517 
518  -- ALGLIB --
519  Copyright 27.01.2010 by Bochkanov Sergey
520 *************************************************************************/
522 {
523  alglib_impl::ae_state _alglib_env_state;
524  alglib_impl::ae_state_init(&_alglib_env_state);
525  try
526  {
527  alglib_impl::cmatrixsolve(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
528  alglib_impl::ae_state_clear(&_alglib_env_state);
529  return;
530  }
532  {
533  throw ap_error(_alglib_env_state.error_msg);
534  }
535 }
536 
537 /*************************************************************************
538 Dense solver. Same as RMatrixLUSolveM(), but for complex matrices.
539 
540 Algorithm features:
541 * automatic detection of degenerate cases
542 * O(M*N^2) complexity
543 * condition number estimation
544 
545 No iterative refinement is provided because exact form of original matrix
546 is not known to subroutine. Use CMatrixSolve or CMatrixMixedSolve if you
547 need iterative refinement.
548 
549 INPUT PARAMETERS
550  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
551  P - array[0..N-1], pivots array, RMatrixLU result
552  N - size of A
553  B - array[0..N-1,0..M-1], right part
554  M - right part size
555 
556 OUTPUT PARAMETERS
557  Info - same as in RMatrixSolve
558  Rep - same as in RMatrixSolve
559  X - same as in RMatrixSolve
560 
561  -- ALGLIB --
562  Copyright 27.01.2010 by Bochkanov Sergey
563 *************************************************************************/
565 {
566  alglib_impl::ae_state _alglib_env_state;
567  alglib_impl::ae_state_init(&_alglib_env_state);
568  try
569  {
570  alglib_impl::cmatrixlusolvem(const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
571  alglib_impl::ae_state_clear(&_alglib_env_state);
572  return;
573  }
575  {
576  throw ap_error(_alglib_env_state.error_msg);
577  }
578 }
579 
580 /*************************************************************************
581 Dense solver. Same as RMatrixLUSolve(), but for complex matrices.
582 
583 Algorithm features:
584 * automatic detection of degenerate cases
585 * O(N^2) complexity
586 * condition number estimation
587 
588 No iterative refinement is provided because exact form of original matrix
589 is not known to subroutine. Use CMatrixSolve or CMatrixMixedSolve if you
590 need iterative refinement.
591 
592 INPUT PARAMETERS
593  LUA - array[0..N-1,0..N-1], LU decomposition, CMatrixLU result
594  P - array[0..N-1], pivots array, CMatrixLU result
595  N - size of A
596  B - array[0..N-1], right part
597 
598 OUTPUT PARAMETERS
599  Info - same as in RMatrixSolve
600  Rep - same as in RMatrixSolve
601  X - same as in RMatrixSolve
602 
603  -- ALGLIB --
604  Copyright 27.01.2010 by Bochkanov Sergey
605 *************************************************************************/
607 {
608  alglib_impl::ae_state _alglib_env_state;
609  alglib_impl::ae_state_init(&_alglib_env_state);
610  try
611  {
612  alglib_impl::cmatrixlusolve(const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
613  alglib_impl::ae_state_clear(&_alglib_env_state);
614  return;
615  }
617  {
618  throw ap_error(_alglib_env_state.error_msg);
619  }
620 }
621 
622 /*************************************************************************
623 Dense solver. Same as RMatrixMixedSolveM(), but for complex matrices.
624 
625 Algorithm features:
626 * automatic detection of degenerate cases
627 * condition number estimation
628 * iterative refinement
629 * O(M*N^2) complexity
630 
631 INPUT PARAMETERS
632  A - array[0..N-1,0..N-1], system matrix
633  LUA - array[0..N-1,0..N-1], LU decomposition, CMatrixLU result
634  P - array[0..N-1], pivots array, CMatrixLU result
635  N - size of A
636  B - array[0..N-1,0..M-1], right part
637  M - right part size
638 
639 OUTPUT PARAMETERS
640  Info - same as in RMatrixSolveM
641  Rep - same as in RMatrixSolveM
642  X - same as in RMatrixSolveM
643 
644  -- ALGLIB --
645  Copyright 27.01.2010 by Bochkanov Sergey
646 *************************************************************************/
648 {
649  alglib_impl::ae_state _alglib_env_state;
650  alglib_impl::ae_state_init(&_alglib_env_state);
651  try
652  {
653  alglib_impl::cmatrixmixedsolvem(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
654  alglib_impl::ae_state_clear(&_alglib_env_state);
655  return;
656  }
658  {
659  throw ap_error(_alglib_env_state.error_msg);
660  }
661 }
662 
663 /*************************************************************************
664 Dense solver. Same as RMatrixMixedSolve(), but for complex matrices.
665 
666 Algorithm features:
667 * automatic detection of degenerate cases
668 * condition number estimation
669 * iterative refinement
670 * O(N^2) complexity
671 
672 INPUT PARAMETERS
673  A - array[0..N-1,0..N-1], system matrix
674  LUA - array[0..N-1,0..N-1], LU decomposition, CMatrixLU result
675  P - array[0..N-1], pivots array, CMatrixLU result
676  N - size of A
677  B - array[0..N-1], right part
678 
679 OUTPUT PARAMETERS
680  Info - same as in RMatrixSolveM
681  Rep - same as in RMatrixSolveM
682  X - same as in RMatrixSolveM
683 
684  -- ALGLIB --
685  Copyright 27.01.2010 by Bochkanov Sergey
686 *************************************************************************/
688 {
689  alglib_impl::ae_state _alglib_env_state;
690  alglib_impl::ae_state_init(&_alglib_env_state);
691  try
692  {
693  alglib_impl::cmatrixmixedsolve(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), const_cast<alglib_impl::ae_matrix*>(lua.c_ptr()), const_cast<alglib_impl::ae_vector*>(p.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
694  alglib_impl::ae_state_clear(&_alglib_env_state);
695  return;
696  }
698  {
699  throw ap_error(_alglib_env_state.error_msg);
700  }
701 }
702 
703 /*************************************************************************
704 Dense solver. Same as RMatrixSolveM(), but for symmetric positive definite
705 matrices.
706 
707 Algorithm features:
708 * automatic detection of degenerate cases
709 * condition number estimation
710 * O(N^3+M*N^2) complexity
711 * matrix is represented by its upper or lower triangle
712 
713 No iterative refinement is provided because such partial representation of
714 matrix does not allow efficient calculation of extra-precise matrix-vector
715 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
716 need iterative refinement.
717 
718 INPUT PARAMETERS
719  A - array[0..N-1,0..N-1], system matrix
720  N - size of A
721  IsUpper - what half of A is provided
722  B - array[0..N-1,0..M-1], right part
723  M - right part size
724 
725 OUTPUT PARAMETERS
726  Info - same as in RMatrixSolve.
727  Returns -3 for non-SPD matrices.
728  Rep - same as in RMatrixSolve
729  X - same as in RMatrixSolve
730 
731  -- ALGLIB --
732  Copyright 27.01.2010 by Bochkanov Sergey
733 *************************************************************************/
734 void spdmatrixsolvem(const real_2d_array &a, const ae_int_t n, const bool isupper, const real_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
735 {
736  alglib_impl::ae_state _alglib_env_state;
737  alglib_impl::ae_state_init(&_alglib_env_state);
738  try
739  {
740  alglib_impl::spdmatrixsolvem(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, isupper, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
741  alglib_impl::ae_state_clear(&_alglib_env_state);
742  return;
743  }
745  {
746  throw ap_error(_alglib_env_state.error_msg);
747  }
748 }
749 
750 /*************************************************************************
751 Dense solver. Same as RMatrixSolve(), but for SPD matrices.
752 
753 Algorithm features:
754 * automatic detection of degenerate cases
755 * condition number estimation
756 * O(N^3) complexity
757 * matrix is represented by its upper or lower triangle
758 
759 No iterative refinement is provided because such partial representation of
760 matrix does not allow efficient calculation of extra-precise matrix-vector
761 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
762 need iterative refinement.
763 
764 INPUT PARAMETERS
765  A - array[0..N-1,0..N-1], system matrix
766  N - size of A
767  IsUpper - what half of A is provided
768  B - array[0..N-1], right part
769 
770 OUTPUT PARAMETERS
771  Info - same as in RMatrixSolve
772  Returns -3 for non-SPD matrices.
773  Rep - same as in RMatrixSolve
774  X - same as in RMatrixSolve
775 
776  -- ALGLIB --
777  Copyright 27.01.2010 by Bochkanov Sergey
778 *************************************************************************/
779 void spdmatrixsolve(const real_2d_array &a, const ae_int_t n, const bool isupper, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
780 {
781  alglib_impl::ae_state _alglib_env_state;
782  alglib_impl::ae_state_init(&_alglib_env_state);
783  try
784  {
785  alglib_impl::spdmatrixsolve(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, isupper, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
786  alglib_impl::ae_state_clear(&_alglib_env_state);
787  return;
788  }
790  {
791  throw ap_error(_alglib_env_state.error_msg);
792  }
793 }
794 
795 /*************************************************************************
796 Dense solver. Same as RMatrixLUSolveM(), but for SPD matrices represented
797 by their Cholesky decomposition.
798 
799 Algorithm features:
800 * automatic detection of degenerate cases
801 * O(M*N^2) complexity
802 * condition number estimation
803 * matrix is represented by its upper or lower triangle
804 
805 No iterative refinement is provided because such partial representation of
806 matrix does not allow efficient calculation of extra-precise matrix-vector
807 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
808 need iterative refinement.
809 
810 INPUT PARAMETERS
811  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
812  SPDMatrixCholesky result
813  N - size of CHA
814  IsUpper - what half of CHA is provided
815  B - array[0..N-1,0..M-1], right part
816  M - right part size
817 
818 OUTPUT PARAMETERS
819  Info - same as in RMatrixSolve
820  Rep - same as in RMatrixSolve
821  X - same as in RMatrixSolve
822 
823  -- ALGLIB --
824  Copyright 27.01.2010 by Bochkanov Sergey
825 *************************************************************************/
826 void spdmatrixcholeskysolvem(const real_2d_array &cha, const ae_int_t n, const bool isupper, const real_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
827 {
828  alglib_impl::ae_state _alglib_env_state;
829  alglib_impl::ae_state_init(&_alglib_env_state);
830  try
831  {
832  alglib_impl::spdmatrixcholeskysolvem(const_cast<alglib_impl::ae_matrix*>(cha.c_ptr()), n, isupper, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
833  alglib_impl::ae_state_clear(&_alglib_env_state);
834  return;
835  }
837  {
838  throw ap_error(_alglib_env_state.error_msg);
839  }
840 }
841 
842 /*************************************************************************
843 Dense solver. Same as RMatrixLUSolve(), but for SPD matrices represented
844 by their Cholesky decomposition.
845 
846 Algorithm features:
847 * automatic detection of degenerate cases
848 * O(N^2) complexity
849 * condition number estimation
850 * matrix is represented by its upper or lower triangle
851 
852 No iterative refinement is provided because such partial representation of
853 matrix does not allow efficient calculation of extra-precise matrix-vector
854 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
855 need iterative refinement.
856 
857 INPUT PARAMETERS
858  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
859  SPDMatrixCholesky result
860  N - size of A
861  IsUpper - what half of CHA is provided
862  B - array[0..N-1], right part
863 
864 OUTPUT PARAMETERS
865  Info - same as in RMatrixSolve
866  Rep - same as in RMatrixSolve
867  X - same as in RMatrixSolve
868 
869  -- ALGLIB --
870  Copyright 27.01.2010 by Bochkanov Sergey
871 *************************************************************************/
872 void spdmatrixcholeskysolve(const real_2d_array &cha, const ae_int_t n, const bool isupper, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
873 {
874  alglib_impl::ae_state _alglib_env_state;
875  alglib_impl::ae_state_init(&_alglib_env_state);
876  try
877  {
878  alglib_impl::spdmatrixcholeskysolve(const_cast<alglib_impl::ae_matrix*>(cha.c_ptr()), n, isupper, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
879  alglib_impl::ae_state_clear(&_alglib_env_state);
880  return;
881  }
883  {
884  throw ap_error(_alglib_env_state.error_msg);
885  }
886 }
887 
888 /*************************************************************************
889 Dense solver. Same as RMatrixSolveM(), but for Hermitian positive definite
890 matrices.
891 
892 Algorithm features:
893 * automatic detection of degenerate cases
894 * condition number estimation
895 * O(N^3+M*N^2) complexity
896 * matrix is represented by its upper or lower triangle
897 
898 No iterative refinement is provided because such partial representation of
899 matrix does not allow efficient calculation of extra-precise matrix-vector
900 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
901 need iterative refinement.
902 
903 INPUT PARAMETERS
904  A - array[0..N-1,0..N-1], system matrix
905  N - size of A
906  IsUpper - what half of A is provided
907  B - array[0..N-1,0..M-1], right part
908  M - right part size
909 
910 OUTPUT PARAMETERS
911  Info - same as in RMatrixSolve.
912  Returns -3 for non-HPD matrices.
913  Rep - same as in RMatrixSolve
914  X - same as in RMatrixSolve
915 
916  -- ALGLIB --
917  Copyright 27.01.2010 by Bochkanov Sergey
918 *************************************************************************/
919 void hpdmatrixsolvem(const complex_2d_array &a, const ae_int_t n, const bool isupper, const complex_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
920 {
921  alglib_impl::ae_state _alglib_env_state;
922  alglib_impl::ae_state_init(&_alglib_env_state);
923  try
924  {
925  alglib_impl::hpdmatrixsolvem(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, isupper, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
926  alglib_impl::ae_state_clear(&_alglib_env_state);
927  return;
928  }
930  {
931  throw ap_error(_alglib_env_state.error_msg);
932  }
933 }
934 
935 /*************************************************************************
936 Dense solver. Same as RMatrixSolve(), but for Hermitian positive definite
937 matrices.
938 
939 Algorithm features:
940 * automatic detection of degenerate cases
941 * condition number estimation
942 * O(N^3) complexity
943 * matrix is represented by its upper or lower triangle
944 
945 No iterative refinement is provided because such partial representation of
946 matrix does not allow efficient calculation of extra-precise matrix-vector
947 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
948 need iterative refinement.
949 
950 INPUT PARAMETERS
951  A - array[0..N-1,0..N-1], system matrix
952  N - size of A
953  IsUpper - what half of A is provided
954  B - array[0..N-1], right part
955 
956 OUTPUT PARAMETERS
957  Info - same as in RMatrixSolve
958  Returns -3 for non-HPD matrices.
959  Rep - same as in RMatrixSolve
960  X - same as in RMatrixSolve
961 
962  -- ALGLIB --
963  Copyright 27.01.2010 by Bochkanov Sergey
964 *************************************************************************/
965 void hpdmatrixsolve(const complex_2d_array &a, const ae_int_t n, const bool isupper, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
966 {
967  alglib_impl::ae_state _alglib_env_state;
968  alglib_impl::ae_state_init(&_alglib_env_state);
969  try
970  {
971  alglib_impl::hpdmatrixsolve(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), n, isupper, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
972  alglib_impl::ae_state_clear(&_alglib_env_state);
973  return;
974  }
976  {
977  throw ap_error(_alglib_env_state.error_msg);
978  }
979 }
980 
981 /*************************************************************************
982 Dense solver. Same as RMatrixLUSolveM(), but for HPD matrices represented
983 by their Cholesky decomposition.
984 
985 Algorithm features:
986 * automatic detection of degenerate cases
987 * O(M*N^2) complexity
988 * condition number estimation
989 * matrix is represented by its upper or lower triangle
990 
991 No iterative refinement is provided because such partial representation of
992 matrix does not allow efficient calculation of extra-precise matrix-vector
993 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
994 need iterative refinement.
995 
996 INPUT PARAMETERS
997  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
998  HPDMatrixCholesky result
999  N - size of CHA
1000  IsUpper - what half of CHA is provided
1001  B - array[0..N-1,0..M-1], right part
1002  M - right part size
1003 
1004 OUTPUT PARAMETERS
1005  Info - same as in RMatrixSolve
1006  Rep - same as in RMatrixSolve
1007  X - same as in RMatrixSolve
1008 
1009  -- ALGLIB --
1010  Copyright 27.01.2010 by Bochkanov Sergey
1011 *************************************************************************/
1012 void hpdmatrixcholeskysolvem(const complex_2d_array &cha, const ae_int_t n, const bool isupper, const complex_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
1013 {
1014  alglib_impl::ae_state _alglib_env_state;
1015  alglib_impl::ae_state_init(&_alglib_env_state);
1016  try
1017  {
1018  alglib_impl::hpdmatrixcholeskysolvem(const_cast<alglib_impl::ae_matrix*>(cha.c_ptr()), n, isupper, const_cast<alglib_impl::ae_matrix*>(b.c_ptr()), m, &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_matrix*>(x.c_ptr()), &_alglib_env_state);
1019  alglib_impl::ae_state_clear(&_alglib_env_state);
1020  return;
1021  }
1023  {
1024  throw ap_error(_alglib_env_state.error_msg);
1025  }
1026 }
1027 
1028 /*************************************************************************
1029 Dense solver. Same as RMatrixLUSolve(), but for HPD matrices represented
1030 by their Cholesky decomposition.
1031 
1032 Algorithm features:
1033 * automatic detection of degenerate cases
1034 * O(N^2) complexity
1035 * condition number estimation
1036 * matrix is represented by its upper or lower triangle
1037 
1038 No iterative refinement is provided because such partial representation of
1039 matrix does not allow efficient calculation of extra-precise matrix-vector
1040 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
1041 need iterative refinement.
1042 
1043 INPUT PARAMETERS
1044  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
1045  SPDMatrixCholesky result
1046  N - size of A
1047  IsUpper - what half of CHA is provided
1048  B - array[0..N-1], right part
1049 
1050 OUTPUT PARAMETERS
1051  Info - same as in RMatrixSolve
1052  Rep - same as in RMatrixSolve
1053  X - same as in RMatrixSolve
1054 
1055  -- ALGLIB --
1056  Copyright 27.01.2010 by Bochkanov Sergey
1057 *************************************************************************/
1058 void hpdmatrixcholeskysolve(const complex_2d_array &cha, const ae_int_t n, const bool isupper, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
1059 {
1060  alglib_impl::ae_state _alglib_env_state;
1061  alglib_impl::ae_state_init(&_alglib_env_state);
1062  try
1063  {
1064  alglib_impl::hpdmatrixcholeskysolve(const_cast<alglib_impl::ae_matrix*>(cha.c_ptr()), n, isupper, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &info, const_cast<alglib_impl::densesolverreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
1065  alglib_impl::ae_state_clear(&_alglib_env_state);
1066  return;
1067  }
1069  {
1070  throw ap_error(_alglib_env_state.error_msg);
1071  }
1072 }
1073 
1074 /*************************************************************************
1075 Dense solver.
1076 
1077 This subroutine finds solution of the linear system A*X=B with non-square,
1078 possibly degenerate A. System is solved in the least squares sense, and
1079 general least squares solution X = X0 + CX*y which minimizes |A*X-B| is
1080 returned. If A is non-degenerate, solution in the usual sense is returned.
1081 
1082 Algorithm features:
1083 * automatic detection (and correct handling!) of degenerate cases
1084 * iterative refinement
1085 * O(N^3) complexity
1086 
1087 INPUT PARAMETERS
1088  A - array[0..NRows-1,0..NCols-1], system matrix
1089  NRows - vertical size of A
1090  NCols - horizontal size of A
1091  B - array[0..NCols-1], right part
1092  Threshold- a number in [0,1]. Singular values beyond Threshold are
1093  considered zero. Set it to 0.0, if you don't understand
1094  what it means, so the solver will choose good value on its
1095  own.
1096 
1097 OUTPUT PARAMETERS
1098  Info - return code:
1099  * -4 SVD subroutine failed
1100  * -1 if NRows<=0 or NCols<=0 or Threshold<0 was passed
1101  * 1 if task is solved
1102  Rep - solver report, see below for more info
1103  X - array[0..N-1,0..M-1], it contains:
1104  * solution of A*X=B (even for singular A)
1105  * zeros, if SVD subroutine failed
1106 
1107 SOLVER REPORT
1108 
1109 Subroutine sets following fields of the Rep structure:
1110 * R2 reciprocal of condition number: 1/cond(A), 2-norm.
1111 * N = NCols
1112 * K dim(Null(A))
1113 * CX array[0..N-1,0..K-1], kernel of A.
1114  Columns of CX store such vectors that A*CX[i]=0.
1115 
1116  -- ALGLIB --
1117  Copyright 24.08.2009 by Bochkanov Sergey
1118 *************************************************************************/
1119 void rmatrixsolvels(const real_2d_array &a, const ae_int_t nrows, const ae_int_t ncols, const real_1d_array &b, const double threshold, ae_int_t &info, densesolverlsreport &rep, real_1d_array &x)
1120 {
1121  alglib_impl::ae_state _alglib_env_state;
1122  alglib_impl::ae_state_init(&_alglib_env_state);
1123  try
1124  {
1125  alglib_impl::rmatrixsolvels(const_cast<alglib_impl::ae_matrix*>(a.c_ptr()), nrows, ncols, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), threshold, &info, const_cast<alglib_impl::densesolverlsreport*>(rep.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
1126  alglib_impl::ae_state_clear(&_alglib_env_state);
1127  return;
1128  }
1130  {
1131  throw ap_error(_alglib_env_state.error_msg);
1132  }
1133 }
1134 
1135 /*************************************************************************
1136 This object stores state of the LinLSQR method.
1137 
1138 You should use ALGLIB functions to work with this object.
1139 *************************************************************************/
1141 {
1143  if( p_struct==NULL )
1144  throw ap_error("ALGLIB: malloc error");
1146  throw ap_error("ALGLIB: malloc error");
1147 }
1148 
1150 {
1152  if( p_struct==NULL )
1153  throw ap_error("ALGLIB: malloc error");
1154  if( !alglib_impl::_linlsqrstate_init_copy(p_struct, const_cast<alglib_impl::linlsqrstate*>(rhs.p_struct), NULL, ae_false) )
1155  throw ap_error("ALGLIB: malloc error");
1156 }
1157 
1159 {
1160  if( this==&rhs )
1161  return *this;
1163  if( !alglib_impl::_linlsqrstate_init_copy(p_struct, const_cast<alglib_impl::linlsqrstate*>(rhs.p_struct), NULL, ae_false) )
1164  throw ap_error("ALGLIB: malloc error");
1165  return *this;
1166 }
1167 
1169 {
1171  ae_free(p_struct);
1172 }
1173 
1175 {
1176  return p_struct;
1177 }
1178 
1180 {
1181  return const_cast<alglib_impl::linlsqrstate*>(p_struct);
1182 }
1184 {
1185 }
1186 
1188 {
1189 }
1190 
1192 {
1193  if( this==&rhs )
1194  return *this;
1196  return *this;
1197 }
1198 
1200 {
1201 }
1202 
1203 
1204 /*************************************************************************
1205 
1206 *************************************************************************/
1208 {
1210  if( p_struct==NULL )
1211  throw ap_error("ALGLIB: malloc error");
1213  throw ap_error("ALGLIB: malloc error");
1214 }
1215 
1217 {
1219  if( p_struct==NULL )
1220  throw ap_error("ALGLIB: malloc error");
1221  if( !alglib_impl::_linlsqrreport_init_copy(p_struct, const_cast<alglib_impl::linlsqrreport*>(rhs.p_struct), NULL, ae_false) )
1222  throw ap_error("ALGLIB: malloc error");
1223 }
1224 
1226 {
1227  if( this==&rhs )
1228  return *this;
1230  if( !alglib_impl::_linlsqrreport_init_copy(p_struct, const_cast<alglib_impl::linlsqrreport*>(rhs.p_struct), NULL, ae_false) )
1231  throw ap_error("ALGLIB: malloc error");
1232  return *this;
1233 }
1234 
1236 {
1238  ae_free(p_struct);
1239 }
1240 
1242 {
1243  return p_struct;
1244 }
1245 
1247 {
1248  return const_cast<alglib_impl::linlsqrreport*>(p_struct);
1249 }
1250 linlsqrreport::linlsqrreport() : _linlsqrreport_owner() ,iterationscount(p_struct->iterationscount),nmv(p_struct->nmv),terminationtype(p_struct->terminationtype)
1251 {
1252 }
1253 
1255 {
1256 }
1257 
1259 {
1260  if( this==&rhs )
1261  return *this;
1263  return *this;
1264 }
1265 
1267 {
1268 }
1269 
1270 /*************************************************************************
1271 This function initializes linear LSQR Solver. This solver is used to solve
1272 non-symmetric (and, possibly, non-square) problems. Least squares solution
1273 is returned for non-compatible systems.
1274 
1275 USAGE:
1276 1. User initializes algorithm state with LinLSQRCreate() call
1277 2. User tunes solver parameters with LinLSQRSetCond() and other functions
1278 3. User calls LinLSQRSolveSparse() function which takes algorithm state
1279  and SparseMatrix object.
1280 4. User calls LinLSQRResults() to get solution
1281 5. Optionally, user may call LinLSQRSolveSparse() again to solve another
1282  problem with different matrix and/or right part without reinitializing
1283  LinLSQRState structure.
1284 
1285 INPUT PARAMETERS:
1286  M - number of rows in A
1287  N - number of variables, N>0
1288 
1289 OUTPUT PARAMETERS:
1290  State - structure which stores algorithm state
1291 
1292  -- ALGLIB --
1293  Copyright 30.11.2011 by Bochkanov Sergey
1294 *************************************************************************/
1295 void linlsqrcreate(const ae_int_t m, const ae_int_t n, linlsqrstate &state)
1296 {
1297  alglib_impl::ae_state _alglib_env_state;
1298  alglib_impl::ae_state_init(&_alglib_env_state);
1299  try
1300  {
1301  alglib_impl::linlsqrcreate(m, n, const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), &_alglib_env_state);
1302  alglib_impl::ae_state_clear(&_alglib_env_state);
1303  return;
1304  }
1306  {
1307  throw ap_error(_alglib_env_state.error_msg);
1308  }
1309 }
1310 
1311 /*************************************************************************
1312 This function changes preconditioning settings of LinLSQQSolveSparse()
1313 function. By default, SolveSparse() uses diagonal preconditioner, but if
1314 you want to use solver without preconditioning, you can call this function
1315 which forces solver to use unit matrix for preconditioning.
1316 
1317 INPUT PARAMETERS:
1318  State - structure which stores algorithm state
1319 
1320  -- ALGLIB --
1321  Copyright 19.11.2012 by Bochkanov Sergey
1322 *************************************************************************/
1324 {
1325  alglib_impl::ae_state _alglib_env_state;
1326  alglib_impl::ae_state_init(&_alglib_env_state);
1327  try
1328  {
1329  alglib_impl::linlsqrsetprecunit(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), &_alglib_env_state);
1330  alglib_impl::ae_state_clear(&_alglib_env_state);
1331  return;
1332  }
1334  {
1335  throw ap_error(_alglib_env_state.error_msg);
1336  }
1337 }
1338 
1339 /*************************************************************************
1340 This function changes preconditioning settings of LinCGSolveSparse()
1341 function. LinCGSolveSparse() will use diagonal of the system matrix as
1342 preconditioner. This preconditioning mode is active by default.
1343 
1344 INPUT PARAMETERS:
1345  State - structure which stores algorithm state
1346 
1347  -- ALGLIB --
1348  Copyright 19.11.2012 by Bochkanov Sergey
1349 *************************************************************************/
1351 {
1352  alglib_impl::ae_state _alglib_env_state;
1353  alglib_impl::ae_state_init(&_alglib_env_state);
1354  try
1355  {
1356  alglib_impl::linlsqrsetprecdiag(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), &_alglib_env_state);
1357  alglib_impl::ae_state_clear(&_alglib_env_state);
1358  return;
1359  }
1361  {
1362  throw ap_error(_alglib_env_state.error_msg);
1363  }
1364 }
1365 
1366 /*************************************************************************
1367 This function sets optional Tikhonov regularization coefficient.
1368 It is zero by default.
1369 
1370 INPUT PARAMETERS:
1371  LambdaI - regularization factor, LambdaI>=0
1372 
1373 OUTPUT PARAMETERS:
1374  State - structure which stores algorithm state
1375 
1376  -- ALGLIB --
1377  Copyright 30.11.2011 by Bochkanov Sergey
1378 *************************************************************************/
1379 void linlsqrsetlambdai(const linlsqrstate &state, const double lambdai)
1380 {
1381  alglib_impl::ae_state _alglib_env_state;
1382  alglib_impl::ae_state_init(&_alglib_env_state);
1383  try
1384  {
1385  alglib_impl::linlsqrsetlambdai(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), lambdai, &_alglib_env_state);
1386  alglib_impl::ae_state_clear(&_alglib_env_state);
1387  return;
1388  }
1390  {
1391  throw ap_error(_alglib_env_state.error_msg);
1392  }
1393 }
1394 
1395 /*************************************************************************
1396 Procedure for solution of A*x=b with sparse A.
1397 
1398 INPUT PARAMETERS:
1399  State - algorithm state
1400  A - sparse M*N matrix in the CRS format (you MUST contvert it
1401  to CRS format by calling SparseConvertToCRS() function
1402  BEFORE you pass it to this function).
1403  B - right part, array[M]
1404 
1405 RESULT:
1406  This function returns no result.
1407  You can get solution by calling LinCGResults()
1408 
1409 NOTE: this function uses lightweight preconditioning - multiplication by
1410  inverse of diag(A). If you want, you can turn preconditioning off by
1411  calling LinLSQRSetPrecUnit(). However, preconditioning cost is low
1412  and preconditioner is very important for solution of badly scaled
1413  problems.
1414 
1415  -- ALGLIB --
1416  Copyright 30.11.2011 by Bochkanov Sergey
1417 *************************************************************************/
1418 void linlsqrsolvesparse(const linlsqrstate &state, const sparsematrix &a, const real_1d_array &b)
1419 {
1420  alglib_impl::ae_state _alglib_env_state;
1421  alglib_impl::ae_state_init(&_alglib_env_state);
1422  try
1423  {
1424  alglib_impl::linlsqrsolvesparse(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), const_cast<alglib_impl::sparsematrix*>(a.c_ptr()), const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &_alglib_env_state);
1425  alglib_impl::ae_state_clear(&_alglib_env_state);
1426  return;
1427  }
1429  {
1430  throw ap_error(_alglib_env_state.error_msg);
1431  }
1432 }
1433 
1434 /*************************************************************************
1435 This function sets stopping criteria.
1436 
1437 INPUT PARAMETERS:
1438  EpsA - algorithm will be stopped if ||A^T*Rk||/(||A||*||Rk||)<=EpsA.
1439  EpsB - algorithm will be stopped if ||Rk||<=EpsB*||B||
1440  MaxIts - algorithm will be stopped if number of iterations
1441  more than MaxIts.
1442 
1443 OUTPUT PARAMETERS:
1444  State - structure which stores algorithm state
1445 
1446 NOTE: if EpsA,EpsB,EpsC and MaxIts are zero then these variables will
1447 be setted as default values.
1448 
1449  -- ALGLIB --
1450  Copyright 30.11.2011 by Bochkanov Sergey
1451 *************************************************************************/
1452 void linlsqrsetcond(const linlsqrstate &state, const double epsa, const double epsb, const ae_int_t maxits)
1453 {
1454  alglib_impl::ae_state _alglib_env_state;
1455  alglib_impl::ae_state_init(&_alglib_env_state);
1456  try
1457  {
1458  alglib_impl::linlsqrsetcond(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), epsa, epsb, maxits, &_alglib_env_state);
1459  alglib_impl::ae_state_clear(&_alglib_env_state);
1460  return;
1461  }
1463  {
1464  throw ap_error(_alglib_env_state.error_msg);
1465  }
1466 }
1467 
1468 /*************************************************************************
1469 LSQR solver: results.
1470 
1471 This function must be called after LinLSQRSolve
1472 
1473 INPUT PARAMETERS:
1474  State - algorithm state
1475 
1476 OUTPUT PARAMETERS:
1477  X - array[N], solution
1478  Rep - optimization report:
1479  * Rep.TerminationType completion code:
1480  * 1 ||Rk||<=EpsB*||B||
1481  * 4 ||A^T*Rk||/(||A||*||Rk||)<=EpsA
1482  * 5 MaxIts steps was taken
1483  * 7 rounding errors prevent further progress,
1484  X contains best point found so far.
1485  (sometimes returned on singular systems)
1486  * Rep.IterationsCount contains iterations count
1487  * NMV contains number of matrix-vector calculations
1488 
1489  -- ALGLIB --
1490  Copyright 30.11.2011 by Bochkanov Sergey
1491 *************************************************************************/
1493 {
1494  alglib_impl::ae_state _alglib_env_state;
1495  alglib_impl::ae_state_init(&_alglib_env_state);
1496  try
1497  {
1498  alglib_impl::linlsqrresults(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), const_cast<alglib_impl::linlsqrreport*>(rep.c_ptr()), &_alglib_env_state);
1499  alglib_impl::ae_state_clear(&_alglib_env_state);
1500  return;
1501  }
1503  {
1504  throw ap_error(_alglib_env_state.error_msg);
1505  }
1506 }
1507 
1508 /*************************************************************************
1509 This function turns on/off reporting.
1510 
1511 INPUT PARAMETERS:
1512  State - structure which stores algorithm state
1513  NeedXRep- whether iteration reports are needed or not
1514 
1515 If NeedXRep is True, algorithm will call rep() callback function if it is
1516 provided to MinCGOptimize().
1517 
1518  -- ALGLIB --
1519  Copyright 30.11.2011 by Bochkanov Sergey
1520 *************************************************************************/
1521 void linlsqrsetxrep(const linlsqrstate &state, const bool needxrep)
1522 {
1523  alglib_impl::ae_state _alglib_env_state;
1524  alglib_impl::ae_state_init(&_alglib_env_state);
1525  try
1526  {
1527  alglib_impl::linlsqrsetxrep(const_cast<alglib_impl::linlsqrstate*>(state.c_ptr()), needxrep, &_alglib_env_state);
1528  alglib_impl::ae_state_clear(&_alglib_env_state);
1529  return;
1530  }
1532  {
1533  throw ap_error(_alglib_env_state.error_msg);
1534  }
1535 }
1536 
1537 /*************************************************************************
1538 This object stores state of the linear CG method.
1539 
1540 You should use ALGLIB functions to work with this object.
1541 Never try to access its fields directly!
1542 *************************************************************************/
1544 {
1546  if( p_struct==NULL )
1547  throw ap_error("ALGLIB: malloc error");
1549  throw ap_error("ALGLIB: malloc error");
1550 }
1551 
1553 {
1555  if( p_struct==NULL )
1556  throw ap_error("ALGLIB: malloc error");
1557  if( !alglib_impl::_lincgstate_init_copy(p_struct, const_cast<alglib_impl::lincgstate*>(rhs.p_struct), NULL, ae_false) )
1558  throw ap_error("ALGLIB: malloc error");
1559 }
1560 
1562 {
1563  if( this==&rhs )
1564  return *this;
1566  if( !alglib_impl::_lincgstate_init_copy(p_struct, const_cast<alglib_impl::lincgstate*>(rhs.p_struct), NULL, ae_false) )
1567  throw ap_error("ALGLIB: malloc error");
1568  return *this;
1569 }
1570 
1572 {
1574  ae_free(p_struct);
1575 }
1576 
1578 {
1579  return p_struct;
1580 }
1581 
1583 {
1584  return const_cast<alglib_impl::lincgstate*>(p_struct);
1585 }
1587 {
1588 }
1589 
1591 {
1592 }
1593 
1595 {
1596  if( this==&rhs )
1597  return *this;
1599  return *this;
1600 }
1601 
1603 {
1604 }
1605 
1606 
1607 /*************************************************************************
1608 
1609 *************************************************************************/
1611 {
1613  if( p_struct==NULL )
1614  throw ap_error("ALGLIB: malloc error");
1616  throw ap_error("ALGLIB: malloc error");
1617 }
1618 
1620 {
1622  if( p_struct==NULL )
1623  throw ap_error("ALGLIB: malloc error");
1624  if( !alglib_impl::_lincgreport_init_copy(p_struct, const_cast<alglib_impl::lincgreport*>(rhs.p_struct), NULL, ae_false) )
1625  throw ap_error("ALGLIB: malloc error");
1626 }
1627 
1629 {
1630  if( this==&rhs )
1631  return *this;
1633  if( !alglib_impl::_lincgreport_init_copy(p_struct, const_cast<alglib_impl::lincgreport*>(rhs.p_struct), NULL, ae_false) )
1634  throw ap_error("ALGLIB: malloc error");
1635  return *this;
1636 }
1637 
1639 {
1641  ae_free(p_struct);
1642 }
1643 
1645 {
1646  return p_struct;
1647 }
1648 
1650 {
1651  return const_cast<alglib_impl::lincgreport*>(p_struct);
1652 }
1653 lincgreport::lincgreport() : _lincgreport_owner() ,iterationscount(p_struct->iterationscount),nmv(p_struct->nmv),terminationtype(p_struct->terminationtype),r2(p_struct->r2)
1654 {
1655 }
1656 
1658 {
1659 }
1660 
1662 {
1663  if( this==&rhs )
1664  return *this;
1666  return *this;
1667 }
1668 
1670 {
1671 }
1672 
1673 /*************************************************************************
1674 This function initializes linear CG Solver. This solver is used to solve
1675 symmetric positive definite problems. If you want to solve nonsymmetric
1676 (or non-positive definite) problem you may use LinLSQR solver provided by
1677 ALGLIB.
1678 
1679 USAGE:
1680 1. User initializes algorithm state with LinCGCreate() call
1681 2. User tunes solver parameters with LinCGSetCond() and other functions
1682 3. Optionally, user sets starting point with LinCGSetStartingPoint()
1683 4. User calls LinCGSolveSparse() function which takes algorithm state and
1684  SparseMatrix object.
1685 5. User calls LinCGResults() to get solution
1686 6. Optionally, user may call LinCGSolveSparse() again to solve another
1687  problem with different matrix and/or right part without reinitializing
1688  LinCGState structure.
1689 
1690 INPUT PARAMETERS:
1691  N - problem dimension, N>0
1692 
1693 OUTPUT PARAMETERS:
1694  State - structure which stores algorithm state
1695 
1696  -- ALGLIB --
1697  Copyright 14.11.2011 by Bochkanov Sergey
1698 *************************************************************************/
1699 void lincgcreate(const ae_int_t n, lincgstate &state)
1700 {
1701  alglib_impl::ae_state _alglib_env_state;
1702  alglib_impl::ae_state_init(&_alglib_env_state);
1703  try
1704  {
1705  alglib_impl::lincgcreate(n, const_cast<alglib_impl::lincgstate*>(state.c_ptr()), &_alglib_env_state);
1706  alglib_impl::ae_state_clear(&_alglib_env_state);
1707  return;
1708  }
1710  {
1711  throw ap_error(_alglib_env_state.error_msg);
1712  }
1713 }
1714 
1715 /*************************************************************************
1716 This function sets starting point.
1717 By default, zero starting point is used.
1718 
1719 INPUT PARAMETERS:
1720  X - starting point, array[N]
1721 
1722 OUTPUT PARAMETERS:
1723  State - structure which stores algorithm state
1724 
1725  -- ALGLIB --
1726  Copyright 14.11.2011 by Bochkanov Sergey
1727 *************************************************************************/
1729 {
1730  alglib_impl::ae_state _alglib_env_state;
1731  alglib_impl::ae_state_init(&_alglib_env_state);
1732  try
1733  {
1734  alglib_impl::lincgsetstartingpoint(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
1735  alglib_impl::ae_state_clear(&_alglib_env_state);
1736  return;
1737  }
1739  {
1740  throw ap_error(_alglib_env_state.error_msg);
1741  }
1742 }
1743 
1744 /*************************************************************************
1745 This function changes preconditioning settings of LinCGSolveSparse()
1746 function. By default, SolveSparse() uses diagonal preconditioner, but if
1747 you want to use solver without preconditioning, you can call this function
1748 which forces solver to use unit matrix for preconditioning.
1749 
1750 INPUT PARAMETERS:
1751  State - structure which stores algorithm state
1752 
1753  -- ALGLIB --
1754  Copyright 19.11.2012 by Bochkanov Sergey
1755 *************************************************************************/
1756 void lincgsetprecunit(const lincgstate &state)
1757 {
1758  alglib_impl::ae_state _alglib_env_state;
1759  alglib_impl::ae_state_init(&_alglib_env_state);
1760  try
1761  {
1762  alglib_impl::lincgsetprecunit(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), &_alglib_env_state);
1763  alglib_impl::ae_state_clear(&_alglib_env_state);
1764  return;
1765  }
1767  {
1768  throw ap_error(_alglib_env_state.error_msg);
1769  }
1770 }
1771 
1772 /*************************************************************************
1773 This function changes preconditioning settings of LinCGSolveSparse()
1774 function. LinCGSolveSparse() will use diagonal of the system matrix as
1775 preconditioner. This preconditioning mode is active by default.
1776 
1777 INPUT PARAMETERS:
1778  State - structure which stores algorithm state
1779 
1780  -- ALGLIB --
1781  Copyright 19.11.2012 by Bochkanov Sergey
1782 *************************************************************************/
1783 void lincgsetprecdiag(const lincgstate &state)
1784 {
1785  alglib_impl::ae_state _alglib_env_state;
1786  alglib_impl::ae_state_init(&_alglib_env_state);
1787  try
1788  {
1789  alglib_impl::lincgsetprecdiag(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), &_alglib_env_state);
1790  alglib_impl::ae_state_clear(&_alglib_env_state);
1791  return;
1792  }
1794  {
1795  throw ap_error(_alglib_env_state.error_msg);
1796  }
1797 }
1798 
1799 /*************************************************************************
1800 This function sets stopping criteria.
1801 
1802 INPUT PARAMETERS:
1803  EpsF - algorithm will be stopped if norm of residual is less than
1804  EpsF*||b||.
1805  MaxIts - algorithm will be stopped if number of iterations is more
1806  than MaxIts.
1807 
1808 OUTPUT PARAMETERS:
1809  State - structure which stores algorithm state
1810 
1811 NOTES:
1812 If both EpsF and MaxIts are zero then small EpsF will be set to small
1813 value.
1814 
1815  -- ALGLIB --
1816  Copyright 14.11.2011 by Bochkanov Sergey
1817 *************************************************************************/
1818 void lincgsetcond(const lincgstate &state, const double epsf, const ae_int_t maxits)
1819 {
1820  alglib_impl::ae_state _alglib_env_state;
1821  alglib_impl::ae_state_init(&_alglib_env_state);
1822  try
1823  {
1824  alglib_impl::lincgsetcond(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), epsf, maxits, &_alglib_env_state);
1825  alglib_impl::ae_state_clear(&_alglib_env_state);
1826  return;
1827  }
1829  {
1830  throw ap_error(_alglib_env_state.error_msg);
1831  }
1832 }
1833 
1834 /*************************************************************************
1835 Procedure for solution of A*x=b with sparse A.
1836 
1837 INPUT PARAMETERS:
1838  State - algorithm state
1839  A - sparse matrix in the CRS format (you MUST contvert it to
1840  CRS format by calling SparseConvertToCRS() function).
1841  IsUpper - whether upper or lower triangle of A is used:
1842  * IsUpper=True => only upper triangle is used and lower
1843  triangle is not referenced at all
1844  * IsUpper=False => only lower triangle is used and upper
1845  triangle is not referenced at all
1846  B - right part, array[N]
1847 
1848 RESULT:
1849  This function returns no result.
1850  You can get solution by calling LinCGResults()
1851 
1852 NOTE: this function uses lightweight preconditioning - multiplication by
1853  inverse of diag(A). If you want, you can turn preconditioning off by
1854  calling LinCGSetPrecUnit(). However, preconditioning cost is low and
1855  preconditioner is very important for solution of badly scaled
1856  problems.
1857 
1858  -- ALGLIB --
1859  Copyright 14.11.2011 by Bochkanov Sergey
1860 *************************************************************************/
1861 void lincgsolvesparse(const lincgstate &state, const sparsematrix &a, const bool isupper, const real_1d_array &b)
1862 {
1863  alglib_impl::ae_state _alglib_env_state;
1864  alglib_impl::ae_state_init(&_alglib_env_state);
1865  try
1866  {
1867  alglib_impl::lincgsolvesparse(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), const_cast<alglib_impl::sparsematrix*>(a.c_ptr()), isupper, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &_alglib_env_state);
1868  alglib_impl::ae_state_clear(&_alglib_env_state);
1869  return;
1870  }
1872  {
1873  throw ap_error(_alglib_env_state.error_msg);
1874  }
1875 }
1876 
1877 /*************************************************************************
1878 CG-solver: results.
1879 
1880 This function must be called after LinCGSolve
1881 
1882 INPUT PARAMETERS:
1883  State - algorithm state
1884 
1885 OUTPUT PARAMETERS:
1886  X - array[N], solution
1887  Rep - optimization report:
1888  * Rep.TerminationType completion code:
1889  * -5 input matrix is either not positive definite,
1890  too large or too small
1891  * -4 overflow/underflow during solution
1892  (ill conditioned problem)
1893  * 1 ||residual||<=EpsF*||b||
1894  * 5 MaxIts steps was taken
1895  * 7 rounding errors prevent further progress,
1896  best point found is returned
1897  * Rep.IterationsCount contains iterations count
1898  * NMV contains number of matrix-vector calculations
1899 
1900  -- ALGLIB --
1901  Copyright 14.11.2011 by Bochkanov Sergey
1902 *************************************************************************/
1904 {
1905  alglib_impl::ae_state _alglib_env_state;
1906  alglib_impl::ae_state_init(&_alglib_env_state);
1907  try
1908  {
1909  alglib_impl::lincgresults(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), const_cast<alglib_impl::lincgreport*>(rep.c_ptr()), &_alglib_env_state);
1910  alglib_impl::ae_state_clear(&_alglib_env_state);
1911  return;
1912  }
1914  {
1915  throw ap_error(_alglib_env_state.error_msg);
1916  }
1917 }
1918 
1919 /*************************************************************************
1920 This function sets restart frequency. By default, algorithm is restarted
1921 after N subsequent iterations.
1922 
1923  -- ALGLIB --
1924  Copyright 14.11.2011 by Bochkanov Sergey
1925 *************************************************************************/
1926 void lincgsetrestartfreq(const lincgstate &state, const ae_int_t srf)
1927 {
1928  alglib_impl::ae_state _alglib_env_state;
1929  alglib_impl::ae_state_init(&_alglib_env_state);
1930  try
1931  {
1932  alglib_impl::lincgsetrestartfreq(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), srf, &_alglib_env_state);
1933  alglib_impl::ae_state_clear(&_alglib_env_state);
1934  return;
1935  }
1937  {
1938  throw ap_error(_alglib_env_state.error_msg);
1939  }
1940 }
1941 
1942 /*************************************************************************
1943 This function sets frequency of residual recalculations.
1944 
1945 Algorithm updates residual r_k using iterative formula, but recalculates
1946 it from scratch after each 10 iterations. It is done to avoid accumulation
1947 of numerical errors and to stop algorithm when r_k starts to grow.
1948 
1949 Such low update frequence (1/10) gives very little overhead, but makes
1950 algorithm a bit more robust against numerical errors. However, you may
1951 change it
1952 
1953 INPUT PARAMETERS:
1954  Freq - desired update frequency, Freq>=0.
1955  Zero value means that no updates will be done.
1956 
1957  -- ALGLIB --
1958  Copyright 14.11.2011 by Bochkanov Sergey
1959 *************************************************************************/
1960 void lincgsetrupdatefreq(const lincgstate &state, const ae_int_t freq)
1961 {
1962  alglib_impl::ae_state _alglib_env_state;
1963  alglib_impl::ae_state_init(&_alglib_env_state);
1964  try
1965  {
1966  alglib_impl::lincgsetrupdatefreq(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), freq, &_alglib_env_state);
1967  alglib_impl::ae_state_clear(&_alglib_env_state);
1968  return;
1969  }
1971  {
1972  throw ap_error(_alglib_env_state.error_msg);
1973  }
1974 }
1975 
1976 /*************************************************************************
1977 This function turns on/off reporting.
1978 
1979 INPUT PARAMETERS:
1980  State - structure which stores algorithm state
1981  NeedXRep- whether iteration reports are needed or not
1982 
1983 If NeedXRep is True, algorithm will call rep() callback function if it is
1984 provided to MinCGOptimize().
1985 
1986  -- ALGLIB --
1987  Copyright 14.11.2011 by Bochkanov Sergey
1988 *************************************************************************/
1989 void lincgsetxrep(const lincgstate &state, const bool needxrep)
1990 {
1991  alglib_impl::ae_state _alglib_env_state;
1992  alglib_impl::ae_state_init(&_alglib_env_state);
1993  try
1994  {
1995  alglib_impl::lincgsetxrep(const_cast<alglib_impl::lincgstate*>(state.c_ptr()), needxrep, &_alglib_env_state);
1996  alglib_impl::ae_state_clear(&_alglib_env_state);
1997  return;
1998  }
2000  {
2001  throw ap_error(_alglib_env_state.error_msg);
2002  }
2003 }
2004 
2005 /*************************************************************************
2006 
2007 *************************************************************************/
2009 {
2011  if( p_struct==NULL )
2012  throw ap_error("ALGLIB: malloc error");
2014  throw ap_error("ALGLIB: malloc error");
2015 }
2016 
2018 {
2020  if( p_struct==NULL )
2021  throw ap_error("ALGLIB: malloc error");
2022  if( !alglib_impl::_nleqstate_init_copy(p_struct, const_cast<alglib_impl::nleqstate*>(rhs.p_struct), NULL, ae_false) )
2023  throw ap_error("ALGLIB: malloc error");
2024 }
2025 
2027 {
2028  if( this==&rhs )
2029  return *this;
2031  if( !alglib_impl::_nleqstate_init_copy(p_struct, const_cast<alglib_impl::nleqstate*>(rhs.p_struct), NULL, ae_false) )
2032  throw ap_error("ALGLIB: malloc error");
2033  return *this;
2034 }
2035 
2037 {
2039  ae_free(p_struct);
2040 }
2041 
2043 {
2044  return p_struct;
2045 }
2046 
2048 {
2049  return const_cast<alglib_impl::nleqstate*>(p_struct);
2050 }
2051 nleqstate::nleqstate() : _nleqstate_owner() ,needf(p_struct->needf),needfij(p_struct->needfij),xupdated(p_struct->xupdated),f(p_struct->f),fi(&p_struct->fi),j(&p_struct->j),x(&p_struct->x)
2052 {
2053 }
2054 
2056 {
2057 }
2058 
2060 {
2061  if( this==&rhs )
2062  return *this;
2064  return *this;
2065 }
2066 
2068 {
2069 }
2070 
2071 
2072 /*************************************************************************
2073 
2074 *************************************************************************/
2076 {
2078  if( p_struct==NULL )
2079  throw ap_error("ALGLIB: malloc error");
2081  throw ap_error("ALGLIB: malloc error");
2082 }
2083 
2085 {
2087  if( p_struct==NULL )
2088  throw ap_error("ALGLIB: malloc error");
2089  if( !alglib_impl::_nleqreport_init_copy(p_struct, const_cast<alglib_impl::nleqreport*>(rhs.p_struct), NULL, ae_false) )
2090  throw ap_error("ALGLIB: malloc error");
2091 }
2092 
2094 {
2095  if( this==&rhs )
2096  return *this;
2098  if( !alglib_impl::_nleqreport_init_copy(p_struct, const_cast<alglib_impl::nleqreport*>(rhs.p_struct), NULL, ae_false) )
2099  throw ap_error("ALGLIB: malloc error");
2100  return *this;
2101 }
2102 
2104 {
2106  ae_free(p_struct);
2107 }
2108 
2110 {
2111  return p_struct;
2112 }
2113 
2115 {
2116  return const_cast<alglib_impl::nleqreport*>(p_struct);
2117 }
2118 nleqreport::nleqreport() : _nleqreport_owner() ,iterationscount(p_struct->iterationscount),nfunc(p_struct->nfunc),njac(p_struct->njac),terminationtype(p_struct->terminationtype)
2119 {
2120 }
2121 
2123 {
2124 }
2125 
2127 {
2128  if( this==&rhs )
2129  return *this;
2131  return *this;
2132 }
2133 
2135 {
2136 }
2137 
2138 /*************************************************************************
2139  LEVENBERG-MARQUARDT-LIKE NONLINEAR SOLVER
2140 
2141 DESCRIPTION:
2142 This algorithm solves system of nonlinear equations
2143  F[0](x[0], ..., x[n-1]) = 0
2144  F[1](x[0], ..., x[n-1]) = 0
2145  ...
2146  F[M-1](x[0], ..., x[n-1]) = 0
2147 with M/N do not necessarily coincide. Algorithm converges quadratically
2148 under following conditions:
2149  * the solution set XS is nonempty
2150  * for some xs in XS there exist such neighbourhood N(xs) that:
2151  * vector function F(x) and its Jacobian J(x) are continuously
2152  differentiable on N
2153  * ||F(x)|| provides local error bound on N, i.e. there exists such
2154  c1, that ||F(x)||>c1*distance(x,XS)
2155 Note that these conditions are much more weaker than usual non-singularity
2156 conditions. For example, algorithm will converge for any affine function
2157 F (whether its Jacobian singular or not).
2158 
2159 
2160 REQUIREMENTS:
2161 Algorithm will request following information during its operation:
2162 * function vector F[] and Jacobian matrix at given point X
2163 * value of merit function f(x)=F[0]^2(x)+...+F[M-1]^2(x) at given point X
2164 
2165 
2166 USAGE:
2167 1. User initializes algorithm state with NLEQCreateLM() call
2168 2. User tunes solver parameters with NLEQSetCond(), NLEQSetStpMax() and
2169  other functions
2170 3. User calls NLEQSolve() function which takes algorithm state and
2171  pointers (delegates, etc.) to callback functions which calculate merit
2172  function value and Jacobian.
2173 4. User calls NLEQResults() to get solution
2174 5. Optionally, user may call NLEQRestartFrom() to solve another problem
2175  with same parameters (N/M) but another starting point and/or another
2176  function vector. NLEQRestartFrom() allows to reuse already initialized
2177  structure.
2178 
2179 
2180 INPUT PARAMETERS:
2181  N - space dimension, N>1:
2182  * if provided, only leading N elements of X are used
2183  * if not provided, determined automatically from size of X
2184  M - system size
2185  X - starting point
2186 
2187 
2188 OUTPUT PARAMETERS:
2189  State - structure which stores algorithm state
2190 
2191 
2192 NOTES:
2193 1. you may tune stopping conditions with NLEQSetCond() function
2194 2. if target function contains exp() or other fast growing functions, and
2195  optimization algorithm makes too large steps which leads to overflow,
2196  use NLEQSetStpMax() function to bound algorithm's steps.
2197 3. this algorithm is a slightly modified implementation of the method
2198  described in 'Levenberg-Marquardt method for constrained nonlinear
2199  equations with strong local convergence properties' by Christian Kanzow
2200  Nobuo Yamashita and Masao Fukushima and further developed in 'On the
2201  convergence of a New Levenberg-Marquardt Method' by Jin-yan Fan and
2202  Ya-Xiang Yuan.
2203 
2204 
2205  -- ALGLIB --
2206  Copyright 20.08.2009 by Bochkanov Sergey
2207 *************************************************************************/
2208 void nleqcreatelm(const ae_int_t n, const ae_int_t m, const real_1d_array &x, nleqstate &state)
2209 {
2210  alglib_impl::ae_state _alglib_env_state;
2211  alglib_impl::ae_state_init(&_alglib_env_state);
2212  try
2213  {
2214  alglib_impl::nleqcreatelm(n, m, const_cast<alglib_impl::ae_vector*>(x.c_ptr()), const_cast<alglib_impl::nleqstate*>(state.c_ptr()), &_alglib_env_state);
2215  alglib_impl::ae_state_clear(&_alglib_env_state);
2216  return;
2217  }
2219  {
2220  throw ap_error(_alglib_env_state.error_msg);
2221  }
2222 }
2223 
2224 /*************************************************************************
2225  LEVENBERG-MARQUARDT-LIKE NONLINEAR SOLVER
2226 
2227 DESCRIPTION:
2228 This algorithm solves system of nonlinear equations
2229  F[0](x[0], ..., x[n-1]) = 0
2230  F[1](x[0], ..., x[n-1]) = 0
2231  ...
2232  F[M-1](x[0], ..., x[n-1]) = 0
2233 with M/N do not necessarily coincide. Algorithm converges quadratically
2234 under following conditions:
2235  * the solution set XS is nonempty
2236  * for some xs in XS there exist such neighbourhood N(xs) that:
2237  * vector function F(x) and its Jacobian J(x) are continuously
2238  differentiable on N
2239  * ||F(x)|| provides local error bound on N, i.e. there exists such
2240  c1, that ||F(x)||>c1*distance(x,XS)
2241 Note that these conditions are much more weaker than usual non-singularity
2242 conditions. For example, algorithm will converge for any affine function
2243 F (whether its Jacobian singular or not).
2244 
2245 
2246 REQUIREMENTS:
2247 Algorithm will request following information during its operation:
2248 * function vector F[] and Jacobian matrix at given point X
2249 * value of merit function f(x)=F[0]^2(x)+...+F[M-1]^2(x) at given point X
2250 
2251 
2252 USAGE:
2253 1. User initializes algorithm state with NLEQCreateLM() call
2254 2. User tunes solver parameters with NLEQSetCond(), NLEQSetStpMax() and
2255  other functions
2256 3. User calls NLEQSolve() function which takes algorithm state and
2257  pointers (delegates, etc.) to callback functions which calculate merit
2258  function value and Jacobian.
2259 4. User calls NLEQResults() to get solution
2260 5. Optionally, user may call NLEQRestartFrom() to solve another problem
2261  with same parameters (N/M) but another starting point and/or another
2262  function vector. NLEQRestartFrom() allows to reuse already initialized
2263  structure.
2264 
2265 
2266 INPUT PARAMETERS:
2267  N - space dimension, N>1:
2268  * if provided, only leading N elements of X are used
2269  * if not provided, determined automatically from size of X
2270  M - system size
2271  X - starting point
2272 
2273 
2274 OUTPUT PARAMETERS:
2275  State - structure which stores algorithm state
2276 
2277 
2278 NOTES:
2279 1. you may tune stopping conditions with NLEQSetCond() function
2280 2. if target function contains exp() or other fast growing functions, and
2281  optimization algorithm makes too large steps which leads to overflow,
2282  use NLEQSetStpMax() function to bound algorithm's steps.
2283 3. this algorithm is a slightly modified implementation of the method
2284  described in 'Levenberg-Marquardt method for constrained nonlinear
2285  equations with strong local convergence properties' by Christian Kanzow
2286  Nobuo Yamashita and Masao Fukushima and further developed in 'On the
2287  convergence of a New Levenberg-Marquardt Method' by Jin-yan Fan and
2288  Ya-Xiang Yuan.
2289 
2290 
2291  -- ALGLIB --
2292  Copyright 20.08.2009 by Bochkanov Sergey
2293 *************************************************************************/
2294 void nleqcreatelm(const ae_int_t m, const real_1d_array &x, nleqstate &state)
2295 {
2296  alglib_impl::ae_state _alglib_env_state;
2297  ae_int_t n;
2298 
2299  n = x.length();
2300  alglib_impl::ae_state_init(&_alglib_env_state);
2301  try
2302  {
2303  alglib_impl::nleqcreatelm(n, m, const_cast<alglib_impl::ae_vector*>(x.c_ptr()), const_cast<alglib_impl::nleqstate*>(state.c_ptr()), &_alglib_env_state);
2304 
2305  alglib_impl::ae_state_clear(&_alglib_env_state);
2306  return;
2307  }
2309  {
2310  throw ap_error(_alglib_env_state.error_msg);
2311  }
2312 }
2313 
2314 /*************************************************************************
2315 This function sets stopping conditions for the nonlinear solver
2316 
2317 INPUT PARAMETERS:
2318  State - structure which stores algorithm state
2319  EpsF - >=0
2320  The subroutine finishes its work if on k+1-th iteration
2321  the condition ||F||<=EpsF is satisfied
2322  MaxIts - maximum number of iterations. If MaxIts=0, the number of
2323  iterations is unlimited.
2324 
2325 Passing EpsF=0 and MaxIts=0 simultaneously will lead to automatic
2326 stopping criterion selection (small EpsF).
2327 
2328 NOTES:
2329 
2330  -- ALGLIB --
2331  Copyright 20.08.2010 by Bochkanov Sergey
2332 *************************************************************************/
2333 void nleqsetcond(const nleqstate &state, const double epsf, const ae_int_t maxits)
2334 {
2335  alglib_impl::ae_state _alglib_env_state;
2336  alglib_impl::ae_state_init(&_alglib_env_state);
2337  try
2338  {
2339  alglib_impl::nleqsetcond(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), epsf, maxits, &_alglib_env_state);
2340  alglib_impl::ae_state_clear(&_alglib_env_state);
2341  return;
2342  }
2344  {
2345  throw ap_error(_alglib_env_state.error_msg);
2346  }
2347 }
2348 
2349 /*************************************************************************
2350 This function turns on/off reporting.
2351 
2352 INPUT PARAMETERS:
2353  State - structure which stores algorithm state
2354  NeedXRep- whether iteration reports are needed or not
2355 
2356 If NeedXRep is True, algorithm will call rep() callback function if it is
2357 provided to NLEQSolve().
2358 
2359  -- ALGLIB --
2360  Copyright 20.08.2010 by Bochkanov Sergey
2361 *************************************************************************/
2362 void nleqsetxrep(const nleqstate &state, const bool needxrep)
2363 {
2364  alglib_impl::ae_state _alglib_env_state;
2365  alglib_impl::ae_state_init(&_alglib_env_state);
2366  try
2367  {
2368  alglib_impl::nleqsetxrep(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), needxrep, &_alglib_env_state);
2369  alglib_impl::ae_state_clear(&_alglib_env_state);
2370  return;
2371  }
2373  {
2374  throw ap_error(_alglib_env_state.error_msg);
2375  }
2376 }
2377 
2378 /*************************************************************************
2379 This function sets maximum step length
2380 
2381 INPUT PARAMETERS:
2382  State - structure which stores algorithm state
2383  StpMax - maximum step length, >=0. Set StpMax to 0.0, if you don't
2384  want to limit step length.
2385 
2386 Use this subroutine when target function contains exp() or other fast
2387 growing functions, and algorithm makes too large steps which lead to
2388 overflow. This function allows us to reject steps that are too large (and
2389 therefore expose us to the possible overflow) without actually calculating
2390 function value at the x+stp*d.
2391 
2392  -- ALGLIB --
2393  Copyright 20.08.2010 by Bochkanov Sergey
2394 *************************************************************************/
2395 void nleqsetstpmax(const nleqstate &state, const double stpmax)
2396 {
2397  alglib_impl::ae_state _alglib_env_state;
2398  alglib_impl::ae_state_init(&_alglib_env_state);
2399  try
2400  {
2401  alglib_impl::nleqsetstpmax(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), stpmax, &_alglib_env_state);
2402  alglib_impl::ae_state_clear(&_alglib_env_state);
2403  return;
2404  }
2406  {
2407  throw ap_error(_alglib_env_state.error_msg);
2408  }
2409 }
2410 
2411 /*************************************************************************
2412 This function provides reverse communication interface
2413 Reverse communication interface is not documented or recommended to use.
2414 See below for functions which provide better documented API
2415 *************************************************************************/
2416 bool nleqiteration(const nleqstate &state)
2417 {
2418  alglib_impl::ae_state _alglib_env_state;
2419  alglib_impl::ae_state_init(&_alglib_env_state);
2420  try
2421  {
2422  ae_bool result = alglib_impl::nleqiteration(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), &_alglib_env_state);
2423  alglib_impl::ae_state_clear(&_alglib_env_state);
2424  return *(reinterpret_cast<bool*>(&result));
2425  }
2427  {
2428  throw ap_error(_alglib_env_state.error_msg);
2429  }
2430 }
2431 
2432 
2433 void nleqsolve(nleqstate &state,
2434  void (*func)(const real_1d_array &x, double &func, void *ptr),
2435  void (*jac)(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr),
2436  void (*rep)(const real_1d_array &x, double func, void *ptr),
2437  void *ptr)
2438 {
2439  alglib_impl::ae_state _alglib_env_state;
2440  if( func==NULL )
2441  throw ap_error("ALGLIB: error in 'nleqsolve()' (func is NULL)");
2442  if( jac==NULL )
2443  throw ap_error("ALGLIB: error in 'nleqsolve()' (jac is NULL)");
2444  alglib_impl::ae_state_init(&_alglib_env_state);
2445  try
2446  {
2447  while( alglib_impl::nleqiteration(state.c_ptr(), &_alglib_env_state) )
2448  {
2449  if( state.needf )
2450  {
2451  func(state.x, state.f, ptr);
2452  continue;
2453  }
2454  if( state.needfij )
2455  {
2456  jac(state.x, state.fi, state.j, ptr);
2457  continue;
2458  }
2459  if( state.xupdated )
2460  {
2461  if( rep!=NULL )
2462  rep(state.x, state.f, ptr);
2463  continue;
2464  }
2465  throw ap_error("ALGLIB: error in 'nleqsolve' (some derivatives were not provided?)");
2466  }
2467  alglib_impl::ae_state_clear(&_alglib_env_state);
2468  }
2470  {
2471  throw ap_error(_alglib_env_state.error_msg);
2472  }
2473 }
2474 
2475 
2476 
2477 /*************************************************************************
2478 NLEQ solver results
2479 
2480 INPUT PARAMETERS:
2481  State - algorithm state.
2482 
2483 OUTPUT PARAMETERS:
2484  X - array[0..N-1], solution
2485  Rep - optimization report:
2486  * Rep.TerminationType completion code:
2487  * -4 ERROR: algorithm has converged to the
2488  stationary point Xf which is local minimum of
2489  f=F[0]^2+...+F[m-1]^2, but is not solution of
2490  nonlinear system.
2491  * 1 sqrt(f)<=EpsF.
2492  * 5 MaxIts steps was taken
2493  * 7 stopping conditions are too stringent,
2494  further improvement is impossible
2495  * Rep.IterationsCount contains iterations count
2496  * NFEV contains number of function calculations
2497  * ActiveConstraints contains number of active constraints
2498 
2499  -- ALGLIB --
2500  Copyright 20.08.2009 by Bochkanov Sergey
2501 *************************************************************************/
2502 void nleqresults(const nleqstate &state, real_1d_array &x, nleqreport &rep)
2503 {
2504  alglib_impl::ae_state _alglib_env_state;
2505  alglib_impl::ae_state_init(&_alglib_env_state);
2506  try
2507  {
2508  alglib_impl::nleqresults(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), const_cast<alglib_impl::nleqreport*>(rep.c_ptr()), &_alglib_env_state);
2509  alglib_impl::ae_state_clear(&_alglib_env_state);
2510  return;
2511  }
2513  {
2514  throw ap_error(_alglib_env_state.error_msg);
2515  }
2516 }
2517 
2518 /*************************************************************************
2519 NLEQ solver results
2520 
2521 Buffered implementation of NLEQResults(), which uses pre-allocated buffer
2522 to store X[]. If buffer size is too small, it resizes buffer. It is
2523 intended to be used in the inner cycles of performance critical algorithms
2524 where array reallocation penalty is too large to be ignored.
2525 
2526  -- ALGLIB --
2527  Copyright 20.08.2009 by Bochkanov Sergey
2528 *************************************************************************/
2529 void nleqresultsbuf(const nleqstate &state, real_1d_array &x, nleqreport &rep)
2530 {
2531  alglib_impl::ae_state _alglib_env_state;
2532  alglib_impl::ae_state_init(&_alglib_env_state);
2533  try
2534  {
2535  alglib_impl::nleqresultsbuf(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), const_cast<alglib_impl::nleqreport*>(rep.c_ptr()), &_alglib_env_state);
2536  alglib_impl::ae_state_clear(&_alglib_env_state);
2537  return;
2538  }
2540  {
2541  throw ap_error(_alglib_env_state.error_msg);
2542  }
2543 }
2544 
2545 /*************************************************************************
2546 This subroutine restarts CG algorithm from new point. All optimization
2547 parameters are left unchanged.
2548 
2549 This function allows to solve multiple optimization problems (which
2550 must have same number of dimensions) without object reallocation penalty.
2551 
2552 INPUT PARAMETERS:
2553  State - structure used for reverse communication previously
2554  allocated with MinCGCreate call.
2555  X - new starting point.
2556  BndL - new lower bounds
2557  BndU - new upper bounds
2558 
2559  -- ALGLIB --
2560  Copyright 30.07.2010 by Bochkanov Sergey
2561 *************************************************************************/
2562 void nleqrestartfrom(const nleqstate &state, const real_1d_array &x)
2563 {
2564  alglib_impl::ae_state _alglib_env_state;
2565  alglib_impl::ae_state_init(&_alglib_env_state);
2566  try
2567  {
2568  alglib_impl::nleqrestartfrom(const_cast<alglib_impl::nleqstate*>(state.c_ptr()), const_cast<alglib_impl::ae_vector*>(x.c_ptr()), &_alglib_env_state);
2569  alglib_impl::ae_state_clear(&_alglib_env_state);
2570  return;
2571  }
2573  {
2574  throw ap_error(_alglib_env_state.error_msg);
2575  }
2576 }
2577 }
2578 
2580 //
2581 // THIS SECTION CONTAINS IMPLEMENTATION OF COMPUTATIONAL CORE
2582 //
2584 namespace alglib_impl
2585 {
2586 static void densesolver_rmatrixlusolveinternal(/* Real */ ae_matrix* lua,
2587  /* Integer */ ae_vector* p,
2588  double scalea,
2589  ae_int_t n,
2590  /* Real */ ae_matrix* a,
2591  ae_bool havea,
2592  /* Real */ ae_matrix* b,
2593  ae_int_t m,
2594  ae_int_t* info,
2595  densesolverreport* rep,
2596  /* Real */ ae_matrix* x,
2597  ae_state *_state);
2598 static void densesolver_spdmatrixcholeskysolveinternal(/* Real */ ae_matrix* cha,
2599  double sqrtscalea,
2600  ae_int_t n,
2601  ae_bool isupper,
2602  /* Real */ ae_matrix* a,
2603  ae_bool havea,
2604  /* Real */ ae_matrix* b,
2605  ae_int_t m,
2606  ae_int_t* info,
2607  densesolverreport* rep,
2608  /* Real */ ae_matrix* x,
2609  ae_state *_state);
2610 static void densesolver_cmatrixlusolveinternal(/* Complex */ ae_matrix* lua,
2611  /* Integer */ ae_vector* p,
2612  double scalea,
2613  ae_int_t n,
2614  /* Complex */ ae_matrix* a,
2615  ae_bool havea,
2616  /* Complex */ ae_matrix* b,
2617  ae_int_t m,
2618  ae_int_t* info,
2619  densesolverreport* rep,
2620  /* Complex */ ae_matrix* x,
2621  ae_state *_state);
2622 static void densesolver_hpdmatrixcholeskysolveinternal(/* Complex */ ae_matrix* cha,
2623  double sqrtscalea,
2624  ae_int_t n,
2625  ae_bool isupper,
2626  /* Complex */ ae_matrix* a,
2627  ae_bool havea,
2628  /* Complex */ ae_matrix* b,
2629  ae_int_t m,
2630  ae_int_t* info,
2631  densesolverreport* rep,
2632  /* Complex */ ae_matrix* x,
2633  ae_state *_state);
2634 static ae_int_t densesolver_densesolverrfsmax(ae_int_t n,
2635  double r1,
2636  double rinf,
2637  ae_state *_state);
2638 static ae_int_t densesolver_densesolverrfsmaxv2(ae_int_t n,
2639  double r2,
2640  ae_state *_state);
2641 static void densesolver_rbasiclusolve(/* Real */ ae_matrix* lua,
2642  /* Integer */ ae_vector* p,
2643  double scalea,
2644  ae_int_t n,
2645  /* Real */ ae_vector* xb,
2646  /* Real */ ae_vector* tmp,
2647  ae_state *_state);
2648 static void densesolver_spdbasiccholeskysolve(/* Real */ ae_matrix* cha,
2649  double sqrtscalea,
2650  ae_int_t n,
2651  ae_bool isupper,
2652  /* Real */ ae_vector* xb,
2653  /* Real */ ae_vector* tmp,
2654  ae_state *_state);
2655 static void densesolver_cbasiclusolve(/* Complex */ ae_matrix* lua,
2656  /* Integer */ ae_vector* p,
2657  double scalea,
2658  ae_int_t n,
2659  /* Complex */ ae_vector* xb,
2660  /* Complex */ ae_vector* tmp,
2661  ae_state *_state);
2662 static void densesolver_hpdbasiccholeskysolve(/* Complex */ ae_matrix* cha,
2663  double sqrtscalea,
2664  ae_int_t n,
2665  ae_bool isupper,
2666  /* Complex */ ae_vector* xb,
2667  /* Complex */ ae_vector* tmp,
2668  ae_state *_state);
2669 
2670 
2671 static double linlsqr_atol = 1.0E-6;
2672 static double linlsqr_btol = 1.0E-6;
2673 static void linlsqr_clearrfields(linlsqrstate* state, ae_state *_state);
2674 
2675 
2676 static double lincg_defaultprecision = 1.0E-6;
2677 static void lincg_clearrfields(lincgstate* state, ae_state *_state);
2678 static void lincg_updateitersdata(lincgstate* state, ae_state *_state);
2679 
2680 
2681 static void nleq_clearrequestfields(nleqstate* state, ae_state *_state);
2682 static ae_bool nleq_increaselambda(double* lambdav,
2683  double* nu,
2684  double lambdaup,
2685  ae_state *_state);
2686 static void nleq_decreaselambda(double* lambdav,
2687  double* nu,
2688  double lambdadown,
2689  ae_state *_state);
2690 
2691 
2692 
2693 
2694 
2695 /*************************************************************************
2696 Dense solver.
2697 
2698 This subroutine solves a system A*x=b, where A is NxN non-denegerate
2699 real matrix, x and b are vectors.
2700 
2701 Algorithm features:
2702 * automatic detection of degenerate cases
2703 * condition number estimation
2704 * iterative refinement
2705 * O(N^3) complexity
2706 
2707 INPUT PARAMETERS
2708  A - array[0..N-1,0..N-1], system matrix
2709  N - size of A
2710  B - array[0..N-1], right part
2711 
2712 OUTPUT PARAMETERS
2713  Info - return code:
2714  * -3 A is singular, or VERY close to singular.
2715  X is filled by zeros in such cases.
2716  * -1 N<=0 was passed
2717  * 1 task is solved (but matrix A may be ill-conditioned,
2718  check R1/RInf parameters for condition numbers).
2719  Rep - solver report, see below for more info
2720  X - array[0..N-1], it contains:
2721  * solution of A*x=b if A is non-singular (well-conditioned
2722  or ill-conditioned, but not very close to singular)
2723  * zeros, if A is singular or VERY close to singular
2724  (in this case Info=-3).
2725 
2726 SOLVER REPORT
2727 
2728 Subroutine sets following fields of the Rep structure:
2729 * R1 reciprocal of condition number: 1/cond(A), 1-norm.
2730 * RInf reciprocal of condition number: 1/cond(A), inf-norm.
2731 
2732  -- ALGLIB --
2733  Copyright 27.01.2010 by Bochkanov Sergey
2734 *************************************************************************/
2735 void rmatrixsolve(/* Real */ ae_matrix* a,
2736  ae_int_t n,
2737  /* Real */ ae_vector* b,
2738  ae_int_t* info,
2739  densesolverreport* rep,
2740  /* Real */ ae_vector* x,
2741  ae_state *_state)
2742 {
2743  ae_frame _frame_block;
2744  ae_matrix bm;
2745  ae_matrix xm;
2746 
2747  ae_frame_make(_state, &_frame_block);
2748  *info = 0;
2750  ae_vector_clear(x);
2751  ae_matrix_init(&bm, 0, 0, DT_REAL, _state, ae_true);
2752  ae_matrix_init(&xm, 0, 0, DT_REAL, _state, ae_true);
2753 
2754  if( n<=0 )
2755  {
2756  *info = -1;
2757  ae_frame_leave(_state);
2758  return;
2759  }
2760  ae_matrix_set_length(&bm, n, 1, _state);
2761  ae_v_move(&bm.ptr.pp_double[0][0], bm.stride, &b->ptr.p_double[0], 1, ae_v_len(0,n-1));
2762  rmatrixsolvem(a, n, &bm, 1, ae_true, info, rep, &xm, _state);
2763  ae_vector_set_length(x, n, _state);
2764  ae_v_move(&x->ptr.p_double[0], 1, &xm.ptr.pp_double[0][0], xm.stride, ae_v_len(0,n-1));
2765  ae_frame_leave(_state);
2766 }
2767 
2768 
2769 /*************************************************************************
2770 Dense solver.
2771 
2772 Similar to RMatrixSolve() but solves task with multiple right parts (where
2773 b and x are NxM matrices).
2774 
2775 Algorithm features:
2776 * automatic detection of degenerate cases
2777 * condition number estimation
2778 * optional iterative refinement
2779 * O(N^3+M*N^2) complexity
2780 
2781 INPUT PARAMETERS
2782  A - array[0..N-1,0..N-1], system matrix
2783  N - size of A
2784  B - array[0..N-1,0..M-1], right part
2785  M - right part size
2786  RFS - iterative refinement switch:
2787  * True - refinement is used.
2788  Less performance, more precision.
2789  * False - refinement is not used.
2790  More performance, less precision.
2791 
2792 OUTPUT PARAMETERS
2793  Info - same as in RMatrixSolve
2794  Rep - same as in RMatrixSolve
2795  X - same as in RMatrixSolve
2796 
2797  -- ALGLIB --
2798  Copyright 27.01.2010 by Bochkanov Sergey
2799 *************************************************************************/
2800 void rmatrixsolvem(/* Real */ ae_matrix* a,
2801  ae_int_t n,
2802  /* Real */ ae_matrix* b,
2803  ae_int_t m,
2804  ae_bool rfs,
2805  ae_int_t* info,
2806  densesolverreport* rep,
2807  /* Real */ ae_matrix* x,
2808  ae_state *_state)
2809 {
2810  ae_frame _frame_block;
2811  ae_matrix da;
2812  ae_matrix emptya;
2813  ae_vector p;
2814  double scalea;
2815  ae_int_t i;
2816  ae_int_t j;
2817 
2818  ae_frame_make(_state, &_frame_block);
2819  *info = 0;
2821  ae_matrix_clear(x);
2822  ae_matrix_init(&da, 0, 0, DT_REAL, _state, ae_true);
2823  ae_matrix_init(&emptya, 0, 0, DT_REAL, _state, ae_true);
2824  ae_vector_init(&p, 0, DT_INT, _state, ae_true);
2825 
2826 
2827  /*
2828  * prepare: check inputs, allocate space...
2829  */
2830  if( n<=0||m<=0 )
2831  {
2832  *info = -1;
2833  ae_frame_leave(_state);
2834  return;
2835  }
2836  ae_matrix_set_length(&da, n, n, _state);
2837 
2838  /*
2839  * 1. scale matrix, max(|A[i,j]|)
2840  * 2. factorize scaled matrix
2841  * 3. solve
2842  */
2843  scalea = 0;
2844  for(i=0; i<=n-1; i++)
2845  {
2846  for(j=0; j<=n-1; j++)
2847  {
2848  scalea = ae_maxreal(scalea, ae_fabs(a->ptr.pp_double[i][j], _state), _state);
2849  }
2850  }
2851  if( ae_fp_eq(scalea,0) )
2852  {
2853  scalea = 1;
2854  }
2855  scalea = 1/scalea;
2856  for(i=0; i<=n-1; i++)
2857  {
2858  ae_v_move(&da.ptr.pp_double[i][0], 1, &a->ptr.pp_double[i][0], 1, ae_v_len(0,n-1));
2859  }
2860  rmatrixlu(&da, n, n, &p, _state);
2861  if( rfs )
2862  {
2863  densesolver_rmatrixlusolveinternal(&da, &p, scalea, n, a, ae_true, b, m, info, rep, x, _state);
2864  }
2865  else
2866  {
2867  densesolver_rmatrixlusolveinternal(&da, &p, scalea, n, &emptya, ae_false, b, m, info, rep, x, _state);
2868  }
2869  ae_frame_leave(_state);
2870 }
2871 
2872 
2873 /*************************************************************************
2874 Dense solver.
2875 
2876 This subroutine solves a system A*X=B, where A is NxN non-denegerate
2877 real matrix given by its LU decomposition, X and B are NxM real matrices.
2878 
2879 Algorithm features:
2880 * automatic detection of degenerate cases
2881 * O(N^2) complexity
2882 * condition number estimation
2883 
2884 No iterative refinement is provided because exact form of original matrix
2885 is not known to subroutine. Use RMatrixSolve or RMatrixMixedSolve if you
2886 need iterative refinement.
2887 
2888 INPUT PARAMETERS
2889  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
2890  P - array[0..N-1], pivots array, RMatrixLU result
2891  N - size of A
2892  B - array[0..N-1], right part
2893 
2894 OUTPUT PARAMETERS
2895  Info - same as in RMatrixSolve
2896  Rep - same as in RMatrixSolve
2897  X - same as in RMatrixSolve
2898 
2899  -- ALGLIB --
2900  Copyright 27.01.2010 by Bochkanov Sergey
2901 *************************************************************************/
2902 void rmatrixlusolve(/* Real */ ae_matrix* lua,
2903  /* Integer */ ae_vector* p,
2904  ae_int_t n,
2905  /* Real */ ae_vector* b,
2906  ae_int_t* info,
2907  densesolverreport* rep,
2908  /* Real */ ae_vector* x,
2909  ae_state *_state)
2910 {
2911  ae_frame _frame_block;
2912  ae_matrix bm;
2913  ae_matrix xm;
2914 
2915  ae_frame_make(_state, &_frame_block);
2916  *info = 0;
2918  ae_vector_clear(x);
2919  ae_matrix_init(&bm, 0, 0, DT_REAL, _state, ae_true);
2920  ae_matrix_init(&xm, 0, 0, DT_REAL, _state, ae_true);
2921 
2922  if( n<=0 )
2923  {
2924  *info = -1;
2925  ae_frame_leave(_state);
2926  return;
2927  }
2928  ae_matrix_set_length(&bm, n, 1, _state);
2929  ae_v_move(&bm.ptr.pp_double[0][0], bm.stride, &b->ptr.p_double[0], 1, ae_v_len(0,n-1));
2930  rmatrixlusolvem(lua, p, n, &bm, 1, info, rep, &xm, _state);
2931  ae_vector_set_length(x, n, _state);
2932  ae_v_move(&x->ptr.p_double[0], 1, &xm.ptr.pp_double[0][0], xm.stride, ae_v_len(0,n-1));
2933  ae_frame_leave(_state);
2934 }
2935 
2936 
2937 /*************************************************************************
2938 Dense solver.
2939 
2940 Similar to RMatrixLUSolve() but solves task with multiple right parts
2941 (where b and x are NxM matrices).
2942 
2943 Algorithm features:
2944 * automatic detection of degenerate cases
2945 * O(M*N^2) complexity
2946 * condition number estimation
2947 
2948 No iterative refinement is provided because exact form of original matrix
2949 is not known to subroutine. Use RMatrixSolve or RMatrixMixedSolve if you
2950 need iterative refinement.
2951 
2952 INPUT PARAMETERS
2953  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
2954  P - array[0..N-1], pivots array, RMatrixLU result
2955  N - size of A
2956  B - array[0..N-1,0..M-1], right part
2957  M - right part size
2958 
2959 OUTPUT PARAMETERS
2960  Info - same as in RMatrixSolve
2961  Rep - same as in RMatrixSolve
2962  X - same as in RMatrixSolve
2963 
2964  -- ALGLIB --
2965  Copyright 27.01.2010 by Bochkanov Sergey
2966 *************************************************************************/
2967 void rmatrixlusolvem(/* Real */ ae_matrix* lua,
2968  /* Integer */ ae_vector* p,
2969  ae_int_t n,
2970  /* Real */ ae_matrix* b,
2971  ae_int_t m,
2972  ae_int_t* info,
2973  densesolverreport* rep,
2974  /* Real */ ae_matrix* x,
2975  ae_state *_state)
2976 {
2977  ae_frame _frame_block;
2978  ae_matrix emptya;
2979  ae_int_t i;
2980  ae_int_t j;
2981  double scalea;
2982 
2983  ae_frame_make(_state, &_frame_block);
2984  *info = 0;
2986  ae_matrix_clear(x);
2987  ae_matrix_init(&emptya, 0, 0, DT_REAL, _state, ae_true);
2988 
2989 
2990  /*
2991  * prepare: check inputs, allocate space...
2992  */
2993  if( n<=0||m<=0 )
2994  {
2995  *info = -1;
2996  ae_frame_leave(_state);
2997  return;
2998  }
2999 
3000  /*
3001  * 1. scale matrix, max(|U[i,j]|)
3002  * we assume that LU is in its normal form, i.e. |L[i,j]|<=1
3003  * 2. solve
3004  */
3005  scalea = 0;
3006  for(i=0; i<=n-1; i++)
3007  {
3008  for(j=i; j<=n-1; j++)
3009  {
3010  scalea = ae_maxreal(scalea, ae_fabs(lua->ptr.pp_double[i][j], _state), _state);
3011  }
3012  }
3013  if( ae_fp_eq(scalea,0) )
3014  {
3015  scalea = 1;
3016  }
3017  scalea = 1/scalea;
3018  densesolver_rmatrixlusolveinternal(lua, p, scalea, n, &emptya, ae_false, b, m, info, rep, x, _state);
3019  ae_frame_leave(_state);
3020 }
3021 
3022 
3023 /*************************************************************************
3024 Dense solver.
3025 
3026 This subroutine solves a system A*x=b, where BOTH ORIGINAL A AND ITS
3027 LU DECOMPOSITION ARE KNOWN. You can use it if for some reasons you have
3028 both A and its LU decomposition.
3029 
3030 Algorithm features:
3031 * automatic detection of degenerate cases
3032 * condition number estimation
3033 * iterative refinement
3034 * O(N^2) complexity
3035 
3036 INPUT PARAMETERS
3037  A - array[0..N-1,0..N-1], system matrix
3038  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
3039  P - array[0..N-1], pivots array, RMatrixLU result
3040  N - size of A
3041  B - array[0..N-1], right part
3042 
3043 OUTPUT PARAMETERS
3044  Info - same as in RMatrixSolveM
3045  Rep - same as in RMatrixSolveM
3046  X - same as in RMatrixSolveM
3047 
3048  -- ALGLIB --
3049  Copyright 27.01.2010 by Bochkanov Sergey
3050 *************************************************************************/
3051 void rmatrixmixedsolve(/* Real */ ae_matrix* a,
3052  /* Real */ ae_matrix* lua,
3053  /* Integer */ ae_vector* p,
3054  ae_int_t n,
3055  /* Real */ ae_vector* b,
3056  ae_int_t* info,
3057  densesolverreport* rep,
3058  /* Real */ ae_vector* x,
3059  ae_state *_state)
3060 {
3061  ae_frame _frame_block;
3062  ae_matrix bm;
3063  ae_matrix xm;
3064 
3065  ae_frame_make(_state, &_frame_block);
3066  *info = 0;
3068  ae_vector_clear(x);
3069  ae_matrix_init(&bm, 0, 0, DT_REAL, _state, ae_true);
3070  ae_matrix_init(&xm, 0, 0, DT_REAL, _state, ae_true);
3071 
3072  if( n<=0 )
3073  {
3074  *info = -1;
3075  ae_frame_leave(_state);
3076  return;
3077  }
3078  ae_matrix_set_length(&bm, n, 1, _state);
3079  ae_v_move(&bm.ptr.pp_double[0][0], bm.stride, &b->ptr.p_double[0], 1, ae_v_len(0,n-1));
3080  rmatrixmixedsolvem(a, lua, p, n, &bm, 1, info, rep, &xm, _state);
3081  ae_vector_set_length(x, n, _state);
3082  ae_v_move(&x->ptr.p_double[0], 1, &xm.ptr.pp_double[0][0], xm.stride, ae_v_len(0,n-1));
3083  ae_frame_leave(_state);
3084 }
3085 
3086 
3087 /*************************************************************************
3088 Dense solver.
3089 
3090 Similar to RMatrixMixedSolve() but solves task with multiple right parts
3091 (where b and x are NxM matrices).
3092 
3093 Algorithm features:
3094 * automatic detection of degenerate cases
3095 * condition number estimation
3096 * iterative refinement
3097 * O(M*N^2) complexity
3098 
3099 INPUT PARAMETERS
3100  A - array[0..N-1,0..N-1], system matrix
3101  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
3102  P - array[0..N-1], pivots array, RMatrixLU result
3103  N - size of A
3104  B - array[0..N-1,0..M-1], right part
3105  M - right part size
3106 
3107 OUTPUT PARAMETERS
3108  Info - same as in RMatrixSolveM
3109  Rep - same as in RMatrixSolveM
3110  X - same as in RMatrixSolveM
3111 
3112  -- ALGLIB --
3113  Copyright 27.01.2010 by Bochkanov Sergey
3114 *************************************************************************/
3115 void rmatrixmixedsolvem(/* Real */ ae_matrix* a,
3116  /* Real */ ae_matrix* lua,
3117  /* Integer */ ae_vector* p,
3118  ae_int_t n,
3119  /* Real */ ae_matrix* b,
3120  ae_int_t m,
3121  ae_int_t* info,
3122  densesolverreport* rep,
3123  /* Real */ ae_matrix* x,
3124  ae_state *_state)
3125 {
3126  double scalea;
3127  ae_int_t i;
3128  ae_int_t j;
3129 
3130  *info = 0;
3132  ae_matrix_clear(x);
3133 
3134 
3135  /*
3136  * prepare: check inputs, allocate space...
3137  */
3138  if( n<=0||m<=0 )
3139  {
3140  *info = -1;
3141  return;
3142  }
3143 
3144  /*
3145  * 1. scale matrix, max(|A[i,j]|)
3146  * 2. factorize scaled matrix
3147  * 3. solve
3148  */
3149  scalea = 0;
3150  for(i=0; i<=n-1; i++)
3151  {
3152  for(j=0; j<=n-1; j++)
3153  {
3154  scalea = ae_maxreal(scalea, ae_fabs(a->ptr.pp_double[i][j], _state), _state);
3155  }
3156  }
3157  if( ae_fp_eq(scalea,0) )
3158  {
3159  scalea = 1;
3160  }
3161  scalea = 1/scalea;
3162  densesolver_rmatrixlusolveinternal(lua, p, scalea, n, a, ae_true, b, m, info, rep, x, _state);
3163 }
3164 
3165 
3166 /*************************************************************************
3167 Dense solver. Same as RMatrixSolveM(), but for complex matrices.
3168 
3169 Algorithm features:
3170 * automatic detection of degenerate cases
3171 * condition number estimation
3172 * iterative refinement
3173 * O(N^3+M*N^2) complexity
3174 
3175 INPUT PARAMETERS
3176  A - array[0..N-1,0..N-1], system matrix
3177  N - size of A
3178  B - array[0..N-1,0..M-1], right part
3179  M - right part size
3180  RFS - iterative refinement switch:
3181  * True - refinement is used.
3182  Less performance, more precision.
3183  * False - refinement is not used.
3184  More performance, less precision.
3185 
3186 OUTPUT PARAMETERS
3187  Info - same as in RMatrixSolve
3188  Rep - same as in RMatrixSolve
3189  X - same as in RMatrixSolve
3190 
3191  -- ALGLIB --
3192  Copyright 27.01.2010 by Bochkanov Sergey
3193 *************************************************************************/
3194 void cmatrixsolvem(/* Complex */ ae_matrix* a,
3195  ae_int_t n,
3196  /* Complex */ ae_matrix* b,
3197  ae_int_t m,
3198  ae_bool rfs,
3199  ae_int_t* info,
3200  densesolverreport* rep,
3201  /* Complex */ ae_matrix* x,
3202  ae_state *_state)
3203 {
3204  ae_frame _frame_block;
3205  ae_matrix da;
3206  ae_matrix emptya;
3207  ae_vector p;
3208  double scalea;
3209  ae_int_t i;
3210  ae_int_t j;
3211 
3212  ae_frame_make(_state, &_frame_block);
3213  *info = 0;
3215  ae_matrix_clear(x);
3216  ae_matrix_init(&da, 0, 0, DT_COMPLEX, _state, ae_true);
3217  ae_matrix_init(&emptya, 0, 0, DT_COMPLEX, _state, ae_true);
3218  ae_vector_init(&p, 0, DT_INT, _state, ae_true);
3219 
3220 
3221  /*
3222  * prepare: check inputs, allocate space...
3223  */
3224  if( n<=0||m<=0 )
3225  {
3226  *info = -1;
3227  ae_frame_leave(_state);
3228  return;
3229  }
3230  ae_matrix_set_length(&da, n, n, _state);
3231 
3232  /*
3233  * 1. scale matrix, max(|A[i,j]|)
3234  * 2. factorize scaled matrix
3235  * 3. solve
3236  */
3237  scalea = 0;
3238  for(i=0; i<=n-1; i++)
3239  {
3240  for(j=0; j<=n-1; j++)
3241  {
3242  scalea = ae_maxreal(scalea, ae_c_abs(a->ptr.pp_complex[i][j], _state), _state);
3243  }
3244  }
3245  if( ae_fp_eq(scalea,0) )
3246  {
3247  scalea = 1;
3248  }
3249  scalea = 1/scalea;
3250  for(i=0; i<=n-1; i++)
3251  {
3252  ae_v_cmove(&da.ptr.pp_complex[i][0], 1, &a->ptr.pp_complex[i][0], 1, "N", ae_v_len(0,n-1));
3253  }
3254  cmatrixlu(&da, n, n, &p, _state);
3255  if( rfs )
3256  {
3257  densesolver_cmatrixlusolveinternal(&da, &p, scalea, n, a, ae_true, b, m, info, rep, x, _state);
3258  }
3259  else
3260  {
3261  densesolver_cmatrixlusolveinternal(&da, &p, scalea, n, &emptya, ae_false, b, m, info, rep, x, _state);
3262  }
3263  ae_frame_leave(_state);
3264 }
3265 
3266 
3267 /*************************************************************************
3268 Dense solver. Same as RMatrixSolve(), but for complex matrices.
3269 
3270 Algorithm features:
3271 * automatic detection of degenerate cases
3272 * condition number estimation
3273 * iterative refinement
3274 * O(N^3) complexity
3275 
3276 INPUT PARAMETERS
3277  A - array[0..N-1,0..N-1], system matrix
3278  N - size of A
3279  B - array[0..N-1], right part
3280 
3281 OUTPUT PARAMETERS
3282  Info - same as in RMatrixSolve
3283  Rep - same as in RMatrixSolve
3284  X - same as in RMatrixSolve
3285 
3286  -- ALGLIB --
3287  Copyright 27.01.2010 by Bochkanov Sergey
3288 *************************************************************************/
3289 void cmatrixsolve(/* Complex */ ae_matrix* a,
3290  ae_int_t n,
3291  /* Complex */ ae_vector* b,
3292  ae_int_t* info,
3293  densesolverreport* rep,
3294  /* Complex */ ae_vector* x,
3295  ae_state *_state)
3296 {
3297  ae_frame _frame_block;
3298  ae_matrix bm;
3299  ae_matrix xm;
3300 
3301  ae_frame_make(_state, &_frame_block);
3302  *info = 0;
3304  ae_vector_clear(x);
3305  ae_matrix_init(&bm, 0, 0, DT_COMPLEX, _state, ae_true);
3306  ae_matrix_init(&xm, 0, 0, DT_COMPLEX, _state, ae_true);
3307 
3308  if( n<=0 )
3309  {
3310  *info = -1;
3311  ae_frame_leave(_state);
3312  return;
3313  }
3314  ae_matrix_set_length(&bm, n, 1, _state);
3315  ae_v_cmove(&bm.ptr.pp_complex[0][0], bm.stride, &b->ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
3316  cmatrixsolvem(a, n, &bm, 1, ae_true, info, rep, &xm, _state);
3317  ae_vector_set_length(x, n, _state);
3318  ae_v_cmove(&x->ptr.p_complex[0], 1, &xm.ptr.pp_complex[0][0], xm.stride, "N", ae_v_len(0,n-1));
3319  ae_frame_leave(_state);
3320 }
3321 
3322 
3323 /*************************************************************************
3324 Dense solver. Same as RMatrixLUSolveM(), but for complex matrices.
3325 
3326 Algorithm features:
3327 * automatic detection of degenerate cases
3328 * O(M*N^2) complexity
3329 * condition number estimation
3330 
3331 No iterative refinement is provided because exact form of original matrix
3332 is not known to subroutine. Use CMatrixSolve or CMatrixMixedSolve if you
3333 need iterative refinement.
3334 
3335 INPUT PARAMETERS
3336  LUA - array[0..N-1,0..N-1], LU decomposition, RMatrixLU result
3337  P - array[0..N-1], pivots array, RMatrixLU result
3338  N - size of A
3339  B - array[0..N-1,0..M-1], right part
3340  M - right part size
3341 
3342 OUTPUT PARAMETERS
3343  Info - same as in RMatrixSolve
3344  Rep - same as in RMatrixSolve
3345  X - same as in RMatrixSolve
3346 
3347  -- ALGLIB --
3348  Copyright 27.01.2010 by Bochkanov Sergey
3349 *************************************************************************/
3350 void cmatrixlusolvem(/* Complex */ ae_matrix* lua,
3351  /* Integer */ ae_vector* p,
3352  ae_int_t n,
3353  /* Complex */ ae_matrix* b,
3354  ae_int_t m,
3355  ae_int_t* info,
3356  densesolverreport* rep,
3357  /* Complex */ ae_matrix* x,
3358  ae_state *_state)
3359 {
3360  ae_frame _frame_block;
3361  ae_matrix emptya;
3362  ae_int_t i;
3363  ae_int_t j;
3364  double scalea;
3365 
3366  ae_frame_make(_state, &_frame_block);
3367  *info = 0;
3369  ae_matrix_clear(x);
3370  ae_matrix_init(&emptya, 0, 0, DT_COMPLEX, _state, ae_true);
3371 
3372 
3373  /*
3374  * prepare: check inputs, allocate space...
3375  */
3376  if( n<=0||m<=0 )
3377  {
3378  *info = -1;
3379  ae_frame_leave(_state);
3380  return;
3381  }
3382 
3383  /*
3384  * 1. scale matrix, max(|U[i,j]|)
3385  * we assume that LU is in its normal form, i.e. |L[i,j]|<=1
3386  * 2. solve
3387  */
3388  scalea = 0;
3389  for(i=0; i<=n-1; i++)
3390  {
3391  for(j=i; j<=n-1; j++)
3392  {
3393  scalea = ae_maxreal(scalea, ae_c_abs(lua->ptr.pp_complex[i][j], _state), _state);
3394  }
3395  }
3396  if( ae_fp_eq(scalea,0) )
3397  {
3398  scalea = 1;
3399  }
3400  scalea = 1/scalea;
3401  densesolver_cmatrixlusolveinternal(lua, p, scalea, n, &emptya, ae_false, b, m, info, rep, x, _state);
3402  ae_frame_leave(_state);
3403 }
3404 
3405 
3406 /*************************************************************************
3407 Dense solver. Same as RMatrixLUSolve(), but for complex matrices.
3408 
3409 Algorithm features:
3410 * automatic detection of degenerate cases
3411 * O(N^2) complexity
3412 * condition number estimation
3413 
3414 No iterative refinement is provided because exact form of original matrix
3415 is not known to subroutine. Use CMatrixSolve or CMatrixMixedSolve if you
3416 need iterative refinement.
3417 
3418 INPUT PARAMETERS
3419  LUA - array[0..N-1,0..N-1], LU decomposition, CMatrixLU result
3420  P - array[0..N-1], pivots array, CMatrixLU result
3421  N - size of A
3422  B - array[0..N-1], right part
3423 
3424 OUTPUT PARAMETERS
3425  Info - same as in RMatrixSolve
3426  Rep - same as in RMatrixSolve
3427  X - same as in RMatrixSolve
3428 
3429  -- ALGLIB --
3430  Copyright 27.01.2010 by Bochkanov Sergey
3431 *************************************************************************/
3432 void cmatrixlusolve(/* Complex */ ae_matrix* lua,
3433  /* Integer */ ae_vector* p,
3434  ae_int_t n,
3435  /* Complex */ ae_vector* b,
3436  ae_int_t* info,
3437  densesolverreport* rep,
3438  /* Complex */ ae_vector* x,
3439  ae_state *_state)
3440 {
3441  ae_frame _frame_block;
3442  ae_matrix bm;
3443  ae_matrix xm;
3444 
3445  ae_frame_make(_state, &_frame_block);
3446  *info = 0;
3448  ae_vector_clear(x);
3449  ae_matrix_init(&bm, 0, 0, DT_COMPLEX, _state, ae_true);
3450  ae_matrix_init(&xm, 0, 0, DT_COMPLEX, _state, ae_true);
3451 
3452  if( n<=0 )
3453  {
3454  *info = -1;
3455  ae_frame_leave(_state);
3456  return;
3457  }
3458  ae_matrix_set_length(&bm, n, 1, _state);
3459  ae_v_cmove(&bm.ptr.pp_complex[0][0], bm.stride, &b->ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
3460  cmatrixlusolvem(lua, p, n, &bm, 1, info, rep, &xm, _state);
3461  ae_vector_set_length(x, n, _state);
3462  ae_v_cmove(&x->ptr.p_complex[0], 1, &xm.ptr.pp_complex[0][0], xm.stride, "N", ae_v_len(0,n-1));
3463  ae_frame_leave(_state);
3464 }
3465 
3466 
3467 /*************************************************************************
3468 Dense solver. Same as RMatrixMixedSolveM(), but for complex matrices.
3469 
3470 Algorithm features:
3471 * automatic detection of degenerate cases
3472 * condition number estimation
3473 * iterative refinement
3474 * O(M*N^2) complexity
3475 
3476 INPUT PARAMETERS
3477  A - array[0..N-1,0..N-1], system matrix
3478  LUA - array[0..N-1,0..N-1], LU decomposition, CMatrixLU result
3479  P - array[0..N-1], pivots array, CMatrixLU result
3480  N - size of A
3481  B - array[0..N-1,0..M-1], right part
3482  M - right part size
3483 
3484 OUTPUT PARAMETERS
3485  Info - same as in RMatrixSolveM
3486  Rep - same as in RMatrixSolveM
3487  X - same as in RMatrixSolveM
3488 
3489  -- ALGLIB --
3490  Copyright 27.01.2010 by Bochkanov Sergey
3491 *************************************************************************/
3492 void cmatrixmixedsolvem(/* Complex */ ae_matrix* a,
3493  /* Complex */ ae_matrix* lua,
3494  /* Integer */ ae_vector* p,
3495  ae_int_t n,
3496  /* Complex */ ae_matrix* b,
3497  ae_int_t m,
3498  ae_int_t* info,
3499  densesolverreport* rep,
3500  /* Complex */ ae_matrix* x,
3501  ae_state *_state)
3502 {
3503  double scalea;
3504  ae_int_t i;
3505  ae_int_t j;
3506 
3507  *info = 0;
3509  ae_matrix_clear(x);
3510 
3511 
3512  /*
3513  * prepare: check inputs, allocate space...
3514  */
3515  if( n<=0||m<=0 )
3516  {
3517  *info = -1;
3518  return;
3519  }
3520 
3521  /*
3522  * 1. scale matrix, max(|A[i,j]|)
3523  * 2. factorize scaled matrix
3524  * 3. solve
3525  */
3526  scalea = 0;
3527  for(i=0; i<=n-1; i++)
3528  {
3529  for(j=0; j<=n-1; j++)
3530  {
3531  scalea = ae_maxreal(scalea, ae_c_abs(a->ptr.pp_complex[i][j], _state), _state);
3532  }
3533  }
3534  if( ae_fp_eq(scalea,0) )
3535  {
3536  scalea = 1;
3537  }
3538  scalea = 1/scalea;
3539  densesolver_cmatrixlusolveinternal(lua, p, scalea, n, a, ae_true, b, m, info, rep, x, _state);
3540 }
3541 
3542 
3543 /*************************************************************************
3544 Dense solver. Same as RMatrixMixedSolve(), but for complex matrices.
3545 
3546 Algorithm features:
3547 * automatic detection of degenerate cases
3548 * condition number estimation
3549 * iterative refinement
3550 * O(N^2) complexity
3551 
3552 INPUT PARAMETERS
3553  A - array[0..N-1,0..N-1], system matrix
3554  LUA - array[0..N-1,0..N-1], LU decomposition, CMatrixLU result
3555  P - array[0..N-1], pivots array, CMatrixLU result
3556  N - size of A
3557  B - array[0..N-1], right part
3558 
3559 OUTPUT PARAMETERS
3560  Info - same as in RMatrixSolveM
3561  Rep - same as in RMatrixSolveM
3562  X - same as in RMatrixSolveM
3563 
3564  -- ALGLIB --
3565  Copyright 27.01.2010 by Bochkanov Sergey
3566 *************************************************************************/
3567 void cmatrixmixedsolve(/* Complex */ ae_matrix* a,
3568  /* Complex */ ae_matrix* lua,
3569  /* Integer */ ae_vector* p,
3570  ae_int_t n,
3571  /* Complex */ ae_vector* b,
3572  ae_int_t* info,
3573  densesolverreport* rep,
3574  /* Complex */ ae_vector* x,
3575  ae_state *_state)
3576 {
3577  ae_frame _frame_block;
3578  ae_matrix bm;
3579  ae_matrix xm;
3580 
3581  ae_frame_make(_state, &_frame_block);
3582  *info = 0;
3584  ae_vector_clear(x);
3585  ae_matrix_init(&bm, 0, 0, DT_COMPLEX, _state, ae_true);
3586  ae_matrix_init(&xm, 0, 0, DT_COMPLEX, _state, ae_true);
3587 
3588  if( n<=0 )
3589  {
3590  *info = -1;
3591  ae_frame_leave(_state);
3592  return;
3593  }
3594  ae_matrix_set_length(&bm, n, 1, _state);
3595  ae_v_cmove(&bm.ptr.pp_complex[0][0], bm.stride, &b->ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
3596  cmatrixmixedsolvem(a, lua, p, n, &bm, 1, info, rep, &xm, _state);
3597  ae_vector_set_length(x, n, _state);
3598  ae_v_cmove(&x->ptr.p_complex[0], 1, &xm.ptr.pp_complex[0][0], xm.stride, "N", ae_v_len(0,n-1));
3599  ae_frame_leave(_state);
3600 }
3601 
3602 
3603 /*************************************************************************
3604 Dense solver. Same as RMatrixSolveM(), but for symmetric positive definite
3605 matrices.
3606 
3607 Algorithm features:
3608 * automatic detection of degenerate cases
3609 * condition number estimation
3610 * O(N^3+M*N^2) complexity
3611 * matrix is represented by its upper or lower triangle
3612 
3613 No iterative refinement is provided because such partial representation of
3614 matrix does not allow efficient calculation of extra-precise matrix-vector
3615 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
3616 need iterative refinement.
3617 
3618 INPUT PARAMETERS
3619  A - array[0..N-1,0..N-1], system matrix
3620  N - size of A
3621  IsUpper - what half of A is provided
3622  B - array[0..N-1,0..M-1], right part
3623  M - right part size
3624 
3625 OUTPUT PARAMETERS
3626  Info - same as in RMatrixSolve.
3627  Returns -3 for non-SPD matrices.
3628  Rep - same as in RMatrixSolve
3629  X - same as in RMatrixSolve
3630 
3631  -- ALGLIB --
3632  Copyright 27.01.2010 by Bochkanov Sergey
3633 *************************************************************************/
3634 void spdmatrixsolvem(/* Real */ ae_matrix* a,
3635  ae_int_t n,
3636  ae_bool isupper,
3637  /* Real */ ae_matrix* b,
3638  ae_int_t m,
3639  ae_int_t* info,
3640  densesolverreport* rep,
3641  /* Real */ ae_matrix* x,
3642  ae_state *_state)
3643 {
3644  ae_frame _frame_block;
3645  ae_matrix da;
3646  double sqrtscalea;
3647  ae_int_t i;
3648  ae_int_t j;
3649  ae_int_t j1;
3650  ae_int_t j2;
3651 
3652  ae_frame_make(_state, &_frame_block);
3653  *info = 0;
3655  ae_matrix_clear(x);
3656  ae_matrix_init(&da, 0, 0, DT_REAL, _state, ae_true);
3657 
3658 
3659  /*
3660  * prepare: check inputs, allocate space...
3661  */
3662  if( n<=0||m<=0 )
3663  {
3664  *info = -1;
3665  ae_frame_leave(_state);
3666  return;
3667  }
3668  ae_matrix_set_length(&da, n, n, _state);
3669 
3670  /*
3671  * 1. scale matrix, max(|A[i,j]|)
3672  * 2. factorize scaled matrix
3673  * 3. solve
3674  */
3675  sqrtscalea = 0;
3676  for(i=0; i<=n-1; i++)
3677  {
3678  if( isupper )
3679  {
3680  j1 = i;
3681  j2 = n-1;
3682  }
3683  else
3684  {
3685  j1 = 0;
3686  j2 = i;
3687  }
3688  for(j=j1; j<=j2; j++)
3689  {
3690  sqrtscalea = ae_maxreal(sqrtscalea, ae_fabs(a->ptr.pp_double[i][j], _state), _state);
3691  }
3692  }
3693  if( ae_fp_eq(sqrtscalea,0) )
3694  {
3695  sqrtscalea = 1;
3696  }
3697  sqrtscalea = 1/sqrtscalea;
3698  sqrtscalea = ae_sqrt(sqrtscalea, _state);
3699  for(i=0; i<=n-1; i++)
3700  {
3701  if( isupper )
3702  {
3703  j1 = i;
3704  j2 = n-1;
3705  }
3706  else
3707  {
3708  j1 = 0;
3709  j2 = i;
3710  }
3711  ae_v_move(&da.ptr.pp_double[i][j1], 1, &a->ptr.pp_double[i][j1], 1, ae_v_len(j1,j2));
3712  }
3713  if( !spdmatrixcholesky(&da, n, isupper, _state) )
3714  {
3715  ae_matrix_set_length(x, n, m, _state);
3716  for(i=0; i<=n-1; i++)
3717  {
3718  for(j=0; j<=m-1; j++)
3719  {
3720  x->ptr.pp_double[i][j] = 0;
3721  }
3722  }
3723  rep->r1 = 0;
3724  rep->rinf = 0;
3725  *info = -3;
3726  ae_frame_leave(_state);
3727  return;
3728  }
3729  *info = 1;
3730  densesolver_spdmatrixcholeskysolveinternal(&da, sqrtscalea, n, isupper, a, ae_true, b, m, info, rep, x, _state);
3731  ae_frame_leave(_state);
3732 }
3733 
3734 
3735 /*************************************************************************
3736 Dense solver. Same as RMatrixSolve(), but for SPD matrices.
3737 
3738 Algorithm features:
3739 * automatic detection of degenerate cases
3740 * condition number estimation
3741 * O(N^3) complexity
3742 * matrix is represented by its upper or lower triangle
3743 
3744 No iterative refinement is provided because such partial representation of
3745 matrix does not allow efficient calculation of extra-precise matrix-vector
3746 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
3747 need iterative refinement.
3748 
3749 INPUT PARAMETERS
3750  A - array[0..N-1,0..N-1], system matrix
3751  N - size of A
3752  IsUpper - what half of A is provided
3753  B - array[0..N-1], right part
3754 
3755 OUTPUT PARAMETERS
3756  Info - same as in RMatrixSolve
3757  Returns -3 for non-SPD matrices.
3758  Rep - same as in RMatrixSolve
3759  X - same as in RMatrixSolve
3760 
3761  -- ALGLIB --
3762  Copyright 27.01.2010 by Bochkanov Sergey
3763 *************************************************************************/
3764 void spdmatrixsolve(/* Real */ ae_matrix* a,
3765  ae_int_t n,
3766  ae_bool isupper,
3767  /* Real */ ae_vector* b,
3768  ae_int_t* info,
3769  densesolverreport* rep,
3770  /* Real */ ae_vector* x,
3771  ae_state *_state)
3772 {
3773  ae_frame _frame_block;
3774  ae_matrix bm;
3775  ae_matrix xm;
3776 
3777  ae_frame_make(_state, &_frame_block);
3778  *info = 0;
3780  ae_vector_clear(x);
3781  ae_matrix_init(&bm, 0, 0, DT_REAL, _state, ae_true);
3782  ae_matrix_init(&xm, 0, 0, DT_REAL, _state, ae_true);
3783 
3784  if( n<=0 )
3785  {
3786  *info = -1;
3787  ae_frame_leave(_state);
3788  return;
3789  }
3790  ae_matrix_set_length(&bm, n, 1, _state);
3791  ae_v_move(&bm.ptr.pp_double[0][0], bm.stride, &b->ptr.p_double[0], 1, ae_v_len(0,n-1));
3792  spdmatrixsolvem(a, n, isupper, &bm, 1, info, rep, &xm, _state);
3793  ae_vector_set_length(x, n, _state);
3794  ae_v_move(&x->ptr.p_double[0], 1, &xm.ptr.pp_double[0][0], xm.stride, ae_v_len(0,n-1));
3795  ae_frame_leave(_state);
3796 }
3797 
3798 
3799 /*************************************************************************
3800 Dense solver. Same as RMatrixLUSolveM(), but for SPD matrices represented
3801 by their Cholesky decomposition.
3802 
3803 Algorithm features:
3804 * automatic detection of degenerate cases
3805 * O(M*N^2) complexity
3806 * condition number estimation
3807 * matrix is represented by its upper or lower triangle
3808 
3809 No iterative refinement is provided because such partial representation of
3810 matrix does not allow efficient calculation of extra-precise matrix-vector
3811 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
3812 need iterative refinement.
3813 
3814 INPUT PARAMETERS
3815  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
3816  SPDMatrixCholesky result
3817  N - size of CHA
3818  IsUpper - what half of CHA is provided
3819  B - array[0..N-1,0..M-1], right part
3820  M - right part size
3821 
3822 OUTPUT PARAMETERS
3823  Info - same as in RMatrixSolve
3824  Rep - same as in RMatrixSolve
3825  X - same as in RMatrixSolve
3826 
3827  -- ALGLIB --
3828  Copyright 27.01.2010 by Bochkanov Sergey
3829 *************************************************************************/
3830 void spdmatrixcholeskysolvem(/* Real */ ae_matrix* cha,
3831  ae_int_t n,
3832  ae_bool isupper,
3833  /* Real */ ae_matrix* b,
3834  ae_int_t m,
3835  ae_int_t* info,
3836  densesolverreport* rep,
3837  /* Real */ ae_matrix* x,
3838  ae_state *_state)
3839 {
3840  ae_frame _frame_block;
3841  ae_matrix emptya;
3842  double sqrtscalea;
3843  ae_int_t i;
3844  ae_int_t j;
3845  ae_int_t j1;
3846  ae_int_t j2;
3847 
3848  ae_frame_make(_state, &_frame_block);
3849  *info = 0;
3851  ae_matrix_clear(x);
3852  ae_matrix_init(&emptya, 0, 0, DT_REAL, _state, ae_true);
3853 
3854 
3855  /*
3856  * prepare: check inputs, allocate space...
3857  */
3858  if( n<=0||m<=0 )
3859  {
3860  *info = -1;
3861  ae_frame_leave(_state);
3862  return;
3863  }
3864 
3865  /*
3866  * 1. scale matrix, max(|U[i,j]|)
3867  * 2. factorize scaled matrix
3868  * 3. solve
3869  */
3870  sqrtscalea = 0;
3871  for(i=0; i<=n-1; i++)
3872  {
3873  if( isupper )
3874  {
3875  j1 = i;
3876  j2 = n-1;
3877  }
3878  else
3879  {
3880  j1 = 0;
3881  j2 = i;
3882  }
3883  for(j=j1; j<=j2; j++)
3884  {
3885  sqrtscalea = ae_maxreal(sqrtscalea, ae_fabs(cha->ptr.pp_double[i][j], _state), _state);
3886  }
3887  }
3888  if( ae_fp_eq(sqrtscalea,0) )
3889  {
3890  sqrtscalea = 1;
3891  }
3892  sqrtscalea = 1/sqrtscalea;
3893  densesolver_spdmatrixcholeskysolveinternal(cha, sqrtscalea, n, isupper, &emptya, ae_false, b, m, info, rep, x, _state);
3894  ae_frame_leave(_state);
3895 }
3896 
3897 
3898 /*************************************************************************
3899 Dense solver. Same as RMatrixLUSolve(), but for SPD matrices represented
3900 by their Cholesky decomposition.
3901 
3902 Algorithm features:
3903 * automatic detection of degenerate cases
3904 * O(N^2) complexity
3905 * condition number estimation
3906 * matrix is represented by its upper or lower triangle
3907 
3908 No iterative refinement is provided because such partial representation of
3909 matrix does not allow efficient calculation of extra-precise matrix-vector
3910 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
3911 need iterative refinement.
3912 
3913 INPUT PARAMETERS
3914  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
3915  SPDMatrixCholesky result
3916  N - size of A
3917  IsUpper - what half of CHA is provided
3918  B - array[0..N-1], right part
3919 
3920 OUTPUT PARAMETERS
3921  Info - same as in RMatrixSolve
3922  Rep - same as in RMatrixSolve
3923  X - same as in RMatrixSolve
3924 
3925  -- ALGLIB --
3926  Copyright 27.01.2010 by Bochkanov Sergey
3927 *************************************************************************/
3928 void spdmatrixcholeskysolve(/* Real */ ae_matrix* cha,
3929  ae_int_t n,
3930  ae_bool isupper,
3931  /* Real */ ae_vector* b,
3932  ae_int_t* info,
3933  densesolverreport* rep,
3934  /* Real */ ae_vector* x,
3935  ae_state *_state)
3936 {
3937  ae_frame _frame_block;
3938  ae_matrix bm;
3939  ae_matrix xm;
3940 
3941  ae_frame_make(_state, &_frame_block);
3942  *info = 0;
3944  ae_vector_clear(x);
3945  ae_matrix_init(&bm, 0, 0, DT_REAL, _state, ae_true);
3946  ae_matrix_init(&xm, 0, 0, DT_REAL, _state, ae_true);
3947 
3948  if( n<=0 )
3949  {
3950  *info = -1;
3951  ae_frame_leave(_state);
3952  return;
3953  }
3954  ae_matrix_set_length(&bm, n, 1, _state);
3955  ae_v_move(&bm.ptr.pp_double[0][0], bm.stride, &b->ptr.p_double[0], 1, ae_v_len(0,n-1));
3956  spdmatrixcholeskysolvem(cha, n, isupper, &bm, 1, info, rep, &xm, _state);
3957  ae_vector_set_length(x, n, _state);
3958  ae_v_move(&x->ptr.p_double[0], 1, &xm.ptr.pp_double[0][0], xm.stride, ae_v_len(0,n-1));
3959  ae_frame_leave(_state);
3960 }
3961 
3962 
3963 /*************************************************************************
3964 Dense solver. Same as RMatrixSolveM(), but for Hermitian positive definite
3965 matrices.
3966 
3967 Algorithm features:
3968 * automatic detection of degenerate cases
3969 * condition number estimation
3970 * O(N^3+M*N^2) complexity
3971 * matrix is represented by its upper or lower triangle
3972 
3973 No iterative refinement is provided because such partial representation of
3974 matrix does not allow efficient calculation of extra-precise matrix-vector
3975 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
3976 need iterative refinement.
3977 
3978 INPUT PARAMETERS
3979  A - array[0..N-1,0..N-1], system matrix
3980  N - size of A
3981  IsUpper - what half of A is provided
3982  B - array[0..N-1,0..M-1], right part
3983  M - right part size
3984 
3985 OUTPUT PARAMETERS
3986  Info - same as in RMatrixSolve.
3987  Returns -3 for non-HPD matrices.
3988  Rep - same as in RMatrixSolve
3989  X - same as in RMatrixSolve
3990 
3991  -- ALGLIB --
3992  Copyright 27.01.2010 by Bochkanov Sergey
3993 *************************************************************************/
3994 void hpdmatrixsolvem(/* Complex */ ae_matrix* a,
3995  ae_int_t n,
3996  ae_bool isupper,
3997  /* Complex */ ae_matrix* b,
3998  ae_int_t m,
3999  ae_int_t* info,
4000  densesolverreport* rep,
4001  /* Complex */ ae_matrix* x,
4002  ae_state *_state)
4003 {
4004  ae_frame _frame_block;
4005  ae_matrix da;
4006  double sqrtscalea;
4007  ae_int_t i;
4008  ae_int_t j;
4009  ae_int_t j1;
4010  ae_int_t j2;
4011 
4012  ae_frame_make(_state, &_frame_block);
4013  *info = 0;
4015  ae_matrix_clear(x);
4016  ae_matrix_init(&da, 0, 0, DT_COMPLEX, _state, ae_true);
4017 
4018 
4019  /*
4020  * prepare: check inputs, allocate space...
4021  */
4022  if( n<=0||m<=0 )
4023  {
4024  *info = -1;
4025  ae_frame_leave(_state);
4026  return;
4027  }
4028  ae_matrix_set_length(&da, n, n, _state);
4029 
4030  /*
4031  * 1. scale matrix, max(|A[i,j]|)
4032  * 2. factorize scaled matrix
4033  * 3. solve
4034  */
4035  sqrtscalea = 0;
4036  for(i=0; i<=n-1; i++)
4037  {
4038  if( isupper )
4039  {
4040  j1 = i;
4041  j2 = n-1;
4042  }
4043  else
4044  {
4045  j1 = 0;
4046  j2 = i;
4047  }
4048  for(j=j1; j<=j2; j++)
4049  {
4050  sqrtscalea = ae_maxreal(sqrtscalea, ae_c_abs(a->ptr.pp_complex[i][j], _state), _state);
4051  }
4052  }
4053  if( ae_fp_eq(sqrtscalea,0) )
4054  {
4055  sqrtscalea = 1;
4056  }
4057  sqrtscalea = 1/sqrtscalea;
4058  sqrtscalea = ae_sqrt(sqrtscalea, _state);
4059  for(i=0; i<=n-1; i++)
4060  {
4061  if( isupper )
4062  {
4063  j1 = i;
4064  j2 = n-1;
4065  }
4066  else
4067  {
4068  j1 = 0;
4069  j2 = i;
4070  }
4071  ae_v_cmove(&da.ptr.pp_complex[i][j1], 1, &a->ptr.pp_complex[i][j1], 1, "N", ae_v_len(j1,j2));
4072  }
4073  if( !hpdmatrixcholesky(&da, n, isupper, _state) )
4074  {
4075  ae_matrix_set_length(x, n, m, _state);
4076  for(i=0; i<=n-1; i++)
4077  {
4078  for(j=0; j<=m-1; j++)
4079  {
4080  x->ptr.pp_complex[i][j] = ae_complex_from_d(0);
4081  }
4082  }
4083  rep->r1 = 0;
4084  rep->rinf = 0;
4085  *info = -3;
4086  ae_frame_leave(_state);
4087  return;
4088  }
4089  *info = 1;
4090  densesolver_hpdmatrixcholeskysolveinternal(&da, sqrtscalea, n, isupper, a, ae_true, b, m, info, rep, x, _state);
4091  ae_frame_leave(_state);
4092 }
4093 
4094 
4095 /*************************************************************************
4096 Dense solver. Same as RMatrixSolve(), but for Hermitian positive definite
4097 matrices.
4098 
4099 Algorithm features:
4100 * automatic detection of degenerate cases
4101 * condition number estimation
4102 * O(N^3) complexity
4103 * matrix is represented by its upper or lower triangle
4104 
4105 No iterative refinement is provided because such partial representation of
4106 matrix does not allow efficient calculation of extra-precise matrix-vector
4107 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
4108 need iterative refinement.
4109 
4110 INPUT PARAMETERS
4111  A - array[0..N-1,0..N-1], system matrix
4112  N - size of A
4113  IsUpper - what half of A is provided
4114  B - array[0..N-1], right part
4115 
4116 OUTPUT PARAMETERS
4117  Info - same as in RMatrixSolve
4118  Returns -3 for non-HPD matrices.
4119  Rep - same as in RMatrixSolve
4120  X - same as in RMatrixSolve
4121 
4122  -- ALGLIB --
4123  Copyright 27.01.2010 by Bochkanov Sergey
4124 *************************************************************************/
4125 void hpdmatrixsolve(/* Complex */ ae_matrix* a,
4126  ae_int_t n,
4127  ae_bool isupper,
4128  /* Complex */ ae_vector* b,
4129  ae_int_t* info,
4130  densesolverreport* rep,
4131  /* Complex */ ae_vector* x,
4132  ae_state *_state)
4133 {
4134  ae_frame _frame_block;
4135  ae_matrix bm;
4136  ae_matrix xm;
4137 
4138  ae_frame_make(_state, &_frame_block);
4139  *info = 0;
4141  ae_vector_clear(x);
4142  ae_matrix_init(&bm, 0, 0, DT_COMPLEX, _state, ae_true);
4143  ae_matrix_init(&xm, 0, 0, DT_COMPLEX, _state, ae_true);
4144 
4145  if( n<=0 )
4146  {
4147  *info = -1;
4148  ae_frame_leave(_state);
4149  return;
4150  }
4151  ae_matrix_set_length(&bm, n, 1, _state);
4152  ae_v_cmove(&bm.ptr.pp_complex[0][0], bm.stride, &b->ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
4153  hpdmatrixsolvem(a, n, isupper, &bm, 1, info, rep, &xm, _state);
4154  ae_vector_set_length(x, n, _state);
4155  ae_v_cmove(&x->ptr.p_complex[0], 1, &xm.ptr.pp_complex[0][0], xm.stride, "N", ae_v_len(0,n-1));
4156  ae_frame_leave(_state);
4157 }
4158 
4159 
4160 /*************************************************************************
4161 Dense solver. Same as RMatrixLUSolveM(), but for HPD matrices represented
4162 by their Cholesky decomposition.
4163 
4164 Algorithm features:
4165 * automatic detection of degenerate cases
4166 * O(M*N^2) complexity
4167 * condition number estimation
4168 * matrix is represented by its upper or lower triangle
4169 
4170 No iterative refinement is provided because such partial representation of
4171 matrix does not allow efficient calculation of extra-precise matrix-vector
4172 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
4173 need iterative refinement.
4174 
4175 INPUT PARAMETERS
4176  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
4177  HPDMatrixCholesky result
4178  N - size of CHA
4179  IsUpper - what half of CHA is provided
4180  B - array[0..N-1,0..M-1], right part
4181  M - right part size
4182 
4183 OUTPUT PARAMETERS
4184  Info - same as in RMatrixSolve
4185  Rep - same as in RMatrixSolve
4186  X - same as in RMatrixSolve
4187 
4188  -- ALGLIB --
4189  Copyright 27.01.2010 by Bochkanov Sergey
4190 *************************************************************************/
4191 void hpdmatrixcholeskysolvem(/* Complex */ ae_matrix* cha,
4192  ae_int_t n,
4193  ae_bool isupper,
4194  /* Complex */ ae_matrix* b,
4195  ae_int_t m,
4196  ae_int_t* info,
4197  densesolverreport* rep,
4198  /* Complex */ ae_matrix* x,
4199  ae_state *_state)
4200 {
4201  ae_frame _frame_block;
4202  ae_matrix emptya;
4203  double sqrtscalea;
4204  ae_int_t i;
4205  ae_int_t j;
4206  ae_int_t j1;
4207  ae_int_t j2;
4208 
4209  ae_frame_make(_state, &_frame_block);
4210  *info = 0;
4212  ae_matrix_clear(x);
4213  ae_matrix_init(&emptya, 0, 0, DT_COMPLEX, _state, ae_true);
4214 
4215 
4216  /*
4217  * prepare: check inputs, allocate space...
4218  */
4219  if( n<=0||m<=0 )
4220  {
4221  *info = -1;
4222  ae_frame_leave(_state);
4223  return;
4224  }
4225 
4226  /*
4227  * 1. scale matrix, max(|U[i,j]|)
4228  * 2. factorize scaled matrix
4229  * 3. solve
4230  */
4231  sqrtscalea = 0;
4232  for(i=0; i<=n-1; i++)
4233  {
4234  if( isupper )
4235  {
4236  j1 = i;
4237  j2 = n-1;
4238  }
4239  else
4240  {
4241  j1 = 0;
4242  j2 = i;
4243  }
4244  for(j=j1; j<=j2; j++)
4245  {
4246  sqrtscalea = ae_maxreal(sqrtscalea, ae_c_abs(cha->ptr.pp_complex[i][j], _state), _state);
4247  }
4248  }
4249  if( ae_fp_eq(sqrtscalea,0) )
4250  {
4251  sqrtscalea = 1;
4252  }
4253  sqrtscalea = 1/sqrtscalea;
4254  densesolver_hpdmatrixcholeskysolveinternal(cha, sqrtscalea, n, isupper, &emptya, ae_false, b, m, info, rep, x, _state);
4255  ae_frame_leave(_state);
4256 }
4257 
4258 
4259 /*************************************************************************
4260 Dense solver. Same as RMatrixLUSolve(), but for HPD matrices represented
4261 by their Cholesky decomposition.
4262 
4263 Algorithm features:
4264 * automatic detection of degenerate cases
4265 * O(N^2) complexity
4266 * condition number estimation
4267 * matrix is represented by its upper or lower triangle
4268 
4269 No iterative refinement is provided because such partial representation of
4270 matrix does not allow efficient calculation of extra-precise matrix-vector
4271 products for large matrices. Use RMatrixSolve or RMatrixMixedSolve if you
4272 need iterative refinement.
4273 
4274 INPUT PARAMETERS
4275  CHA - array[0..N-1,0..N-1], Cholesky decomposition,
4276  SPDMatrixCholesky result
4277  N - size of A
4278  IsUpper - what half of CHA is provided
4279  B - array[0..N-1], right part
4280 
4281 OUTPUT PARAMETERS
4282  Info - same as in RMatrixSolve
4283  Rep - same as in RMatrixSolve
4284  X - same as in RMatrixSolve
4285 
4286  -- ALGLIB --
4287  Copyright 27.01.2010 by Bochkanov Sergey
4288 *************************************************************************/
4289 void hpdmatrixcholeskysolve(/* Complex */ ae_matrix* cha,
4290  ae_int_t n,
4291  ae_bool isupper,
4292  /* Complex */ ae_vector* b,
4293  ae_int_t* info,
4294  densesolverreport* rep,
4295  /* Complex */ ae_vector* x,
4296  ae_state *_state)
4297 {
4298  ae_frame _frame_block;
4299  ae_matrix bm;
4300  ae_matrix xm;
4301 
4302  ae_frame_make(_state, &_frame_block);
4303  *info = 0;
4305  ae_vector_clear(x);
4306  ae_matrix_init(&bm, 0, 0, DT_COMPLEX, _state, ae_true);
4307  ae_matrix_init(&xm, 0, 0, DT_COMPLEX, _state, ae_true);
4308 
4309  if( n<=0 )
4310  {
4311  *info = -1;
4312  ae_frame_leave(_state);
4313  return;
4314  }
4315  ae_matrix_set_length(&bm, n, 1, _state);
4316  ae_v_cmove(&bm.ptr.pp_complex[0][0], bm.stride, &b->ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
4317  hpdmatrixcholeskysolvem(cha, n, isupper, &bm, 1, info, rep, &xm, _state);
4318  ae_vector_set_length(x, n, _state);
4319  ae_v_cmove(&x->ptr.p_complex[0], 1, &xm.ptr.pp_complex[0][0], xm.stride, "N", ae_v_len(0,n-1));
4320  ae_frame_leave(_state);
4321 }
4322 
4323 
4324 /*************************************************************************
4325 Dense solver.
4326 
4327 This subroutine finds solution of the linear system A*X=B with non-square,
4328 possibly degenerate A. System is solved in the least squares sense, and
4329 general least squares solution X = X0 + CX*y which minimizes |A*X-B| is
4330 returned. If A is non-degenerate, solution in the usual sense is returned.
4331 
4332 Algorithm features:
4333 * automatic detection (and correct handling!) of degenerate cases
4334 * iterative refinement
4335 * O(N^3) complexity
4336 
4337 INPUT PARAMETERS
4338  A - array[0..NRows-1,0..NCols-1], system matrix
4339  NRows - vertical size of A
4340  NCols - horizontal size of A
4341  B - array[0..NCols-1], right part
4342  Threshold- a number in [0,1]. Singular values beyond Threshold are
4343  considered zero. Set it to 0.0, if you don't understand
4344  what it means, so the solver will choose good value on its
4345  own.
4346 
4347 OUTPUT PARAMETERS
4348  Info - return code:
4349  * -4 SVD subroutine failed
4350  * -1 if NRows<=0 or NCols<=0 or Threshold<0 was passed
4351  * 1 if task is solved
4352  Rep - solver report, see below for more info
4353  X - array[0..N-1,0..M-1], it contains:
4354  * solution of A*X=B (even for singular A)
4355  * zeros, if SVD subroutine failed
4356 
4357 SOLVER REPORT
4358 
4359 Subroutine sets following fields of the Rep structure:
4360 * R2 reciprocal of condition number: 1/cond(A), 2-norm.
4361 * N = NCols
4362 * K dim(Null(A))
4363 * CX array[0..N-1,0..K-1], kernel of A.
4364  Columns of CX store such vectors that A*CX[i]=0.
4365 
4366  -- ALGLIB --
4367  Copyright 24.08.2009 by Bochkanov Sergey
4368 *************************************************************************/
4369 void rmatrixsolvels(/* Real */ ae_matrix* a,
4370  ae_int_t nrows,
4371  ae_int_t ncols,
4372  /* Real */ ae_vector* b,
4373  double threshold,
4374  ae_int_t* info,
4375  densesolverlsreport* rep,
4376  /* Real */ ae_vector* x,
4377  ae_state *_state)
4378 {
4379  ae_frame _frame_block;
4380  ae_vector sv;
4381  ae_matrix u;
4382  ae_matrix vt;
4383  ae_vector rp;
4384  ae_vector utb;
4385  ae_vector sutb;
4386  ae_vector tmp;
4387  ae_vector ta;
4388  ae_vector tx;
4389  ae_vector buf;
4390  ae_vector w;
4391  ae_int_t i;
4392  ae_int_t j;
4393  ae_int_t nsv;
4394  ae_int_t kernelidx;
4395  double v;
4396  double verr;
4397  ae_bool svdfailed;
4398  ae_bool zeroa;
4399  ae_int_t rfs;
4400  ae_int_t nrfs;
4401  ae_bool terminatenexttime;
4402  ae_bool smallerr;
4403 
4404  ae_frame_make(_state, &_frame_block);
4405  *info = 0;
4407  ae_vector_clear(x);
4408  ae_vector_init(&sv, 0, DT_REAL, _state, ae_true);
4409  ae_matrix_init(&u, 0, 0, DT_REAL, _state, ae_true);
4410  ae_matrix_init(&vt, 0, 0, DT_REAL, _state, ae_true);
4411  ae_vector_init(&rp, 0, DT_REAL, _state, ae_true);
4412  ae_vector_init(&utb, 0, DT_REAL, _state, ae_true);
4413  ae_vector_init(&sutb, 0, DT_REAL, _state, ae_true);
4414  ae_vector_init(&tmp, 0, DT_REAL, _state, ae_true);
4415  ae_vector_init(&ta, 0, DT_REAL, _state, ae_true);
4416  ae_vector_init(&tx, 0, DT_REAL, _state, ae_true);
4417  ae_vector_init(&buf, 0, DT_REAL, _state, ae_true);
4418  ae_vector_init(&w, 0, DT_REAL, _state, ae_true);
4419 
4420  if( (nrows<=0||ncols<=0)||ae_fp_less(threshold,0) )
4421  {
4422  *info = -1;
4423  ae_frame_leave(_state);
4424  return;
4425  }
4426  if( ae_fp_eq(threshold,0) )
4427  {
4428  threshold = 1000*ae_machineepsilon;
4429  }
4430 
4431  /*
4432  * Factorize A first
4433  */
4434  svdfailed = !rmatrixsvd(a, nrows, ncols, 1, 2, 2, &sv, &u, &vt, _state);
4435  zeroa = ae_fp_eq(sv.ptr.p_double[0],0);
4436  if( svdfailed||zeroa )
4437  {
4438  if( svdfailed )
4439  {
4440  *info = -4;
4441  }
4442  else
4443  {
4444  *info = 1;
4445  }
4446  ae_vector_set_length(x, ncols, _state);
4447  for(i=0; i<=ncols-1; i++)
4448  {
4449  x->ptr.p_double[i] = 0;
4450  }
4451  rep->n = ncols;
4452  rep->k = ncols;
4453  ae_matrix_set_length(&rep->cx, ncols, ncols, _state);
4454  for(i=0; i<=ncols-1; i++)
4455  {
4456  for(j=0; j<=ncols-1; j++)
4457  {
4458  if( i==j )
4459  {
4460  rep->cx.ptr.pp_double[i][j] = 1;
4461  }
4462  else
4463  {
4464  rep->cx.ptr.pp_double[i][j] = 0;
4465  }
4466  }
4467  }
4468  rep->r2 = 0;
4469  ae_frame_leave(_state);
4470  return;
4471  }
4472  nsv = ae_minint(ncols, nrows, _state);
4473  if( nsv==ncols )
4474  {
4475  rep->r2 = sv.ptr.p_double[nsv-1]/sv.ptr.p_double[0];
4476  }
4477  else
4478  {
4479  rep->r2 = 0;
4480  }
4481  rep->n = ncols;
4482  *info = 1;
4483 
4484  /*
4485  * Iterative refinement of xc combined with solution:
4486  * 1. xc = 0
4487  * 2. calculate r = bc-A*xc using extra-precise dot product
4488  * 3. solve A*y = r
4489  * 4. update x:=x+r
4490  * 5. goto 2
4491  *
4492  * This cycle is executed until one of two things happens:
4493  * 1. maximum number of iterations reached
4494  * 2. last iteration decreased error to the lower limit
4495  */
4496  ae_vector_set_length(&utb, nsv, _state);
4497  ae_vector_set_length(&sutb, nsv, _state);
4498  ae_vector_set_length(x, ncols, _state);
4499  ae_vector_set_length(&tmp, ncols, _state);
4500  ae_vector_set_length(&ta, ncols+1, _state);
4501  ae_vector_set_length(&tx, ncols+1, _state);
4502  ae_vector_set_length(&buf, ncols+1, _state);
4503  for(i=0; i<=ncols-1; i++)
4504  {
4505  x->ptr.p_double[i] = 0;
4506  }
4507  kernelidx = nsv;
4508  for(i=0; i<=nsv-1; i++)
4509  {
4510  if( ae_fp_less_eq(sv.ptr.p_double[i],threshold*sv.ptr.p_double[0]) )
4511  {
4512  kernelidx = i;
4513  break;
4514  }
4515  }
4516  rep->k = ncols-kernelidx;
4517  nrfs = densesolver_densesolverrfsmaxv2(ncols, rep->r2, _state);
4518  terminatenexttime = ae_false;
4519  ae_vector_set_length(&rp, nrows, _state);
4520  for(rfs=0; rfs<=nrfs; rfs++)
4521  {
4522  if( terminatenexttime )
4523  {
4524  break;
4525  }
4526 
4527  /*
4528  * calculate right part
4529  */
4530  if( rfs==0 )
4531  {
4532  ae_v_move(&rp.ptr.p_double[0], 1, &b->ptr.p_double[0], 1, ae_v_len(0,nrows-1));
4533  }
4534  else
4535  {
4536  smallerr = ae_true;
4537  for(i=0; i<=nrows-1; i++)
4538  {
4539  ae_v_move(&ta.ptr.p_double[0], 1, &a->ptr.pp_double[i][0], 1, ae_v_len(0,ncols-1));
4540  ta.ptr.p_double[ncols] = -1;
4541  ae_v_move(&tx.ptr.p_double[0], 1, &x->ptr.p_double[0], 1, ae_v_len(0,ncols-1));
4542  tx.ptr.p_double[ncols] = b->ptr.p_double[i];
4543  xdot(&ta, &tx, ncols+1, &buf, &v, &verr, _state);
4544  rp.ptr.p_double[i] = -v;
4545  smallerr = smallerr&&ae_fp_less(ae_fabs(v, _state),4*verr);
4546  }
4547  if( smallerr )
4548  {
4549  terminatenexttime = ae_true;
4550  }
4551  }
4552 
4553  /*
4554  * solve A*dx = rp
4555  */
4556  for(i=0; i<=ncols-1; i++)
4557  {
4558  tmp.ptr.p_double[i] = 0;
4559  }
4560  for(i=0; i<=nsv-1; i++)
4561  {
4562  utb.ptr.p_double[i] = 0;
4563  }
4564  for(i=0; i<=nrows-1; i++)
4565  {
4566  v = rp.ptr.p_double[i];
4567  ae_v_addd(&utb.ptr.p_double[0], 1, &u.ptr.pp_double[i][0], 1, ae_v_len(0,nsv-1), v);
4568  }
4569  for(i=0; i<=nsv-1; i++)
4570  {
4571  if( i<kernelidx )
4572  {
4573  sutb.ptr.p_double[i] = utb.ptr.p_double[i]/sv.ptr.p_double[i];
4574  }
4575  else
4576  {
4577  sutb.ptr.p_double[i] = 0;
4578  }
4579  }
4580  for(i=0; i<=nsv-1; i++)
4581  {
4582  v = sutb.ptr.p_double[i];
4583  ae_v_addd(&tmp.ptr.p_double[0], 1, &vt.ptr.pp_double[i][0], 1, ae_v_len(0,ncols-1), v);
4584  }
4585 
4586  /*
4587  * update x: x:=x+dx
4588  */
4589  ae_v_add(&x->ptr.p_double[0], 1, &tmp.ptr.p_double[0], 1, ae_v_len(0,ncols-1));
4590  }
4591 
4592  /*
4593  * fill CX
4594  */
4595  if( rep->k>0 )
4596  {
4597  ae_matrix_set_length(&rep->cx, ncols, rep->k, _state);
4598  for(i=0; i<=rep->k-1; i++)
4599  {
4600  ae_v_move(&rep->cx.ptr.pp_double[0][i], rep->cx.stride, &vt.ptr.pp_double[kernelidx+i][0], 1, ae_v_len(0,ncols-1));
4601  }
4602  }
4603  ae_frame_leave(_state);
4604 }
4605 
4606 
4607 /*************************************************************************
4608 Internal LU solver
4609 
4610  -- ALGLIB --
4611  Copyright 27.01.2010 by Bochkanov Sergey
4612 *************************************************************************/
4613 static void densesolver_rmatrixlusolveinternal(/* Real */ ae_matrix* lua,
4614  /* Integer */ ae_vector* p,
4615  double scalea,
4616  ae_int_t n,
4617  /* Real */ ae_matrix* a,
4618  ae_bool havea,
4619  /* Real */ ae_matrix* b,
4620  ae_int_t m,
4621  ae_int_t* info,
4622  densesolverreport* rep,
4623  /* Real */ ae_matrix* x,
4624  ae_state *_state)
4625 {
4626  ae_frame _frame_block;
4627  ae_int_t i;
4628  ae_int_t j;
4629  ae_int_t k;
4630  ae_int_t rfs;
4631  ae_int_t nrfs;
4632  ae_vector xc;
4633  ae_vector y;
4634  ae_vector bc;
4635  ae_vector xa;
4636  ae_vector xb;
4637  ae_vector tx;
4638  double v;
4639  double verr;
4640  double mxb;
4641  double scaleright;
4642  ae_bool smallerr;
4643  ae_bool terminatenexttime;
4644 
4645  ae_frame_make(_state, &_frame_block);
4646  *info = 0;
4648  ae_matrix_clear(x);
4649  ae_vector_init(&xc, 0, DT_REAL, _state, ae_true);
4650  ae_vector_init(&y, 0, DT_REAL, _state, ae_true);
4651  ae_vector_init(&bc, 0, DT_REAL, _state, ae_true);
4652  ae_vector_init(&xa, 0, DT_REAL, _state, ae_true);
4653  ae_vector_init(&xb, 0, DT_REAL, _state, ae_true);
4654  ae_vector_init(&tx, 0, DT_REAL, _state, ae_true);
4655 
4656  ae_assert(ae_fp_greater(scalea,0), "Assertion failed", _state);
4657 
4658  /*
4659  * prepare: check inputs, allocate space...
4660  */
4661  if( n<=0||m<=0 )
4662  {
4663  *info = -1;
4664  ae_frame_leave(_state);
4665  return;
4666  }
4667  for(i=0; i<=n-1; i++)
4668  {
4669  if( p->ptr.p_int[i]>n-1||p->ptr.p_int[i]<i )
4670  {
4671  *info = -1;
4672  ae_frame_leave(_state);
4673  return;
4674  }
4675  }
4676  ae_matrix_set_length(x, n, m, _state);
4677  ae_vector_set_length(&y, n, _state);
4678  ae_vector_set_length(&xc, n, _state);
4679  ae_vector_set_length(&bc, n, _state);
4680  ae_vector_set_length(&tx, n+1, _state);
4681  ae_vector_set_length(&xa, n+1, _state);
4682  ae_vector_set_length(&xb, n+1, _state);
4683 
4684  /*
4685  * estimate condition number, test for near singularity
4686  */
4687  rep->r1 = rmatrixlurcond1(lua, n, _state);
4688  rep->rinf = rmatrixlurcondinf(lua, n, _state);
4689  if( ae_fp_less(rep->r1,rcondthreshold(_state))||ae_fp_less(rep->rinf,rcondthreshold(_state)) )
4690  {
4691  for(i=0; i<=n-1; i++)
4692  {
4693  for(j=0; j<=m-1; j++)
4694  {
4695  x->ptr.pp_double[i][j] = 0;
4696  }
4697  }
4698  rep->r1 = 0;
4699  rep->rinf = 0;
4700  *info = -3;
4701  ae_frame_leave(_state);
4702  return;
4703  }
4704  *info = 1;
4705 
4706  /*
4707  * solve
4708  */
4709  for(k=0; k<=m-1; k++)
4710  {
4711 
4712  /*
4713  * copy B to contiguous storage
4714  */
4715  ae_v_move(&bc.ptr.p_double[0], 1, &b->ptr.pp_double[0][k], b->stride, ae_v_len(0,n-1));
4716 
4717  /*
4718  * Scale right part:
4719  * * MX stores max(|Bi|)
4720  * * ScaleRight stores actual scaling applied to B when solving systems
4721  * it is chosen to make |scaleRight*b| close to 1.
4722  */
4723  mxb = 0;
4724  for(i=0; i<=n-1; i++)
4725  {
4726  mxb = ae_maxreal(mxb, ae_fabs(bc.ptr.p_double[i], _state), _state);
4727  }
4728  if( ae_fp_eq(mxb,0) )
4729  {
4730  mxb = 1;
4731  }
4732  scaleright = 1/mxb;
4733 
4734  /*
4735  * First, non-iterative part of solution process.
4736  * We use separate code for this task because
4737  * XDot is quite slow and we want to save time.
4738  */
4739  ae_v_moved(&xc.ptr.p_double[0], 1, &bc.ptr.p_double[0], 1, ae_v_len(0,n-1), scaleright);
4740  densesolver_rbasiclusolve(lua, p, scalea, n, &xc, &tx, _state);
4741 
4742  /*
4743  * Iterative refinement of xc:
4744  * * calculate r = bc-A*xc using extra-precise dot product
4745  * * solve A*y = r
4746  * * update x:=x+r
4747  *
4748  * This cycle is executed until one of two things happens:
4749  * 1. maximum number of iterations reached
4750  * 2. last iteration decreased error to the lower limit
4751  */
4752  if( havea )
4753  {
4754  nrfs = densesolver_densesolverrfsmax(n, rep->r1, rep->rinf, _state);
4755  terminatenexttime = ae_false;
4756  for(rfs=0; rfs<=nrfs-1; rfs++)
4757  {
4758  if( terminatenexttime )
4759  {
4760  break;
4761  }
4762 
4763  /*
4764  * generate right part
4765  */
4766  smallerr = ae_true;
4767  ae_v_move(&xb.ptr.p_double[0], 1, &xc.ptr.p_double[0], 1, ae_v_len(0,n-1));
4768  for(i=0; i<=n-1; i++)
4769  {
4770  ae_v_moved(&xa.ptr.p_double[0], 1, &a->ptr.pp_double[i][0], 1, ae_v_len(0,n-1), scalea);
4771  xa.ptr.p_double[n] = -1;
4772  xb.ptr.p_double[n] = scaleright*bc.ptr.p_double[i];
4773  xdot(&xa, &xb, n+1, &tx, &v, &verr, _state);
4774  y.ptr.p_double[i] = -v;
4775  smallerr = smallerr&&ae_fp_less(ae_fabs(v, _state),4*verr);
4776  }
4777  if( smallerr )
4778  {
4779  terminatenexttime = ae_true;
4780  }
4781 
4782  /*
4783  * solve and update
4784  */
4785  densesolver_rbasiclusolve(lua, p, scalea, n, &y, &tx, _state);
4786  ae_v_add(&xc.ptr.p_double[0], 1, &y.ptr.p_double[0], 1, ae_v_len(0,n-1));
4787  }
4788  }
4789 
4790  /*
4791  * Store xc.
4792  * Post-scale result.
4793  */
4794  v = scalea*mxb;
4795  ae_v_moved(&x->ptr.pp_double[0][k], x->stride, &xc.ptr.p_double[0], 1, ae_v_len(0,n-1), v);
4796  }
4797  ae_frame_leave(_state);
4798 }
4799 
4800 
4801 /*************************************************************************
4802 Internal Cholesky solver
4803 
4804  -- ALGLIB --
4805  Copyright 27.01.2010 by Bochkanov Sergey
4806 *************************************************************************/
4807 static void densesolver_spdmatrixcholeskysolveinternal(/* Real */ ae_matrix* cha,
4808  double sqrtscalea,
4809  ae_int_t n,
4810  ae_bool isupper,
4811  /* Real */ ae_matrix* a,
4812  ae_bool havea,
4813  /* Real */ ae_matrix* b,
4814  ae_int_t m,
4815  ae_int_t* info,
4816  densesolverreport* rep,
4817  /* Real */ ae_matrix* x,
4818  ae_state *_state)
4819 {
4820  ae_frame _frame_block;
4821  ae_int_t i;
4822  ae_int_t j;
4823  ae_int_t k;
4824  ae_vector xc;
4825  ae_vector y;
4826  ae_vector bc;
4827  ae_vector xa;
4828  ae_vector xb;
4829  ae_vector tx;
4830  double v;
4831  double mxb;
4832  double scaleright;
4833 
4834  ae_frame_make(_state, &_frame_block);
4835  *info = 0;
4837  ae_matrix_clear(x);
4838  ae_vector_init(&xc, 0, DT_REAL, _state, ae_true);
4839  ae_vector_init(&y, 0, DT_REAL, _state, ae_true);
4840  ae_vector_init(&bc, 0, DT_REAL, _state, ae_true);
4841  ae_vector_init(&xa, 0, DT_REAL, _state, ae_true);
4842  ae_vector_init(&xb, 0, DT_REAL, _state, ae_true);
4843  ae_vector_init(&tx, 0, DT_REAL, _state, ae_true);
4844 
4845  ae_assert(ae_fp_greater(sqrtscalea,0), "Assertion failed", _state);
4846 
4847  /*
4848  * prepare: check inputs, allocate space...
4849  */
4850  if( n<=0||m<=0 )
4851  {
4852  *info = -1;
4853  ae_frame_leave(_state);
4854  return;
4855  }
4856  ae_matrix_set_length(x, n, m, _state);
4857  ae_vector_set_length(&y, n, _state);
4858  ae_vector_set_length(&xc, n, _state);
4859  ae_vector_set_length(&bc, n, _state);
4860  ae_vector_set_length(&tx, n+1, _state);
4861  ae_vector_set_length(&xa, n+1, _state);
4862  ae_vector_set_length(&xb, n+1, _state);
4863 
4864  /*
4865  * estimate condition number, test for near singularity
4866  */
4867  rep->r1 = spdmatrixcholeskyrcond(cha, n, isupper, _state);
4868  rep->rinf = rep->r1;
4869  if( ae_fp_less(rep->r1,rcondthreshold(_state)) )
4870  {
4871  for(i=0; i<=n-1; i++)
4872  {
4873  for(j=0; j<=m-1; j++)
4874  {
4875  x->ptr.pp_double[i][j] = 0;
4876  }
4877  }
4878  rep->r1 = 0;
4879  rep->rinf = 0;
4880  *info = -3;
4881  ae_frame_leave(_state);
4882  return;
4883  }
4884  *info = 1;
4885 
4886  /*
4887  * solve
4888  */
4889  for(k=0; k<=m-1; k++)
4890  {
4891 
4892  /*
4893  * copy B to contiguous storage
4894  */
4895  ae_v_move(&bc.ptr.p_double[0], 1, &b->ptr.pp_double[0][k], b->stride, ae_v_len(0,n-1));
4896 
4897  /*
4898  * Scale right part:
4899  * * MX stores max(|Bi|)
4900  * * ScaleRight stores actual scaling applied to B when solving systems
4901  * it is chosen to make |scaleRight*b| close to 1.
4902  */
4903  mxb = 0;
4904  for(i=0; i<=n-1; i++)
4905  {
4906  mxb = ae_maxreal(mxb, ae_fabs(bc.ptr.p_double[i], _state), _state);
4907  }
4908  if( ae_fp_eq(mxb,0) )
4909  {
4910  mxb = 1;
4911  }
4912  scaleright = 1/mxb;
4913 
4914  /*
4915  * First, non-iterative part of solution process.
4916  * We use separate code for this task because
4917  * XDot is quite slow and we want to save time.
4918  */
4919  ae_v_moved(&xc.ptr.p_double[0], 1, &bc.ptr.p_double[0], 1, ae_v_len(0,n-1), scaleright);
4920  densesolver_spdbasiccholeskysolve(cha, sqrtscalea, n, isupper, &xc, &tx, _state);
4921 
4922  /*
4923  * Store xc.
4924  * Post-scale result.
4925  */
4926  v = ae_sqr(sqrtscalea, _state)*mxb;
4927  ae_v_moved(&x->ptr.pp_double[0][k], x->stride, &xc.ptr.p_double[0], 1, ae_v_len(0,n-1), v);
4928  }
4929  ae_frame_leave(_state);
4930 }
4931 
4932 
4933 /*************************************************************************
4934 Internal LU solver
4935 
4936  -- ALGLIB --
4937  Copyright 27.01.2010 by Bochkanov Sergey
4938 *************************************************************************/
4939 static void densesolver_cmatrixlusolveinternal(/* Complex */ ae_matrix* lua,
4940  /* Integer */ ae_vector* p,
4941  double scalea,
4942  ae_int_t n,
4943  /* Complex */ ae_matrix* a,
4944  ae_bool havea,
4945  /* Complex */ ae_matrix* b,
4946  ae_int_t m,
4947  ae_int_t* info,
4948  densesolverreport* rep,
4949  /* Complex */ ae_matrix* x,
4950  ae_state *_state)
4951 {
4952  ae_frame _frame_block;
4953  ae_int_t i;
4954  ae_int_t j;
4955  ae_int_t k;
4956  ae_int_t rfs;
4957  ae_int_t nrfs;
4958  ae_vector xc;
4959  ae_vector y;
4960  ae_vector bc;
4961  ae_vector xa;
4962  ae_vector xb;
4963  ae_vector tx;
4964  ae_vector tmpbuf;
4965  ae_complex v;
4966  double verr;
4967  double mxb;
4968  double scaleright;
4969  ae_bool smallerr;
4970  ae_bool terminatenexttime;
4971 
4972  ae_frame_make(_state, &_frame_block);
4973  *info = 0;
4975  ae_matrix_clear(x);
4976  ae_vector_init(&xc, 0, DT_COMPLEX, _state, ae_true);
4977  ae_vector_init(&y, 0, DT_COMPLEX, _state, ae_true);
4978  ae_vector_init(&bc, 0, DT_COMPLEX, _state, ae_true);
4979  ae_vector_init(&xa, 0, DT_COMPLEX, _state, ae_true);
4980  ae_vector_init(&xb, 0, DT_COMPLEX, _state, ae_true);
4981  ae_vector_init(&tx, 0, DT_COMPLEX, _state, ae_true);
4982  ae_vector_init(&tmpbuf, 0, DT_REAL, _state, ae_true);
4983 
4984  ae_assert(ae_fp_greater(scalea,0), "Assertion failed", _state);
4985 
4986  /*
4987  * prepare: check inputs, allocate space...
4988  */
4989  if( n<=0||m<=0 )
4990  {
4991  *info = -1;
4992  ae_frame_leave(_state);
4993  return;
4994  }
4995  for(i=0; i<=n-1; i++)
4996  {
4997  if( p->ptr.p_int[i]>n-1||p->ptr.p_int[i]<i )
4998  {
4999  *info = -1;
5000  ae_frame_leave(_state);
5001  return;
5002  }
5003  }
5004  ae_matrix_set_length(x, n, m, _state);
5005  ae_vector_set_length(&y, n, _state);
5006  ae_vector_set_length(&xc, n, _state);
5007  ae_vector_set_length(&bc, n, _state);
5008  ae_vector_set_length(&tx, n, _state);
5009  ae_vector_set_length(&xa, n+1, _state);
5010  ae_vector_set_length(&xb, n+1, _state);
5011  ae_vector_set_length(&tmpbuf, 2*n+2, _state);
5012 
5013  /*
5014  * estimate condition number, test for near singularity
5015  */
5016  rep->r1 = cmatrixlurcond1(lua, n, _state);
5017  rep->rinf = cmatrixlurcondinf(lua, n, _state);
5018  if( ae_fp_less(rep->r1,rcondthreshold(_state))||ae_fp_less(rep->rinf,rcondthreshold(_state)) )
5019  {
5020  for(i=0; i<=n-1; i++)
5021  {
5022  for(j=0; j<=m-1; j++)
5023  {
5024  x->ptr.pp_complex[i][j] = ae_complex_from_d(0);
5025  }
5026  }
5027  rep->r1 = 0;
5028  rep->rinf = 0;
5029  *info = -3;
5030  ae_frame_leave(_state);
5031  return;
5032  }
5033  *info = 1;
5034 
5035  /*
5036  * solve
5037  */
5038  for(k=0; k<=m-1; k++)
5039  {
5040 
5041  /*
5042  * copy B to contiguous storage
5043  */
5044  ae_v_cmove(&bc.ptr.p_complex[0], 1, &b->ptr.pp_complex[0][k], b->stride, "N", ae_v_len(0,n-1));
5045 
5046  /*
5047  * Scale right part:
5048  * * MX stores max(|Bi|)
5049  * * ScaleRight stores actual scaling applied to B when solving systems
5050  * it is chosen to make |scaleRight*b| close to 1.
5051  */
5052  mxb = 0;
5053  for(i=0; i<=n-1; i++)
5054  {
5055  mxb = ae_maxreal(mxb, ae_c_abs(bc.ptr.p_complex[i], _state), _state);
5056  }
5057  if( ae_fp_eq(mxb,0) )
5058  {
5059  mxb = 1;
5060  }
5061  scaleright = 1/mxb;
5062 
5063  /*
5064  * First, non-iterative part of solution process.
5065  * We use separate code for this task because
5066  * XDot is quite slow and we want to save time.
5067  */
5068  ae_v_cmoved(&xc.ptr.p_complex[0], 1, &bc.ptr.p_complex[0], 1, "N", ae_v_len(0,n-1), scaleright);
5069  densesolver_cbasiclusolve(lua, p, scalea, n, &xc, &tx, _state);
5070 
5071  /*
5072  * Iterative refinement of xc:
5073  * * calculate r = bc-A*xc using extra-precise dot product
5074  * * solve A*y = r
5075  * * update x:=x+r
5076  *
5077  * This cycle is executed until one of two things happens:
5078  * 1. maximum number of iterations reached
5079  * 2. last iteration decreased error to the lower limit
5080  */
5081  if( havea )
5082  {
5083  nrfs = densesolver_densesolverrfsmax(n, rep->r1, rep->rinf, _state);
5084  terminatenexttime = ae_false;
5085  for(rfs=0; rfs<=nrfs-1; rfs++)
5086  {
5087  if( terminatenexttime )
5088  {
5089  break;
5090  }
5091 
5092  /*
5093  * generate right part
5094  */
5095  smallerr = ae_true;
5096  ae_v_cmove(&xb.ptr.p_complex[0], 1, &xc.ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
5097  for(i=0; i<=n-1; i++)
5098  {
5099  ae_v_cmoved(&xa.ptr.p_complex[0], 1, &a->ptr.pp_complex[i][0], 1, "N", ae_v_len(0,n-1), scalea);
5100  xa.ptr.p_complex[n] = ae_complex_from_d(-1);
5101  xb.ptr.p_complex[n] = ae_c_mul_d(bc.ptr.p_complex[i],scaleright);
5102  xcdot(&xa, &xb, n+1, &tmpbuf, &v, &verr, _state);
5103  y.ptr.p_complex[i] = ae_c_neg(v);
5104  smallerr = smallerr&&ae_fp_less(ae_c_abs(v, _state),4*verr);
5105  }
5106  if( smallerr )
5107  {
5108  terminatenexttime = ae_true;
5109  }
5110 
5111  /*
5112  * solve and update
5113  */
5114  densesolver_cbasiclusolve(lua, p, scalea, n, &y, &tx, _state);
5115  ae_v_cadd(&xc.ptr.p_complex[0], 1, &y.ptr.p_complex[0], 1, "N", ae_v_len(0,n-1));
5116  }
5117  }
5118 
5119  /*
5120  * Store xc.
5121  * Post-scale result.
5122  */
5123  v = ae_complex_from_d(scalea*mxb);
5124  ae_v_cmovec(&x->ptr.pp_complex[0][k], x->stride, &xc.ptr.p_complex[0], 1, "N", ae_v_len(0,n-1), v);
5125  }
5126  ae_frame_leave(_state);
5127 }
5128 
5129 
5130 /*************************************************************************
5131 Internal Cholesky solver
5132 
5133  -- ALGLIB --
5134  Copyright 27.01.2010 by Bochkanov Sergey
5135 *************************************************************************/
5136 static void densesolver_hpdmatrixcholeskysolveinternal(/* Complex */ ae_matrix* cha,
5137  double sqrtscalea,
5138  ae_int_t n,
5139  ae_bool isupper,
5140  /* Complex */ ae_matrix* a,
5141  ae_bool havea,
5142  /* Complex */ ae_matrix* b,
5143  ae_int_t m,
5144  ae_int_t* info,
5145  densesolverreport* rep,
5146  /* Complex */ ae_matrix* x,
5147  ae_state *_state)
5148 {
5149  ae_frame _frame_block;
5150  ae_int_t i;
5151  ae_int_t j;
5152  ae_int_t k;
5153  ae_vector xc;
5154  ae_vector y;
5155  ae_vector bc;
5156  ae_vector xa;
5157  ae_vector xb;
5158  ae_vector tx;
5159  double v;
5160  double mxb;
5161  double scaleright;
5162 
5163  ae_frame_make(_state, &_frame_block);
5164  *info = 0;
5166  ae_matrix_clear(x);
5167  ae_vector_init(&xc, 0, DT_COMPLEX, _state, ae_true);
5168  ae_vector_init(&y, 0, DT_COMPLEX, _state, ae_true);
5169  ae_vector_init(&bc, 0, DT_COMPLEX, _state, ae_true);
5170  ae_vector_init(&xa, 0, DT_COMPLEX, _state, ae_true);
5171  ae_vector_init(&xb, 0, DT_COMPLEX, _state, ae_true);
5172  ae_vector_init(&tx, 0, DT_COMPLEX, _state, ae_true);
5173 
5174  ae_assert(ae_fp_greater(sqrtscalea,0), "Assertion failed", _state);
5175 
5176  /*
5177  * prepare: check inputs, allocate space...
5178  */
5179  if( n<=0||m<=0 )
5180  {
5181  *info = -1;
5182  ae_frame_leave(_state);
5183  return;
5184  }
5185  ae_matrix_set_length(x, n, m, _state);
5186  ae_vector_set_length(&y, n, _state);
5187  ae_vector_set_length(&xc, n, _state);
5188  ae_vector_set_length(&bc, n, _state);
5189  ae_vector_set_length(&tx, n+1, _state);
5190  ae_vector_set_length(&xa, n+1, _state);
5191  ae_vector_set_length(&xb, n+1, _state);
5192 
5193  /*
5194  * estimate condition number, test for near singularity
5195  */
5196  rep->r1 = hpdmatrixcholeskyrcond(cha, n, isupper, _state);
5197  rep->rinf = rep->r1;
5198  if( ae_fp_less(rep->r1,rcondthreshold(_state)) )
5199  {
5200  for(i=0; i<=n-1; i++)
5201  {
5202  for(j=0; j<=m-1; j++)
5203  {
5204  x->ptr.pp_complex[i][j] = ae_complex_from_d(0);
5205  }
5206  }
5207  rep->r1 = 0;
5208  rep->rinf = 0;
5209  *info = -3;
5210  ae_frame_leave(_state);
5211  return;
5212  }
5213  *info = 1;
5214 
5215  /*
5216  * solve
5217  */
5218  for(k=0; k<=m-1; k++)
5219  {
5220 
5221  /*
5222  * copy B to contiguous storage
5223  */
5224  ae_v_cmove(&bc.ptr.p_complex[0], 1, &b->ptr.pp_complex[0][k], b->stride, "N", ae_v_len(0,n-1));
5225 
5226  /*
5227  * Scale right part:
5228  * * MX stores max(|Bi|)
5229  * * ScaleRight stores actual scaling applied to B when solving systems
5230  * it is chosen to make |scaleRight*b| close to 1.
5231  */
5232  mxb = 0;
5233  for(i=0; i<=n-1; i++)
5234  {
5235  mxb = ae_maxreal(mxb, ae_c_abs(bc.ptr.p_complex[i], _state), _state);
5236  }
5237  if( ae_fp_eq(mxb,0) )
5238  {
5239  mxb = 1;
5240  }
5241  scaleright = 1/mxb;
5242 
5243  /*
5244  * First, non-iterative part of solution process.
5245  * We use separate code for this task because
5246  * XDot is quite slow and we want to save time.
5247  */
5248  ae_v_cmoved(&xc.ptr.p_complex[0], 1, &bc.ptr.p_complex[0], 1, "N", ae_v_len(0,n-1), scaleright);
5249  densesolver_hpdbasiccholeskysolve(cha, sqrtscalea, n, isupper, &xc, &tx, _state);
5250 
5251  /*
5252  * Store xc.
5253  * Post-scale result.
5254  */
5255  v = ae_sqr(sqrtscalea, _state)*mxb;
5256  ae_v_cmoved(&x->ptr.pp_complex[0][k], x->stride, &xc.ptr.p_complex[0], 1, "N", ae_v_len(0,n-1), v);
5257  }
5258  ae_frame_leave(_state);
5259 }
5260 
5261 
5262 /*************************************************************************
5263 Internal subroutine.
5264 Returns maximum count of RFS iterations as function of:
5265 1. machine epsilon
5266 2. task size.
5267 3. condition number
5268 
5269  -- ALGLIB --
5270  Copyright 27.01.2010 by Bochkanov Sergey
5271 *************************************************************************/
5272 static ae_int_t densesolver_densesolverrfsmax(ae_int_t n,
5273  double r1,
5274  double rinf,
5275  ae_state *_state)
5276 {
5277  ae_int_t result;
5278 
5279 
5280  result = 5;
5281  return result;
5282 }
5283 
5284 
5285 /*************************************************************************
5286 Internal subroutine.
5287 Returns maximum count of RFS iterations as function of:
5288 1. machine epsilon
5289 2. task size.
5290 3. norm-2 condition number
5291 
5292  -- ALGLIB --
5293  Copyright 27.01.2010 by Bochkanov Sergey
5294 *************************************************************************/
5295 static ae_int_t densesolver_densesolverrfsmaxv2(ae_int_t n,
5296  double r2,
5297  ae_state *_state)
5298 {
5299  ae_int_t result;
5300 
5301 
5302  result = densesolver_densesolverrfsmax(n, 0, 0, _state);
5303  return result;
5304 }
5305 
5306 
5307 /*************************************************************************
5308 Basic LU solver for ScaleA*PLU*x = y.
5309 
5310 This subroutine assumes that:
5311 * L is well-scaled, and it is U which needs scaling by ScaleA.
5312 * A=PLU is well-conditioned, so no zero divisions or overflow may occur
5313 
5314  -- ALGLIB --
5315  Copyright 27.01.2010 by Bochkanov Sergey
5316 *************************************************************************/
5317 static void densesolver_rbasiclusolve(/* Real */ ae_matrix* lua,
5318  /* Integer */ ae_vector* p,
5319  double scalea,
5320  ae_int_t n,
5321  /* Real */ ae_vector* xb,
5322  /* Real */ ae_vector* tmp,
5323  ae_state *_state)
5324 {
5325  ae_int_t i;
5326  double v;
5327 
5328 
5329  for(i=0; i<=n-1; i++)
5330  {
5331  if( p->ptr.p_int[i]!=i )
5332  {
5333  v = xb->ptr.p_double[i];
5334  xb->ptr.p_double[i] = xb->ptr.p_double[p->ptr.p_int[i]];
5335  xb->ptr.p_double[p->ptr.p_int[i]] = v;
5336  }
5337  }
5338  for(i=1; i<=n-1; i++)
5339  {
5340  v = ae_v_dotproduct(&lua->ptr.pp_double[i][0], 1, &xb->ptr.p_double[0], 1, ae_v_len(0,i-1));
5341  xb->ptr.p_double[i] = xb->ptr.p_double[i]-v;
5342  }
5343  xb->ptr.p_double[n-1] = xb->ptr.p_double[n-1]/(scalea*lua->ptr.pp_double[n-1][n-1]);
5344  for(i=n-2; i>=0; i--)
5345  {
5346  ae_v_moved(&tmp->ptr.p_double[i+1], 1, &lua->ptr.pp_double[i][i+1], 1, ae_v_len(i+1,n-1), scalea);
5347  v = ae_v_dotproduct(&tmp->ptr.p_double[i+1], 1, &xb->ptr.p_double[i+1], 1, ae_v_len(i+1,n-1));
5348  xb->ptr.p_double[i] = (xb->ptr.p_double[i]-v)/(scalea*lua->ptr.pp_double[i][i]);
5349  }
5350 }
5351 
5352 
5353 /*************************************************************************
5354 Basic Cholesky solver for ScaleA*Cholesky(A)'*x = y.
5355 
5356 This subroutine assumes that:
5357 * A*ScaleA is well scaled
5358 * A is well-conditioned, so no zero divisions or overflow may occur
5359 
5360  -- ALGLIB --
5361  Copyright 27.01.2010 by Bochkanov Sergey
5362 *************************************************************************/
5363 static void densesolver_spdbasiccholeskysolve(/* Real */ ae_matrix* cha,
5364  double sqrtscalea,
5365  ae_int_t n,
5366  ae_bool isupper,
5367  /* Real */ ae_vector* xb,
5368  /* Real */ ae_vector* tmp,
5369  ae_state *_state)
5370 {
5371  ae_int_t i;
5372  double v;
5373 
5374 
5375 
5376  /*
5377  * A = L*L' or A=U'*U
5378  */
5379  if( isupper )
5380  {
5381 
5382  /*
5383  * Solve U'*y=b first.
5384  */
5385  for(i=0; i<=n-1; i++)
5386  {
5387  xb->ptr.p_double[i] = xb->ptr.p_double[i]/(sqrtscalea*cha->ptr.pp_double[i][i]);
5388  if( i<n-1 )
5389  {
5390  v = xb->ptr.p_double[i];
5391  ae_v_moved(&tmp->ptr.p_double[i+1], 1, &cha->ptr.pp_double[i][i+1], 1, ae_v_len(i+1,n-1), sqrtscalea);
5392  ae_v_subd(&xb->ptr.p_double[i+1], 1, &tmp->ptr.p_double[i+1], 1, ae_v_len(i+1,n-1), v);
5393  }
5394  }
5395 
5396  /*
5397  * Solve U*x=y then.
5398  */
5399  for(i=n-1; i>=0; i--)
5400  {
5401  if( i<n-1 )
5402  {
5403  ae_v_moved(&tmp->ptr.p_double[i+1], 1, &cha->ptr.pp_double[i][i+1], 1, ae_v_len(i+1,n-1), sqrtscalea);
5404  v = ae_v_dotproduct(&tmp->ptr.p_double[i+1], 1, &xb->ptr.p_double[i+1], 1, ae_v_len(i+1,n-1));
5405  xb->ptr.p_double[i] = xb->ptr.p_double[i]-v;
5406  }
5407  xb->ptr.p_double[i] = xb->ptr.p_double[i]/(sqrtscalea*cha->ptr.pp_double[i][i]);
5408  }
5409  }
5410  else
5411  {
5412 
5413  /*
5414  * Solve L*y=b first
5415  */
5416  for(i=0; i<=n-1; i++)
5417  {
5418  if( i>0 )
5419  {
5420  ae_v_moved(&tmp->ptr.p_double[0], 1, &cha->ptr.pp_double[i][0], 1, ae_v_len(0,i-1), sqrtscalea);
5421  v = ae_v_dotproduct(&tmp->ptr.p_double[0], 1, &xb->ptr.p_double[0], 1, ae_v_len(0,i-1));
5422  xb->ptr.p_double[i] = xb->ptr.p_double[i]-v;
5423  }
5424  xb->ptr.p_double[i] = xb->ptr.p_double[i]/(sqrtscalea*cha->ptr.pp_double[i][i]);
5425  }
5426 
5427  /*
5428  * Solve L'*x=y then.
5429  */
5430  for(i=n-1; i>=0; i--)
5431  {
5432  xb->ptr.p_double[i] = xb->ptr.p_double[i]/(sqrtscalea*cha->ptr.pp_double[i][i]);
5433  if( i>0 )
5434  {
5435  v = xb->ptr.p_double[i];
5436  ae_v_moved(&tmp->ptr.p_double[0], 1, &cha->ptr.pp_double[i][0], 1, ae_v_len(0,i-1), sqrtscalea);
5437  ae_v_subd(&xb->ptr.p_double[0], 1, &tmp->ptr.p_double[0], 1, ae_v_len(0,i-1), v);
5438  }
5439  }
5440  }
5441 }
5442 
5443 
5444 /*************************************************************************
5445 Basic LU solver for ScaleA*PLU*x = y.
5446 
5447 This subroutine assumes that:
5448 * L is well-scaled, and it is U which needs scaling by ScaleA.
5449 * A=PLU is well-conditioned, so no zero divisions or overflow may occur
5450 
5451  -- ALGLIB --
5452  Copyright 27.01.2010 by Bochkanov Sergey
5453 *************************************************************************/
5454 static void densesolver_cbasiclusolve(/* Complex */ ae_matrix* lua,
5455  /* Integer */ ae_vector* p,
5456  double scalea,
5457  ae_int_t n,
5458  /* Complex */ ae_vector* xb,
5459  /* Complex */ ae_vector* tmp,
5460  ae_state *_state)
5461 {
5462  ae_int_t i;
5463  ae_complex v;
5464 
5465 
5466  for(i=0; i<=n-1; i++)
5467  {
5468  if( p->ptr.p_int[i]!=i )
5469  {
5470  v = xb->ptr.p_complex[i];
5471  xb->ptr.p_complex[i] = xb->ptr.p_complex[p->ptr.p_int[i]];
5472  xb->ptr.p_complex[p->ptr.p_int[i]] = v;
5473  }
5474  }
5475  for(i=1; i<=n-1; i++)
5476  {
5477  v = ae_v_cdotproduct(&lua->ptr.pp_complex[i][0], 1, "N", &xb->ptr.p_complex[0], 1, "N", ae_v_len(0,i-1));
5478  xb->ptr.p_complex[i] = ae_c_sub(xb->ptr.p_complex[i],v);
5479  }
5480  xb->ptr.p_complex[n-1] = ae_c_div(xb->ptr.p_complex[n-1],ae_c_mul_d(lua->ptr.pp_complex[n-1][n-1],scalea));
5481  for(i=n-2; i>=0; i--)
5482  {
5483  ae_v_cmoved(&tmp->ptr.p_complex[i+1], 1, &lua->ptr.pp_complex[i][i+1], 1, "N", ae_v_len(i+1,n-1), scalea);
5484  v = ae_v_cdotproduct(&tmp->ptr.p_complex[i+1], 1, "N", &xb->ptr.p_complex[i+1], 1, "N", ae_v_len(i+1,n-1));
5485  xb->ptr.p_complex[i] = ae_c_div(ae_c_sub(xb->ptr.p_complex[i],v),ae_c_mul_d(lua->ptr.pp_complex[i][i],scalea));
5486  }
5487 }
5488 
5489 
5490 /*************************************************************************
5491 Basic Cholesky solver for ScaleA*Cholesky(A)'*x = y.
5492 
5493 This subroutine assumes that:
5494 * A*ScaleA is well scaled
5495 * A is well-conditioned, so no zero divisions or overflow may occur
5496 
5497  -- ALGLIB --
5498  Copyright 27.01.2010 by Bochkanov Sergey
5499 *************************************************************************/
5500 static void densesolver_hpdbasiccholeskysolve(/* Complex */ ae_matrix* cha,
5501  double sqrtscalea,
5502  ae_int_t n,
5503  ae_bool isupper,
5504  /* Complex */ ae_vector* xb,
5505  /* Complex */ ae_vector* tmp,
5506  ae_state *_state)
5507 {
5508  ae_int_t i;
5509  ae_complex v;
5510 
5511 
5512 
5513  /*
5514  * A = L*L' or A=U'*U
5515  */
5516  if( isupper )
5517  {
5518 
5519  /*
5520  * Solve U'*y=b first.
5521  */
5522  for(i=0; i<=n-1; i++)
5523  {
5524  xb->ptr.p_complex[i] = ae_c_div(xb->ptr.p_complex[i],ae_c_mul_d(ae_c_conj(cha->ptr.pp_complex[i][i], _state),sqrtscalea));
5525  if( i<n-1 )
5526  {
5527  v = xb->ptr.p_complex[i];
5528  ae_v_cmoved(&tmp->ptr.p_complex[i+1], 1, &cha->ptr.pp_complex[i][i+1], 1, "Conj", ae_v_len(i+1,n-1), sqrtscalea);
5529  ae_v_csubc(&xb->ptr.p_complex[i+1], 1, &tmp->ptr.p_complex[i+1], 1, "N", ae_v_len(i+1,n-1), v);
5530  }
5531  }
5532 
5533  /*
5534  * Solve U*x=y then.
5535  */
5536  for(i=n-1; i>=0; i--)
5537  {
5538  if( i<n-1 )
5539  {
5540  ae_v_cmoved(&tmp->ptr.p_complex[i+1], 1, &cha->ptr.pp_complex[i][i+1], 1, "N", ae_v_len(i+1,n-1), sqrtscalea);
5541  v = ae_v_cdotproduct(&tmp->ptr.p_complex[i+1], 1, "N", &xb->ptr.p_complex[i+1], 1, "N", ae_v_len(i+1,n-1));
5542  xb->ptr.p_complex[i] = ae_c_sub(xb->ptr.p_complex[i],v);
5543  }
5544  xb->ptr.p_complex[i] = ae_c_div(xb->ptr.p_complex[i],ae_c_mul_d(cha->ptr.pp_complex[i][i],sqrtscalea));
5545  }
5546  }
5547  else
5548  {
5549 
5550  /*
5551  * Solve L*y=b first
5552  */
5553  for(i=0; i<=n-1; i++)
5554  {
5555  if( i>0 )
5556  {
5557  ae_v_cmoved(&tmp->ptr.p_complex[0], 1, &cha->ptr.pp_complex[i][0], 1, "N", ae_v_len(0,i-1), sqrtscalea);
5558  v = ae_v_cdotproduct(&tmp->ptr.p_complex[0], 1, "N", &xb->ptr.p_complex[0], 1, "N", ae_v_len(0,i-1));
5559  xb->ptr.p_complex[i] = ae_c_sub(xb->ptr.p_complex[i],v);
5560  }
5561  xb->ptr.p_complex[i] = ae_c_div(xb->ptr.p_complex[i],ae_c_mul_d(cha->ptr.pp_complex[i][i],sqrtscalea));
5562  }
5563 
5564  /*
5565  * Solve L'*x=y then.
5566  */
5567  for(i=n-1; i>=0; i--)
5568  {
5569  xb->ptr.p_complex[i] = ae_c_div(xb->ptr.p_complex[i],ae_c_mul_d(ae_c_conj(cha->ptr.pp_complex[i][i], _state),sqrtscalea));
5570  if( i>0 )
5571  {
5572  v = xb->ptr.p_complex[i];
5573  ae_v_cmoved(&tmp->ptr.p_complex[0], 1, &cha->ptr.pp_complex[i][0], 1, "Conj", ae_v_len(0,i-1), sqrtscalea);
5574  ae_v_csubc(&xb->ptr.p_complex[0], 1, &tmp->ptr.p_complex[0], 1, "N", ae_v_len(0,i-1), v);
5575  }
5576  }
5577  }
5578 }
5579 
5580 
5581 ae_bool _densesolverreport_init(void* _p, ae_state *_state, ae_bool make_automatic)
5582 {
5584  ae_touch_ptr((void*)p);
5585  return ae_true;
5586 }
5587 
5588 
5589 ae_bool _densesolverreport_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
5590 {
5591  densesolverreport *dst = (densesolverreport*)_dst;
5592  densesolverreport *src = (densesolverreport*)_src;
5593  dst->r1 = src->r1;
5594  dst->rinf = src->rinf;
5595  return ae_true;
5596 }
5597 
5598 
5600 {
5602  ae_touch_ptr((void*)p);
5603 }
5604 
5605 
5607 {
5609  ae_touch_ptr((void*)p);
5610 }
5611 
5612 
5613 ae_bool _densesolverlsreport_init(void* _p, ae_state *_state, ae_bool make_automatic)
5614 {
5616  ae_touch_ptr((void*)p);
5617  if( !ae_matrix_init(&p->cx, 0, 0, DT_REAL, _state, make_automatic) )
5618  return ae_false;
5619  return ae_true;
5620 }
5621 
5622 
5623 ae_bool _densesolverlsreport_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
5624 {
5627  dst->r2 = src->r2;
5628  if( !ae_matrix_init_copy(&dst->cx, &src->cx, _state, make_automatic) )
5629  return ae_false;
5630  dst->n = src->n;
5631  dst->k = src->k;
5632  return ae_true;
5633 }
5634 
5635 
5637 {
5639  ae_touch_ptr((void*)p);
5640  ae_matrix_clear(&p->cx);
5641 }
5642 
5643 
5645 {
5647  ae_touch_ptr((void*)p);
5648  ae_matrix_destroy(&p->cx);
5649 }
5650 
5651 
5652 
5653 
5654 /*************************************************************************
5655 This function initializes linear LSQR Solver. This solver is used to solve
5656 non-symmetric (and, possibly, non-square) problems. Least squares solution
5657 is returned for non-compatible systems.
5658 
5659 USAGE:
5660 1. User initializes algorithm state with LinLSQRCreate() call
5661 2. User tunes solver parameters with LinLSQRSetCond() and other functions
5662 3. User calls LinLSQRSolveSparse() function which takes algorithm state
5663  and SparseMatrix object.
5664 4. User calls LinLSQRResults() to get solution
5665 5. Optionally, user may call LinLSQRSolveSparse() again to solve another
5666  problem with different matrix and/or right part without reinitializing
5667  LinLSQRState structure.
5668 
5669 INPUT PARAMETERS:
5670  M - number of rows in A
5671  N - number of variables, N>0
5672 
5673 OUTPUT PARAMETERS:
5674  State - structure which stores algorithm state
5675 
5676  -- ALGLIB --
5677  Copyright 30.11.2011 by Bochkanov Sergey
5678 *************************************************************************/
5680  ae_int_t n,
5681  linlsqrstate* state,
5682  ae_state *_state)
5683 {
5684  ae_int_t i;
5685 
5686  _linlsqrstate_clear(state);
5687 
5688  ae_assert(m>0, "LinLSQRCreate: M<=0", _state);
5689  ae_assert(n>0, "LinLSQRCreate: N<=0", _state);
5690  state->m = m;
5691  state->n = n;
5692  state->prectype = 0;
5693  state->epsa = linlsqr_atol;
5694  state->epsb = linlsqr_btol;
5695  state->epsc = 1/ae_sqrt(ae_machineepsilon, _state);
5696  state->maxits = 0;
5697  state->lambdai = 0;
5698  state->xrep = ae_false;
5699  state->running = ae_false;
5700 
5701  /*
5702  * * allocate arrays
5703  * * set RX to NAN (just for the case user calls Results() without
5704  * calling SolveSparse()
5705  * * set B to zero
5706  */
5707  normestimatorcreate(m, n, 2, 2, &state->nes, _state);
5708  ae_vector_set_length(&state->rx, state->n, _state);
5709  ae_vector_set_length(&state->ui, state->m+state->n, _state);
5710  ae_vector_set_length(&state->uip1, state->m+state->n, _state);
5711  ae_vector_set_length(&state->vip1, state->n, _state);
5712  ae_vector_set_length(&state->vi, state->n, _state);
5713  ae_vector_set_length(&state->omegai, state->n, _state);
5714  ae_vector_set_length(&state->omegaip1, state->n, _state);
5715  ae_vector_set_length(&state->d, state->n, _state);
5716  ae_vector_set_length(&state->x, state->m+state->n, _state);
5717  ae_vector_set_length(&state->mv, state->m+state->n, _state);
5718  ae_vector_set_length(&state->mtv, state->n, _state);
5719  ae_vector_set_length(&state->b, state->m, _state);
5720  for(i=0; i<=n-1; i++)
5721  {
5722  state->rx.ptr.p_double[i] = _state->v_nan;
5723  }
5724  for(i=0; i<=m-1; i++)
5725  {
5726  state->b.ptr.p_double[i] = 0;
5727  }
5728  ae_vector_set_length(&state->rstate.ia, 1+1, _state);
5729  ae_vector_set_length(&state->rstate.ra, 0+1, _state);
5730  state->rstate.stage = -1;
5731 }
5732 
5733 
5734 /*************************************************************************
5735 This function sets right part. By default, right part is zero.
5736 
5737 INPUT PARAMETERS:
5738  B - right part, array[N].
5739 
5740 OUTPUT PARAMETERS:
5741  State - structure which stores algorithm state
5742 
5743  -- ALGLIB --
5744  Copyright 30.11.2011 by Bochkanov Sergey
5745 *************************************************************************/
5747  /* Real */ ae_vector* b,
5748  ae_state *_state)
5749 {
5750  ae_int_t i;
5751 
5752 
5753  ae_assert(!state->running, "LinLSQRSetB: you can not change B when LinLSQRIteration is running", _state);
5754  ae_assert(state->m<=b->cnt, "LinLSQRSetB: Length(B)<M", _state);
5755  ae_assert(isfinitevector(b, state->m, _state), "LinLSQRSetB: B contains infinite or NaN values", _state);
5756  state->bnorm2 = 0;
5757  for(i=0; i<=state->m-1; i++)
5758  {
5759  state->b.ptr.p_double[i] = b->ptr.p_double[i];
5760  state->bnorm2 = state->bnorm2+b->ptr.p_double[i]*b->ptr.p_double[i];
5761  }
5762 }
5763 
5764 
5765 /*************************************************************************
5766 This function changes preconditioning settings of LinLSQQSolveSparse()
5767 function. By default, SolveSparse() uses diagonal preconditioner, but if
5768 you want to use solver without preconditioning, you can call this function
5769 which forces solver to use unit matrix for preconditioning.
5770 
5771 INPUT PARAMETERS:
5772  State - structure which stores algorithm state
5773 
5774  -- ALGLIB --
5775  Copyright 19.11.2012 by Bochkanov Sergey
5776 *************************************************************************/
5778 {
5779 
5780 
5781  ae_assert(!state->running, "LinLSQRSetPrecUnit: you can not change preconditioner, because function LinLSQRIteration is running!", _state);
5782  state->prectype = -1;
5783 }
5784 
5785 
5786 /*************************************************************************
5787 This function changes preconditioning settings of LinCGSolveSparse()
5788 function. LinCGSolveSparse() will use diagonal of the system matrix as
5789 preconditioner. This preconditioning mode is active by default.
5790 
5791 INPUT PARAMETERS:
5792  State - structure which stores algorithm state
5793 
5794  -- ALGLIB --
5795  Copyright 19.11.2012 by Bochkanov Sergey
5796 *************************************************************************/
5798 {
5799 
5800 
5801  ae_assert(!state->running, "LinLSQRSetPrecDiag: you can not change preconditioner, because function LinCGIteration is running!", _state);
5802  state->prectype = 0;
5803 }
5804 
5805 
5806 /*************************************************************************
5807 This function sets optional Tikhonov regularization coefficient.
5808 It is zero by default.
5809 
5810 INPUT PARAMETERS:
5811  LambdaI - regularization factor, LambdaI>=0
5812 
5813 OUTPUT PARAMETERS:
5814  State - structure which stores algorithm state
5815 
5816  -- ALGLIB --
5817  Copyright 30.11.2011 by Bochkanov Sergey
5818 *************************************************************************/
5820  double lambdai,
5821  ae_state *_state)
5822 {
5823 
5824 
5825  ae_assert(!state->running, "LinLSQRSetLambdaI: you can not set LambdaI, because function LinLSQRIteration is running", _state);
5826  ae_assert(ae_isfinite(lambdai, _state)&&ae_fp_greater_eq(lambdai,0), "LinLSQRSetLambdaI: LambdaI is infinite or NaN", _state);
5827  state->lambdai = lambdai;
5828 }
5829 
5830 
5831 /*************************************************************************
5832 
5833  -- ALGLIB --
5834  Copyright 30.11.2011 by Bochkanov Sergey
5835 *************************************************************************/
5837 {
5838  ae_int_t summn;
5839  double bnorm;
5840  ae_int_t i;
5841  ae_bool result;
5842 
5843 
5844 
5845  /*
5846  * Reverse communication preparations
5847  * I know it looks ugly, but it works the same way
5848  * anywhere from C++ to Python.
5849  *
5850  * This code initializes locals by:
5851  * * random values determined during code
5852  * generation - on first subroutine call
5853  * * values from previous call - on subsequent calls
5854  */
5855  if( state->rstate.stage>=0 )
5856  {
5857  summn = state->rstate.ia.ptr.p_int[0];
5858  i = state->rstate.ia.ptr.p_int[1];
5859  bnorm = state->rstate.ra.ptr.p_double[0];
5860  }
5861  else
5862  {
5863  summn = -983;
5864  i = -989;
5865  bnorm = -834;
5866  }
5867  if( state->rstate.stage==0 )
5868  {
5869  goto lbl_0;
5870  }
5871  if( state->rstate.stage==1 )
5872  {
5873  goto lbl_1;
5874  }
5875  if( state->rstate.stage==2 )
5876  {
5877  goto lbl_2;
5878  }
5879  if( state->rstate.stage==3 )
5880  {
5881  goto lbl_3;
5882  }
5883  if( state->rstate.stage==4 )
5884  {
5885  goto lbl_4;
5886  }
5887  if( state->rstate.stage==5 )
5888  {
5889  goto lbl_5;
5890  }
5891  if( state->rstate.stage==6 )
5892  {
5893  goto lbl_6;
5894  }
5895 
5896  /*
5897  * Routine body
5898  */
5899  ae_assert(state->b.cnt>0, "LinLSQRIteration: using non-allocated array B", _state);
5900  bnorm = ae_sqrt(state->bnorm2, _state);
5901  state->running = ae_true;
5902  state->repnmv = 0;
5903  linlsqr_clearrfields(state, _state);
5904  state->repiterationscount = 0;
5905  summn = state->m+state->n;
5906  state->r2 = state->bnorm2;
5907 
5908  /*
5909  *estimate for ANorm
5910  */
5911  normestimatorrestart(&state->nes, _state);
5912 lbl_7:
5913  if( !normestimatoriteration(&state->nes, _state) )
5914  {
5915  goto lbl_8;
5916  }
5917  if( !state->nes.needmv )
5918  {
5919  goto lbl_9;
5920  }
5921  ae_v_move(&state->x.ptr.p_double[0], 1, &state->nes.x.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
5922  state->repnmv = state->repnmv+1;
5923  linlsqr_clearrfields(state, _state);
5924  state->needmv = ae_true;
5925  state->rstate.stage = 0;
5926  goto lbl_rcomm;
5927 lbl_0:
5928  state->needmv = ae_false;
5929  ae_v_move(&state->nes.mv.ptr.p_double[0], 1, &state->mv.ptr.p_double[0], 1, ae_v_len(0,state->m-1));
5930  goto lbl_7;
5931 lbl_9:
5932  if( !state->nes.needmtv )
5933  {
5934  goto lbl_11;
5935  }
5936  ae_v_move(&state->x.ptr.p_double[0], 1, &state->nes.x.ptr.p_double[0], 1, ae_v_len(0,state->m-1));
5937 
5938  /*
5939  *matrix-vector multiplication
5940  */
5941  state->repnmv = state->repnmv+1;
5942  linlsqr_clearrfields(state, _state);
5943  state->needmtv = ae_true;
5944  state->rstate.stage = 1;
5945  goto lbl_rcomm;
5946 lbl_1:
5947  state->needmtv = ae_false;
5948  ae_v_move(&state->nes.mtv.ptr.p_double[0], 1, &state->mtv.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
5949  goto lbl_7;
5950 lbl_11:
5951  goto lbl_7;
5952 lbl_8:
5953  normestimatorresults(&state->nes, &state->anorm, _state);
5954 
5955  /*
5956  *initialize .RX by zeros
5957  */
5958  for(i=0; i<=state->n-1; i++)
5959  {
5960  state->rx.ptr.p_double[i] = 0;
5961  }
5962 
5963  /*
5964  *output first report
5965  */
5966  if( !state->xrep )
5967  {
5968  goto lbl_13;
5969  }
5970  ae_v_move(&state->x.ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
5971  linlsqr_clearrfields(state, _state);
5972  state->xupdated = ae_true;
5973  state->rstate.stage = 2;
5974  goto lbl_rcomm;
5975 lbl_2:
5976  state->xupdated = ae_false;
5977 lbl_13:
5978 
5979  /*
5980  * LSQR, Step 0.
5981  *
5982  * Algorithm outline corresponds to one which was described at p.50 of
5983  * "LSQR - an algorithm for sparse linear equations and sparse least
5984  * squares" by C.Paige and M.Saunders with one small addition - we
5985  * explicitly extend system matrix by additional N lines in order
5986  * to handle non-zero lambda, i.e. original A is replaced by
5987  * [ A ]
5988  * A_mod = [ ]
5989  * [ lambda*I ].
5990  *
5991  * Step 0:
5992  * x[0] = 0
5993  * beta[1]*u[1] = b
5994  * alpha[1]*v[1] = A_mod'*u[1]
5995  * w[1] = v[1]
5996  * phiBar[1] = beta[1]
5997  * rhoBar[1] = alpha[1]
5998  * d[0] = 0
5999  *
6000  * NOTE:
6001  * There are three criteria for stopping:
6002  * (S0) maximum number of iterations
6003  * (S1) ||Rk||<=EpsB*||B||;
6004  * (S2) ||A^T*Rk||/(||A||*||Rk||)<=EpsA.
6005  * It is very important that S2 always checked AFTER S1. It is necessary
6006  * to avoid division by zero when Rk=0.
6007  */
6008  state->betai = bnorm;
6009  if( ae_fp_eq(state->betai,0) )
6010  {
6011 
6012  /*
6013  * Zero right part
6014  */
6015  state->running = ae_false;
6016  state->repterminationtype = 1;
6017  result = ae_false;
6018  return result;
6019  }
6020  for(i=0; i<=summn-1; i++)
6021  {
6022  if( i<state->m )
6023  {
6024  state->ui.ptr.p_double[i] = state->b.ptr.p_double[i]/state->betai;
6025  }
6026  else
6027  {
6028  state->ui.ptr.p_double[i] = 0;
6029  }
6030  state->x.ptr.p_double[i] = state->ui.ptr.p_double[i];
6031  }
6032  state->repnmv = state->repnmv+1;
6033  linlsqr_clearrfields(state, _state);
6034  state->needmtv = ae_true;
6035  state->rstate.stage = 3;
6036  goto lbl_rcomm;
6037 lbl_3:
6038  state->needmtv = ae_false;
6039  for(i=0; i<=state->n-1; i++)
6040  {
6041  state->mtv.ptr.p_double[i] = state->mtv.ptr.p_double[i]+state->lambdai*state->ui.ptr.p_double[state->m+i];
6042  }
6043  state->alphai = 0;
6044  for(i=0; i<=state->n-1; i++)
6045  {
6046  state->alphai = state->alphai+state->mtv.ptr.p_double[i]*state->mtv.ptr.p_double[i];
6047  }
6048  state->alphai = ae_sqrt(state->alphai, _state);
6049  if( ae_fp_eq(state->alphai,0) )
6050  {
6051 
6052  /*
6053  * Orthogonality stopping criterion is met
6054  */
6055  state->running = ae_false;
6056  state->repterminationtype = 4;
6057  result = ae_false;
6058  return result;
6059  }
6060  for(i=0; i<=state->n-1; i++)
6061  {
6062  state->vi.ptr.p_double[i] = state->mtv.ptr.p_double[i]/state->alphai;
6063  state->omegai.ptr.p_double[i] = state->vi.ptr.p_double[i];
6064  }
6065  state->phibari = state->betai;
6066  state->rhobari = state->alphai;
6067  for(i=0; i<=state->n-1; i++)
6068  {
6069  state->d.ptr.p_double[i] = 0;
6070  }
6071  state->dnorm = 0;
6072 
6073  /*
6074  * Steps I=1, 2, ...
6075  */
6076 lbl_15:
6077  if( ae_false )
6078  {
6079  goto lbl_16;
6080  }
6081 
6082  /*
6083  * At I-th step State.RepIterationsCount=I.
6084  */
6085  state->repiterationscount = state->repiterationscount+1;
6086 
6087  /*
6088  * Bidiagonalization part:
6089  * beta[i+1]*u[i+1] = A_mod*v[i]-alpha[i]*u[i]
6090  * alpha[i+1]*v[i+1] = A_mod'*u[i+1] - beta[i+1]*v[i]
6091  *
6092  * NOTE: beta[i+1]=0 or alpha[i+1]=0 will lead to successful termination
6093  * in the end of the current iteration. In this case u/v are zero.
6094  * NOTE2: algorithm won't fail on zero alpha or beta (there will be no
6095  * division by zero because it will be stopped BEFORE division
6096  * occurs). However, near-zero alpha and beta won't stop algorithm
6097  * and, although no division by zero will happen, orthogonality
6098  * in U and V will be lost.
6099  */
6100  ae_v_move(&state->x.ptr.p_double[0], 1, &state->vi.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6101  state->repnmv = state->repnmv+1;
6102  linlsqr_clearrfields(state, _state);
6103  state->needmv = ae_true;
6104  state->rstate.stage = 4;
6105  goto lbl_rcomm;
6106 lbl_4:
6107  state->needmv = ae_false;
6108  for(i=0; i<=state->n-1; i++)
6109  {
6110  state->mv.ptr.p_double[state->m+i] = state->lambdai*state->vi.ptr.p_double[i];
6111  }
6112  state->betaip1 = 0;
6113  for(i=0; i<=summn-1; i++)
6114  {
6115  state->uip1.ptr.p_double[i] = state->mv.ptr.p_double[i]-state->alphai*state->ui.ptr.p_double[i];
6116  state->betaip1 = state->betaip1+state->uip1.ptr.p_double[i]*state->uip1.ptr.p_double[i];
6117  }
6118  if( ae_fp_neq(state->betaip1,0) )
6119  {
6120  state->betaip1 = ae_sqrt(state->betaip1, _state);
6121  for(i=0; i<=summn-1; i++)
6122  {
6123  state->uip1.ptr.p_double[i] = state->uip1.ptr.p_double[i]/state->betaip1;
6124  }
6125  }
6126  ae_v_move(&state->x.ptr.p_double[0], 1, &state->uip1.ptr.p_double[0], 1, ae_v_len(0,state->m-1));
6127  state->repnmv = state->repnmv+1;
6128  linlsqr_clearrfields(state, _state);
6129  state->needmtv = ae_true;
6130  state->rstate.stage = 5;
6131  goto lbl_rcomm;
6132 lbl_5:
6133  state->needmtv = ae_false;
6134  for(i=0; i<=state->n-1; i++)
6135  {
6136  state->mtv.ptr.p_double[i] = state->mtv.ptr.p_double[i]+state->lambdai*state->uip1.ptr.p_double[state->m+i];
6137  }
6138  state->alphaip1 = 0;
6139  for(i=0; i<=state->n-1; i++)
6140  {
6141  state->vip1.ptr.p_double[i] = state->mtv.ptr.p_double[i]-state->betaip1*state->vi.ptr.p_double[i];
6142  state->alphaip1 = state->alphaip1+state->vip1.ptr.p_double[i]*state->vip1.ptr.p_double[i];
6143  }
6144  if( ae_fp_neq(state->alphaip1,0) )
6145  {
6146  state->alphaip1 = ae_sqrt(state->alphaip1, _state);
6147  for(i=0; i<=state->n-1; i++)
6148  {
6149  state->vip1.ptr.p_double[i] = state->vip1.ptr.p_double[i]/state->alphaip1;
6150  }
6151  }
6152 
6153  /*
6154  * Build next orthogonal transformation
6155  */
6156  state->rhoi = safepythag2(state->rhobari, state->betaip1, _state);
6157  state->ci = state->rhobari/state->rhoi;
6158  state->si = state->betaip1/state->rhoi;
6159  state->theta = state->si*state->alphaip1;
6160  state->rhobarip1 = -state->ci*state->alphaip1;
6161  state->phii = state->ci*state->phibari;
6162  state->phibarip1 = state->si*state->phibari;
6163 
6164  /*
6165  * Update .RNorm
6166  *
6167  * This tricky formula is necessary because simply writing
6168  * State.R2:=State.PhiBarIP1*State.PhiBarIP1 does NOT guarantees
6169  * monotonic decrease of R2. Roundoff error combined with 80-bit
6170  * precision used internally by Intel chips allows R2 to increase
6171  * slightly in some rare, but possible cases. This property is
6172  * undesirable, so we prefer to guard against R increase.
6173  */
6174  state->r2 = ae_minreal(state->r2, state->phibarip1*state->phibarip1, _state);
6175 
6176  /*
6177  * Update d and DNorm, check condition-related stopping criteria
6178  */
6179  for(i=0; i<=state->n-1; i++)
6180  {
6181  state->d.ptr.p_double[i] = 1/state->rhoi*(state->vi.ptr.p_double[i]-state->theta*state->d.ptr.p_double[i]);
6182  state->dnorm = state->dnorm+state->d.ptr.p_double[i]*state->d.ptr.p_double[i];
6183  }
6184  if( ae_fp_greater_eq(ae_sqrt(state->dnorm, _state)*state->anorm,state->epsc) )
6185  {
6186  state->running = ae_false;
6187  state->repterminationtype = 7;
6188  result = ae_false;
6189  return result;
6190  }
6191 
6192  /*
6193  * Update x, output report
6194  */
6195  for(i=0; i<=state->n-1; i++)
6196  {
6197  state->rx.ptr.p_double[i] = state->rx.ptr.p_double[i]+state->phii/state->rhoi*state->omegai.ptr.p_double[i];
6198  }
6199  if( !state->xrep )
6200  {
6201  goto lbl_17;
6202  }
6203  ae_v_move(&state->x.ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6204  linlsqr_clearrfields(state, _state);
6205  state->xupdated = ae_true;
6206  state->rstate.stage = 6;
6207  goto lbl_rcomm;
6208 lbl_6:
6209  state->xupdated = ae_false;
6210 lbl_17:
6211 
6212  /*
6213  * Check stopping criteria
6214  * 1. achieved required number of iterations;
6215  * 2. ||Rk||<=EpsB*||B||;
6216  * 3. ||A^T*Rk||/(||A||*||Rk||)<=EpsA;
6217  */
6218  if( state->maxits>0&&state->repiterationscount>=state->maxits )
6219  {
6220 
6221  /*
6222  * Achieved required number of iterations
6223  */
6224  state->running = ae_false;
6225  state->repterminationtype = 5;
6226  result = ae_false;
6227  return result;
6228  }
6229  if( ae_fp_less_eq(state->phibarip1,state->epsb*bnorm) )
6230  {
6231 
6232  /*
6233  * ||Rk||<=EpsB*||B||, here ||Rk||=PhiBar
6234  */
6235  state->running = ae_false;
6236  state->repterminationtype = 1;
6237  result = ae_false;
6238  return result;
6239  }
6240  if( ae_fp_less_eq(state->alphaip1*ae_fabs(state->ci, _state)/state->anorm,state->epsa) )
6241  {
6242 
6243  /*
6244  * ||A^T*Rk||/(||A||*||Rk||)<=EpsA, here ||A^T*Rk||=PhiBar*Alpha[i+1]*|.C|
6245  */
6246  state->running = ae_false;
6247  state->repterminationtype = 4;
6248  result = ae_false;
6249  return result;
6250  }
6251 
6252  /*
6253  * Update omega
6254  */
6255  for(i=0; i<=state->n-1; i++)
6256  {
6257  state->omegaip1.ptr.p_double[i] = state->vip1.ptr.p_double[i]-state->theta/state->rhoi*state->omegai.ptr.p_double[i];
6258  }
6259 
6260  /*
6261  * Prepare for the next iteration - rename variables:
6262  * u[i] := u[i+1]
6263  * v[i] := v[i+1]
6264  * rho[i] := rho[i+1]
6265  * ...
6266  */
6267  ae_v_move(&state->ui.ptr.p_double[0], 1, &state->uip1.ptr.p_double[0], 1, ae_v_len(0,summn-1));
6268  ae_v_move(&state->vi.ptr.p_double[0], 1, &state->vip1.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6269  ae_v_move(&state->omegai.ptr.p_double[0], 1, &state->omegaip1.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6270  state->alphai = state->alphaip1;
6271  state->betai = state->betaip1;
6272  state->phibari = state->phibarip1;
6273  state->rhobari = state->rhobarip1;
6274  goto lbl_15;
6275 lbl_16:
6276  result = ae_false;
6277  return result;
6278 
6279  /*
6280  * Saving state
6281  */
6282 lbl_rcomm:
6283  result = ae_true;
6284  state->rstate.ia.ptr.p_int[0] = summn;
6285  state->rstate.ia.ptr.p_int[1] = i;
6286  state->rstate.ra.ptr.p_double[0] = bnorm;
6287  return result;
6288 }
6289 
6290 
6291 /*************************************************************************
6292 Procedure for solution of A*x=b with sparse A.
6293 
6294 INPUT PARAMETERS:
6295  State - algorithm state
6296  A - sparse M*N matrix in the CRS format (you MUST contvert it
6297  to CRS format by calling SparseConvertToCRS() function
6298  BEFORE you pass it to this function).
6299  B - right part, array[M]
6300 
6301 RESULT:
6302  This function returns no result.
6303  You can get solution by calling LinCGResults()
6304 
6305 NOTE: this function uses lightweight preconditioning - multiplication by
6306  inverse of diag(A). If you want, you can turn preconditioning off by
6307  calling LinLSQRSetPrecUnit(). However, preconditioning cost is low
6308  and preconditioner is very important for solution of badly scaled
6309  problems.
6310 
6311  -- ALGLIB --
6312  Copyright 30.11.2011 by Bochkanov Sergey
6313 *************************************************************************/
6315  sparsematrix* a,
6316  /* Real */ ae_vector* b,
6317  ae_state *_state)
6318 {
6319  ae_int_t n;
6320  ae_int_t i;
6321  ae_int_t j;
6322  ae_int_t t0;
6323  ae_int_t t1;
6324  double v;
6325 
6326 
6327  n = state->n;
6328  ae_assert(!state->running, "LinLSQRSolveSparse: you can not call this function when LinLSQRIteration is running", _state);
6329  ae_assert(b->cnt>=state->m, "LinLSQRSolveSparse: Length(B)<M", _state);
6330  ae_assert(isfinitevector(b, state->m, _state), "LinLSQRSolveSparse: B contains infinite or NaN values", _state);
6331 
6332  /*
6333  * Allocate temporaries
6334  */
6335  rvectorsetlengthatleast(&state->tmpd, n, _state);
6336  rvectorsetlengthatleast(&state->tmpx, n, _state);
6337 
6338  /*
6339  * Compute diagonal scaling matrix D
6340  */
6341  if( state->prectype==0 )
6342  {
6343 
6344  /*
6345  * Default preconditioner - inverse of column norms
6346  */
6347  for(i=0; i<=n-1; i++)
6348  {
6349  state->tmpd.ptr.p_double[i] = 0;
6350  }
6351  t0 = 0;
6352  t1 = 0;
6353  while(sparseenumerate(a, &t0, &t1, &i, &j, &v, _state))
6354  {
6355  state->tmpd.ptr.p_double[j] = state->tmpd.ptr.p_double[j]+ae_sqr(v, _state);
6356  }
6357  for(i=0; i<=n-1; i++)
6358  {
6359  if( ae_fp_greater(state->tmpd.ptr.p_double[i],0) )
6360  {
6361  state->tmpd.ptr.p_double[i] = 1/ae_sqrt(state->tmpd.ptr.p_double[i], _state);
6362  }
6363  else
6364  {
6365  state->tmpd.ptr.p_double[i] = 1;
6366  }
6367  }
6368  }
6369  else
6370  {
6371 
6372  /*
6373  * No diagonal scaling
6374  */
6375  for(i=0; i<=n-1; i++)
6376  {
6377  state->tmpd.ptr.p_double[i] = 1;
6378  }
6379  }
6380 
6381  /*
6382  * Solve.
6383  *
6384  * Instead of solving A*x=b we solve preconditioned system (A*D)*(inv(D)*x)=b.
6385  * Transformed A is not calculated explicitly, we just modify multiplication
6386  * by A or A'. After solution we modify State.RX so it will store untransformed
6387  * variables
6388  */
6389  linlsqrsetb(state, b, _state);
6390  linlsqrrestart(state, _state);
6391  while(linlsqriteration(state, _state))
6392  {
6393  if( state->needmv )
6394  {
6395  for(i=0; i<=n-1; i++)
6396  {
6397  state->tmpx.ptr.p_double[i] = state->tmpd.ptr.p_double[i]*state->x.ptr.p_double[i];
6398  }
6399  sparsemv(a, &state->tmpx, &state->mv, _state);
6400  }
6401  if( state->needmtv )
6402  {
6403  sparsemtv(a, &state->x, &state->mtv, _state);
6404  for(i=0; i<=n-1; i++)
6405  {
6406  state->mtv.ptr.p_double[i] = state->tmpd.ptr.p_double[i]*state->mtv.ptr.p_double[i];
6407  }
6408  }
6409  }
6410  for(i=0; i<=n-1; i++)
6411  {
6412  state->rx.ptr.p_double[i] = state->tmpd.ptr.p_double[i]*state->rx.ptr.p_double[i];
6413  }
6414 }
6415 
6416 
6417 /*************************************************************************
6418 This function sets stopping criteria.
6419 
6420 INPUT PARAMETERS:
6421  EpsA - algorithm will be stopped if ||A^T*Rk||/(||A||*||Rk||)<=EpsA.
6422  EpsB - algorithm will be stopped if ||Rk||<=EpsB*||B||
6423  MaxIts - algorithm will be stopped if number of iterations
6424  more than MaxIts.
6425 
6426 OUTPUT PARAMETERS:
6427  State - structure which stores algorithm state
6428 
6429 NOTE: if EpsA,EpsB,EpsC and MaxIts are zero then these variables will
6430 be setted as default values.
6431 
6432  -- ALGLIB --
6433  Copyright 30.11.2011 by Bochkanov Sergey
6434 *************************************************************************/
6436  double epsa,
6437  double epsb,
6438  ae_int_t maxits,
6439  ae_state *_state)
6440 {
6441 
6442 
6443  ae_assert(!state->running, "LinLSQRSetCond: you can not call this function when LinLSQRIteration is running", _state);
6444  ae_assert(ae_isfinite(epsa, _state)&&ae_fp_greater_eq(epsa,0), "LinLSQRSetCond: EpsA is negative, INF or NAN", _state);
6445  ae_assert(ae_isfinite(epsb, _state)&&ae_fp_greater_eq(epsb,0), "LinLSQRSetCond: EpsB is negative, INF or NAN", _state);
6446  ae_assert(maxits>=0, "LinLSQRSetCond: MaxIts is negative", _state);
6447  if( (ae_fp_eq(epsa,0)&&ae_fp_eq(epsb,0))&&maxits==0 )
6448  {
6449  state->epsa = linlsqr_atol;
6450  state->epsb = linlsqr_btol;
6451  state->maxits = state->n;
6452  }
6453  else
6454  {
6455  state->epsa = epsa;
6456  state->epsb = epsb;
6457  state->maxits = maxits;
6458  }
6459 }
6460 
6461 
6462 /*************************************************************************
6463 LSQR solver: results.
6464 
6465 This function must be called after LinLSQRSolve
6466 
6467 INPUT PARAMETERS:
6468  State - algorithm state
6469 
6470 OUTPUT PARAMETERS:
6471  X - array[N], solution
6472  Rep - optimization report:
6473  * Rep.TerminationType completion code:
6474  * 1 ||Rk||<=EpsB*||B||
6475  * 4 ||A^T*Rk||/(||A||*||Rk||)<=EpsA
6476  * 5 MaxIts steps was taken
6477  * 7 rounding errors prevent further progress,
6478  X contains best point found so far.
6479  (sometimes returned on singular systems)
6480  * Rep.IterationsCount contains iterations count
6481  * NMV contains number of matrix-vector calculations
6482 
6483  -- ALGLIB --
6484  Copyright 30.11.2011 by Bochkanov Sergey
6485 *************************************************************************/
6487  /* Real */ ae_vector* x,
6488  linlsqrreport* rep,
6489  ae_state *_state)
6490 {
6491 
6492  ae_vector_clear(x);
6493  _linlsqrreport_clear(rep);
6494 
6495  ae_assert(!state->running, "LinLSQRResult: you can not call this function when LinLSQRIteration is running", _state);
6496  if( x->cnt<state->n )
6497  {
6498  ae_vector_set_length(x, state->n, _state);
6499  }
6500  ae_v_move(&x->ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6501  rep->iterationscount = state->repiterationscount;
6502  rep->nmv = state->repnmv;
6503  rep->terminationtype = state->repterminationtype;
6504 }
6505 
6506 
6507 /*************************************************************************
6508 This function turns on/off reporting.
6509 
6510 INPUT PARAMETERS:
6511  State - structure which stores algorithm state
6512  NeedXRep- whether iteration reports are needed or not
6513 
6514 If NeedXRep is True, algorithm will call rep() callback function if it is
6515 provided to MinCGOptimize().
6516 
6517  -- ALGLIB --
6518  Copyright 30.11.2011 by Bochkanov Sergey
6519 *************************************************************************/
6521  ae_bool needxrep,
6522  ae_state *_state)
6523 {
6524 
6525 
6526  state->xrep = needxrep;
6527 }
6528 
6529 
6530 /*************************************************************************
6531 This function restarts LinLSQRIteration
6532 
6533  -- ALGLIB --
6534  Copyright 30.11.2011 by Bochkanov Sergey
6535 *************************************************************************/
6536 void linlsqrrestart(linlsqrstate* state, ae_state *_state)
6537 {
6538 
6539 
6540  ae_vector_set_length(&state->rstate.ia, 1+1, _state);
6541  ae_vector_set_length(&state->rstate.ra, 0+1, _state);
6542  state->rstate.stage = -1;
6543  linlsqr_clearrfields(state, _state);
6544 }
6545 
6546 
6547 /*************************************************************************
6548 Clears request fileds (to be sure that we don't forgot to clear something)
6549 *************************************************************************/
6550 static void linlsqr_clearrfields(linlsqrstate* state, ae_state *_state)
6551 {
6552 
6553 
6554  state->xupdated = ae_false;
6555  state->needmv = ae_false;
6556  state->needmtv = ae_false;
6557  state->needmv2 = ae_false;
6558  state->needvmv = ae_false;
6559  state->needprec = ae_false;
6560 }
6561 
6562 
6563 ae_bool _linlsqrstate_init(void* _p, ae_state *_state, ae_bool make_automatic)
6564 {
6565  linlsqrstate *p = (linlsqrstate*)_p;
6566  ae_touch_ptr((void*)p);
6567  if( !_normestimatorstate_init(&p->nes, _state, make_automatic) )
6568  return ae_false;
6569  if( !ae_vector_init(&p->rx, 0, DT_REAL, _state, make_automatic) )
6570  return ae_false;
6571  if( !ae_vector_init(&p->b, 0, DT_REAL, _state, make_automatic) )
6572  return ae_false;
6573  if( !ae_vector_init(&p->ui, 0, DT_REAL, _state, make_automatic) )
6574  return ae_false;
6575  if( !ae_vector_init(&p->uip1, 0, DT_REAL, _state, make_automatic) )
6576  return ae_false;
6577  if( !ae_vector_init(&p->vi, 0, DT_REAL, _state, make_automatic) )
6578  return ae_false;
6579  if( !ae_vector_init(&p->vip1, 0, DT_REAL, _state, make_automatic) )
6580  return ae_false;
6581  if( !ae_vector_init(&p->omegai, 0, DT_REAL, _state, make_automatic) )
6582  return ae_false;
6583  if( !ae_vector_init(&p->omegaip1, 0, DT_REAL, _state, make_automatic) )
6584  return ae_false;
6585  if( !ae_vector_init(&p->d, 0, DT_REAL, _state, make_automatic) )
6586  return ae_false;
6587  if( !ae_vector_init(&p->x, 0, DT_REAL, _state, make_automatic) )
6588  return ae_false;
6589  if( !ae_vector_init(&p->mv, 0, DT_REAL, _state, make_automatic) )
6590  return ae_false;
6591  if( !ae_vector_init(&p->mtv, 0, DT_REAL, _state, make_automatic) )
6592  return ae_false;
6593  if( !ae_vector_init(&p->tmpd, 0, DT_REAL, _state, make_automatic) )
6594  return ae_false;
6595  if( !ae_vector_init(&p->tmpx, 0, DT_REAL, _state, make_automatic) )
6596  return ae_false;
6597  if( !_rcommstate_init(&p->rstate, _state, make_automatic) )
6598  return ae_false;
6599  return ae_true;
6600 }
6601 
6602 
6603 ae_bool _linlsqrstate_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
6604 {
6605  linlsqrstate *dst = (linlsqrstate*)_dst;
6606  linlsqrstate *src = (linlsqrstate*)_src;
6607  if( !_normestimatorstate_init_copy(&dst->nes, &src->nes, _state, make_automatic) )
6608  return ae_false;
6609  if( !ae_vector_init_copy(&dst->rx, &src->rx, _state, make_automatic) )
6610  return ae_false;
6611  if( !ae_vector_init_copy(&dst->b, &src->b, _state, make_automatic) )
6612  return ae_false;
6613  dst->n = src->n;
6614  dst->m = src->m;
6615  dst->prectype = src->prectype;
6616  if( !ae_vector_init_copy(&dst->ui, &src->ui, _state, make_automatic) )
6617  return ae_false;
6618  if( !ae_vector_init_copy(&dst->uip1, &src->uip1, _state, make_automatic) )
6619  return ae_false;
6620  if( !ae_vector_init_copy(&dst->vi, &src->vi, _state, make_automatic) )
6621  return ae_false;
6622  if( !ae_vector_init_copy(&dst->vip1, &src->vip1, _state, make_automatic) )
6623  return ae_false;
6624  if( !ae_vector_init_copy(&dst->omegai, &src->omegai, _state, make_automatic) )
6625  return ae_false;
6626  if( !ae_vector_init_copy(&dst->omegaip1, &src->omegaip1, _state, make_automatic) )
6627  return ae_false;
6628  dst->alphai = src->alphai;
6629  dst->alphaip1 = src->alphaip1;
6630  dst->betai = src->betai;
6631  dst->betaip1 = src->betaip1;
6632  dst->phibari = src->phibari;
6633  dst->phibarip1 = src->phibarip1;
6634  dst->phii = src->phii;
6635  dst->rhobari = src->rhobari;
6636  dst->rhobarip1 = src->rhobarip1;
6637  dst->rhoi = src->rhoi;
6638  dst->ci = src->ci;
6639  dst->si = src->si;
6640  dst->theta = src->theta;
6641  dst->lambdai = src->lambdai;
6642  if( !ae_vector_init_copy(&dst->d, &src->d, _state, make_automatic) )
6643  return ae_false;
6644  dst->anorm = src->anorm;
6645  dst->bnorm2 = src->bnorm2;
6646  dst->dnorm = src->dnorm;
6647  dst->r2 = src->r2;
6648  if( !ae_vector_init_copy(&dst->x, &src->x, _state, make_automatic) )
6649  return ae_false;
6650  if( !ae_vector_init_copy(&dst->mv, &src->mv, _state, make_automatic) )
6651  return ae_false;
6652  if( !ae_vector_init_copy(&dst->mtv, &src->mtv, _state, make_automatic) )
6653  return ae_false;
6654  dst->epsa = src->epsa;
6655  dst->epsb = src->epsb;
6656  dst->epsc = src->epsc;
6657  dst->maxits = src->maxits;
6658  dst->xrep = src->xrep;
6659  dst->xupdated = src->xupdated;
6660  dst->needmv = src->needmv;
6661  dst->needmtv = src->needmtv;
6662  dst->needmv2 = src->needmv2;
6663  dst->needvmv = src->needvmv;
6664  dst->needprec = src->needprec;
6666  dst->repnmv = src->repnmv;
6668  dst->running = src->running;
6669  if( !ae_vector_init_copy(&dst->tmpd, &src->tmpd, _state, make_automatic) )
6670  return ae_false;
6671  if( !ae_vector_init_copy(&dst->tmpx, &src->tmpx, _state, make_automatic) )
6672  return ae_false;
6673  if( !_rcommstate_init_copy(&dst->rstate, &src->rstate, _state, make_automatic) )
6674  return ae_false;
6675  return ae_true;
6676 }
6677 
6678 
6679 void _linlsqrstate_clear(void* _p)
6680 {
6681  linlsqrstate *p = (linlsqrstate*)_p;
6682  ae_touch_ptr((void*)p);
6683  _normestimatorstate_clear(&p->nes);
6684  ae_vector_clear(&p->rx);
6685  ae_vector_clear(&p->b);
6686  ae_vector_clear(&p->ui);
6687  ae_vector_clear(&p->uip1);
6688  ae_vector_clear(&p->vi);
6689  ae_vector_clear(&p->vip1);
6690  ae_vector_clear(&p->omegai);
6691  ae_vector_clear(&p->omegaip1);
6692  ae_vector_clear(&p->d);
6693  ae_vector_clear(&p->x);
6694  ae_vector_clear(&p->mv);
6695  ae_vector_clear(&p->mtv);
6696  ae_vector_clear(&p->tmpd);
6697  ae_vector_clear(&p->tmpx);
6698  _rcommstate_clear(&p->rstate);
6699 }
6700 
6701 
6703 {
6704  linlsqrstate *p = (linlsqrstate*)_p;
6705  ae_touch_ptr((void*)p);
6706  _normestimatorstate_destroy(&p->nes);
6707  ae_vector_destroy(&p->rx);
6708  ae_vector_destroy(&p->b);
6709  ae_vector_destroy(&p->ui);
6710  ae_vector_destroy(&p->uip1);
6711  ae_vector_destroy(&p->vi);
6712  ae_vector_destroy(&p->vip1);
6713  ae_vector_destroy(&p->omegai);
6714  ae_vector_destroy(&p->omegaip1);
6715  ae_vector_destroy(&p->d);
6716  ae_vector_destroy(&p->x);
6717  ae_vector_destroy(&p->mv);
6718  ae_vector_destroy(&p->mtv);
6719  ae_vector_destroy(&p->tmpd);
6720  ae_vector_destroy(&p->tmpx);
6721  _rcommstate_destroy(&p->rstate);
6722 }
6723 
6724 
6725 ae_bool _linlsqrreport_init(void* _p, ae_state *_state, ae_bool make_automatic)
6726 {
6727  linlsqrreport *p = (linlsqrreport*)_p;
6728  ae_touch_ptr((void*)p);
6729  return ae_true;
6730 }
6731 
6732 
6733 ae_bool _linlsqrreport_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
6734 {
6735  linlsqrreport *dst = (linlsqrreport*)_dst;
6736  linlsqrreport *src = (linlsqrreport*)_src;
6737  dst->iterationscount = src->iterationscount;
6738  dst->nmv = src->nmv;
6739  dst->terminationtype = src->terminationtype;
6740  return ae_true;
6741 }
6742 
6743 
6744 void _linlsqrreport_clear(void* _p)
6745 {
6746  linlsqrreport *p = (linlsqrreport*)_p;
6747  ae_touch_ptr((void*)p);
6748 }
6749 
6750 
6752 {
6753  linlsqrreport *p = (linlsqrreport*)_p;
6754  ae_touch_ptr((void*)p);
6755 }
6756 
6757 
6758 
6759 
6760 /*************************************************************************
6761 This function initializes linear CG Solver. This solver is used to solve
6762 symmetric positive definite problems. If you want to solve nonsymmetric
6763 (or non-positive definite) problem you may use LinLSQR solver provided by
6764 ALGLIB.
6765 
6766 USAGE:
6767 1. User initializes algorithm state with LinCGCreate() call
6768 2. User tunes solver parameters with LinCGSetCond() and other functions
6769 3. Optionally, user sets starting point with LinCGSetStartingPoint()
6770 4. User calls LinCGSolveSparse() function which takes algorithm state and
6771  SparseMatrix object.
6772 5. User calls LinCGResults() to get solution
6773 6. Optionally, user may call LinCGSolveSparse() again to solve another
6774  problem with different matrix and/or right part without reinitializing
6775  LinCGState structure.
6776 
6777 INPUT PARAMETERS:
6778  N - problem dimension, N>0
6779 
6780 OUTPUT PARAMETERS:
6781  State - structure which stores algorithm state
6782 
6783  -- ALGLIB --
6784  Copyright 14.11.2011 by Bochkanov Sergey
6785 *************************************************************************/
6786 void lincgcreate(ae_int_t n, lincgstate* state, ae_state *_state)
6787 {
6788  ae_int_t i;
6789 
6790  _lincgstate_clear(state);
6791 
6792  ae_assert(n>0, "LinCGCreate: N<=0", _state);
6793  state->n = n;
6794  state->prectype = 0;
6795  state->itsbeforerestart = n;
6796  state->itsbeforerupdate = 10;
6797  state->epsf = lincg_defaultprecision;
6798  state->maxits = 0;
6799  state->xrep = ae_false;
6800  state->running = ae_false;
6801 
6802  /*
6803  * * allocate arrays
6804  * * set RX to NAN (just for the case user calls Results() without
6805  * calling SolveSparse()
6806  * * set starting point to zero
6807  * * we do NOT initialize B here because we assume that user should
6808  * initializate it using LinCGSetB() function. In case he forgets
6809  * to do so, exception will be thrown in the LinCGIteration().
6810  */
6811  ae_vector_set_length(&state->rx, state->n, _state);
6812  ae_vector_set_length(&state->startx, state->n, _state);
6813  ae_vector_set_length(&state->b, state->n, _state);
6814  for(i=0; i<=state->n-1; i++)
6815  {
6816  state->rx.ptr.p_double[i] = _state->v_nan;
6817  state->startx.ptr.p_double[i] = 0.0;
6818  state->b.ptr.p_double[i] = 0;
6819  }
6820  ae_vector_set_length(&state->cx, state->n, _state);
6821  ae_vector_set_length(&state->p, state->n, _state);
6822  ae_vector_set_length(&state->r, state->n, _state);
6823  ae_vector_set_length(&state->cr, state->n, _state);
6824  ae_vector_set_length(&state->z, state->n, _state);
6825  ae_vector_set_length(&state->cz, state->n, _state);
6826  ae_vector_set_length(&state->x, state->n, _state);
6827  ae_vector_set_length(&state->mv, state->n, _state);
6828  ae_vector_set_length(&state->pv, state->n, _state);
6829  lincg_updateitersdata(state, _state);
6830  ae_vector_set_length(&state->rstate.ia, 0+1, _state);
6831  ae_vector_set_length(&state->rstate.ra, 2+1, _state);
6832  state->rstate.stage = -1;
6833 }
6834 
6835 
6836 /*************************************************************************
6837 This function sets starting point.
6838 By default, zero starting point is used.
6839 
6840 INPUT PARAMETERS:
6841  X - starting point, array[N]
6842 
6843 OUTPUT PARAMETERS:
6844  State - structure which stores algorithm state
6845 
6846  -- ALGLIB --
6847  Copyright 14.11.2011 by Bochkanov Sergey
6848 *************************************************************************/
6850  /* Real */ ae_vector* x,
6851  ae_state *_state)
6852 {
6853 
6854 
6855  ae_assert(!state->running, "LinCGSetStartingPoint: you can not change starting point because LinCGIteration() function is running", _state);
6856  ae_assert(state->n<=x->cnt, "LinCGSetStartingPoint: Length(X)<N", _state);
6857  ae_assert(isfinitevector(x, state->n, _state), "LinCGSetStartingPoint: X contains infinite or NaN values!", _state);
6858  ae_v_move(&state->startx.ptr.p_double[0], 1, &x->ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6859 }
6860 
6861 
6862 /*************************************************************************
6863 This function sets right part. By default, right part is zero.
6864 
6865 INPUT PARAMETERS:
6866  B - right part, array[N].
6867 
6868 OUTPUT PARAMETERS:
6869  State - structure which stores algorithm state
6870 
6871  -- ALGLIB --
6872  Copyright 14.11.2011 by Bochkanov Sergey
6873 *************************************************************************/
6874 void lincgsetb(lincgstate* state,
6875  /* Real */ ae_vector* b,
6876  ae_state *_state)
6877 {
6878 
6879 
6880  ae_assert(!state->running, "LinCGSetB: you can not set B, because function LinCGIteration is running!", _state);
6881  ae_assert(b->cnt>=state->n, "LinCGSetB: Length(B)<N", _state);
6882  ae_assert(isfinitevector(b, state->n, _state), "LinCGSetB: B contains infinite or NaN values!", _state);
6883  ae_v_move(&state->b.ptr.p_double[0], 1, &b->ptr.p_double[0], 1, ae_v_len(0,state->n-1));
6884 }
6885 
6886 
6887 /*************************************************************************
6888 This function changes preconditioning settings of LinCGSolveSparse()
6889 function. By default, SolveSparse() uses diagonal preconditioner, but if
6890 you want to use solver without preconditioning, you can call this function
6891 which forces solver to use unit matrix for preconditioning.
6892 
6893 INPUT PARAMETERS:
6894  State - structure which stores algorithm state
6895 
6896  -- ALGLIB --
6897  Copyright 19.11.2012 by Bochkanov Sergey
6898 *************************************************************************/
6899 void lincgsetprecunit(lincgstate* state, ae_state *_state)
6900 {
6901 
6902 
6903  ae_assert(!state->running, "LinCGSetPrecUnit: you can not change preconditioner, because function LinCGIteration is running!", _state);
6904  state->prectype = -1;
6905 }
6906 
6907 
6908 /*************************************************************************
6909 This function changes preconditioning settings of LinCGSolveSparse()
6910 function. LinCGSolveSparse() will use diagonal of the system matrix as
6911 preconditioner. This preconditioning mode is active by default.
6912 
6913 INPUT PARAMETERS:
6914  State - structure which stores algorithm state
6915 
6916  -- ALGLIB --
6917  Copyright 19.11.2012 by Bochkanov Sergey
6918 *************************************************************************/
6919 void lincgsetprecdiag(lincgstate* state, ae_state *_state)
6920 {
6921 
6922 
6923  ae_assert(!state->running, "LinCGSetPrecDiag: you can not change preconditioner, because function LinCGIteration is running!", _state);
6924  state->prectype = 0;
6925 }
6926 
6927 
6928 /*************************************************************************
6929 This function sets stopping criteria.
6930 
6931 INPUT PARAMETERS:
6932  EpsF - algorithm will be stopped if norm of residual is less than
6933  EpsF*||b||.
6934  MaxIts - algorithm will be stopped if number of iterations is more
6935  than MaxIts.
6936 
6937 OUTPUT PARAMETERS:
6938  State - structure which stores algorithm state
6939 
6940 NOTES:
6941 If both EpsF and MaxIts are zero then small EpsF will be set to small
6942 value.
6943 
6944  -- ALGLIB --
6945  Copyright 14.11.2011 by Bochkanov Sergey
6946 *************************************************************************/
6948  double epsf,
6949  ae_int_t maxits,
6950  ae_state *_state)
6951 {
6952 
6953 
6954  ae_assert(!state->running, "LinCGSetCond: you can not change stopping criteria when LinCGIteration() is running", _state);
6955  ae_assert(ae_isfinite(epsf, _state)&&ae_fp_greater_eq(epsf,0), "LinCGSetCond: EpsF is negative or contains infinite or NaN values", _state);
6956  ae_assert(maxits>=0, "LinCGSetCond: MaxIts is negative", _state);
6957  if( ae_fp_eq(epsf,0)&&maxits==0 )
6958  {
6959  state->epsf = lincg_defaultprecision;
6960  state->maxits = maxits;
6961  }
6962  else
6963  {
6964  state->epsf = epsf;
6965  state->maxits = maxits;
6966  }
6967 }
6968 
6969 
6970 /*************************************************************************
6971 Reverse communication version of linear CG.
6972 
6973  -- ALGLIB --
6974  Copyright 14.11.2011 by Bochkanov Sergey
6975 *************************************************************************/
6977 {
6978  ae_int_t i;
6979  double uvar;
6980  double bnorm;
6981  double v;
6982  ae_bool result;
6983 
6984 
6985 
6986  /*
6987  * Reverse communication preparations
6988  * I know it looks ugly, but it works the same way
6989  * anywhere from C++ to Python.
6990  *
6991  * This code initializes locals by:
6992  * * random values determined during code
6993  * generation - on first subroutine call
6994  * * values from previous call - on subsequent calls
6995  */
6996  if( state->rstate.stage>=0 )
6997  {
6998  i = state->rstate.ia.ptr.p_int[0];
6999  uvar = state->rstate.ra.ptr.p_double[0];
7000  bnorm = state->rstate.ra.ptr.p_double[1];
7001  v = state->rstate.ra.ptr.p_double[2];
7002  }
7003  else
7004  {
7005  i = -983;
7006  uvar = -989;
7007  bnorm = -834;
7008  v = 900;
7009  }
7010  if( state->rstate.stage==0 )
7011  {
7012  goto lbl_0;
7013  }
7014  if( state->rstate.stage==1 )
7015  {
7016  goto lbl_1;
7017  }
7018  if( state->rstate.stage==2 )
7019  {
7020  goto lbl_2;
7021  }
7022  if( state->rstate.stage==3 )
7023  {
7024  goto lbl_3;
7025  }
7026  if( state->rstate.stage==4 )
7027  {
7028  goto lbl_4;
7029  }
7030  if( state->rstate.stage==5 )
7031  {
7032  goto lbl_5;
7033  }
7034  if( state->rstate.stage==6 )
7035  {
7036  goto lbl_6;
7037  }
7038  if( state->rstate.stage==7 )
7039  {
7040  goto lbl_7;
7041  }
7042 
7043  /*
7044  * Routine body
7045  */
7046  ae_assert(state->b.cnt>0, "LinCGIteration: B is not initialized (you must initialize B by LinCGSetB() call", _state);
7047  state->running = ae_true;
7048  state->repnmv = 0;
7049  lincg_clearrfields(state, _state);
7050  lincg_updateitersdata(state, _state);
7051 
7052  /*
7053  * Start 0-th iteration
7054  */
7055  ae_v_move(&state->rx.ptr.p_double[0], 1, &state->startx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7056  ae_v_move(&state->x.ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7057  state->repnmv = state->repnmv+1;
7058  lincg_clearrfields(state, _state);
7059  state->needvmv = ae_true;
7060  state->rstate.stage = 0;
7061  goto lbl_rcomm;
7062 lbl_0:
7063  state->needvmv = ae_false;
7064  bnorm = 0;
7065  state->r2 = 0;
7066  state->meritfunction = 0;
7067  for(i=0; i<=state->n-1; i++)
7068  {
7069  state->r.ptr.p_double[i] = state->b.ptr.p_double[i]-state->mv.ptr.p_double[i];
7070  state->r2 = state->r2+state->r.ptr.p_double[i]*state->r.ptr.p_double[i];
7071  state->meritfunction = state->meritfunction+state->mv.ptr.p_double[i]*state->rx.ptr.p_double[i]-2*state->b.ptr.p_double[i]*state->rx.ptr.p_double[i];
7072  bnorm = bnorm+state->b.ptr.p_double[i]*state->b.ptr.p_double[i];
7073  }
7074  bnorm = ae_sqrt(bnorm, _state);
7075 
7076  /*
7077  * Output first report
7078  */
7079  if( !state->xrep )
7080  {
7081  goto lbl_8;
7082  }
7083  ae_v_move(&state->x.ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7084  lincg_clearrfields(state, _state);
7085  state->xupdated = ae_true;
7086  state->rstate.stage = 1;
7087  goto lbl_rcomm;
7088 lbl_1:
7089  state->xupdated = ae_false;
7090 lbl_8:
7091 
7092  /*
7093  * Is x0 a solution?
7094  */
7095  if( !ae_isfinite(state->r2, _state)||ae_fp_less_eq(ae_sqrt(state->r2, _state),state->epsf*bnorm) )
7096  {
7097  state->running = ae_false;
7098  if( ae_isfinite(state->r2, _state) )
7099  {
7100  state->repterminationtype = 1;
7101  }
7102  else
7103  {
7104  state->repterminationtype = -4;
7105  }
7106  result = ae_false;
7107  return result;
7108  }
7109 
7110  /*
7111  * Calculate Z and P
7112  */
7113  ae_v_move(&state->x.ptr.p_double[0], 1, &state->r.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7114  state->repnmv = state->repnmv+1;
7115  lincg_clearrfields(state, _state);
7116  state->needprec = ae_true;
7117  state->rstate.stage = 2;
7118  goto lbl_rcomm;
7119 lbl_2:
7120  state->needprec = ae_false;
7121  for(i=0; i<=state->n-1; i++)
7122  {
7123  state->z.ptr.p_double[i] = state->pv.ptr.p_double[i];
7124  state->p.ptr.p_double[i] = state->z.ptr.p_double[i];
7125  }
7126 
7127  /*
7128  * Other iterations(1..N)
7129  */
7130  state->repiterationscount = 0;
7131 lbl_10:
7132  if( ae_false )
7133  {
7134  goto lbl_11;
7135  }
7136  state->repiterationscount = state->repiterationscount+1;
7137 
7138  /*
7139  * Calculate Alpha
7140  */
7141  ae_v_move(&state->x.ptr.p_double[0], 1, &state->p.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7142  state->repnmv = state->repnmv+1;
7143  lincg_clearrfields(state, _state);
7144  state->needvmv = ae_true;
7145  state->rstate.stage = 3;
7146  goto lbl_rcomm;
7147 lbl_3:
7148  state->needvmv = ae_false;
7149  if( !ae_isfinite(state->vmv, _state)||ae_fp_less_eq(state->vmv,0) )
7150  {
7151 
7152  /*
7153  * a) Overflow when calculating VMV
7154  * b) non-positive VMV (non-SPD matrix)
7155  */
7156  state->running = ae_false;
7157  if( ae_isfinite(state->vmv, _state) )
7158  {
7159  state->repterminationtype = -5;
7160  }
7161  else
7162  {
7163  state->repterminationtype = -4;
7164  }
7165  result = ae_false;
7166  return result;
7167  }
7168  state->alpha = 0;
7169  for(i=0; i<=state->n-1; i++)
7170  {
7171  state->alpha = state->alpha+state->r.ptr.p_double[i]*state->z.ptr.p_double[i];
7172  }
7173  state->alpha = state->alpha/state->vmv;
7174  if( !ae_isfinite(state->alpha, _state) )
7175  {
7176 
7177  /*
7178  * Overflow when calculating Alpha
7179  */
7180  state->running = ae_false;
7181  state->repterminationtype = -4;
7182  result = ae_false;
7183  return result;
7184  }
7185 
7186  /*
7187  * Next step toward solution
7188  */
7189  for(i=0; i<=state->n-1; i++)
7190  {
7191  state->cx.ptr.p_double[i] = state->rx.ptr.p_double[i]+state->alpha*state->p.ptr.p_double[i];
7192  }
7193 
7194  /*
7195  * Calculate R:
7196  * * use recurrent relation to update R
7197  * * at every ItsBeforeRUpdate-th iteration recalculate it from scratch, using matrix-vector product
7198  * in case R grows instead of decreasing, algorithm is terminated with positive completion code
7199  */
7200  if( !(state->itsbeforerupdate==0||state->repiterationscount%state->itsbeforerupdate!=0) )
7201  {
7202  goto lbl_12;
7203  }
7204 
7205  /*
7206  * Calculate R using recurrent formula
7207  */
7208  for(i=0; i<=state->n-1; i++)
7209  {
7210  state->cr.ptr.p_double[i] = state->r.ptr.p_double[i]-state->alpha*state->mv.ptr.p_double[i];
7211  state->x.ptr.p_double[i] = state->cr.ptr.p_double[i];
7212  }
7213  goto lbl_13;
7214 lbl_12:
7215 
7216  /*
7217  * Calculate R using matrix-vector multiplication
7218  */
7219  ae_v_move(&state->x.ptr.p_double[0], 1, &state->cx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7220  state->repnmv = state->repnmv+1;
7221  lincg_clearrfields(state, _state);
7222  state->needmv = ae_true;
7223  state->rstate.stage = 4;
7224  goto lbl_rcomm;
7225 lbl_4:
7226  state->needmv = ae_false;
7227  for(i=0; i<=state->n-1; i++)
7228  {
7229  state->cr.ptr.p_double[i] = state->b.ptr.p_double[i]-state->mv.ptr.p_double[i];
7230  state->x.ptr.p_double[i] = state->cr.ptr.p_double[i];
7231  }
7232 
7233  /*
7234  * Calculating merit function
7235  * Check emergency stopping criterion
7236  */
7237  v = 0;
7238  for(i=0; i<=state->n-1; i++)
7239  {
7240  v = v+state->mv.ptr.p_double[i]*state->cx.ptr.p_double[i]-2*state->b.ptr.p_double[i]*state->cx.ptr.p_double[i];
7241  }
7242  if( ae_fp_less(v,state->meritfunction) )
7243  {
7244  goto lbl_14;
7245  }
7246  for(i=0; i<=state->n-1; i++)
7247  {
7248  if( !ae_isfinite(state->rx.ptr.p_double[i], _state) )
7249  {
7250  state->running = ae_false;
7251  state->repterminationtype = -4;
7252  result = ae_false;
7253  return result;
7254  }
7255  }
7256 
7257  /*
7258  *output last report
7259  */
7260  if( !state->xrep )
7261  {
7262  goto lbl_16;
7263  }
7264  ae_v_move(&state->x.ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7265  lincg_clearrfields(state, _state);
7266  state->xupdated = ae_true;
7267  state->rstate.stage = 5;
7268  goto lbl_rcomm;
7269 lbl_5:
7270  state->xupdated = ae_false;
7271 lbl_16:
7272  state->running = ae_false;
7273  state->repterminationtype = 7;
7274  result = ae_false;
7275  return result;
7276 lbl_14:
7277  state->meritfunction = v;
7278 lbl_13:
7279  ae_v_move(&state->rx.ptr.p_double[0], 1, &state->cx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7280 
7281  /*
7282  * calculating RNorm
7283  *
7284  * NOTE: monotonic decrease of R2 is not guaranteed by algorithm.
7285  */
7286  state->r2 = 0;
7287  for(i=0; i<=state->n-1; i++)
7288  {
7289  state->r2 = state->r2+state->cr.ptr.p_double[i]*state->cr.ptr.p_double[i];
7290  }
7291 
7292  /*
7293  *output report
7294  */
7295  if( !state->xrep )
7296  {
7297  goto lbl_18;
7298  }
7299  ae_v_move(&state->x.ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7300  lincg_clearrfields(state, _state);
7301  state->xupdated = ae_true;
7302  state->rstate.stage = 6;
7303  goto lbl_rcomm;
7304 lbl_6:
7305  state->xupdated = ae_false;
7306 lbl_18:
7307 
7308  /*
7309  *stopping criterion
7310  *achieved the required precision
7311  */
7312  if( !ae_isfinite(state->r2, _state)||ae_fp_less_eq(ae_sqrt(state->r2, _state),state->epsf*bnorm) )
7313  {
7314  state->running = ae_false;
7315  if( ae_isfinite(state->r2, _state) )
7316  {
7317  state->repterminationtype = 1;
7318  }
7319  else
7320  {
7321  state->repterminationtype = -4;
7322  }
7323  result = ae_false;
7324  return result;
7325  }
7326  if( state->repiterationscount>=state->maxits&&state->maxits>0 )
7327  {
7328  for(i=0; i<=state->n-1; i++)
7329  {
7330  if( !ae_isfinite(state->rx.ptr.p_double[i], _state) )
7331  {
7332  state->running = ae_false;
7333  state->repterminationtype = -4;
7334  result = ae_false;
7335  return result;
7336  }
7337  }
7338 
7339  /*
7340  *if X is finite number
7341  */
7342  state->running = ae_false;
7343  state->repterminationtype = 5;
7344  result = ae_false;
7345  return result;
7346  }
7347  ae_v_move(&state->x.ptr.p_double[0], 1, &state->cr.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7348 
7349  /*
7350  *prepere of parameters for next iteration
7351  */
7352  state->repnmv = state->repnmv+1;
7353  lincg_clearrfields(state, _state);
7354  state->needprec = ae_true;
7355  state->rstate.stage = 7;
7356  goto lbl_rcomm;
7357 lbl_7:
7358  state->needprec = ae_false;
7359  ae_v_move(&state->cz.ptr.p_double[0], 1, &state->pv.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7360  if( state->repiterationscount%state->itsbeforerestart!=0 )
7361  {
7362  state->beta = 0;
7363  uvar = 0;
7364  for(i=0; i<=state->n-1; i++)
7365  {
7366  state->beta = state->beta+state->cz.ptr.p_double[i]*state->cr.ptr.p_double[i];
7367  uvar = uvar+state->z.ptr.p_double[i]*state->r.ptr.p_double[i];
7368  }
7369 
7370  /*
7371  *check that UVar is't INF or is't zero
7372  */
7373  if( !ae_isfinite(uvar, _state)||ae_fp_eq(uvar,0) )
7374  {
7375  state->running = ae_false;
7376  state->repterminationtype = -4;
7377  result = ae_false;
7378  return result;
7379  }
7380 
7381  /*
7382  *calculate .BETA
7383  */
7384  state->beta = state->beta/uvar;
7385 
7386  /*
7387  *check that .BETA neither INF nor NaN
7388  */
7389  if( !ae_isfinite(state->beta, _state) )
7390  {
7391  state->running = ae_false;
7392  state->repterminationtype = -1;
7393  result = ae_false;
7394  return result;
7395  }
7396  for(i=0; i<=state->n-1; i++)
7397  {
7398  state->p.ptr.p_double[i] = state->cz.ptr.p_double[i]+state->beta*state->p.ptr.p_double[i];
7399  }
7400  }
7401  else
7402  {
7403  ae_v_move(&state->p.ptr.p_double[0], 1, &state->cz.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7404  }
7405 
7406  /*
7407  *prepere data for next iteration
7408  */
7409  for(i=0; i<=state->n-1; i++)
7410  {
7411 
7412  /*
7413  *write (k+1)th iteration to (k )th iteration
7414  */
7415  state->r.ptr.p_double[i] = state->cr.ptr.p_double[i];
7416  state->z.ptr.p_double[i] = state->cz.ptr.p_double[i];
7417  }
7418  goto lbl_10;
7419 lbl_11:
7420  result = ae_false;
7421  return result;
7422 
7423  /*
7424  * Saving state
7425  */
7426 lbl_rcomm:
7427  result = ae_true;
7428  state->rstate.ia.ptr.p_int[0] = i;
7429  state->rstate.ra.ptr.p_double[0] = uvar;
7430  state->rstate.ra.ptr.p_double[1] = bnorm;
7431  state->rstate.ra.ptr.p_double[2] = v;
7432  return result;
7433 }
7434 
7435 
7436 /*************************************************************************
7437 Procedure for solution of A*x=b with sparse A.
7438 
7439 INPUT PARAMETERS:
7440  State - algorithm state
7441  A - sparse matrix in the CRS format (you MUST contvert it to
7442  CRS format by calling SparseConvertToCRS() function).
7443  IsUpper - whether upper or lower triangle of A is used:
7444  * IsUpper=True => only upper triangle is used and lower
7445  triangle is not referenced at all
7446  * IsUpper=False => only lower triangle is used and upper
7447  triangle is not referenced at all
7448  B - right part, array[N]
7449 
7450 RESULT:
7451  This function returns no result.
7452  You can get solution by calling LinCGResults()
7453 
7454 NOTE: this function uses lightweight preconditioning - multiplication by
7455  inverse of diag(A). If you want, you can turn preconditioning off by
7456  calling LinCGSetPrecUnit(). However, preconditioning cost is low and
7457  preconditioner is very important for solution of badly scaled
7458  problems.
7459 
7460  -- ALGLIB --
7461  Copyright 14.11.2011 by Bochkanov Sergey
7462 *************************************************************************/
7464  sparsematrix* a,
7465  ae_bool isupper,
7466  /* Real */ ae_vector* b,
7467  ae_state *_state)
7468 {
7469  ae_int_t n;
7470  ae_int_t i;
7471  double v;
7472  double vmv;
7473 
7474 
7475  n = state->n;
7476  ae_assert(b->cnt>=state->n, "LinCGSetB: Length(B)<N", _state);
7477  ae_assert(isfinitevector(b, state->n, _state), "LinCGSetB: B contains infinite or NaN values!", _state);
7478 
7479  /*
7480  * Allocate temporaries
7481  */
7482  rvectorsetlengthatleast(&state->tmpd, n, _state);
7483 
7484  /*
7485  * Compute diagonal scaling matrix D
7486  */
7487  if( state->prectype==0 )
7488  {
7489 
7490  /*
7491  * Default preconditioner - inverse of matrix diagonal
7492  */
7493  for(i=0; i<=n-1; i++)
7494  {
7495  v = sparsegetdiagonal(a, i, _state);
7496  if( ae_fp_greater(v,0) )
7497  {
7498  state->tmpd.ptr.p_double[i] = 1/ae_sqrt(v, _state);
7499  }
7500  else
7501  {
7502  state->tmpd.ptr.p_double[i] = 1;
7503  }
7504  }
7505  }
7506  else
7507  {
7508 
7509  /*
7510  * No diagonal scaling
7511  */
7512  for(i=0; i<=n-1; i++)
7513  {
7514  state->tmpd.ptr.p_double[i] = 1;
7515  }
7516  }
7517 
7518  /*
7519  * Solve
7520  */
7521  lincgrestart(state, _state);
7522  lincgsetb(state, b, _state);
7523  while(lincgiteration(state, _state))
7524  {
7525 
7526  /*
7527  * Process different requests from optimizer
7528  */
7529  if( state->needmv )
7530  {
7531  sparsesmv(a, isupper, &state->x, &state->mv, _state);
7532  }
7533  if( state->needvmv )
7534  {
7535  sparsesmv(a, isupper, &state->x, &state->mv, _state);
7536  vmv = ae_v_dotproduct(&state->x.ptr.p_double[0], 1, &state->mv.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7537  state->vmv = vmv;
7538  }
7539  if( state->needprec )
7540  {
7541  for(i=0; i<=n-1; i++)
7542  {
7543  state->pv.ptr.p_double[i] = state->x.ptr.p_double[i]*ae_sqr(state->tmpd.ptr.p_double[i], _state);
7544  }
7545  }
7546  }
7547 }
7548 
7549 
7550 /*************************************************************************
7551 CG-solver: results.
7552 
7553 This function must be called after LinCGSolve
7554 
7555 INPUT PARAMETERS:
7556  State - algorithm state
7557 
7558 OUTPUT PARAMETERS:
7559  X - array[N], solution
7560  Rep - optimization report:
7561  * Rep.TerminationType completion code:
7562  * -5 input matrix is either not positive definite,
7563  too large or too small
7564  * -4 overflow/underflow during solution
7565  (ill conditioned problem)
7566  * 1 ||residual||<=EpsF*||b||
7567  * 5 MaxIts steps was taken
7568  * 7 rounding errors prevent further progress,
7569  best point found is returned
7570  * Rep.IterationsCount contains iterations count
7571  * NMV contains number of matrix-vector calculations
7572 
7573  -- ALGLIB --
7574  Copyright 14.11.2011 by Bochkanov Sergey
7575 *************************************************************************/
7577  /* Real */ ae_vector* x,
7578  lincgreport* rep,
7579  ae_state *_state)
7580 {
7581 
7582  ae_vector_clear(x);
7583  _lincgreport_clear(rep);
7584 
7585  ae_assert(!state->running, "LinCGResult: you can not get result, because function LinCGIteration has been launched!", _state);
7586  if( x->cnt<state->n )
7587  {
7588  ae_vector_set_length(x, state->n, _state);
7589  }
7590  ae_v_move(&x->ptr.p_double[0], 1, &state->rx.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
7591  rep->iterationscount = state->repiterationscount;
7592  rep->nmv = state->repnmv;
7593  rep->terminationtype = state->repterminationtype;
7594  rep->r2 = state->r2;
7595 }
7596 
7597 
7598 /*************************************************************************
7599 This function sets restart frequency. By default, algorithm is restarted
7600 after N subsequent iterations.
7601 
7602  -- ALGLIB --
7603  Copyright 14.11.2011 by Bochkanov Sergey
7604 *************************************************************************/
7606  ae_int_t srf,
7607  ae_state *_state)
7608 {
7609 
7610 
7611  ae_assert(!state->running, "LinCGSetRestartFreq: you can not change restart frequency when LinCGIteration() is running", _state);
7612  ae_assert(srf>0, "LinCGSetRestartFreq: non-positive SRF", _state);
7613  state->itsbeforerestart = srf;
7614 }
7615 
7616 
7617 /*************************************************************************
7618 This function sets frequency of residual recalculations.
7619 
7620 Algorithm updates residual r_k using iterative formula, but recalculates
7621 it from scratch after each 10 iterations. It is done to avoid accumulation
7622 of numerical errors and to stop algorithm when r_k starts to grow.
7623 
7624 Such low update frequence (1/10) gives very little overhead, but makes
7625 algorithm a bit more robust against numerical errors. However, you may
7626 change it
7627 
7628 INPUT PARAMETERS:
7629  Freq - desired update frequency, Freq>=0.
7630  Zero value means that no updates will be done.
7631 
7632  -- ALGLIB --
7633  Copyright 14.11.2011 by Bochkanov Sergey
7634 *************************************************************************/
7636  ae_int_t freq,
7637  ae_state *_state)
7638 {
7639 
7640 
7641  ae_assert(!state->running, "LinCGSetRUpdateFreq: you can not change update frequency when LinCGIteration() is running", _state);
7642  ae_assert(freq>=0, "LinCGSetRUpdateFreq: non-positive Freq", _state);
7643  state->itsbeforerupdate = freq;
7644 }
7645 
7646 
7647 /*************************************************************************
7648 This function turns on/off reporting.
7649 
7650 INPUT PARAMETERS:
7651  State - structure which stores algorithm state
7652  NeedXRep- whether iteration reports are needed or not
7653 
7654 If NeedXRep is True, algorithm will call rep() callback function if it is
7655 provided to MinCGOptimize().
7656 
7657  -- ALGLIB --
7658  Copyright 14.11.2011 by Bochkanov Sergey
7659 *************************************************************************/
7660 void lincgsetxrep(lincgstate* state, ae_bool needxrep, ae_state *_state)
7661 {
7662 
7663 
7664  state->xrep = needxrep;
7665 }
7666 
7667 
7668 /*************************************************************************
7669 Procedure for restart function LinCGIteration
7670 
7671  -- ALGLIB --
7672  Copyright 14.11.2011 by Bochkanov Sergey
7673 *************************************************************************/
7674 void lincgrestart(lincgstate* state, ae_state *_state)
7675 {
7676 
7677 
7678  ae_vector_set_length(&state->rstate.ia, 0+1, _state);
7679  ae_vector_set_length(&state->rstate.ra, 2+1, _state);
7680  state->rstate.stage = -1;
7681  lincg_clearrfields(state, _state);
7682 }
7683 
7684 
7685 /*************************************************************************
7686 Clears request fileds (to be sure that we don't forgot to clear something)
7687 *************************************************************************/
7688 static void lincg_clearrfields(lincgstate* state, ae_state *_state)
7689 {
7690 
7691 
7692  state->xupdated = ae_false;
7693  state->needmv = ae_false;
7694  state->needmtv = ae_false;
7695  state->needmv2 = ae_false;
7696  state->needvmv = ae_false;
7697  state->needprec = ae_false;
7698 }
7699 
7700 
7701 /*************************************************************************
7702 Clears request fileds (to be sure that we don't forgot to clear something)
7703 *************************************************************************/
7704 static void lincg_updateitersdata(lincgstate* state, ae_state *_state)
7705 {
7706 
7707 
7708  state->repiterationscount = 0;
7709  state->repnmv = 0;
7710  state->repterminationtype = 0;
7711 }
7712 
7713 
7714 ae_bool _lincgstate_init(void* _p, ae_state *_state, ae_bool make_automatic)
7715 {
7716  lincgstate *p = (lincgstate*)_p;
7717  ae_touch_ptr((void*)p);
7718  if( !ae_vector_init(&p->rx, 0, DT_REAL, _state, make_automatic) )
7719  return ae_false;
7720  if( !ae_vector_init(&p->b, 0, DT_REAL, _state, make_automatic) )
7721  return ae_false;
7722  if( !ae_vector_init(&p->cx, 0, DT_REAL, _state, make_automatic) )
7723  return ae_false;
7724  if( !ae_vector_init(&p->cr, 0, DT_REAL, _state, make_automatic) )
7725  return ae_false;
7726  if( !ae_vector_init(&p->cz, 0, DT_REAL, _state, make_automatic) )
7727  return ae_false;
7728  if( !ae_vector_init(&p->p, 0, DT_REAL, _state, make_automatic) )
7729  return ae_false;
7730  if( !ae_vector_init(&p->r, 0, DT_REAL, _state, make_automatic) )
7731  return ae_false;
7732  if( !ae_vector_init(&p->z, 0, DT_REAL, _state, make_automatic) )
7733  return ae_false;
7734  if( !ae_vector_init(&p->x, 0, DT_REAL, _state, make_automatic) )
7735  return ae_false;
7736  if( !ae_vector_init(&p->mv, 0, DT_REAL, _state, make_automatic) )
7737  return ae_false;
7738  if( !ae_vector_init(&p->pv, 0, DT_REAL, _state, make_automatic) )
7739  return ae_false;
7740  if( !ae_vector_init(&p->startx, 0, DT_REAL, _state, make_automatic) )
7741  return ae_false;
7742  if( !ae_vector_init(&p->tmpd, 0, DT_REAL, _state, make_automatic) )
7743  return ae_false;
7744  if( !_rcommstate_init(&p->rstate, _state, make_automatic) )
7745  return ae_false;
7746  return ae_true;
7747 }
7748 
7749 
7750 ae_bool _lincgstate_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
7751 {
7752  lincgstate *dst = (lincgstate*)_dst;
7753  lincgstate *src = (lincgstate*)_src;
7754  if( !ae_vector_init_copy(&dst->rx, &src->rx, _state, make_automatic) )
7755  return ae_false;
7756  if( !ae_vector_init_copy(&dst->b, &src->b, _state, make_automatic) )
7757  return ae_false;
7758  dst->n = src->n;
7759  dst->prectype = src->prectype;
7760  if( !ae_vector_init_copy(&dst->cx, &src->cx, _state, make_automatic) )
7761  return ae_false;
7762  if( !ae_vector_init_copy(&dst->cr, &src->cr, _state, make_automatic) )
7763  return ae_false;
7764  if( !ae_vector_init_copy(&dst->cz, &src->cz, _state, make_automatic) )
7765  return ae_false;
7766  if( !ae_vector_init_copy(&dst->p, &src->p, _state, make_automatic) )
7767  return ae_false;
7768  if( !ae_vector_init_copy(&dst->r, &src->r, _state, make_automatic) )
7769  return ae_false;
7770  if( !ae_vector_init_copy(&dst->z, &src->z, _state, make_automatic) )
7771  return ae_false;
7772  dst->alpha = src->alpha;
7773  dst->beta = src->beta;
7774  dst->r2 = src->r2;
7775  dst->meritfunction = src->meritfunction;
7776  if( !ae_vector_init_copy(&dst->x, &src->x, _state, make_automatic) )
7777  return ae_false;
7778  if( !ae_vector_init_copy(&dst->mv, &src->mv, _state, make_automatic) )
7779  return ae_false;
7780  if( !ae_vector_init_copy(&dst->pv, &src->pv, _state, make_automatic) )
7781  return ae_false;
7782  dst->vmv = src->vmv;
7783  if( !ae_vector_init_copy(&dst->startx, &src->startx, _state, make_automatic) )
7784  return ae_false;
7785  dst->epsf = src->epsf;
7786  dst->maxits = src->maxits;
7787  dst->itsbeforerestart = src->itsbeforerestart;
7788  dst->itsbeforerupdate = src->itsbeforerupdate;
7789  dst->xrep = src->xrep;
7790  dst->xupdated = src->xupdated;
7791  dst->needmv = src->needmv;
7792  dst->needmtv = src->needmtv;
7793  dst->needmv2 = src->needmv2;
7794  dst->needvmv = src->needvmv;
7795  dst->needprec = src->needprec;
7797  dst->repnmv = src->repnmv;
7799  dst->running = src->running;
7800  if( !ae_vector_init_copy(&dst->tmpd, &src->tmpd, _state, make_automatic) )
7801  return ae_false;
7802  if( !_rcommstate_init_copy(&dst->rstate, &src->rstate, _state, make_automatic) )
7803  return ae_false;
7804  return ae_true;
7805 }
7806 
7807 
7808 void _lincgstate_clear(void* _p)
7809 {
7810  lincgstate *p = (lincgstate*)_p;
7811  ae_touch_ptr((void*)p);
7812  ae_vector_clear(&p->rx);
7813  ae_vector_clear(&p->b);
7814  ae_vector_clear(&p->cx);
7815  ae_vector_clear(&p->cr);
7816  ae_vector_clear(&p->cz);
7817  ae_vector_clear(&p->p);
7818  ae_vector_clear(&p->r);
7819  ae_vector_clear(&p->z);
7820  ae_vector_clear(&p->x);
7821  ae_vector_clear(&p->mv);
7822  ae_vector_clear(&p->pv);
7823  ae_vector_clear(&p->startx);
7824  ae_vector_clear(&p->tmpd);
7825  _rcommstate_clear(&p->rstate);
7826 }
7827 
7828 
7829 void _lincgstate_destroy(void* _p)
7830 {
7831  lincgstate *p = (lincgstate*)_p;
7832  ae_touch_ptr((void*)p);
7833  ae_vector_destroy(&p->rx);
7834  ae_vector_destroy(&p->b);
7835  ae_vector_destroy(&p->cx);
7836  ae_vector_destroy(&p->cr);
7837  ae_vector_destroy(&p->cz);
7838  ae_vector_destroy(&p->p);
7839  ae_vector_destroy(&p->r);
7840  ae_vector_destroy(&p->z);
7841  ae_vector_destroy(&p->x);
7842  ae_vector_destroy(&p->mv);
7843  ae_vector_destroy(&p->pv);
7844  ae_vector_destroy(&p->startx);
7845  ae_vector_destroy(&p->tmpd);
7846  _rcommstate_destroy(&p->rstate);
7847 }
7848 
7849 
7850 ae_bool _lincgreport_init(void* _p, ae_state *_state, ae_bool make_automatic)
7851 {
7852  lincgreport *p = (lincgreport*)_p;
7853  ae_touch_ptr((void*)p);
7854  return ae_true;
7855 }
7856 
7857 
7858 ae_bool _lincgreport_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
7859 {
7860  lincgreport *dst = (lincgreport*)_dst;
7861  lincgreport *src = (lincgreport*)_src;
7862  dst->iterationscount = src->iterationscount;
7863  dst->nmv = src->nmv;
7864  dst->terminationtype = src->terminationtype;
7865  dst->r2 = src->r2;
7866  return ae_true;
7867 }
7868 
7869 
7870 void _lincgreport_clear(void* _p)
7871 {
7872  lincgreport *p = (lincgreport*)_p;
7873  ae_touch_ptr((void*)p);
7874 }
7875 
7876 
7877 void _lincgreport_destroy(void* _p)
7878 {
7879  lincgreport *p = (lincgreport*)_p;
7880  ae_touch_ptr((void*)p);
7881 }
7882 
7883 
7884 
7885 
7886 /*************************************************************************
7887  LEVENBERG-MARQUARDT-LIKE NONLINEAR SOLVER
7888 
7889 DESCRIPTION:
7890 This algorithm solves system of nonlinear equations
7891  F[0](x[0], ..., x[n-1]) = 0
7892  F[1](x[0], ..., x[n-1]) = 0
7893  ...
7894  F[M-1](x[0], ..., x[n-1]) = 0
7895 with M/N do not necessarily coincide. Algorithm converges quadratically
7896 under following conditions:
7897  * the solution set XS is nonempty
7898  * for some xs in XS there exist such neighbourhood N(xs) that:
7899  * vector function F(x) and its Jacobian J(x) are continuously
7900  differentiable on N
7901  * ||F(x)|| provides local error bound on N, i.e. there exists such
7902  c1, that ||F(x)||>c1*distance(x,XS)
7903 Note that these conditions are much more weaker than usual non-singularity
7904 conditions. For example, algorithm will converge for any affine function
7905 F (whether its Jacobian singular or not).
7906 
7907 
7908 REQUIREMENTS:
7909 Algorithm will request following information during its operation:
7910 * function vector F[] and Jacobian matrix at given point X
7911 * value of merit function f(x)=F[0]^2(x)+...+F[M-1]^2(x) at given point X
7912 
7913 
7914 USAGE:
7915 1. User initializes algorithm state with NLEQCreateLM() call
7916 2. User tunes solver parameters with NLEQSetCond(), NLEQSetStpMax() and
7917  other functions
7918 3. User calls NLEQSolve() function which takes algorithm state and
7919  pointers (delegates, etc.) to callback functions which calculate merit
7920  function value and Jacobian.
7921 4. User calls NLEQResults() to get solution
7922 5. Optionally, user may call NLEQRestartFrom() to solve another problem
7923  with same parameters (N/M) but another starting point and/or another
7924  function vector. NLEQRestartFrom() allows to reuse already initialized
7925  structure.
7926 
7927 
7928 INPUT PARAMETERS:
7929  N - space dimension, N>1:
7930  * if provided, only leading N elements of X are used
7931  * if not provided, determined automatically from size of X
7932  M - system size
7933  X - starting point
7934 
7935 
7936 OUTPUT PARAMETERS:
7937  State - structure which stores algorithm state
7938 
7939 
7940 NOTES:
7941 1. you may tune stopping conditions with NLEQSetCond() function
7942 2. if target function contains exp() or other fast growing functions, and
7943  optimization algorithm makes too large steps which leads to overflow,
7944  use NLEQSetStpMax() function to bound algorithm's steps.
7945 3. this algorithm is a slightly modified implementation of the method
7946  described in 'Levenberg-Marquardt method for constrained nonlinear
7947  equations with strong local convergence properties' by Christian Kanzow
7948  Nobuo Yamashita and Masao Fukushima and further developed in 'On the
7949  convergence of a New Levenberg-Marquardt Method' by Jin-yan Fan and
7950  Ya-Xiang Yuan.
7951 
7952 
7953  -- ALGLIB --
7954  Copyright 20.08.2009 by Bochkanov Sergey
7955 *************************************************************************/
7957  ae_int_t m,
7958  /* Real */ ae_vector* x,
7959  nleqstate* state,
7960  ae_state *_state)
7961 {
7962 
7963  _nleqstate_clear(state);
7964 
7965  ae_assert(n>=1, "NLEQCreateLM: N<1!", _state);
7966  ae_assert(m>=1, "NLEQCreateLM: M<1!", _state);
7967  ae_assert(x->cnt>=n, "NLEQCreateLM: Length(X)<N!", _state);
7968  ae_assert(isfinitevector(x, n, _state), "NLEQCreateLM: X contains infinite or NaN values!", _state);
7969 
7970  /*
7971  * Initialize
7972  */
7973  state->n = n;
7974  state->m = m;
7975  nleqsetcond(state, 0, 0, _state);
7976  nleqsetxrep(state, ae_false, _state);
7977  nleqsetstpmax(state, 0, _state);
7978  ae_vector_set_length(&state->x, n, _state);
7979  ae_vector_set_length(&state->xbase, n, _state);
7980  ae_matrix_set_length(&state->j, m, n, _state);
7981  ae_vector_set_length(&state->fi, m, _state);
7982  ae_vector_set_length(&state->rightpart, n, _state);
7983  ae_vector_set_length(&state->candstep, n, _state);
7984  nleqrestartfrom(state, x, _state);
7985 }
7986 
7987 
7988 /*************************************************************************
7989 This function sets stopping conditions for the nonlinear solver
7990 
7991 INPUT PARAMETERS:
7992  State - structure which stores algorithm state
7993  EpsF - >=0
7994  The subroutine finishes its work if on k+1-th iteration
7995  the condition ||F||<=EpsF is satisfied
7996  MaxIts - maximum number of iterations. If MaxIts=0, the number of
7997  iterations is unlimited.
7998 
7999 Passing EpsF=0 and MaxIts=0 simultaneously will lead to automatic
8000 stopping criterion selection (small EpsF).
8001 
8002 NOTES:
8003 
8004  -- ALGLIB --
8005  Copyright 20.08.2010 by Bochkanov Sergey
8006 *************************************************************************/
8008  double epsf,
8009  ae_int_t maxits,
8010  ae_state *_state)
8011 {
8012 
8013 
8014  ae_assert(ae_isfinite(epsf, _state), "NLEQSetCond: EpsF is not finite number!", _state);
8015  ae_assert(ae_fp_greater_eq(epsf,0), "NLEQSetCond: negative EpsF!", _state);
8016  ae_assert(maxits>=0, "NLEQSetCond: negative MaxIts!", _state);
8017  if( ae_fp_eq(epsf,0)&&maxits==0 )
8018  {
8019  epsf = 1.0E-6;
8020  }
8021  state->epsf = epsf;
8022  state->maxits = maxits;
8023 }
8024 
8025 
8026 /*************************************************************************
8027 This function turns on/off reporting.
8028 
8029 INPUT PARAMETERS:
8030  State - structure which stores algorithm state
8031  NeedXRep- whether iteration reports are needed or not
8032 
8033 If NeedXRep is True, algorithm will call rep() callback function if it is
8034 provided to NLEQSolve().
8035 
8036  -- ALGLIB --
8037  Copyright 20.08.2010 by Bochkanov Sergey
8038 *************************************************************************/
8039 void nleqsetxrep(nleqstate* state, ae_bool needxrep, ae_state *_state)
8040 {
8041 
8042 
8043  state->xrep = needxrep;
8044 }
8045 
8046 
8047 /*************************************************************************
8048 This function sets maximum step length
8049 
8050 INPUT PARAMETERS:
8051  State - structure which stores algorithm state
8052  StpMax - maximum step length, >=0. Set StpMax to 0.0, if you don't
8053  want to limit step length.
8054 
8055 Use this subroutine when target function contains exp() or other fast
8056 growing functions, and algorithm makes too large steps which lead to
8057 overflow. This function allows us to reject steps that are too large (and
8058 therefore expose us to the possible overflow) without actually calculating
8059 function value at the x+stp*d.
8060 
8061  -- ALGLIB --
8062  Copyright 20.08.2010 by Bochkanov Sergey
8063 *************************************************************************/
8064 void nleqsetstpmax(nleqstate* state, double stpmax, ae_state *_state)
8065 {
8066 
8067 
8068  ae_assert(ae_isfinite(stpmax, _state), "NLEQSetStpMax: StpMax is not finite!", _state);
8069  ae_assert(ae_fp_greater_eq(stpmax,0), "NLEQSetStpMax: StpMax<0!", _state);
8070  state->stpmax = stpmax;
8071 }
8072 
8073 
8074 /*************************************************************************
8075 
8076  -- ALGLIB --
8077  Copyright 20.03.2009 by Bochkanov Sergey
8078 *************************************************************************/
8080 {
8081  ae_int_t n;
8082  ae_int_t m;
8083  ae_int_t i;
8084  double lambdaup;
8085  double lambdadown;
8086  double lambdav;
8087  double rho;
8088  double mu;
8089  double stepnorm;
8090  ae_bool b;
8091  ae_bool result;
8092 
8093 
8094 
8095  /*
8096  * Reverse communication preparations
8097  * I know it looks ugly, but it works the same way
8098  * anywhere from C++ to Python.
8099  *
8100  * This code initializes locals by:
8101  * * random values determined during code
8102  * generation - on first subroutine call
8103  * * values from previous call - on subsequent calls
8104  */
8105  if( state->rstate.stage>=0 )
8106  {
8107  n = state->rstate.ia.ptr.p_int[0];
8108  m = state->rstate.ia.ptr.p_int[1];
8109  i = state->rstate.ia.ptr.p_int[2];
8110  b = state->rstate.ba.ptr.p_bool[0];
8111  lambdaup = state->rstate.ra.ptr.p_double[0];
8112  lambdadown = state->rstate.ra.ptr.p_double[1];
8113  lambdav = state->rstate.ra.ptr.p_double[2];
8114  rho = state->rstate.ra.ptr.p_double[3];
8115  mu = state->rstate.ra.ptr.p_double[4];
8116  stepnorm = state->rstate.ra.ptr.p_double[5];
8117  }
8118  else
8119  {
8120  n = -983;
8121  m = -989;
8122  i = -834;
8123  b = ae_false;
8124  lambdaup = -287;
8125  lambdadown = 364;
8126  lambdav = 214;
8127  rho = -338;
8128  mu = -686;
8129  stepnorm = 912;
8130  }
8131  if( state->rstate.stage==0 )
8132  {
8133  goto lbl_0;
8134  }
8135  if( state->rstate.stage==1 )
8136  {
8137  goto lbl_1;
8138  }
8139  if( state->rstate.stage==2 )
8140  {
8141  goto lbl_2;
8142  }
8143  if( state->rstate.stage==3 )
8144  {
8145  goto lbl_3;
8146  }
8147  if( state->rstate.stage==4 )
8148  {
8149  goto lbl_4;
8150  }
8151 
8152  /*
8153  * Routine body
8154  */
8155 
8156  /*
8157  * Prepare
8158  */
8159  n = state->n;
8160  m = state->m;
8161  state->repterminationtype = 0;
8162  state->repiterationscount = 0;
8163  state->repnfunc = 0;
8164  state->repnjac = 0;
8165 
8166  /*
8167  * Calculate F/G, initialize algorithm
8168  */
8169  nleq_clearrequestfields(state, _state);
8170  state->needf = ae_true;
8171  state->rstate.stage = 0;
8172  goto lbl_rcomm;
8173 lbl_0:
8174  state->needf = ae_false;
8175  state->repnfunc = state->repnfunc+1;
8176  ae_v_move(&state->xbase.ptr.p_double[0], 1, &state->x.ptr.p_double[0], 1, ae_v_len(0,n-1));
8177  state->fbase = state->f;
8178  state->fprev = ae_maxrealnumber;
8179  if( !state->xrep )
8180  {
8181  goto lbl_5;
8182  }
8183 
8184  /*
8185  * progress report
8186  */
8187  nleq_clearrequestfields(state, _state);
8188  state->xupdated = ae_true;
8189  state->rstate.stage = 1;
8190  goto lbl_rcomm;
8191 lbl_1:
8192  state->xupdated = ae_false;
8193 lbl_5:
8194  if( ae_fp_less_eq(state->f,ae_sqr(state->epsf, _state)) )
8195  {
8196  state->repterminationtype = 1;
8197  result = ae_false;
8198  return result;
8199  }
8200 
8201  /*
8202  * Main cycle
8203  */
8204  lambdaup = 10;
8205  lambdadown = 0.3;
8206  lambdav = 0.001;
8207  rho = 1;
8208 lbl_7:
8209  if( ae_false )
8210  {
8211  goto lbl_8;
8212  }
8213 
8214  /*
8215  * Get Jacobian;
8216  * before we get to this point we already have State.XBase filled
8217  * with current point and State.FBase filled with function value
8218  * at XBase
8219  */
8220  nleq_clearrequestfields(state, _state);
8221  state->needfij = ae_true;
8222  ae_v_move(&state->x.ptr.p_double[0], 1, &state->xbase.ptr.p_double[0], 1, ae_v_len(0,n-1));
8223  state->rstate.stage = 2;
8224  goto lbl_rcomm;
8225 lbl_2:
8226  state->needfij = ae_false;
8227  state->repnfunc = state->repnfunc+1;
8228  state->repnjac = state->repnjac+1;
8229  rmatrixmv(n, m, &state->j, 0, 0, 1, &state->fi, 0, &state->rightpart, 0, _state);
8230  ae_v_muld(&state->rightpart.ptr.p_double[0], 1, ae_v_len(0,n-1), -1);
8231 
8232  /*
8233  * Inner cycle: find good lambda
8234  */
8235 lbl_9:
8236  if( ae_false )
8237  {
8238  goto lbl_10;
8239  }
8240 
8241  /*
8242  * Solve (J^T*J + (Lambda+Mu)*I)*y = J^T*F
8243  * to get step d=-y where:
8244  * * Mu=||F|| - is damping parameter for nonlinear system
8245  * * Lambda - is additional Levenberg-Marquardt parameter
8246  * for better convergence when far away from minimum
8247  */
8248  for(i=0; i<=n-1; i++)
8249  {
8250  state->candstep.ptr.p_double[i] = 0;
8251  }
8252  fblssolvecgx(&state->j, m, n, lambdav, &state->rightpart, &state->candstep, &state->cgbuf, _state);
8253 
8254  /*
8255  * Normalize step (it must be no more than StpMax)
8256  */
8257  stepnorm = 0;
8258  for(i=0; i<=n-1; i++)
8259  {
8260  if( ae_fp_neq(state->candstep.ptr.p_double[i],0) )
8261  {
8262  stepnorm = 1;
8263  break;
8264  }
8265  }
8266  linminnormalized(&state->candstep, &stepnorm, n, _state);
8267  if( ae_fp_neq(state->stpmax,0) )
8268  {
8269  stepnorm = ae_minreal(stepnorm, state->stpmax, _state);
8270  }
8271 
8272  /*
8273  * Test new step - is it good enough?
8274  * * if not, Lambda is increased and we try again.
8275  * * if step is good, we decrease Lambda and move on.
8276  *
8277  * We can break this cycle on two occasions:
8278  * * step is so small that x+step==x (in floating point arithmetics)
8279  * * lambda is so large
8280  */
8281  ae_v_move(&state->x.ptr.p_double[0], 1, &state->xbase.ptr.p_double[0], 1, ae_v_len(0,n-1));
8282  ae_v_addd(&state->x.ptr.p_double[0], 1, &state->candstep.ptr.p_double[0], 1, ae_v_len(0,n-1), stepnorm);
8283  b = ae_true;
8284  for(i=0; i<=n-1; i++)
8285  {
8286  if( ae_fp_neq(state->x.ptr.p_double[i],state->xbase.ptr.p_double[i]) )
8287  {
8288  b = ae_false;
8289  break;
8290  }
8291  }
8292  if( b )
8293  {
8294 
8295  /*
8296  * Step is too small, force zero step and break
8297  */
8298  stepnorm = 0;
8299  ae_v_move(&state->x.ptr.p_double[0], 1, &state->xbase.ptr.p_double[0], 1, ae_v_len(0,n-1));
8300  state->f = state->fbase;
8301  goto lbl_10;
8302  }
8303  nleq_clearrequestfields(state, _state);
8304  state->needf = ae_true;
8305  state->rstate.stage = 3;
8306  goto lbl_rcomm;
8307 lbl_3:
8308  state->needf = ae_false;
8309  state->repnfunc = state->repnfunc+1;
8310  if( ae_fp_less(state->f,state->fbase) )
8311  {
8312 
8313  /*
8314  * function value decreased, move on
8315  */
8316  nleq_decreaselambda(&lambdav, &rho, lambdadown, _state);
8317  goto lbl_10;
8318  }
8319  if( !nleq_increaselambda(&lambdav, &rho, lambdaup, _state) )
8320  {
8321 
8322  /*
8323  * Lambda is too large (near overflow), force zero step and break
8324  */
8325  stepnorm = 0;
8326  ae_v_move(&state->x.ptr.p_double[0], 1, &state->xbase.ptr.p_double[0], 1, ae_v_len(0,n-1));
8327  state->f = state->fbase;
8328  goto lbl_10;
8329  }
8330  goto lbl_9;
8331 lbl_10:
8332 
8333  /*
8334  * Accept step:
8335  * * new position
8336  * * new function value
8337  */
8338  state->fbase = state->f;
8339  ae_v_addd(&state->xbase.ptr.p_double[0], 1, &state->candstep.ptr.p_double[0], 1, ae_v_len(0,n-1), stepnorm);
8340  state->repiterationscount = state->repiterationscount+1;
8341 
8342  /*
8343  * Report new iteration
8344  */
8345  if( !state->xrep )
8346  {
8347  goto lbl_11;
8348  }
8349  nleq_clearrequestfields(state, _state);
8350  state->xupdated = ae_true;
8351  state->f = state->fbase;
8352  ae_v_move(&state->x.ptr.p_double[0], 1, &state->xbase.ptr.p_double[0], 1, ae_v_len(0,n-1));
8353  state->rstate.stage = 4;
8354  goto lbl_rcomm;
8355 lbl_4:
8356  state->xupdated = ae_false;
8357 lbl_11:
8358 
8359  /*
8360  * Test stopping conditions on F, step (zero/non-zero) and MaxIts;
8361  * If one of the conditions is met, RepTerminationType is changed.
8362  */
8363  if( ae_fp_less_eq(ae_sqrt(state->f, _state),state->epsf) )
8364  {
8365  state->repterminationtype = 1;
8366  }
8367  if( ae_fp_eq(stepnorm,0)&&state->repterminationtype==0 )
8368  {
8369  state->repterminationtype = -4;
8370  }
8371  if( state->repiterationscount>=state->maxits&&state->maxits>0 )
8372  {
8373  state->repterminationtype = 5;
8374  }
8375  if( state->repterminationtype!=0 )
8376  {
8377  goto lbl_8;
8378  }
8379 
8380  /*
8381  * Now, iteration is finally over
8382  */
8383  goto lbl_7;
8384 lbl_8:
8385  result = ae_false;
8386  return result;
8387 
8388  /*
8389  * Saving state
8390  */
8391 lbl_rcomm:
8392  result = ae_true;
8393  state->rstate.ia.ptr.p_int[0] = n;
8394  state->rstate.ia.ptr.p_int[1] = m;
8395  state->rstate.ia.ptr.p_int[2] = i;
8396  state->rstate.ba.ptr.p_bool[0] = b;
8397  state->rstate.ra.ptr.p_double[0] = lambdaup;
8398  state->rstate.ra.ptr.p_double[1] = lambdadown;
8399  state->rstate.ra.ptr.p_double[2] = lambdav;
8400  state->rstate.ra.ptr.p_double[3] = rho;
8401  state->rstate.ra.ptr.p_double[4] = mu;
8402  state->rstate.ra.ptr.p_double[5] = stepnorm;
8403  return result;
8404 }
8405 
8406 
8407 /*************************************************************************
8408 NLEQ solver results
8409 
8410 INPUT PARAMETERS:
8411  State - algorithm state.
8412 
8413 OUTPUT PARAMETERS:
8414  X - array[0..N-1], solution
8415  Rep - optimization report:
8416  * Rep.TerminationType completion code:
8417  * -4 ERROR: algorithm has converged to the
8418  stationary point Xf which is local minimum of
8419  f=F[0]^2+...+F[m-1]^2, but is not solution of
8420  nonlinear system.
8421  * 1 sqrt(f)<=EpsF.
8422  * 5 MaxIts steps was taken
8423  * 7 stopping conditions are too stringent,
8424  further improvement is impossible
8425  * Rep.IterationsCount contains iterations count
8426  * NFEV contains number of function calculations
8427  * ActiveConstraints contains number of active constraints
8428 
8429  -- ALGLIB --
8430  Copyright 20.08.2009 by Bochkanov Sergey
8431 *************************************************************************/
8433  /* Real */ ae_vector* x,
8434  nleqreport* rep,
8435  ae_state *_state)
8436 {
8437 
8438  ae_vector_clear(x);
8439  _nleqreport_clear(rep);
8440 
8441  nleqresultsbuf(state, x, rep, _state);
8442 }
8443 
8444 
8445 /*************************************************************************
8446 NLEQ solver results
8447 
8448 Buffered implementation of NLEQResults(), which uses pre-allocated buffer
8449 to store X[]. If buffer size is too small, it resizes buffer. It is
8450 intended to be used in the inner cycles of performance critical algorithms
8451 where array reallocation penalty is too large to be ignored.
8452 
8453  -- ALGLIB --
8454  Copyright 20.08.2009 by Bochkanov Sergey
8455 *************************************************************************/
8457  /* Real */ ae_vector* x,
8458  nleqreport* rep,
8459  ae_state *_state)
8460 {
8461 
8462 
8463  if( x->cnt<state->n )
8464  {
8465  ae_vector_set_length(x, state->n, _state);
8466  }
8467  ae_v_move(&x->ptr.p_double[0], 1, &state->xbase.ptr.p_double[0], 1, ae_v_len(0,state->n-1));
8468  rep->iterationscount = state->repiterationscount;
8469  rep->nfunc = state->repnfunc;
8470  rep->njac = state->repnjac;
8471  rep->terminationtype = state->repterminationtype;
8472 }
8473 
8474 
8475 /*************************************************************************
8476 This subroutine restarts CG algorithm from new point. All optimization
8477 parameters are left unchanged.
8478 
8479 This function allows to solve multiple optimization problems (which
8480 must have same number of dimensions) without object reallocation penalty.
8481 
8482 INPUT PARAMETERS:
8483  State - structure used for reverse communication previously
8484  allocated with MinCGCreate call.
8485  X - new starting point.
8486  BndL - new lower bounds
8487  BndU - new upper bounds
8488 
8489  -- ALGLIB --
8490  Copyright 30.07.2010 by Bochkanov Sergey
8491 *************************************************************************/
8493  /* Real */ ae_vector* x,
8494  ae_state *_state)
8495 {
8496 
8497 
8498  ae_assert(x->cnt>=state->n, "NLEQRestartFrom: Length(X)<N!", _state);
8499  ae_assert(isfinitevector(x, state->n, _state), "NLEQRestartFrom: X contains infinite or NaN values!", _state);
8500  ae_v_move(&state->x.ptr.p_double[0], 1, &x->ptr.p_double[0], 1, ae_v_len(0,state->n-1));
8501  ae_vector_set_length(&state->rstate.ia, 2+1, _state);
8502  ae_vector_set_length(&state->rstate.ba, 0+1, _state);
8503  ae_vector_set_length(&state->rstate.ra, 5+1, _state);
8504  state->rstate.stage = -1;
8505  nleq_clearrequestfields(state, _state);
8506 }
8507 
8508 
8509 /*************************************************************************
8510 Clears request fileds (to be sure that we don't forgot to clear something)
8511 *************************************************************************/
8512 static void nleq_clearrequestfields(nleqstate* state, ae_state *_state)
8513 {
8514 
8515 
8516  state->needf = ae_false;
8517  state->needfij = ae_false;
8518  state->xupdated = ae_false;
8519 }
8520 
8521 
8522 /*************************************************************************
8523 Increases lambda, returns False when there is a danger of overflow
8524 *************************************************************************/
8525 static ae_bool nleq_increaselambda(double* lambdav,
8526  double* nu,
8527  double lambdaup,
8528  ae_state *_state)
8529 {
8530  double lnlambda;
8531  double lnnu;
8532  double lnlambdaup;
8533  double lnmax;
8534  ae_bool result;
8535 
8536 
8537  result = ae_false;
8538  lnlambda = ae_log(*lambdav, _state);
8539  lnlambdaup = ae_log(lambdaup, _state);
8540  lnnu = ae_log(*nu, _state);
8541  lnmax = 0.5*ae_log(ae_maxrealnumber, _state);
8542  if( ae_fp_greater(lnlambda+lnlambdaup+lnnu,lnmax) )
8543  {
8544  return result;
8545  }
8546  if( ae_fp_greater(lnnu+ae_log(2, _state),lnmax) )
8547  {
8548  return result;
8549  }
8550  *lambdav = *lambdav*lambdaup*(*nu);
8551  *nu = *nu*2;
8552  result = ae_true;
8553  return result;
8554 }
8555 
8556 
8557 /*************************************************************************
8558 Decreases lambda, but leaves it unchanged when there is danger of underflow.
8559 *************************************************************************/
8560 static void nleq_decreaselambda(double* lambdav,
8561  double* nu,
8562  double lambdadown,
8563  ae_state *_state)
8564 {
8565 
8566 
8567  *nu = 1;
8568  if( ae_fp_less(ae_log(*lambdav, _state)+ae_log(lambdadown, _state),ae_log(ae_minrealnumber, _state)) )
8569  {
8570  *lambdav = ae_minrealnumber;
8571  }
8572  else
8573  {
8574  *lambdav = *lambdav*lambdadown;
8575  }
8576 }
8577 
8578 
8579 ae_bool _nleqstate_init(void* _p, ae_state *_state, ae_bool make_automatic)
8580 {
8581  nleqstate *p = (nleqstate*)_p;
8582  ae_touch_ptr((void*)p);
8583  if( !ae_vector_init(&p->x, 0, DT_REAL, _state, make_automatic) )
8584  return ae_false;
8585  if( !ae_vector_init(&p->fi, 0, DT_REAL, _state, make_automatic) )
8586  return ae_false;
8587  if( !ae_matrix_init(&p->j, 0, 0, DT_REAL, _state, make_automatic) )
8588  return ae_false;
8589  if( !_rcommstate_init(&p->rstate, _state, make_automatic) )
8590  return ae_false;
8591  if( !ae_vector_init(&p->xbase, 0, DT_REAL, _state, make_automatic) )
8592  return ae_false;
8593  if( !ae_vector_init(&p->candstep, 0, DT_REAL, _state, make_automatic) )
8594  return ae_false;
8595  if( !ae_vector_init(&p->rightpart, 0, DT_REAL, _state, make_automatic) )
8596  return ae_false;
8597  if( !ae_vector_init(&p->cgbuf, 0, DT_REAL, _state, make_automatic) )
8598  return ae_false;
8599  return ae_true;
8600 }
8601 
8602 
8603 ae_bool _nleqstate_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
8604 {
8605  nleqstate *dst = (nleqstate*)_dst;
8606  nleqstate *src = (nleqstate*)_src;
8607  dst->n = src->n;
8608  dst->m = src->m;
8609  dst->epsf = src->epsf;
8610  dst->maxits = src->maxits;
8611  dst->xrep = src->xrep;
8612  dst->stpmax = src->stpmax;
8613  if( !ae_vector_init_copy(&dst->x, &src->x, _state, make_automatic) )
8614  return ae_false;
8615  dst->f = src->f;
8616  if( !ae_vector_init_copy(&dst->fi, &src->fi, _state, make_automatic) )
8617  return ae_false;
8618  if( !ae_matrix_init_copy(&dst->j, &src->j, _state, make_automatic) )
8619  return ae_false;
8620  dst->needf = src->needf;
8621  dst->needfij = src->needfij;
8622  dst->xupdated = src->xupdated;
8623  if( !_rcommstate_init_copy(&dst->rstate, &src->rstate, _state, make_automatic) )
8624  return ae_false;
8626  dst->repnfunc = src->repnfunc;
8627  dst->repnjac = src->repnjac;
8629  if( !ae_vector_init_copy(&dst->xbase, &src->xbase, _state, make_automatic) )
8630  return ae_false;
8631  dst->fbase = src->fbase;
8632  dst->fprev = src->fprev;
8633  if( !ae_vector_init_copy(&dst->candstep, &src->candstep, _state, make_automatic) )
8634  return ae_false;
8635  if( !ae_vector_init_copy(&dst->rightpart, &src->rightpart, _state, make_automatic) )
8636  return ae_false;
8637  if( !ae_vector_init_copy(&dst->cgbuf, &src->cgbuf, _state, make_automatic) )
8638  return ae_false;
8639  return ae_true;
8640 }
8641 
8642 
8643 void _nleqstate_clear(void* _p)
8644 {
8645  nleqstate *p = (nleqstate*)_p;
8646  ae_touch_ptr((void*)p);
8647  ae_vector_clear(&p->x);
8648  ae_vector_clear(&p->fi);
8649  ae_matrix_clear(&p->j);
8650  _rcommstate_clear(&p->rstate);
8651  ae_vector_clear(&p->xbase);
8652  ae_vector_clear(&p->candstep);
8653  ae_vector_clear(&p->rightpart);
8654  ae_vector_clear(&p->cgbuf);
8655 }
8656 
8657 
8658 void _nleqstate_destroy(void* _p)
8659 {
8660  nleqstate *p = (nleqstate*)_p;
8661  ae_touch_ptr((void*)p);
8662  ae_vector_destroy(&p->x);
8663  ae_vector_destroy(&p->fi);
8664  ae_matrix_destroy(&p->j);
8665  _rcommstate_destroy(&p->rstate);
8666  ae_vector_destroy(&p->xbase);
8667  ae_vector_destroy(&p->candstep);
8668  ae_vector_destroy(&p->rightpart);
8669  ae_vector_destroy(&p->cgbuf);
8670 }
8671 
8672 
8673 ae_bool _nleqreport_init(void* _p, ae_state *_state, ae_bool make_automatic)
8674 {
8675  nleqreport *p = (nleqreport*)_p;
8676  ae_touch_ptr((void*)p);
8677  return ae_true;
8678 }
8679 
8680 
8681 ae_bool _nleqreport_init_copy(void* _dst, void* _src, ae_state *_state, ae_bool make_automatic)
8682 {
8683  nleqreport *dst = (nleqreport*)_dst;
8684  nleqreport *src = (nleqreport*)_src;
8685  dst->iterationscount = src->iterationscount;
8686  dst->nfunc = src->nfunc;
8687  dst->njac = src->njac;
8688  dst->terminationtype = src->terminationtype;
8689  return ae_true;
8690 }
8691 
8692 
8693 void _nleqreport_clear(void* _p)
8694 {
8695  nleqreport *p = (nleqreport*)_p;
8696  ae_touch_ptr((void*)p);
8697 }
8698 
8699 
8700 void _nleqreport_destroy(void* _p)
8701 {
8702  nleqreport *p = (nleqreport*)_p;
8703  ae_touch_ptr((void*)p);
8704 }
8705 
8706 
8707 
8708 }
8709 
ae_int_t & nfunc
Definition: solvers.h:424
void ae_v_cmove(ae_complex *vdst, ae_int_t stride_dst, const ae_complex *vsrc, ae_int_t stride_src, const char *conj_src, ae_int_t n)
Definition: ap.cpp:3871
struct alglib_impl::ae_state ae_state
void cmatrixsolvem(const complex_2d_array &a, const ae_int_t n, const complex_2d_array &b, const ae_int_t m, const bool rfs, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
Definition: solvers.cpp:483
alglib_impl::lincgstate * c_ptr()
Definition: solvers.cpp:1577
void rmatrixsolvem(const real_2d_array &a, const ae_int_t n, const real_2d_array &b, const ae_int_t m, const bool rfs, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
Definition: solvers.cpp:260
int nrows
void rmatrixsolvem(ae_matrix *a, ae_int_t n, ae_matrix *b, ae_int_t m, ae_bool rfs, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:2800
void lincgsetb(lincgstate *state, ae_vector *b, ae_state *_state)
Definition: solvers.cpp:6874
ae_bool ae_fp_greater_eq(double v1, double v2)
Definition: ap.cpp:1351
alglib_impl::lincgreport * c_ptr()
Definition: solvers.cpp:1644
alglib_impl::nleqstate * c_ptr()
Definition: solvers.cpp:2042
void nleqrestartfrom(const nleqstate &state, const real_1d_array &x)
Definition: solvers.cpp:2562
void spdmatrixsolvem(ae_matrix *a, ae_int_t n, ae_bool isupper, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3634
void rmatrixlu(real_2d_array &a, const ae_int_t m, const ae_int_t n, integer_1d_array &pivots)
Definition: linalg.cpp:2942
void _lincgreport_clear(void *_p)
Definition: solvers.cpp:7870
void rmatrixmv(const ae_int_t m, const ae_int_t n, const real_2d_array &a, const ae_int_t ia, const ae_int_t ja, const ae_int_t opa, const real_1d_array &x, const ae_int_t ix, real_1d_array &y, const ae_int_t iy)
Definition: linalg.cpp:319
double ae_c_abs(ae_complex z, ae_state *state)
Definition: ap.cpp:3639
void ae_v_moved(double *vdst, ae_int_t stride_dst, const double *vsrc, ae_int_t stride_src, ae_int_t n, double alpha)
Definition: ap.cpp:4425
ae_bool normestimatoriteration(normestimatorstate *state, ae_state *_state)
Definition: linalg.cpp:32286
ae_int_t iterationscount
Definition: solvers.h:181
densesolverreport & operator=(const densesolverreport &rhs)
Definition: solvers.cpp:94
void nleqsetstpmax(nleqstate *state, double stpmax, ae_state *_state)
Definition: solvers.cpp:8064
void cmatrixlusolvem(ae_matrix *lua, ae_vector *p, ae_int_t n, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3350
void _linlsqrreport_destroy(void *_p)
Definition: solvers.cpp:6751
void lincgsetprecunit(lincgstate *state, ae_state *_state)
Definition: solvers.cpp:6899
double hpdmatrixcholeskyrcond(const complex_2d_array &a, const ae_int_t n, const bool isupper)
Definition: linalg.cpp:3514
bool spdmatrixcholesky(real_2d_array &a, const ae_int_t n, const bool isupper)
Definition: linalg.cpp:3079
virtual ~lincgreport()
Definition: solvers.cpp:1669
ae_int_t repterminationtype
Definition: solvers.h:140
real_2d_array j
Definition: solvers.h:395
void cmatrixmixedsolvem(ae_matrix *a, ae_matrix *lua, ae_vector *p, ae_int_t n, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3492
ae_int_t repiterationscount
Definition: solvers.h:92
void nleqsetcond(const nleqstate &state, const double epsf, const ae_int_t maxits)
Definition: solvers.cpp:2333
double sparsegetdiagonal(const sparsematrix &s, const ae_int_t i)
Definition: linalg.cpp:5236
bool nleqiteration(const nleqstate &state)
Definition: solvers.cpp:2416
void ae_v_muld(double *vdst, ae_int_t stride_dst, ae_int_t n, double alpha)
Definition: ap.cpp:4538
void nleqresults(nleqstate *state, ae_vector *x, nleqreport *rep, ae_state *_state)
Definition: solvers.cpp:8432
ae_vector rightpart
Definition: solvers.h:176
void rmatrixmixedsolvem(ae_matrix *a, ae_matrix *lua, ae_vector *p, ae_int_t n, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3115
real_1d_array x
Definition: solvers.h:396
alglib_impl::nleqreport * p_struct
Definition: solvers.h:414
normestimatorstate nes
Definition: solvers.h:47
double ae_fabs(double x, ae_state *state)
Definition: ap.cpp:1520
alglib_impl::densesolverreport * p_struct
Definition: solvers.h:210
void _nleqreport_destroy(void *_p)
Definition: solvers.cpp:8700
void _densesolverreport_clear(void *_p)
Definition: solvers.cpp:5599
void _nleqstate_clear(void *_p)
Definition: solvers.cpp:8643
void nleqresultsbuf(const nleqstate &state, real_1d_array &x, nleqreport &rep)
Definition: solvers.cpp:2529
void _lincgreport_destroy(void *_p)
Definition: solvers.cpp:7877
void cmatrixmixedsolvem(const complex_2d_array &a, const complex_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const complex_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
Definition: solvers.cpp:647
#define ae_false
Definition: ap.h:196
void * ae_malloc(size_t size, ae_state *state)
Definition: ap.cpp:222
ae_int_t stride
Definition: ap.h:446
double rho
void _normestimatorstate_clear(void *_p)
Definition: linalg.cpp:32601
alglib_impl::nleqreport * c_ptr()
Definition: solvers.cpp:2109
void nleqcreatelm(ae_int_t n, ae_int_t m, ae_vector *x, nleqstate *state, ae_state *_state)
Definition: solvers.cpp:7956
void xdot(ae_vector *a, ae_vector *b, ae_int_t n, ae_vector *temp, double *r, double *rerr, ae_state *_state)
void lincgsolvesparse(lincgstate *state, sparsematrix *a, ae_bool isupper, ae_vector *b, ae_state *_state)
Definition: solvers.cpp:7463
void hpdmatrixcholeskysolve(const complex_2d_array &cha, const ae_int_t n, const bool isupper, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
Definition: solvers.cpp:1058
union alglib_impl::ae_matrix::@12 ptr
void lincgsetstartingpoint(lincgstate *state, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:6849
ae_int_t iterationscount
Definition: solvers.h:147
void linlsqrresults(const linlsqrstate &state, real_1d_array &x, linlsqrreport &rep)
Definition: solvers.cpp:1492
void rmatrixsolve(ae_matrix *a, ae_int_t n, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:2735
void lincgsetxrep(const lincgstate &state, const bool needxrep)
Definition: solvers.cpp:1989
ae_int_t repterminationtype
Definition: solvers.h:94
void ae_frame_make(ae_state *state, ae_frame *tmp)
Definition: ap.cpp:402
static double * y
virtual ~lincgstate()
Definition: solvers.cpp:1602
void rmatrixsolvels(ae_matrix *a, ae_int_t nrows, ae_int_t ncols, ae_vector *b, double threshold, ae_int_t *info, densesolverlsreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:4369
rcommstate rstate
Definition: solvers.h:167
void rmatrixmixedsolve(ae_matrix *a, ae_matrix *lua, ae_vector *p, ae_int_t n, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:3051
void rmatrixmixedsolvem(const real_2d_array &a, const real_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const real_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
Definition: solvers.cpp:439
ae_complex ae_c_conj(ae_complex lhs, ae_state *state)
Definition: ap.cpp:3623
ae_int_t repiterationscount
Definition: solvers.h:138
ae_vector ia
Definition: ap.h:837
void lincgsetstartingpoint(const lincgstate &state, const real_1d_array &x)
Definition: solvers.cpp:1728
void linlsqrsetlambdai(linlsqrstate *state, double lambdai, ae_state *_state)
Definition: solvers.cpp:5819
ae_bool _nleqreport_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:8681
ae_complex ae_complex_from_d(double v)
Definition: ap.cpp:3607
void cmatrixsolvem(ae_matrix *a, ae_int_t n, ae_matrix *b, ae_int_t m, ae_bool rfs, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3194
doublereal * w
double * p_double
Definition: ap.h:437
void _densesolverlsreport_destroy(void *_p)
Definition: solvers.cpp:5644
void hpdmatrixcholeskysolvem(ae_matrix *cha, ae_int_t n, ae_bool isupper, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:4191
ae_bool _nleqstate_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:8603
void nleqsetxrep(nleqstate *state, ae_bool needxrep, ae_state *_state)
Definition: solvers.cpp:8039
void ae_state_clear(ae_state *state)
Definition: ap.cpp:373
const alglib_impl::ae_matrix * c_ptr() const
Definition: ap.cpp:6463
void spdmatrixsolvem(const real_2d_array &a, const ae_int_t n, const bool isupper, const real_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
Definition: solvers.cpp:734
ae_bool ae_fp_eq(double v1, double v2)
Definition: ap.cpp:1313
_linlsqrreport_owner & operator=(const _linlsqrreport_owner &rhs)
Definition: solvers.cpp:1225
ae_bool nleqiteration(nleqstate *state, ae_state *_state)
Definition: solvers.cpp:8079
void hpdmatrixcholeskysolve(ae_matrix *cha, ae_int_t n, ae_bool isupper, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:4289
void linlsqrsetb(linlsqrstate *state, ae_vector *b, ae_state *_state)
Definition: solvers.cpp:5746
void nleqrestartfrom(nleqstate *state, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:8492
void sparsemtv(const sparsematrix &s, const real_1d_array &x, real_1d_array &y)
Definition: linalg.cpp:5350
ae_bool ae_matrix_init_copy(ae_matrix *dst, ae_matrix *src, ae_state *state, ae_bool make_automatic)
Definition: ap.cpp:801
ae_bool ae_matrix_init(ae_matrix *dst, ae_int_t rows, ae_int_t cols, ae_datatype datatype, ae_state *state, ae_bool make_automatic)
Definition: ap.cpp:756
ae_int_t & njac
Definition: solvers.h:425
doublereal * x
void ae_matrix_destroy(ae_matrix *dst)
Definition: ap.cpp:909
double v_nan
Definition: ap.h:355
#define i
ae_bool _linlsqrstate_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:6563
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void ae_v_add(double *vdst, ae_int_t stride_dst, const double *vsrc, ae_int_t stride_src, ae_int_t n)
Definition: ap.cpp:4452
alglib_impl::densesolverlsreport * p_struct
Definition: solvers.h:238
void nleqresults(const nleqstate &state, real_1d_array &x, nleqreport &rep)
Definition: solvers.cpp:2502
ae_bool _lincgstate_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:7750
double & f
Definition: solvers.h:393
void cmatrixlusolvem(const complex_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const complex_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
Definition: solvers.cpp:564
void _lincgstate_destroy(void *_p)
Definition: solvers.cpp:7829
ae_bool _densesolverreport_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:5589
void hpdmatrixsolve(ae_matrix *a, ae_int_t n, ae_bool isupper, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:4125
void linlsqrsetprecdiag(const linlsqrstate &state)
Definition: solvers.cpp:1350
void normestimatorrestart(normestimatorstate *state, ae_state *_state)
Definition: linalg.cpp:32531
void threshold(double *phi, unsigned long nvox, double limit)
Definition: lib_vwk.cpp:524
alglib_impl::densesolverlsreport * c_ptr()
Definition: solvers.cpp:144
double safepythag2(double x, double y, ae_state *_state)
void hpdmatrixsolvem(ae_matrix *a, ae_int_t n, ae_bool isupper, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3994
void normestimatorcreate(const ae_int_t m, const ae_int_t n, const ae_int_t nstart, const ae_int_t nits, normestimatorstate &state)
Definition: linalg.cpp:6152
void linminnormalized(ae_vector *d, double *stp, ae_int_t n, ae_state *_state)
void _rcommstate_destroy(rcommstate *p)
Definition: ap.cpp:4605
void spdmatrixcholeskysolvem(ae_matrix *cha, ae_int_t n, ae_bool isupper, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:3830
ae_int_t ae_v_len(ae_int_t a, ae_int_t b)
Definition: ap.cpp:4562
virtual ~nleqreport()
Definition: solvers.cpp:2134
void linlsqrsolvesparse(const linlsqrstate &state, const sparsematrix &a, const real_1d_array &b)
Definition: solvers.cpp:1418
void ae_vector_destroy(ae_vector *dst)
Definition: ap.cpp:707
alglib_impl::nleqstate * p_struct
Definition: solvers.h:381
void nleqresultsbuf(nleqstate *state, ae_vector *x, nleqreport *rep, ae_state *_state)
Definition: solvers.cpp:8456
ae_bool _rcommstate_init(rcommstate *p, ae_state *_state, ae_bool make_automatic)
Definition: ap.cpp:4570
void rmatrixsolve(const real_2d_array &a, const ae_int_t n, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
Definition: solvers.cpp:213
void lincgresults(const lincgstate &state, real_1d_array &x, lincgreport &rep)
Definition: solvers.cpp:1903
void rmatrixlusolvem(ae_matrix *lua, ae_vector *p, ae_int_t n, ae_matrix *b, ae_int_t m, ae_int_t *info, densesolverreport *rep, ae_matrix *x, ae_state *_state)
Definition: solvers.cpp:2967
doublereal * b
ae_bool _linlsqrstate_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:6603
void linlsqrsetxrep(const linlsqrstate &state, const bool needxrep)
Definition: solvers.cpp:1521
ae_int_t & nmv
Definition: solvers.h:305
void sparsemv(const sparsematrix &s, const real_1d_array &x, real_1d_array &y)
Definition: linalg.cpp:5310
ae_bool _normestimatorstate_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: linalg.cpp:32567
void sparsesmv(const sparsematrix &s, const bool isupper, const real_1d_array &x, real_1d_array &y)
Definition: linalg.cpp:5443
void linlsqrsetxrep(linlsqrstate *state, ae_bool needxrep, ae_state *_state)
Definition: solvers.cpp:6520
ae_bool _rcommstate_init_copy(rcommstate *dst, rcommstate *src, ae_state *_state, ae_bool make_automatic)
Definition: ap.cpp:4583
void ae_v_move(double *vdst, ae_int_t stride_dst, const double *vsrc, ae_int_t stride_src, ae_int_t n)
Definition: ap.cpp:4371
ae_bool _nleqreport_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:8673
void linlsqrsolvesparse(linlsqrstate *state, sparsematrix *a, ae_vector *b, ae_state *_state)
Definition: solvers.cpp:6314
void nleqsetxrep(const nleqstate &state, const bool needxrep)
Definition: solvers.cpp:2362
void cmatrixsolve(const complex_2d_array &a, const ae_int_t n, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
Definition: solvers.cpp:521
ae_complex ae_c_div(ae_complex lhs, ae_complex rhs)
Definition: ap.cpp:3701
double * f
ae_bool _densesolverlsreport_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:5623
ae_int_t itsbeforerupdate
Definition: solvers.h:130
void linlsqrsetlambdai(const linlsqrstate &state, const double lambdai)
Definition: solvers.cpp:1379
void ae_vector_clear(ae_vector *dst)
Definition: ap.cpp:692
ae_bool _lincgreport_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:7858
linlsqrreport & operator=(const linlsqrreport &rhs)
Definition: solvers.cpp:1258
void ae_v_csubc(ae_complex *vdst, ae_int_t stride_dst, const ae_complex *vsrc, ae_int_t stride_src, const char *conj_src, ae_int_t n, ae_complex alpha)
Definition: ap.cpp:4276
ae_int_t length() const
Definition: ap.cpp:5882
void lincgsetrupdatefreq(const lincgstate &state, const ae_int_t freq)
Definition: solvers.cpp:1960
void lincgsetprecdiag(const lincgstate &state)
Definition: solvers.cpp:1783
void spdmatrixcholeskysolvem(const real_2d_array &cha, const ae_int_t n, const bool isupper, const real_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
Definition: solvers.cpp:826
ae_bool & needf
Definition: solvers.h:390
ae_complex ** pp_complex
Definition: ap.h:456
alglib_impl::densesolverreport * c_ptr()
Definition: solvers.cpp:77
ae_bool ae_fp_less(double v1, double v2)
Definition: ap.cpp:1327
_lincgstate_owner & operator=(const _lincgstate_owner &rhs)
Definition: solvers.cpp:1561
void hpdmatrixsolvem(const complex_2d_array &a, const ae_int_t n, const bool isupper, const complex_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
Definition: solvers.cpp:919
void rmatrixlusolvem(const real_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const real_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, real_2d_array &x)
Definition: solvers.cpp:351
ae_bool _lincgreport_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:7850
ae_vector ra
Definition: ap.h:839
ae_bool lincgiteration(lincgstate *state, ae_state *_state)
Definition: solvers.cpp:6976
ae_bool _densesolverlsreport_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:5613
double spdmatrixcholeskyrcond(const real_2d_array &a, const ae_int_t n, const bool isupper)
Definition: linalg.cpp:3475
void spdmatrixcholeskysolve(const real_2d_array &cha, const ae_int_t n, const bool isupper, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
Definition: solvers.cpp:872
virtual ~nleqstate()
Definition: solvers.cpp:2067
double rmatrixlurcondinf(const real_2d_array &lua, const ae_int_t n)
Definition: linalg.cpp:3436
#define ae_bool
Definition: ap.h:194
void lincgsolvesparse(const lincgstate &state, const sparsematrix &a, const bool isupper, const real_1d_array &b)
Definition: solvers.cpp:1861
ae_complex ae_c_sub(ae_complex lhs, ae_complex rhs)
Definition: ap.cpp:3693
ae_bool ae_fp_neq(double v1, double v2)
Definition: ap.cpp:1321
ae_bool isfinitevector(ae_vector *x, ae_int_t n, ae_state *_state)
void lincgsetprecunit(const lincgstate &state)
Definition: solvers.cpp:1756
void ae_v_cmoved(ae_complex *vdst, ae_int_t stride_dst, const ae_complex *vsrc, ae_int_t stride_src, const char *conj_src, ae_int_t n, double alpha)
Definition: ap.cpp:3965
void rvectorsetlengthatleast(ae_vector *x, ae_int_t n, ae_state *_state)
ae_int_t & iterationscount
Definition: solvers.h:423
double rmatrixlurcond1(const real_2d_array &lua, const ae_int_t n)
Definition: linalg.cpp:3401
void ae_v_cadd(ae_complex *vdst, ae_int_t stride_dst, const ae_complex *vsrc, ae_int_t stride_src, const char *conj_src, ae_int_t n)
Definition: ap.cpp:4069
void linlsqrcreate(const ae_int_t m, const ae_int_t n, linlsqrstate &state)
Definition: solvers.cpp:1295
void cmatrixlusolve(const complex_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
Definition: solvers.cpp:606
void ae_touch_ptr(void *p)
Definition: ap.cpp:294
alglib_impl::lincgstate * p_struct
Definition: solvers.h:326
double ae_maxreal(double m1, double m2, ae_state *state)
Definition: ap.cpp:1577
ae_error_type
Definition: ap.h:201
ae_bool & xupdated
Definition: solvers.h:392
ae_complex ae_v_cdotproduct(const ae_complex *v0, ae_int_t stride0, const char *conj0, const ae_complex *v1, ae_int_t stride1, const char *conj1, ae_int_t n)
Definition: ap.cpp:3807
double rcondthreshold(ae_state *_state)
Definition: linalg.cpp:25246
ae_bool ae_vector_set_length(ae_vector *dst, ae_int_t newsize, ae_state *state)
Definition: ap.cpp:658
_densesolverreport_owner & operator=(const _densesolverreport_owner &rhs)
Definition: solvers.cpp:61
ae_int_t & terminationtype
Definition: solvers.h:363
bool rmatrixsvd(const real_2d_array &a, const ae_int_t m, const ae_int_t n, const ae_int_t uneeded, const ae_int_t vtneeded, const ae_int_t additionalmemory, real_1d_array &w, real_2d_array &u, real_2d_array &vt)
Definition: linalg.cpp:1851
void nleqsetcond(nleqstate *state, double epsf, ae_int_t maxits, ae_state *_state)
Definition: solvers.cpp:8007
void lincgsetrestartfreq(const lincgstate &state, const ae_int_t srf)
Definition: solvers.cpp:1926
void normestimatorresults(const normestimatorstate &state, double &nrm)
Definition: linalg.cpp:6244
double ae_log(double x, ae_state *state)
Definition: ap.cpp:1679
void linlsqrsetprecdiag(linlsqrstate *state, ae_state *_state)
Definition: solvers.cpp:5797
virtual ~linlsqrreport()
Definition: solvers.cpp:1266
struct alglib_impl::ae_vector ae_vector
ae_vector candstep
Definition: solvers.h:175
const alglib_impl::ae_vector * c_ptr() const
Definition: ap.cpp:5907
void _densesolverlsreport_clear(void *_p)
Definition: solvers.cpp:5636
ae_int_t & terminationtype
Definition: solvers.h:426
#define j
double ae_minreal(double m1, double m2, ae_state *state)
Definition: ap.cpp:1582
void cmatrixmixedsolve(ae_matrix *a, ae_matrix *lua, ae_vector *p, ae_int_t n, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:3567
ae_bool linlsqriteration(linlsqrstate *state, ae_state *_state)
Definition: solvers.cpp:5836
alglib_impl::linlsqrreport * p_struct
Definition: solvers.h:295
int m
bool hpdmatrixcholesky(complex_2d_array &a, const ae_int_t n, const bool isupper)
Definition: linalg.cpp:3034
ae_vector ba
Definition: ap.h:838
ae_bool _nleqstate_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:8579
struct alglib_impl::ae_matrix ae_matrix
ae_bool _lincgstate_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:7714
void nleqsolve(nleqstate &state, void(*func)(const real_1d_array &x, double &func, void *ptr), void(*jac)(const real_1d_array &x, real_1d_array &fi, real_2d_array &jac, void *ptr), void(*rep)(const real_1d_array &x, double func, void *ptr), void *ptr)
Definition: solvers.cpp:2433
void _linlsqrstate_clear(void *_p)
Definition: solvers.cpp:6679
void lincgsetprecdiag(lincgstate *state, ae_state *_state)
Definition: solvers.cpp:6919
linlsqrstate & operator=(const linlsqrstate &rhs)
Definition: solvers.cpp:1191
void _lincgstate_clear(void *_p)
Definition: solvers.cpp:7808
void _linlsqrstate_destroy(void *_p)
Definition: solvers.cpp:6702
ae_complex * p_complex
Definition: ap.h:438
double ** pp_double
Definition: ap.h:455
void ae_state_init(ae_state *state)
Definition: ap.cpp:309
ae_int_t & terminationtype
Definition: solvers.h:306
alglib_impl::linlsqrstate * c_ptr()
Definition: solvers.cpp:1174
void spdmatrixsolve(ae_matrix *a, ae_int_t n, ae_bool isupper, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:3764
double ae_sqrt(double x, ae_state *state)
Definition: ap.cpp:1535
void ae_assert(ae_bool cond, const char *msg, ae_state *state)
Definition: ap.cpp:1227
union alglib_impl::ae_vector::@11 ptr
bool sparseenumerate(const sparsematrix &s, ae_int_t &t0, ae_int_t &t1, ae_int_t &i, ae_int_t &j, double &v)
Definition: linalg.cpp:5696
void lincgsetcond(lincgstate *state, double epsf, ae_int_t maxits, ae_state *_state)
Definition: solvers.cpp:6947
ae_int_t & iterationscount
Definition: solvers.h:361
void rmatrixsolvels(const real_2d_array &a, const ae_int_t nrows, const ae_int_t ncols, const real_1d_array &b, const double threshold, ae_int_t &info, densesolverlsreport &rep, real_1d_array &x)
Definition: solvers.cpp:1119
ae_int_t repiterationscount
Definition: solvers.h:168
const char *volatile error_msg
Definition: ap.h:389
virtual ~densesolverreport()
Definition: solvers.cpp:102
ae_int_t repterminationtype
Definition: solvers.h:171
void xcdot(ae_vector *a, ae_vector *b, ae_int_t n, ae_vector *temp, ae_complex *r, double *rerr, ae_state *_state)
void rmatrixlusolve(ae_matrix *lua, ae_vector *p, ae_int_t n, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:2902
void _linlsqrreport_clear(void *_p)
Definition: solvers.cpp:6744
#define ae_machineepsilon
Definition: ap.h:825
void cmatrixmixedsolve(const complex_2d_array &a, const complex_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
Definition: solvers.cpp:687
_densesolverlsreport_owner & operator=(const _densesolverlsreport_owner &rhs)
Definition: solvers.cpp:128
void linlsqrrestart(linlsqrstate *state, ae_state *_state)
Definition: solvers.cpp:6536
_nleqstate_owner & operator=(const _nleqstate_owner &rhs)
Definition: solvers.cpp:2026
void hpdmatrixcholeskysolvem(const complex_2d_array &cha, const ae_int_t n, const bool isupper, const complex_2d_array &b, const ae_int_t m, ae_int_t &info, densesolverreport &rep, complex_2d_array &x)
Definition: solvers.cpp:1012
densesolverlsreport & operator=(const densesolverlsreport &rhs)
Definition: solvers.cpp:161
int mu
void linlsqrcreate(ae_int_t m, ae_int_t n, linlsqrstate *state, ae_state *_state)
Definition: solvers.cpp:5679
void rmatrixmixedsolve(const real_2d_array &a, const real_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
Definition: solvers.cpp:395
void _rcommstate_clear(rcommstate *p)
Definition: ap.cpp:4597
ptrdiff_t ae_int_t
Definition: ap.h:186
void lincgcreate(const ae_int_t n, lincgstate &state)
Definition: solvers.cpp:1699
ae_int_t & iterationscount
Definition: solvers.h:304
doublereal * u
alglib_impl::linlsqrstate * p_struct
Definition: solvers.h:269
void ae_v_subd(double *vdst, ae_int_t stride_dst, const double *vsrc, ae_int_t stride_src, ae_int_t n, double alpha)
Definition: ap.cpp:4533
ae_bool ae_vector_init(ae_vector *dst, ae_int_t size, ae_datatype datatype, ae_state *state, ae_bool make_automatic)
Definition: ap.cpp:580
void hpdmatrixsolve(const complex_2d_array &a, const ae_int_t n, const bool isupper, const complex_1d_array &b, ae_int_t &info, densesolverreport &rep, complex_1d_array &x)
Definition: solvers.cpp:965
ae_complex ae_c_mul_d(ae_complex lhs, double rhs)
Definition: ap.cpp:3749
void _normestimatorstate_destroy(void *_p)
Definition: linalg.cpp:32617
void _nleqstate_destroy(void *_p)
Definition: solvers.cpp:8658
ae_bool ae_isfinite(double x, ae_state *state)
Definition: ap.cpp:1495
double ae_sqr(double x, ae_state *state)
Definition: ap.cpp:1530
void rmatrixlusolve(const real_2d_array &lua, const integer_1d_array &p, const ae_int_t n, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
Definition: solvers.cpp:305
void linlsqrsetcond(linlsqrstate *state, double epsa, double epsb, ae_int_t maxits, ae_state *_state)
Definition: solvers.cpp:6435
void lincgcreate(ae_int_t n, lincgstate *state, ae_state *_state)
Definition: solvers.cpp:6786
_lincgreport_owner & operator=(const _lincgreport_owner &rhs)
Definition: solvers.cpp:1628
void _densesolverreport_destroy(void *_p)
Definition: solvers.cpp:5606
_linlsqrstate_owner & operator=(const _linlsqrstate_owner &rhs)
Definition: solvers.cpp:1158
lincgreport & operator=(const lincgreport &rhs)
Definition: solvers.cpp:1661
void lincgsetcond(const lincgstate &state, const double epsf, const ae_int_t maxits)
Definition: solvers.cpp:1818
float r2
void linlsqrresults(linlsqrstate *state, ae_vector *x, linlsqrreport *rep, ae_state *_state)
Definition: solvers.cpp:6486
void nleqcreatelm(const ae_int_t n, const ae_int_t m, const real_1d_array &x, nleqstate &state)
Definition: solvers.cpp:2208
nleqstate & operator=(const nleqstate &rhs)
Definition: solvers.cpp:2059
ae_int_t itsbeforerestart
Definition: solvers.h:129
ae_complex ae_c_neg(ae_complex lhs)
Definition: ap.cpp:3615
void linlsqrsetprecunit(linlsqrstate *state, ae_state *_state)
Definition: solvers.cpp:5777
Definition: queue_bag.h:9
ae_bool _normestimatorstate_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: linalg.cpp:32541
ae_int_t * p_int
Definition: ap.h:436
void lincgsetrupdatefreq(lincgstate *state, ae_int_t freq, ae_state *_state)
Definition: solvers.cpp:7635
void nleqsetstpmax(const nleqstate &state, const double stpmax)
Definition: solvers.cpp:2395
ae_bool & needfij
Definition: solvers.h:391
ae_bool _linlsqrreport_init_copy(void *_dst, void *_src, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:6733
void cmatrixlu(complex_2d_array &a, const ae_int_t m, const ae_int_t n, integer_1d_array &pivots)
Definition: linalg.cpp:2989
void linlsqrsetcond(const linlsqrstate &state, const double epsa, const double epsb, const ae_int_t maxits)
Definition: solvers.cpp:1452
void ae_v_addd(double *vdst, ae_int_t stride_dst, const double *vsrc, ae_int_t stride_src, ae_int_t n, double alpha)
Definition: ap.cpp:4479
virtual ~linlsqrstate()
Definition: solvers.cpp:1199
ae_bool ae_vector_init_copy(ae_vector *dst, ae_vector *src, ae_state *state, ae_bool make_automatic)
Definition: ap.cpp:614
ae_bool ae_fp_less_eq(double v1, double v2)
Definition: ap.cpp:1335
void spdmatrixcholeskysolve(ae_matrix *cha, ae_int_t n, ae_bool isupper, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:3928
void cmatrixlusolve(ae_matrix *lua, ae_vector *p, ae_int_t n, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:3432
alglib_impl::ae_int_t ae_int_t
Definition: ap.h:889
void ae_frame_leave(ae_state *state)
Definition: ap.cpp:415
alglib_impl::sparsematrix * c_ptr()
Definition: linalg.cpp:4775
ae_bool * p_bool
Definition: ap.h:435
void ae_matrix_clear(ae_matrix *dst)
Definition: ap.cpp:891
void spdmatrixsolve(const real_2d_array &a, const ae_int_t n, const bool isupper, const real_1d_array &b, ae_int_t &info, densesolverreport &rep, real_1d_array &x)
Definition: solvers.cpp:779
#define ae_true
Definition: ap.h:195
void linlsqrsetprecunit(const linlsqrstate &state)
Definition: solvers.cpp:1323
int * n
void lincgsetxrep(lincgstate *state, ae_bool needxrep, ae_state *_state)
Definition: solvers.cpp:7660
nleqreport & operator=(const nleqreport &rhs)
Definition: solvers.cpp:2126
alglib_impl::lincgreport * p_struct
Definition: solvers.h:352
ae_bool ae_fp_greater(double v1, double v2)
Definition: ap.cpp:1343
ae_int_t cnt
Definition: ap.h:429
double cmatrixlurcond1(const complex_2d_array &lua, const ae_int_t n)
Definition: linalg.cpp:3548
ae_bool ae_matrix_set_length(ae_matrix *dst, ae_int_t rows, ae_int_t cols, ae_state *state)
Definition: ap.cpp:854
doublereal * a
void lincgsetrestartfreq(lincgstate *state, ae_int_t srf, ae_state *_state)
Definition: solvers.cpp:7605
ae_int_t terminationtype
Definition: solvers.h:149
alglib_impl::linlsqrreport * c_ptr()
Definition: solvers.cpp:1241
#define ae_minrealnumber
Definition: ap.h:827
real_1d_array fi
Definition: solvers.h:394
ae_bool _linlsqrreport_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:6725
void _nleqreport_clear(void *_p)
Definition: solvers.cpp:8693
void lincgresults(lincgstate *state, ae_vector *x, lincgreport *rep, ae_state *_state)
Definition: solvers.cpp:7576
ae_int_t ae_minint(ae_int_t m1, ae_int_t m2, ae_state *state)
Definition: ap.cpp:1572
_nleqreport_owner & operator=(const _nleqreport_owner &rhs)
Definition: solvers.cpp:2093
void ae_free(void *p)
Definition: ap.cpp:237
void fblssolvecgx(ae_matrix *a, ae_int_t m, ae_int_t n, double alpha, ae_vector *b, ae_vector *x, ae_vector *buf, ae_state *_state)
Definition: linalg.cpp:31514
float r1
double ae_v_dotproduct(const double *v0, ae_int_t stride0, const double *v1, ae_int_t stride1, ae_int_t n)
Definition: ap.cpp:4344
lincgstate & operator=(const lincgstate &rhs)
Definition: solvers.cpp:1594
void cmatrixsolve(ae_matrix *a, ae_int_t n, ae_vector *b, ae_int_t *info, densesolverreport *rep, ae_vector *x, ae_state *_state)
Definition: solvers.cpp:3289
void lincgrestart(lincgstate *state, ae_state *_state)
Definition: solvers.cpp:7674
void ae_v_cmovec(ae_complex *vdst, ae_int_t stride_dst, const ae_complex *vsrc, ae_int_t stride_src, const char *conj_src, ae_int_t n, ae_complex alpha)
Definition: ap.cpp:4015
#define ae_maxrealnumber
Definition: ap.h:826
ae_int_t terminationtype
Definition: solvers.h:184
double cmatrixlurcondinf(const complex_2d_array &lua, const ae_int_t n)
Definition: linalg.cpp:3583
#define xc
ae_int_t & nmv
Definition: solvers.h:362
ae_bool _densesolverreport_init(void *_p, ae_state *_state, ae_bool make_automatic)
Definition: solvers.cpp:5581