Skip to content

Commit

Permalink
Merge commit '2be67d42646a9d5c790ef2397cd2570feea0cbb9'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezander committed Sep 27, 2017
2 parents b2c1028 + c50578f commit 487bf1b
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 27 deletions.
5 changes: 5 additions & 0 deletions gpc/gpcbasis_norm.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
m = size(I,2);
check_boolean(length(syschars)==1 || length(syschars)==m, 'length of polynomial system must be one or match the size of the multiindices', mfilename);

if isequal(syschars, lower(syschars))
norm_I = ones(size(I,1), 1);
return;
end

if length(syschars)==1
N = max(max(I));
nrm = polysys_sqnorm(syschars, 0:N);
Expand Down
2 changes: 1 addition & 1 deletion mathutil/chopabs.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
% A=chopabs( A );
% disp(A);
%
% See also ROUND, CEIL, FLOOR
% See also CHOPCOMPL, ROUND, CEIL, FLOOR

% Elmar Zander
% Copyright 2006, Institute of Scientific Computing, TU Braunschweig.
Expand Down
42 changes: 42 additions & 0 deletions mathutil/chopcompl.m
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));
59 changes: 50 additions & 9 deletions objects/util/CachedFunction.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
classdef CachedFunction
classdef CachedFunction < handle
properties
result_cache@containers.Map
cached_function
cache_filename
verbosity = 0;
version_tag = 0;

last_save_time = -inf;
%always_save = true;
always_save = false;
unsaved_changes = false;
end


methods(Static)
function hashstr=get_hashstr(data)
function hashstr=get_hashstr(args)
% GET_HASHSTR Generate unique hash value from data
digest = java.security.MessageDigest.getInstance('SHA1');
data = typecast(data, 'uint8');
digest.update(data)
for i=1:length(args)
data = typecast(args{i}, 'uint8');
digest.update(data)
end
hash = typecast(digest.digest(), 'uint8');
hashstr = sprintf('%.2X', hash);
end
Expand All @@ -38,7 +45,15 @@

function save_cache_file(filename, cache, version_tag, verbosity)
% SAVE_CACHE_FILE Save the cache to a file
save(filename, 'cache', 'version_tag');
if verbosity>=2
strvarexpand('Saving cache file "$filename$" ($datestr(now)$');
end
t=tic;
save(filename, 'cache', 'version_tag', '-v7.3');
dt=toc(t);
if verbosity>=2 || (verbosity > 0 && dt>1)
strvarexpand('Time for writing to cache file "$filename$": $dt$');
end
end
end

Expand All @@ -52,26 +67,52 @@ function save_cache_file(filename, cache, version_tag, verbosity)

cfunc.cache_filename = filename;
cfunc.result_cache = cfunc.load_cache_file(filename, version_tag, cfunc.verbosity);
cfunc.unsaved_changes = false;
cfunc.last_save_time = now();

cfunc.cached_function = func;
cfunc.version_tag = version_tag;
cfunc.verbosity = verbosity;
end

function delete(cfunc)
% DELETE Class destructor, should save the file, if not done already.
if cfunc.unsaved_changes
cfunc.save_cache_file(cfunc.cache_filename, ...
cfunc.result_cache, cfunc.version_tag, cfunc.verbosity);
cfunc.unsaved_changes = false;
end
end

function result=call(cfunc, varargin)
% CALL Call the function or get the result from the cache
args = varargin;
[result, found] = model.cached_function.retrieve(args);
[result, found] = cfunc.retrieve(args);
if ~found
result = funcall(cfunc.cached_function, args{:});
model.cached_function.store(args, result);
cfunc.store(args, result);
end
end

function do_save(cfunc)
cfunc.save_cache_file(cfunc.cache_filename, ...
cfunc.result_cache, cfunc.version_tag, cfunc.verbosity);
cfunc.last_save_time = now();
cfunc.unsaved_changes = false;
end

function store(cfunc, args, result)
hashstr=cfunc.get_hashstr(args);
cfunc.result_cache(hashstr) = {result, args};
cfunc.save_cache_file(cfunc.cache_filename, ...
cfunc.result_cache, cfunc.version_tag, cfunc.verbosity);

