2 % Copyright (C) 2009-2015 Peter Rakyta, Ph.D.
4 % This program is free software: you can redistribute it and/or modify
5 % it under the terms of the GNU General Public License as published by
6 % the Free Software Foundation, either version 3 of the License, or
7 % (at your option) any later version.
9 % This program is distributed in the hope that it will be useful,
10 % but WITHOUT ANY WARRANTY; without even the implied warranty of
11 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 % GNU General Public License
for more details.
14 % You should have received a copy of the GNU General Public License
15 % along with
this program. If not, see http:
17 %> @addtogroup basic Basic Functionalities
20 %> @brief A
class containing common basic functionalities.
22 %> @brief A
class containing common basic functionalities.
27 properties (Access=
protected)
28 %> True
if MKL component is built,
false otherwise.
30 %> The current version of the package.
32 %> Name of the package.
33 ProgramName =
'Eotvos Quantum Transport Utilities';
34 %> Short name of the package.
35 ProgramShortName =
'EQuUs';
36 %> Maximal size of full matrixes to be handled
45 %> @brief Function to load XML files (based on xerces libraries), providing octave compatibility.
46 %> @
param filename Absolute path to the file to be opened. (In matlab relative path is sufficient)
47 %> @
return The loaded document
object model (see http:
48 function tree = xmlread( obj, filename )
52 parser = javaObject(
'org.apache.xerces.parsers.DOMParser');
54 error (
'xmlread: could not load Xerces parser object, you may have to add required .jar files to your javapath. See the documentation for further details')
57 parser.parse(filename);
58 tree = parser.getDocument;
60 tree = xmlread(filename);
67 %> @brief Function to export XML files (based on xerces libraries), providing octave compatibility.
68 %> @
param filename Absolute path to the file to be opened. (In matlab relative path is sufficient)
69 %> @
param DOM The loaded document
object model (see http:
70 function xmlwrite( obj, filename, DOM )
74 jfile = javaObject ('java.io.File', filename);
75 jos = javaObject ('java.io.FileOutputStream', jfile);
76 jfmt = javaObject ('org.apache.xml.serialize.OutputFormat', DOM);
78 error ('xmlwrite: could not load Xerces serializer
object, you may have to add required .jar files to your javapath. See the documentation for further details')
81 jfmt.setIndenting (1);
82 serializer = javaObject ('org.apache.xml.serialize.XMLSerializer', ...
84 serializer.serialize (DOM);
86 if (nargout > 0 && strcmp (class (jos), 'java.io.StringWriter'))
87 str =
char (jos.toString ());
91 xmlwrite(filename, DOM);
100 %> @brief Checks whether the MKL component is deployed
101 %> @return Returns with true if MKL component is deployed, false otherwise.
104 if isempty(obj.DeployedMKL)
105 obj.DeployedMKL = obj.checkMEXfile( 'dgetPartialInv' ) & ...
106 obj.checkMEXfile( 'zgetPartialInv' );
109 ret = obj.DeployedMKL;
116 %> @brief Calculates a partial inverse of a sparse matrix. If MKL component is not developed, the backslash operator is used insted.
117 %> @
param A A sparse matrix to be inverted
118 %> @
param sizeInv The size of partial inverse to be calculated.
119 %> @return Returns the calculated partial inverse
120 function invA = partialInv( obj, A, sizeInv )
123 if ( size(A,1) > obj.MaxSize )
124 error('EQuUs:
CommonFunctions:partialInv', ['Matrix A has to be sparse or a full matrix smaller than ', num2str(obj.MaxSize), 'x', num2str(obj.MaxSize)]);
128 if (~obj.IsDeployedMKL()) && issparse(A)
129 evec = sparse( size(A,1)-sizeInv+1:size(A,1), 1:sizeInv, 1, size(A,1), sizeInv);
130 invA = full(A \ evec);
131 invA = invA( size(A,1)-sizeInv+1:end, :);
133 evec = zeros( size(A,1), sizeInv);
134 evec( end-sizeInv+1:end, : ) = eye(sizeInv);
135 invA = full(A \ evec);
136 invA = invA( size(A,1)-sizeInv+1:end, :);
140 invA = dgetPartialInv(A, sizeInv);
142 invA = zgetPartialInv(A, sizeInv);
145 save('matrix.mat', 'A');
154 %> @brief Calculates the diagonal part of the inverse of a sparse matrix. If MKL component is advised to build.
155 %> @
param A A sparse matrix to be inverted
156 %> @return Returns the diagonal elements of the inverse
159 if ~issparse(A) && ( size(A,1) > obj.MaxSize )
160 error('EQuUs:
CommonFunctions:diagInv', ['Matrix A has to be sparse or smaller full matrix than ', num2str(obj.MaxSize), 'x', num2str(obj.MaxSize)]);
163 if ~obj.IsDeployedMKL() && (~issparse(A))
169 invA = dgetDiagInv(A);
171 invA = zgetDiagInv(A);
174 save('matrix.mat', 'A');
184 %> @brief Calculates the generalized eigenvalue problem based on the zggev and dggev LAPACK functions.
185 %> @
param A A square matrix on the left side.
186 %> @
param B A square matrix on the right side.
187 %> @return Returns the calculated generalized eigenvalues and the right and left sided eigenvectors.
188 function [eigenvecs_right, eigenvecs_left, eigvals] = eig( obj, A, B )
189 %TODO check whether LAPACK is deployed
190 % if both the matrices A and B are reals
191 if isreal(A) && isreal(B)
192 % check the presence of the compiled MEX file
194 [directory, name, ext] = fileparts(which(fncname));
195 if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
196 warning('CommonFunction:eig', ...
197 'Function dggev is not compiled yet. Please compile first with the MKL component');
198 [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
202 [alphavarR, alphavarI, betavar, eigenvecs_left, eigenvecs_right] = dggev(A,B);
203 eigenvecs_left = eigenvecs_left';
204 eigvals = (alphavarR + 1i*alphavarI)./betavar;
216 % check the presence of the compiled MEX file
218 [directory, name, ext] = fileparts(which(fncname));
219 if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
220 warning('CommonFunction:eig', ...
221 'Function zggev is not compiled yet. Please compile first with the MKL component');
222 [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
226 [alphavar, betavar, eigenvecs_left, eigenvecs_right] = zggev(A,B);
227 eigenvecs_left = eigenvecs_left';
228 eigvals = alphavar./betavar;
232 %--------------------------------
233 % Calculates the right and left eigevectors with MATLAB
234 function [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB()
236 [modusmtx_right,k] = eig(A,B);
237 [modusmtx_left,k_left] = eig(transpose(A),transpose(B));
238 modusmtx_left = transpose(modusmtx_left);
239 modusmtx_left_tmp = zeros(size(modusmtx_left));
242 k_left = diag(k_left);
244 for idx = 1:length(k)
245 [~,jdx] = min( abs(k_left -k(idx)));
246 modusmtx_left_tmp(idx,:) = modusmtx_left(jdx,:);
249 eigenvecs_right = modusmtx_right;
250 eigenvecs_left = modusmtx_left_tmp;
255 % end nested functions
261 %> @brief Gets the current version of the package.
262 %> @return Returns the current version of the package.
268 %> @brief Gets the name of the package.
269 %> @return Returns the name of the package.
270 function ret = getProgramName( obj )
271 ret = obj.ProgramName;
274 %% getProgramShortName
275 %> @brief Gets the
short name of the package.
276 %> @return Returns the
short name of the package.
277 function ret = getProgramShortName( obj )
278 ret = obj.ProgramShortName;
286 methods (Access = public, Static = true )
289 %> @brief Simphson integral: y is a vector of
function values at equal distant points: y_i = f(x_i), x_i-x_{i-1}=h
290 %> @
param y Function values to be integrated
291 %> @
param h Increment between the x_i points.
292 function ret = SimphsonInt(y,h)
293 if ~exist(
'h',
'var' )
297 if mod(length(y),2) == 0
298 warning('length of the vector to integral has to be (2n+1)')
306 ret = h/3*( ya + yb ) + 2*h/3*sum(y(2:2:end-2)) + 4*h/3*sum(y(1:2:end-1));
313 %> @brief Inverts badly conditioned matrix A with SVD regularization
314 %> @
param A A square matrix to be inverted.
315 %> @return Returns the calculated inverse.
317 SVDtolerance = 1e-15;
320 [U,S,V] = svd(full(A));
327 indexes = logical( abs(S) < smax*SVDtolerance );
328 S(indexes) = smax*SVDtolerance;
337 %> @brief Checks the presence of a given MEX file.
339 %> @return Returns true if the MEX file is present, false otherwise.
340 function ret = checkMEXfile( fncname )
341 [directory, name, ext] = fileparts(which(fncname));
342 if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
351 %> @brief Checks whether the running environment is matlab or octave.
352 %> @return Returns true if running environment is octave, false otherwise.
354 persistent cacheval; % speeds up repeated calls
356 if isempty (cacheval)
357 cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
function Transport(Energy, B)
Calculates the conductance at a given energy value.
A class containing common basic functionalities.
Structure param contains data structures describing the physical parameters of the scattering center ...