-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '2be67d42646a9d5c790ef2397cd2570feea0cbb9'
- Loading branch information
Showing
12 changed files
with
275 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function A=chopcompl( A, delta ) | ||
% CHOPCOMPL Replace real or imaginary part of complex numbers close to zero with zero. | ||
% CHOPCOMPL(A) returns A with all real or imaginary parts of the numbers | ||
% in A smaller then 1e-10 in magnitude replaced by 0. CHOPCHOPCOMPL(A, | ||
% DELTA) returns A with all numbers in A smaller then DELTA in magnitude | ||
% replaced by 0. | ||
% | ||
% Note: this function was named CHOPCOMPL to distinguish it from the CHOP | ||
% function of the control toolbox and further to make clear that it does | ||
% absolute chopping; not relative like the control toolbox function does. | ||
% | ||
% Example (<a href="matlab:run_example chopcompl">run</a>) | ||
% N = 10; | ||
% f = cos(linspace(-1,1,N)); | ||
% % Do an FFT of an even function there are lots | ||
% % tiny complex parts, which should really be zero | ||
% F = fft(f).*exp(-2*pi*i*(0:N-1)/(2*N)) | ||
% % Apply chopcompl and they are gone (same with odd functions like...) | ||
% F = chopcompl( F ) | ||
% | ||
% See also CHOPABS, ROUND, CEIL, FLOOR | ||
|
||
% Elmar Zander | ||
% Copyright 2016, Institute of Scientific Computing, TU Braunschweig. | ||
% | ||
% This program is free software: you can redistribute it and/or modify it | ||
% under the terms of the GNU General Public License as published by the | ||
% Free Software Foundation, either version 3 of the License, or (at your | ||
% option) any later version. | ||
% See the GNU General Public License for more details. You should have | ||
% received a copy of the GNU General Public License along with this | ||
% program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
if nargin<2 | ||
delta=1e-10; | ||
end | ||
|
||
ind = abs(real(A))<delta; | ||
A(ind)=1i * imag(A(ind)); | ||
|
||
ind = abs(imag(A))<delta; | ||
A(ind)=real(A(ind)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
classdef CachedFunctionDir < handle | ||
properties | ||
cached_function | ||
cache_filename | ||
verbosity = 0; | ||
version_tag = 0; | ||
end | ||
|
||
|
||
methods(Static) | ||
function hashstr=get_hashstr(args) | ||
% GET_HASHSTR Generate unique hash value from data | ||
digest = java.security.MessageDigest.getInstance('SHA1'); | ||
for i=1:length(args) | ||
data = typecast(args{i}, 'uint8'); | ||
digest.update(data) | ||
end | ||
hash = typecast(digest.digest(), 'uint8'); | ||
hashstr = sprintf('%.2X', hash); | ||
end | ||
end | ||
|
||
methods | ||
function cfunc=CachedFunctionDir(func, filename, varargin) | ||
% CACHEDFUNCTION Create a CacheFunction object | ||
options = varargin2options(varargin, mfilename); | ||
[version_tag, options] = get_option(options, 'version_tag', 0); | ||
[verbosity, options] = get_option(options, 'verbosity', 0); | ||
check_unsupported_options(options); | ||
|
||
cfunc.cache_filename = filename; | ||
cfunc.cached_function = func; | ||
cfunc.version_tag = version_tag; | ||
cfunc.verbosity = verbosity; | ||
end | ||
|
||
function result=call(cfunc, varargin) | ||
% CALL Call the function or get the result from the cache | ||
args = varargin; | ||
[result, found] = cfunc.retrieve(args); | ||
if ~found | ||
result = funcall(cfunc.cached_function, args{:}); | ||
cfunc.store(args, result); | ||
end | ||
end | ||
|
||
function filename=get_filename(cfunc, basename) | ||
filename = [fullfile(cfunc.cache_filename, basename), '.mat']; | ||
end | ||
|
||
function store(cfunc, args, result) | ||
hashstr=cfunc.get_hashstr(args); | ||
filename = cfunc.get_filename(hashstr); | ||
makesavepath( filename ); | ||
save(filename, 'args', 'result'); | ||
end | ||
|
||
function [result, found, origargs] = retrieve(cfunc, args) | ||
hashstr=cfunc.get_hashstr(args); | ||
filename = cfunc.get_filename(hashstr); | ||
|
||
result = []; | ||
origargs = []; | ||
found = exist(filename, 'file'); | ||
if found | ||
if cfunc.verbosity>0 | ||
disp('Looking up response...'); | ||
end | ||
load(filename, 'result', 'args'); | ||
% todo: compare args and origargs | ||
origargs = args; | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.