max_dt_in_minutes = 0.5;
max_dt = (max_dt_in_minutes * 60) / (60*60*24);
save_now = cfunc.always_save || cfunc.last_save_time<now - max_dt;
if save_now
cfunc.do_save();
else
cfunc.unsaved_changes = true;
end
end

function [result, found, origargs] = retrieve(cfunc, args)
Expand Down
76 changes: 76 additions & 0 deletions objects/util/CachedFunctionDir.m
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
8 changes: 7 additions & 1 deletion plotting/legend_add.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ function legend_add( text )
h = legend();
text_strings = get(h, 'String');

legend( [text_strings, {text}] );
if isversion('9.2.0') % R2017a
options = {'AutoUpdate', 'off'};
else
options = {};
end

legend( [text_strings, {text}], options{:} );
5 changes: 5 additions & 0 deletions plotting/multiplot_init.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
% the sizes and placements of the subplots, there are the additional
% options 'title_dist', 'title_height_diff', and 'title_ypos_diff'. To
% understand their effect, please try them out.
% separate_figs: {false}, true
% If set to true, the plots won't go into subplots, but into separate
% figures. This is useful, when you want to save your figures to a
% files, because you can just leave you MULTIPLOT_INIT and MULTIPLOT
% statements where they are and just flip this option.
%
% Example (<a href="matlab:run_example multiplot_init">run</a>)
% multiplot_init(3,2, 'title', 'Sin functions', 'title_dist', 0.02)
Expand Down
33 changes: 24 additions & 9 deletions util/disp_func.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function str=disp_func( func )
function str=disp_func( func, varargin )
% DISP_FUNC Convert function handle to string description.
% STR=DISP_FUNC( FUNC ) returns the string representation of FUNC.
% Partially supplied arguments are put at the appropriate places.
Expand Down Expand Up @@ -26,11 +26,16 @@

check_num_args(nargin, 1, inf, mfilename);

options = varargin2options(varargin, mfilename);
[indent,options]=get_option(options, 'indent', false);
%[separgs,options]=get_option(options,'separgs', false);
check_unsupported_options(options);

if isempty(func)
str='<none>';
else
[handle,args]=collect_args( func );
str=sprintf('%s(%s)', handle2str( handle ), args2str(args));
str=sprintf('%s(%s)', handle2str( handle ), args2str(args, indent));
end

if nargout==0
Expand All @@ -46,7 +51,7 @@
elseif ischar(handle)
s=handle;
else
error( 'unknown' );
error( 'sglib:unknown_func_type', 'Unknown function type' );
end

function [handle,args]=collect_args( func )
Expand All @@ -73,7 +78,7 @@
out=varargin;


function s=args2str( args )
function s=args2str( args, indent )
% ARGS2STR Convert arguments to string representation
last={};

Expand All @@ -87,26 +92,36 @@
args(end)=[];
end

if indent
sep = sprintf(',\n ');
else
sep = ', ';
end

s='';
for i=1:length(args)
if is_pos_arg(args{i})
s=[s strvarexpand( ', <arg$args{i}{1}$>' )]; %#ok<AGROW>
s=[s sep strvarexpand( '<arg$args{i}{1}$>', 'quotes', true )]; %#ok<AGROW>
else
s=[s strvarexpand( ', $args{i}$' )]; %#ok<AGROW>
s=[s sep strvarexpand( '$args{i}$', 'quotes', true )]; %#ok<AGROW>
end
end

% If there are trailing arguments, put an ellipses in between
if ~isempty( last )
s=[s ', ...'];
for i=1:length(last)
s=[s strvarexpand( ', $last{i}$' )]; %#ok<AGROW>
s=[s sep strvarexpand( '$last{i}$', 'quotes', true )]; %#ok<AGROW>
end
end

% Cut the ', ' from the beginning of the string
if length(s)>=2
s=s(3:end);
if length(s)>=length(sep)
if indent % only cut the comma, not the whole separator
s=s(2:end);
else
s=s(length(sep)+1:end);
end
end


Expand Down
Loading

0 comments on commit 487bf1b

Please sign in to comment.