/usr/share/octave/packages/linear-algebra-2.2.0/lobpcg.m is in octave-linear-algebra 2.2.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 | ## Copyright (C) 2000-2011 A.V. Knyazev <Andrew.Knyazev@ucdenver.edu>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU Lesser General Public License as published by the Free
## Software Foundation; either version 3 of the License, or (at your option) any
## later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
## for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {[@var{blockVectorX}, @var{lambda}] =} lobpcg (@var{blockVectorX}, @var{operatorA})
## @deftypefnx {Function File} {[@var{blockVectorX}, @var{lambda}, @var{failureFlag}] =} lobpcg (@var{blockVectorX}, @var{operatorA})
## @deftypefnx {Function File} {[@var{blockVectorX}, @var{lambda}, @var{failureFlag}, @var{lambdaHistory}, @var{residualNormsHistory}] =} lobpcg (@var{blockVectorX}, @var{operatorA}, @var{operatorB}, @var{operatorT}, @var{blockVectorY}, @var{residualTolerance}, @var{maxIterations}, @var{verbosityLevel})
## Solves Hermitian partial eigenproblems using preconditioning.
##
## The first form outputs the array of algebraic smallest eigenvalues @var{lambda} and
## corresponding matrix of orthonormalized eigenvectors @var{blockVectorX} of the
## Hermitian (full or sparse) operator @var{operatorA} using input matrix
## @var{blockVectorX} as an initial guess, without preconditioning, somewhat
## similar to:
##
## @example
## # for real symmetric operator operatorA
## opts.issym = 1; opts.isreal = 1; K = size (blockVectorX, 2);
## [blockVectorX, lambda] = eigs (operatorA, K, 'SR', opts);
##
## # for Hermitian operator operatorA
## K = size (blockVectorX, 2);
## [blockVectorX, lambda] = eigs (operatorA, K, 'SR');
## @end example
##
## The second form returns a convergence flag. If @var{failureFlag} is 0 then
## all the eigenvalues converged; otherwise not all converged.
##
## The third form computes smallest eigenvalues @var{lambda} and corresponding eigenvectors
## @var{blockVectorX} of the generalized eigenproblem Ax=lambda Bx, where
## Hermitian operators @var{operatorA} and @var{operatorB} are given as functions, as
## well as a preconditioner, @var{operatorT}. The operators @var{operatorB} and
## @var{operatorT} must be in addition @emph{positive definite}. To compute the largest
## eigenpairs of @var{operatorA}, simply apply the code to @var{operatorA} multiplied by
## -1. The code does not involve @emph{any} matrix factorizations of @var{operatorA} and
## @var{operatorB}, thus, e.g., it preserves the sparsity and the structure of
## @var{operatorA} and @var{operatorB}.
##
## @var{residualTolerance} and @var{maxIterations} control tolerance and max number of
## steps, and @var{verbosityLevel} = 0, 1, or 2 controls the amount of printed
## info. @var{lambdaHistory} is a matrix with all iterative lambdas, and
## @var{residualNormsHistory} are matrices of the history of 2-norms of residuals
##
## Required input:
## @itemize @bullet
## @item
## @var{blockVectorX} (class numeric) - initial approximation to eigenvectors,
## full or sparse matrix n-by-blockSize. @var{blockVectorX} must be full rank.
## @item
## @var{operatorA} (class numeric, char, or function_handle) - the main operator
## of the eigenproblem, can be a matrix, a function name, or handle
## @end itemize
##
## Optional function input:
## @itemize @bullet
## @item
## @var{operatorB} (class numeric, char, or function_handle) - the second operator,
## if solving a generalized eigenproblem, can be a matrix, a function name, or
## handle; by default if empty, @code{operatorB = I}.
## @item
## @var{operatorT} (class char or function_handle) - the preconditioner, by
## default @code{operatorT(blockVectorX) = blockVectorX}.
## @end itemize
##
## Optional constraints input:
## @itemize @bullet
## @item
## @var{blockVectorY} (class numeric) - a full or sparse n-by-sizeY matrix of
## constraints, where sizeY < n. @var{blockVectorY} must be full rank. The
## iterations will be performed in the (operatorB-) orthogonal complement of the
## column-space of @var{blockVectorY}.
## @end itemize
##
## Optional scalar input parameters:
## @itemize @bullet
## @item
## @var{residualTolerance} (class numeric) - tolerance, by default, @code{residualTolerance = n * sqrt (eps)}
## @item
## @var{maxIterations} - max number of iterations, by default, @code{maxIterations = min (n, 20)}
## @item
## @var{verbosityLevel} - either 0 (no info), 1, or 2 (with pictures); by
## default, @code{verbosityLevel = 0}.
## @end itemize
##
## Required output:
## @itemize @bullet
## @item
## @var{blockVectorX} and @var{lambda} (class numeric) both are computed
## blockSize eigenpairs, where @code{blockSize = size (blockVectorX, 2)}
## for the initial guess @var{blockVectorX} if it is full rank.
## @end itemize
##
## Optional output:
## @itemize @bullet
## @item
## @var{failureFlag} (class integer) as described above.
## @item
## @var{lambdaHistory} (class numeric) as described above.
## @item
## @var{residualNormsHistory} (class numeric) as described above.
## @end itemize
##
## Functions @code{operatorA(blockVectorX)}, @code{operatorB(blockVectorX)} and
## @code{operatorT(blockVectorX)} must support @var{blockVectorX} being a matrix, not
## just a column vector.
##
## Every iteration involves one application of @var{operatorA} and @var{operatorB}, and
## one of @var{operatorT}.
##
## Main memory requirements: 6 (9 if @code{isempty(operatorB)=0}) matrices of the
## same size as @var{blockVectorX}, 2 matrices of the same size as @var{blockVectorY}
## (if present), and two square matrices of the size 3*blockSize.
##
## In all examples below, we use the Laplacian operator in a 20x20 square
## with the mesh size 1 which can be generated in MATLAB by running:
## @example
## A = delsq (numgrid ('S', 21));
## n = size (A, 1);
## @end example
##
## or in MATLAB and Octave by:
## @example
## [~,~,A] = laplacian ([19, 19]);
## n = size (A, 1);
## @end example
##
## Note that @code{laplacian} is a function of the specfun octave-forge package.
##
## The following Example:
## @example
## [blockVectorX, lambda, failureFlag] = lobpcg (randn (n, 8), A, 1e-5, 50, 2);
## @end example
##
## attempts to compute 8 first eigenpairs without preconditioning, but not all
## eigenpairs converge after 50 steps, so failureFlag=1.
##
## The next Example:
## @example
## blockVectorY = [];
## lambda_all = [];
## for j = 1:4
## [blockVectorX, lambda] = lobpcg (randn (n, 2), A, blockVectorY, 1e-5, 200, 2);
## blockVectorY = [blockVectorY, blockVectorX];
## lambda_all = [lambda_all' lambda']';
## pause;
## end
## @end example
##
## attemps to compute the same 8 eigenpairs by calling the code 4 times with
## blockSize=2 using orthogonalization to the previously founded eigenvectors.
##
## The following Example:
## @example
## R = ichol (A, struct('michol', 'on'));
## precfun = @@(x)R\(R'\x);
## [blockVectorX, lambda, failureFlag] = lobpcg (randn (n, 8), A, [], @@(x)precfun(x), 1e-5, 60, 2);
## @end example
##
## computes the same eigenpairs in less then 25 steps, so that failureFlag=0
## using the preconditioner function @code{precfun}, defined inline. If @code{precfun}
## is defined as an octave function in a file, the function handle
## @code{@@(x)precfun(x)} can be equivalently replaced by the function name @code{precfun}. Running:
##
## @example
## [blockVectorX, lambda, failureFlag] = lobpcg (randn (n, 8), A, speye (n), @@(x)precfun(x), 1e-5, 50, 2);
## @end example
##
## produces similar answers, but is somewhat slower and needs more memory as
## technically a generalized eigenproblem with B=I is solved here.
##
## The following example for a mostly diagonally dominant sparse matrix A
## demonstrates different types of preconditioning, compared to the standard
## use of the main diagonal of A:
##
## @example
## clear all; close all;
## n = 1000;
## M = spdiags ([1:n]', 0, n, n);
## precfun = @@(x)M\x;
## A = M + sprandsym (n, .1);
## Xini = randn (n, 5);
## maxiter = 15;
## tol = 1e-5;
## [~,~,~,~,rnp] = lobpcg (Xini, A, tol, maxiter, 1);
## [~,~,~,~,r] = lobpcg (Xini, A, [], @@(x)precfun(x), tol, maxiter, 1);
## subplot (2,2,1), semilogy (r'); hold on;
## semilogy (rnp', ':>');
## title ('No preconditioning (top)'); axis tight;
## M(1,2) = 2;
## precfun = @@(x)M\x; % M is no longer symmetric
## [~,~,~,~,rns] = lobpcg (Xini, A, [], @@(x)precfun(x), tol, maxiter, 1);
## subplot (2,2,2), semilogy (r'); hold on;
## semilogy (rns', '--s');
## title ('Nonsymmetric preconditioning (square)'); axis tight;
## M(1,2) = 0;
## precfun = @@(x)M\(x+10*sin(x)); % nonlinear preconditioning
## [~,~,~,~,rnl] = lobpcg (Xini, A, [], @@(x)precfun(x), tol, maxiter, 1);
## subplot (2,2,3), semilogy (r'); hold on;
## semilogy (rnl', '-.*');
## title ('Nonlinear preconditioning (star)'); axis tight;
## M = abs (M - 3.5 * speye (n, n));
## precfun = @@(x)M\x;
## [~,~,~,~,rs] = lobpcg (Xini, A, [], @@(x)precfun(x), tol, maxiter, 1);
## subplot (2,2,4), semilogy (r'); hold on;
## semilogy (rs', '-d');
## title ('Selective preconditioning (diamond)'); axis tight;
## @end example
##
## @heading References
## This main function @code{lobpcg} is a version of the preconditioned conjugate
## gradient method (Algorithm 5.1) described in A. V. Knyazev, Toward the Optimal
## Preconditioned Eigensolver:
## Locally Optimal Block Preconditioned Conjugate Gradient Method,
## SIAM Journal on Scientific Computing 23 (2001), no. 2, pp. 517-541.
## @uref{http://dx.doi.org/10.1137/S1064827500366124}
##
## @heading Known bugs/features
## @itemize @bullet
## @item
## an excessively small requested tolerance may result in often restarts and
## instability. The code is not written to produce an eps-level accuracy! Use
## common sense.
## @item
## the code may be very sensitive to the number of eigenpairs computed,
## if there is a cluster of eigenvalues not completely included, cf.
## @example
## operatorA = diag ([1 1.99 2:99]);
## [blockVectorX, lambda] = lobpcg (randn (100, 1),operatorA, 1e-10, 80, 2);
## [blockVectorX, lambda] = lobpcg (randn (100, 2),operatorA, 1e-10, 80, 2);
## [blockVectorX, lambda] = lobpcg (randn (100, 3),operatorA, 1e-10, 80, 2);
## @end example
## @end itemize
##
## @heading Distribution
## The main distribution site: @uref{http://math.ucdenver.edu/~aknyazev/}
##
## A C-version of this code is a part of the @uref{http://code.google.com/p/blopex/}
## package and is directly available, e.g., in PETSc and HYPRE.
## @end deftypefn
function [blockVectorX,lambda,varargout] = lobpcg(blockVectorX,operatorA,varargin)
%Begin
% constants
CONVENTIONAL_CONSTRAINTS = 1;
SYMMETRIC_CONSTRAINTS = 2;
%Initial settings
failureFlag = 1;
if nargin < 2
error('BLOPEX:lobpcg:NotEnoughInputs',...
strcat('There must be at least 2 input agruments: ',...
'blockVectorX and operatorA'));
end
if nargin > 8
warning('BLOPEX:lobpcg:TooManyInputs',...
strcat('There must be at most 8 input agruments ',...
'unless arguments are passed to a function'));
end
if ~isnumeric(blockVectorX)
error('BLOPEX:lobpcg:FirstInputNotNumeric',...
'The first input argument blockVectorX must be numeric');
end
[n,blockSize]=size(blockVectorX);
if blockSize > n
error('BLOPEX:lobpcg:FirstInputFat',...
'The first input argument blockVectorX must be tall, not fat');
end
if n < 6
error('BLOPEX:lobpcg:MatrixTooSmall',...
'The code does not work for matrices of small sizes');
end
if isa(operatorA,'numeric')
nA = size(operatorA,1);
if any(size(operatorA) ~= nA)
error('BLOPEX:lobpcg:MatrixNotSquare',...
'operatorA must be a square matrix or a string');
end
if size(operatorA) ~= n
error('BLOPEX:lobpcg:MatrixWrongSize',...
['The size ' int2str(size(operatorA))...
' of operatorA is not the same as ' int2str(n)...
' - the number of rows of blockVectorX']);
end
end
count_string = 0;
operatorT = [];
operatorB = [];
residualTolerance = [];
maxIterations = [];
verbosityLevel = [];
blockVectorY = []; sizeY = 0;
for j = 1:nargin-2
if isequal(size(varargin{j}),[n,n])
if isempty(operatorB)
operatorB = varargin{j};
else
error('BLOPEX:lobpcg:TooManyMatrixInputs',...
strcat('Too many matrix input arguments. ',...
'Preconditioner operatorT must be an M-function'));
end
elseif isequal(size(varargin{j},1),n) && size(varargin{j},2) < n
if isempty(blockVectorY)
blockVectorY = varargin{j};
sizeY=size(blockVectorY,2);
else
error('BLOPEX:lobpcg:WrongConstraintsFormat',...
'Something wrong with blockVectorY input argument');
end
elseif ischar(varargin{j}) || isa(varargin{j},'function_handle')
if count_string == 0
if isempty(operatorB)
operatorB = varargin{j};
count_string = count_string + 1;
else
operatorT = varargin{j};
end
elseif count_string == 1
operatorT = varargin{j};
else
warning('BLOPEX:lobpcg:TooManyStringFunctionHandleInputs',...
'Too many string or FunctionHandle input arguments');
end
elseif isequal(size(varargin{j}),[n,n])
error('BLOPEX:lobpcg:WrongPreconditionerFormat',...
'Preconditioner operatorT must be an M-function');
elseif max(size(varargin{j})) == 1
if isempty(residualTolerance)
residualTolerance = varargin{j};
elseif isempty(maxIterations)
maxIterations = varargin{j};
elseif isempty(verbosityLevel)
verbosityLevel = varargin{j};
else
warning('BLOPEX:lobpcg:TooManyScalarInputs',...
'Too many scalar parameters, need only three');
end
elseif isempty(varargin{j})
if isempty(operatorB)
count_string = count_string + 1;
elseif ~isempty(operatorT)
count_string = count_string + 1;
elseif ~isempty(blockVectorY)
error('BLOPEX:lobpcg:UnrecognizedEmptyInput',...
['Unrecognized empty input argument number ' int2str(j+2)]);
end
else
error('BLOPEX:lobpcg:UnrecognizedInput',...
['Input argument number ' int2str(j+2) ' not recognized.']);
end
end
if verbosityLevel
if issparse(blockVectorX)
fprintf(['The sparse initial guess with %i colunms '...
'and %i raws is detected \n'],n,blockSize);
else
fprintf(['The full initial guess with %i colunms '...
'and %i raws is detected \n'],n,blockSize);
end
if ischar(operatorA)
fprintf('The main operator is detected as an M-function %s \n',...
operatorA);
elseif isa(operatorA,'function_handle')
fprintf('The main operator is detected as an M-function %s \n',...
func2str(operatorA));
elseif issparse(operatorA)
fprintf('The main operator is detected as a sparse matrix \n');
else
fprintf('The main operator is detected as a full matrix \n');
end
if isempty(operatorB)
fprintf('Solving standard eigenvalue problem, not generalized \n');
elseif ischar(operatorB)
fprintf(['The second operator of the generalized eigenproblem \n'...
'is detected as an M-function %s \n'],operatorB);
elseif isa(operatorB,'function_handle')
fprintf(['The second operator of the generalized eigenproblem \n'...
'is detected as an M-function %s \n'],func2str(operatorB));
elseif issparse(operatorB)
fprintf(strcat('The second operator of the generalized',...
'eigenproblem \n is detected as a sparse matrix \n'));
else
fprintf(strcat('The second operator of the generalized',...
'eigenproblem \n is detected as a full matrix \n'));
end
if isempty(operatorT)
fprintf('No preconditioner is detected \n');
elseif ischar(operatorT)
fprintf('The preconditioner is detected as an M-function %s \n',...
operatorT);
elseif isa(operatorT,'function_handle')
fprintf('The preconditioner is detected as an M-function %s \n',...
func2str(operatorT));
end
if isempty(blockVectorY)
fprintf('No matrix of constraints is detected \n')
elseif issparse(blockVectorY)
fprintf('The sparse matrix of %i constraints is detected \n',sizeY);
else
fprintf('The full matrix of %i constraints is detected \n',sizeY);
end
if issparse(blockVectorY) ~= issparse(blockVectorX)
warning('BLOPEX:lobpcg:SparsityInconsistent',...
strcat('The sparsity formats of the initial guess and ',...
'the constraints are inconsistent'));
end
end
% Set defaults
if isempty(residualTolerance)
residualTolerance = sqrt(eps)*n;
end
if isempty(maxIterations)
maxIterations = min(n,20);
end
if isempty(verbosityLevel)
verbosityLevel = 0;
end
if verbosityLevel
fprintf('Tolerance %e and maximum number of iterations %i \n',...
residualTolerance,maxIterations)
end
%constraints preprocessing
if isempty(blockVectorY)
constraintStyle = 0;
else
% constraintStyle = SYMMETRIC_CONSTRAINTS; % more accurate?
constraintStyle = CONVENTIONAL_CONSTRAINTS;
end
if constraintStyle == CONVENTIONAL_CONSTRAINTS
if isempty(operatorB)
gramY = blockVectorY'*blockVectorY;
else
if isnumeric(operatorB)
blockVectorBY = operatorB*blockVectorY;
else
blockVectorBY = feval(operatorB,blockVectorY);
end
gramY=blockVectorY'*blockVectorBY;
end
gramY=(gramY'+gramY)*0.5;
if isempty(operatorB)
blockVectorX = blockVectorX - ...
blockVectorY*(gramY\(blockVectorY'*blockVectorX));
else
blockVectorX =blockVectorX - ...
blockVectorY*(gramY\(blockVectorBY'*blockVectorX));
end
elseif constraintStyle == SYMMETRIC_CONSTRAINTS
if ~isempty(operatorB)
if isnumeric(operatorB)
blockVectorY = operatorB*blockVectorY;
else
blockVectorY = feval(operatorB,blockVectorY);
end
end
if isempty(operatorT)
gramY = blockVectorY'*blockVectorY;
else
blockVectorTY = feval(operatorT,blockVectorY);
gramY = blockVectorY'*blockVectorTY;
end
gramY=(gramY'+gramY)*0.5;
if isempty(operatorT)
blockVectorX = blockVectorX - ...
blockVectorY*(gramY\(blockVectorY'*blockVectorX));
else
blockVectorX = blockVectorX - ...
blockVectorTY*(gramY\(blockVectorY'*blockVectorX));
end
end
%Making the initial vectors (operatorB-) orthonormal
if isempty(operatorB)
%[blockVectorX,gramXBX] = qr(blockVectorX,0);
gramXBX=blockVectorX'*blockVectorX;
if ~isreal(gramXBX)
gramXBX=(gramXBX+gramXBX')*0.5;
end
[gramXBX,cholFlag]=chol(gramXBX);
if cholFlag ~= 0
error('BLOPEX:lobpcg:ConstraintsTooTight',...
'The initial approximation after constraints is not full rank');
end
blockVectorX = blockVectorX/gramXBX;
else
%[blockVectorX,blockVectorBX] = orth(operatorB,blockVectorX);
if isnumeric(operatorB)
blockVectorBX = operatorB*blockVectorX;
else
blockVectorBX = feval(operatorB,blockVectorX);
end
gramXBX=blockVectorX'*blockVectorBX;
if ~isreal(gramXBX)
gramXBX=(gramXBX+gramXBX')*0.5;
end
[gramXBX,cholFlag]=chol(gramXBX);
if cholFlag ~= 0
error('BLOPEX:lobpcg:InitialNotFullRank',...
sprintf('%s\n%s', ...
'The initial approximation after constraints is not full rank',...
'or/and operatorB is not positive definite'));
end
blockVectorX = blockVectorX/gramXBX;
blockVectorBX = blockVectorBX/gramXBX;
end
% Checking if the problem is big enough for the algorithm,
% i.e. n-sizeY > 5*blockSize
% Theoretically, the algorithm should be able to run if
% n-sizeY > 3*blockSize,
% but the extreme cases might be unstable, so we use 5 instead of 3 here.
if n-sizeY < 5*blockSize
error('BLOPEX:lobpcg:MatrixTooSmall','%s\n%s', ...
'The problem size is too small, relative to the block size.',...
'Try using eig() or eigs() instead.');
end
% Preallocation
residualNormsHistory=zeros(blockSize,maxIterations);
lambdaHistory=zeros(blockSize,maxIterations+1);
condestGhistory=zeros(1,maxIterations+1);
blockVectorBR=zeros(n,blockSize);
blockVectorAR=zeros(n,blockSize);
blockVectorP=zeros(n,blockSize);
blockVectorAP=zeros(n,blockSize);
blockVectorBP=zeros(n,blockSize);
%Initial settings for the loop
if isnumeric(operatorA)
blockVectorAX = operatorA*blockVectorX;
else
blockVectorAX = feval(operatorA,blockVectorX);
end
gramXAX = full(blockVectorX'*blockVectorAX);
gramXAX = (gramXAX + gramXAX')*0.5;
% eig(...,'chol') uses only the diagonal and upper triangle -
% not true in MATLAB
% Octave v3.2.3-4, eig() does not support inputting 'chol'
[coordX,gramXAX]=eig(gramXAX,eye(blockSize));
lambda=diag(gramXAX); %eig returns non-ordered eigenvalues on the diagonal
if issparse(blockVectorX)
coordX=sparse(coordX);
end
blockVectorX = blockVectorX*coordX;
blockVectorAX = blockVectorAX*coordX;
if ~isempty(operatorB)
blockVectorBX = blockVectorBX*coordX;
end
clear coordX
condestGhistory(1)=-log10(eps)/2; %if too small cause unnecessary restarts
lambdaHistory(1:blockSize,1)=lambda;
activeMask = true(blockSize,1);
% currentBlockSize = blockSize; %iterate all
%
% restart=1;%steepest descent
%The main part of the method is the loop of the CG method: begin
for iterationNumber=1:maxIterations
% %Computing the active residuals
% if isempty(operatorB)
% if currentBlockSize > 1
% blockVectorR(:,activeMask)=blockVectorAX(:,activeMask) - ...
% blockVectorX(:,activeMask)*spdiags(lambda(activeMask),0,currentBlockSize,currentBlockSize);
% else
% blockVectorR(:,activeMask)=blockVectorAX(:,activeMask) - ...
% blockVectorX(:,activeMask)*lambda(activeMask);
% %to make blockVectorR full when lambda is just a scalar
% end
% else
% if currentBlockSize > 1
% blockVectorR(:,activeMask)=blockVectorAX(:,activeMask) - ...
% blockVectorBX(:,activeMask)*spdiags(lambda(activeMask),0,currentBlockSize,currentBlockSize);
% else
% blockVectorR(:,activeMask)=blockVectorAX(:,activeMask) - ...
% blockVectorBX(:,activeMask)*lambda(activeMask);
% %to make blockVectorR full when lambda is just a scalar
% end
% end
%Computing all residuals
if isempty(operatorB)
if blockSize > 1
blockVectorR = blockVectorAX - ...
blockVectorX*spdiags(lambda,0,blockSize,blockSize);
else
blockVectorR = blockVectorAX - blockVectorX*lambda;
%to make blockVectorR full when lambda is just a scalar
end
else
if blockSize > 1
blockVectorR=blockVectorAX - ...
blockVectorBX*spdiags(lambda,0,blockSize,blockSize);
else
blockVectorR = blockVectorAX - blockVectorBX*lambda;
%to make blockVectorR full when lambda is just a scalar
end
end
%Satisfying the constraints for the active residulas
if constraintStyle == SYMMETRIC_CONSTRAINTS
if isempty(operatorT)
blockVectorR(:,activeMask) = blockVectorR(:,activeMask) - ...
blockVectorY*(gramY\(blockVectorY'*...
blockVectorR(:,activeMask)));
else
blockVectorR(:,activeMask) = blockVectorR(:,activeMask) - ...
blockVectorY*(gramY\(blockVectorTY'*...
blockVectorR(:,activeMask)));
end
end
residualNorms=full(sqrt(sum(conj(blockVectorR).*blockVectorR)'));
residualNormsHistory(1:blockSize,iterationNumber)=residualNorms;
%index antifreeze
activeMask = full(residualNorms > residualTolerance) & activeMask;
%activeMask = full(residualNorms > residualTolerance);
%above allows vectors back into active, which causes problems with frosen Ps
%activeMask = full(residualNorms > 0); %iterate all, ignore freeze
currentBlockSize = sum(activeMask);
if currentBlockSize == 0
failureFlag=0; %all eigenpairs converged
break
end
%Applying the preconditioner operatorT to the active residulas
if ~isempty(operatorT)
blockVectorR(:,activeMask) = ...
feval(operatorT,blockVectorR(:,activeMask));
end
if constraintStyle == CONVENTIONAL_CONSTRAINTS
if isempty(operatorB)
blockVectorR(:,activeMask) = blockVectorR(:,activeMask) - ...
blockVectorY*(gramY\(blockVectorY'*...
blockVectorR(:,activeMask)));
else
blockVectorR(:,activeMask) = blockVectorR(:,activeMask) - ...
blockVectorY*(gramY\(blockVectorBY'*...
blockVectorR(:,activeMask)));
end
end
%Making active (preconditioned) residuals orthogonal to blockVectorX
if isempty(operatorB)
blockVectorR(:,activeMask) = blockVectorR(:,activeMask) - ...
blockVectorX*(blockVectorX'*blockVectorR(:,activeMask));
else
blockVectorR(:,activeMask) = blockVectorR(:,activeMask) - ...
blockVectorX*(blockVectorBX'*blockVectorR(:,activeMask));
end
%Making active residuals orthonormal
if isempty(operatorB)
%[blockVectorR(:,activeMask),gramRBR]=...
%qr(blockVectorR(:,activeMask),0); %to increase stability
gramRBR=blockVectorR(:,activeMask)'*blockVectorR(:,activeMask);
if ~isreal(gramRBR)
gramRBR=(gramRBR+gramRBR')*0.5;
end
[gramRBR,cholFlag]=chol(gramRBR);
if cholFlag == 0
blockVectorR(:,activeMask) = blockVectorR(:,activeMask)/gramRBR;
else
warning('BLOPEX:lobpcg:ResidualNotFullRank',...
'The residual is not full rank.');
break
end
else
if isnumeric(operatorB)
blockVectorBR(:,activeMask) = ...
operatorB*blockVectorR(:,activeMask);
else
blockVectorBR(:,activeMask) = ...
feval(operatorB,blockVectorR(:,activeMask));
end
gramRBR=blockVectorR(:,activeMask)'*blockVectorBR(:,activeMask);
if ~isreal(gramRBR)
gramRBR=(gramRBR+gramRBR')*0.5;
end
[gramRBR,cholFlag]=chol(gramRBR);
if cholFlag == 0
blockVectorR(:,activeMask) = ...
blockVectorR(:,activeMask)/gramRBR;
blockVectorBR(:,activeMask) = ...
blockVectorBR(:,activeMask)/gramRBR;
else
warning('BLOPEX:lobpcg:ResidualNotFullRankOrElse',...
strcat('The residual is not full rank or/and operatorB ',...
'is not positive definite.'));
break
end
end
clear gramRBR;
if isnumeric(operatorA)
blockVectorAR(:,activeMask) = operatorA*blockVectorR(:,activeMask);
else
blockVectorAR(:,activeMask) = ...
feval(operatorA,blockVectorR(:,activeMask));
end
if iterationNumber > 1
%Making active conjugate directions orthonormal
if isempty(operatorB)
%[blockVectorP(:,activeMask),gramPBP] = qr(blockVectorP(:,activeMask),0);
gramPBP=blockVectorP(:,activeMask)'*blockVectorP(:,activeMask);
if ~isreal(gramPBP)
gramPBP=(gramPBP+gramPBP')*0.5;
end
[gramPBP,cholFlag]=chol(gramPBP);
if cholFlag == 0
blockVectorP(:,activeMask) = ...
blockVectorP(:,activeMask)/gramPBP;
blockVectorAP(:,activeMask) = ...
blockVectorAP(:,activeMask)/gramPBP;
else
warning('BLOPEX:lobpcg:DirectionNotFullRank',...
'The direction matrix is not full rank.');
break
end
else
gramPBP=blockVectorP(:,activeMask)'*blockVectorBP(:,activeMask);
if ~isreal(gramPBP)
gramPBP=(gramPBP+gramPBP')*0.5;
end
[gramPBP,cholFlag]=chol(gramPBP);
if cholFlag == 0
blockVectorP(:,activeMask) = ...
blockVectorP(:,activeMask)/gramPBP;
blockVectorAP(:,activeMask) = ...
blockVectorAP(:,activeMask)/gramPBP;
blockVectorBP(:,activeMask) = ...
blockVectorBP(:,activeMask)/gramPBP;
else
warning('BLOPEX:lobpcg:DirectionNotFullRank',...
strcat('The direction matrix is not full rank ',...
'or/and operatorB is not positive definite.'));
break
end
end
clear gramPBP
end
condestGmean = mean(condestGhistory(max(1,iterationNumber-10-...
round(log(currentBlockSize))):iterationNumber));
% restart=1;
% The Raileight-Ritz method for [blockVectorX blockVectorR blockVectorP]
if residualNorms > eps^0.6
explicitGramFlag = 0;
else
explicitGramFlag = 1; %suggested by Garrett Moran, private
end
activeRSize=size(blockVectorR(:,activeMask),2);
if iterationNumber == 1
activePSize=0;
restart=1;
else
activePSize=size(blockVectorP(:,activeMask),2);
restart=0;
end
gramXAR=full(blockVectorAX'*blockVectorR(:,activeMask));
gramRAR=full(blockVectorAR(:,activeMask)'*blockVectorR(:,activeMask));
gramRAR=(gramRAR'+gramRAR)*0.5;
if explicitGramFlag
gramXAX=full(blockVectorAX'*blockVectorX);
gramXAX=(gramXAX'+gramXAX)*0.5;
if isempty(operatorB)
gramXBX=full(blockVectorX'*blockVectorX);
gramRBR=full(blockVectorR(:,activeMask)'*...
blockVectorR(:,activeMask));
gramXBR=full(blockVectorX'*blockVectorR(:,activeMask));
else
gramXBX=full(blockVectorBX'*blockVectorX);
gramRBR=full(blockVectorBR(:,activeMask)'*...
blockVectorR(:,activeMask));
gramXBR=full(blockVectorBX'*blockVectorR(:,activeMask));
end
gramXBX=(gramXBX'+gramXBX)*0.5;
gramRBR=(gramRBR'+gramRBR)*0.5;
end
for cond_try=1:2, %cond_try == 2 when restart
if ~restart
gramXAP=full(blockVectorAX'*blockVectorP(:,activeMask));
gramRAP=full(blockVectorAR(:,activeMask)'*...
blockVectorP(:,activeMask));
gramPAP=full(blockVectorAP(:,activeMask)'*...
blockVectorP(:,activeMask));
gramPAP=(gramPAP'+gramPAP)*0.5;
if explicitGramFlag
gramA = [ gramXAX gramXAR gramXAP
gramXAR' gramRAR gramRAP
gramXAP' gramRAP' gramPAP ];
else
gramA = [ diag(lambda) gramXAR gramXAP
gramXAR' gramRAR gramRAP
gramXAP' gramRAP' gramPAP ];
end
clear gramXAP gramRAP gramPAP
if isempty(operatorB)
gramXBP=full(blockVectorX'*blockVectorP(:,activeMask));
gramRBP=full(blockVectorR(:,activeMask)'*...
blockVectorP(:,activeMask));
else
gramXBP=full(blockVectorBX'*blockVectorP(:,activeMask));
gramRBP=full(blockVectorBR(:,activeMask)'*...
blockVectorP(:,activeMask));
%or blockVectorR(:,activeMask)'*blockVectorBP(:,activeMask);
end
if explicitGramFlag
if isempty(operatorB)
gramPBP=full(blockVectorP(:,activeMask)'*...
blockVectorP(:,activeMask));
else
gramPBP=full(blockVectorBP(:,activeMask)'*...
blockVectorP(:,activeMask));
end
gramPBP=(gramPBP'+gramPBP)*0.5;
gramB = [ gramXBX gramXBR gramXBP
gramXBR' gramRBR gramRBP
gramXBP' gramRBP' gramPBP ];
clear gramPBP
else
gramB=[eye(blockSize) zeros(blockSize,activeRSize) gramXBP
zeros(blockSize,activeRSize)' eye(activeRSize) gramRBP
gramXBP' gramRBP' eye(activePSize) ];
end
clear gramXBP gramRBP;
else
if explicitGramFlag
gramA = [ gramXAX gramXAR
gramXAR' gramRAR ];
gramB = [ gramXBX gramXBR
gramXBR' eye(activeRSize) ];
clear gramXAX gramXBX gramXBR
else
gramA = [ diag(lambda) gramXAR
gramXAR' gramRAR ];
gramB = eye(blockSize+activeRSize);
end
clear gramXAR gramRAR;
end
condestG = log10(cond(gramB))+1;
if (condestG/condestGmean > 2 && condestG > 2 )|| condestG > 8
%black magic - need to guess the restart
if verbosityLevel
fprintf('Restart on step %i as condestG %5.4e \n',...
iterationNumber,condestG);
end
if cond_try == 1 && ~restart
restart=1; %steepest descent restart for stability
else
warning('BLOPEX:lobpcg:IllConditioning',...
'Gramm matrix ill-conditioned: results unpredictable');
end
else
break
end
end
[gramA,gramB]=eig(gramA,gramB);
lambda=diag(gramB(1:blockSize,1:blockSize));
coordX=gramA(:,1:blockSize);
clear gramA gramB
if issparse(blockVectorX)
coordX=sparse(coordX);
end
if ~restart
blockVectorP = blockVectorR(:,activeMask)*...
coordX(blockSize+1:blockSize+activeRSize,:) + ...
blockVectorP(:,activeMask)*...
coordX(blockSize+activeRSize+1:blockSize + ...
activeRSize+activePSize,:);
blockVectorAP = blockVectorAR(:,activeMask)*...
coordX(blockSize+1:blockSize+activeRSize,:) + ...
blockVectorAP(:,activeMask)*...
coordX(blockSize+activeRSize+1:blockSize + ...
activeRSize+activePSize,:);
if ~isempty(operatorB)
blockVectorBP = blockVectorBR(:,activeMask)*...
coordX(blockSize+1:blockSize+activeRSize,:) + ...
blockVectorBP(:,activeMask)*...
coordX(blockSize+activeRSize+1:blockSize+activeRSize+activePSize,:);
end
else %use block steepest descent
blockVectorP = blockVectorR(:,activeMask)*...
coordX(blockSize+1:blockSize+activeRSize,:);
blockVectorAP = blockVectorAR(:,activeMask)*...
coordX(blockSize+1:blockSize+activeRSize,:);
if ~isempty(operatorB)
blockVectorBP = blockVectorBR(:,activeMask)*...
coordX(blockSize+1:blockSize+activeRSize,:);
end
end
blockVectorX = blockVectorX*coordX(1:blockSize,:) + blockVectorP;
blockVectorAX=blockVectorAX*coordX(1:blockSize,:) + blockVectorAP;
if ~isempty(operatorB)
blockVectorBX=blockVectorBX*coordX(1:blockSize,:) + blockVectorBP;
end
clear coordX
%%end RR
lambdaHistory(1:blockSize,iterationNumber+1)=lambda;
condestGhistory(iterationNumber+1)=condestG;
if verbosityLevel
fprintf('Iteration %i current block size %i \n',...
iterationNumber,currentBlockSize);
fprintf('Eigenvalues lambda %17.16e \n',lambda);
fprintf('Residual Norms %e \n',residualNorms');
end
end
%The main step of the method was the CG cycle: end
%Postprocessing
%Making sure blockVectorX's "exactly" satisfy the blockVectorY constrains??
%Making sure blockVectorX's are "exactly" othonormalized by final "exact" RR
if isempty(operatorB)
gramXBX=full(blockVectorX'*blockVectorX);
else
if isnumeric(operatorB)
blockVectorBX = operatorB*blockVectorX;
else
blockVectorBX = feval(operatorB,blockVectorX);
end
gramXBX=full(blockVectorX'*blockVectorBX);
end
gramXBX=(gramXBX'+gramXBX)*0.5;
if isnumeric(operatorA)
blockVectorAX = operatorA*blockVectorX;
else
blockVectorAX = feval(operatorA,blockVectorX);
end
gramXAX = full(blockVectorX'*blockVectorAX);
gramXAX = (gramXAX + gramXAX')*0.5;
%Raileigh-Ritz for blockVectorX, which is already operatorB-orthonormal
[coordX,gramXBX] = eig(gramXAX,gramXBX);
lambda=diag(gramXBX);
if issparse(blockVectorX)
coordX=sparse(coordX);
end
blockVectorX = blockVectorX*coordX;
blockVectorAX = blockVectorAX*coordX;
if ~isempty(operatorB)
blockVectorBX = blockVectorBX*coordX;
end
%Computing all residuals
if isempty(operatorB)
if blockSize > 1
blockVectorR = blockVectorAX - ...
blockVectorX*spdiags(lambda,0,blockSize,blockSize);
else
blockVectorR = blockVectorAX - blockVectorX*lambda;
%to make blockVectorR full when lambda is just a scalar
end
else
if blockSize > 1
blockVectorR=blockVectorAX - ...
blockVectorBX*spdiags(lambda,0,blockSize,blockSize);
else
blockVectorR = blockVectorAX - blockVectorBX*lambda;
%to make blockVectorR full when lambda is just a scalar
end
end
residualNorms=full(sqrt(sum(conj(blockVectorR).*blockVectorR)'));
residualNormsHistory(1:blockSize,iterationNumber)=residualNorms;
if verbosityLevel
fprintf('Final Eigenvalues lambda %17.16e \n',lambda);
fprintf('Final Residual Norms %e \n',residualNorms');
end
if verbosityLevel == 2
whos
figure(491)
semilogy((abs(residualNormsHistory(1:blockSize,1:iterationNumber-1)))');
title('Residuals for Different Eigenpairs','fontsize',16);
ylabel('Eucledian norm of residuals','fontsize',16);
xlabel('Iteration number','fontsize',16);
%axis tight;
%axis([0 maxIterations+1 1e-15 1e3])
set(gca,'FontSize',14);
figure(492);
semilogy(abs((lambdaHistory(1:blockSize,1:iterationNumber)-...
repmat(lambda,1,iterationNumber)))');
title('Eigenvalue errors for Different Eigenpairs','fontsize',16);
ylabel('Estimated eigenvalue errors','fontsize',16);
xlabel('Iteration number','fontsize',16);
%axis tight;
%axis([0 maxIterations+1 1e-15 1e3])
set(gca,'FontSize',14);
drawnow;
end
varargout(1)={failureFlag};
varargout(2)={lambdaHistory(1:blockSize,1:iterationNumber)};
varargout(3)={residualNormsHistory(1:blockSize,1:iterationNumber-1)};
end
|