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.
102 function ret = IsDeployedMKL( obj )
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');
156 %> @brief Calculates the generalized eigenvalue problem based on the zggev and dggev LAPACK functions.
157 %> @
param A A square matrix on the left side.
158 %> @
param B A square matrix on the right side.
159 %> @return Returns the calculated generalized eigenvalues and the right and left sided eigenvectors.
160 function [eigenvecs_right, eigenvecs_left, eigvals] = eig( obj, A, B )
161 %TODO check whether LAPACK is deployed
162 % if both the matrices A and B are reals
163 if isreal(A) && isreal(B)
164 % check the presence of the compiled MEX file
166 [directory, name, ext] = fileparts(which(fncname));
167 if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
168 warning('CommonFunction:eig', ...
169 'Function dggev is not compiled yet. Please compile first with the MKL component');
170 [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
174 [alphavarR, alphavarI, betavar, eigenvecs_left, eigenvecs_right] = dggev(A,B);
175 eigenvecs_left = eigenvecs_left';
176 eigvals = (alphavarR + 1i*alphavarI)./betavar;
188 % check the presence of the compiled MEX file
190 [directory, name, ext] = fileparts(which(fncname));
191 if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
192 warning('CommonFunction:eig', ...
193 'Function zggev is not compiled yet. Please compile first with the MKL component');
194 [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB();
198 [alphavar, betavar, eigenvecs_left, eigenvecs_right] = zggev(A,B);
199 eigenvecs_left = eigenvecs_left';
200 eigvals = alphavar./betavar;
204 %--------------------------------
205 % Calculates the right and left eigevectors with MATLAB
206 function [eigenvecs_right, eigenvecs_left, eigvals] = eig_MATLAB()
208 [modusmtx_right,k] = eig(A,B);
209 [modusmtx_left,k_left] = eig(transpose(A),transpose(B));
210 modusmtx_left = transpose(modusmtx_left);
211 modusmtx_left_tmp = zeros(size(modusmtx_left));
214 k_left = diag(k_left);
216 for idx = 1:length(k)
217 [~,jdx] = min( abs(k_left -k(idx)));
218 modusmtx_left_tmp(idx,:) = modusmtx_left(jdx,:);
221 eigenvecs_right = modusmtx_right;
222 eigenvecs_left = modusmtx_left_tmp;
227 % end nested functions
233 %> @brief Gets the current version of the package.
234 %> @return Returns the current version of the package.
235 function ret = getVersion( obj )
240 %> @brief Gets the name of the package.
241 %> @return Returns the name of the package.
242 function ret = getProgramName( obj )
243 ret = obj.ProgramName;
246 %% getProgramShortName
247 %> @brief Gets the
short name of the package.
248 %> @return Returns the
short name of the package.
249 function ret = getProgramShortName( obj )
250 ret = obj.ProgramShortName;
258 methods (Access = public, Static = true )
261 %> @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
262 %> @
param y Function values to be integrated
263 %> @
param h Increment between the x_i points.
264 function ret = SimphsonInt(y,h)
265 if ~exist(
'h',
'var' )
269 if mod(length(y),2) == 0
270 warning('length of the vector to integral has to be (2n+1)')
278 ret = h/3*( ya + yb ) + 2*h/3*sum(y(2:2:end-2)) + 4*h/3*sum(y(1:2:end-1));
285 %> @brief Inverts badly conditioned matrix A with SVD regularization
286 %> @
param A A square matrix to be inverted.
287 %> @return Returns the calculated inverse.
288 function invA = inv_SVD( A )
289 SVDtolerance = 1e-15;
292 [U,S,V] = svd(full(A));
299 indexes = logical( abs(S) < smax*SVDtolerance );
300 S(indexes) = smax*SVDtolerance;
309 %> @brief Checks the presence of a given MEX file.
310 %> @
param fncname The name of the function.
311 %> @return Returns true if the MEX file is present, false otherwise.
312 function ret = checkMEXfile( fncname )
313 [directory, name, ext] = fileparts(which(fncname));
314 if (~strcmpi(name, fncname) || ~strcmpi(ext, ['.' mexext]))
323 %> @brief Checks whether the running environment is matlab or octave.
324 %> @return Returns true if running environment is octave, false otherwise.
325 function retval = isOctave()
326 persistent cacheval; % speeds up repeated calls
328 if isempty (cacheval)
329 cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
function Transport(Energy, B)
creating the Ribbon class representing the twoterminal setup
A class containing common basic functionalities.
Structure param contains data structures describing the physical parameters of the scattering center ...