function [t, measurement_overhead, measurement_details] = timeit(f, num_outputs)
%TIMEIT Measure time required to run function.
%   T = TIMEIT(F) measures the time (in seconds) required to run F, which is a
%   function handle.  TIMEIT calls F with either no output arguments or one
%   output argument depending on nargout(F).
%
%   T = TIMEIT(F,N) calls F with N output arguments.  N can be 0, 1, 2, 3, 4, or 5.
%
%   TIMEIT handles automatically the usual benchmarking procedures of "warming
%   up" F, figuring out how many times to repeat F in a timing loop, etc.
%   TIMEIT also compensates for the estimated time-measurement overhead
%   associated with tic/toc and with calling function handles.  TIMEIT returns
%   the median of several repeated measurements.
%
%   Examples
%   --------
%   How much time does it take to compute sum(A.' .* B, 1), where A is
%   12000-by-400 and B is 400-by-12000?
%
%       A = rand(12000, 400);
%       B = rand(400, 12000);
%       f = @() sum(A.' .* B, 1);
%       timeit(f)
%
%   How much time does it take to call svd with three output arguments?
%
%       X = [1 2; 3 4; 5 6; 7 8];
%       f = @() svd(X);
%       timeit(f, 3)
%
%   How much time does it take to dilate the text.png image with
%   a 25-by-25 all-ones structuring element? (This example uses Image Processing
%   Toolbox functions.)
%
%       bw = imread('text.png');
%       se = strel(ones(25, 25));
%       g = @() imdilate(bw, se);
%       timeit(g)

%   Steve Eddins
%   Copyright 2008-2010 The MathWorks, Inc.

if nargin < 2
    num_outputs = min(numOutputs(f), 1);
else
    if num_outputs > 5
        warning('MATLAB:timeit:tooManyOutputs', ...
            'Too many function output arguments specified. timeit will call your function with 5 output arguments.');
    end
end

t_rough = roughEstimate(f, num_outputs);

% Calculate the number of inner-loop repetitions so that the inner for-loop
% takes at least about 1ms to execute.
desired_inner_loop_time = 0.001;
num_inner_iterations = max(ceil(desired_inner_loop_time / t_rough), 1);

% Run the outer loop enough times to give a reasonable set of inputs to median.
num_outer_iterations = 11;

% If the estimated running time for the timing loops is too long,
% reduce the number of outer loop iterations.
estimated_running_time = num_outer_iterations * num_inner_iterations * t_rough;
long_time = 15;
min_outer_iterations = 3;
if estimated_running_time > long_time
    num_outer_iterations = ceil(long_time / (num_inner_iterations * t_rough));
    num_outer_iterations = max(num_outer_iterations, min_outer_iterations);
end

times = zeros(num_outer_iterations, 1);

for k = 1:num_outer_iterations
    % Coding note: An earlier version of this code constructed an "outputs" cell
    % array, which was used in comma-separated form for the left-hand side of
    % the call to f().  It turned out, though, that the comma-separated output
    % argument added significant measurement overhead.  Therefore, the cases
    % for different numbers of output arguments are hard-coded into the switch
    % statement below.
    switch num_outputs
        case 0
            tic();
            for p = 1:num_inner_iterations
                f();
            end
            times(k) = toc();

        case 1
            tic();
            for p = 1:num_inner_iterations
                output = f();
            end
            times(k) = toc();

        case 2
            tic();
            for p = 1:num_inner_iterations
                [output1, output2] = f();
            end
            times(k) = toc();

        case 3
            tic();
            for p = 1:num_inner_iterations
                [output1, output2, output3] = f();
            end
            times(k) = toc();

        case 4
            tic();
            for p = 1:num_inner_iterations
                [output1, output2, output3, output4] = f();
            end
            times(k) = toc();

        otherwise
            tic();
            for p = 1:num_inner_iterations
                [output1, output2, output3, output4, output5] = f();
            end
            times(k) = toc();
    end

end

t = median(times) / num_inner_iterations;

measurement_details.EmptyFunctionCallTime = emptyFunctionCallTime();
measurement_details.SimpleFunctionHandleCallTime = simpleFunctionHandleCallTime();
measurement_details.AnonymousFunctionHandleCallTime = anonFunctionHandleCallTime();
measurement_details.TicTocCallTime = tictocCallTime();
measurement_overhead = (tictocCallTime() / num_inner_iterations) + ...
    functionHandleCallOverhead(f);

t = max(t - measurement_overhead, 0);

if t < (5 * measurement_overhead)
    warning('MATLAB:timeit:HighOverhead', 'The measured time for F may be inaccurate because it is close to the estimated time-measurement overhead (%.1e seconds).  Try measuring something that takes longer.', measurement_overhead);
end

function t = roughEstimate(f, num_f_outputs)
%   Return rough estimate of time required for one execution of
%   f().  Basic warmups are done, but no fancy looping, medians,
%   etc.

% Warm up tic/toc.
tic();
elapsed = toc();
tic();
elapsed = toc();

% Call f() in a loop for at least a millisecond.
times = [];
time_threshold = 3;
iter_count = 0;
while sum(times) < 0.001
    iter_count = iter_count + 1;

    switch num_f_outputs
        case 0
            tic();
            f();
            times(end+1) = toc();

        case 1
            tic();
            output1 = f();
            times(end+1) = toc();

        case 2
            tic();
            [output1, output2] = f();
            times(end+1) = toc();

        case 3
            tic();
            [output1, output2, output3] = f();
            times(end+1) = toc();

        case 4
            tic();
            [output1, output2, output3, output4] = f();
            times(end+1) = toc();

        otherwise
            tic();
            [output1, output2, output3, output4, output5] = f();
            times(end+1) = toc();
    end

    if iter_count == 1
        if times > time_threshold
            % If the first call to f() takes more than time_threshold to run,
            % then just use the result from that call.  The assumption is that
            % first-time effects are negligible compared to the running time for
            % f().
            break;
        else
            % Discard first timing.
            times = [];
        end
    end
end

t = median(times);

function n = numOutputs(f)
%   Return the number of output arguments to be used when calling the function
%   handle f.
%   * If nargout(f) > 0, return 1.
%   * If nargout(f) == 0, return 0.
%   * If nargout(f) < 0, use try/catch to determine whether to call f with one
%     or zero output arguments.
%     Note: It is not documented (as of R2008b) that nargout can return -1.
%     However, it appears to do so for functions that use varargout and for
%     anonymous function handles.

n = nargout(f);
if n < 0
   try
      a = f();
      % If the line above doesn't throw an error, then it's OK to call f() with
      % one output argument.
      n = 1;

   catch %#ok<CTCH>
      % If we get here, assume it's because f() has zero output arguments.  In
      % recent versions of MATLAB we could catch the specific exception ID
      % MATLAB:maxlhs, but that would limit the use of timeit to MATLAB versions
      % since the introduction of MExceptions.
      n = 0;
   end
end

function t = tictocCallTime
% Return the estimated time required to call tic/toc.

persistent ttct
if ~isempty(ttct)
    t = ttct;
    return
end

% Warm up tic/toc.
temp = tic(); elapsed = toc();
temp = tic(); elapsed = toc();
temp = tic(); elapsed = toc();

num_repeats = 11;
times = zeros(1, num_repeats);

for k = 1:num_repeats
   times(k) = tictocTimeExperiment();
end

t = min(times);
ttct = t;

function t = tictocTimeExperiment
% Call tic/toc 100 times and return the average time required.

elapsed = 0;
% Call tic/toc 100 times.
tic(); elapsed = elapsed +  toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();
tic(); elapsed = elapsed + toc();

t = elapsed / 100;

function emptyFunction()

function t = functionHandleCallOverhead(f)
% Return the estimated overhead, in seconds, for calling a function handle
% compared to calling a normal function.

fcns = functions(f);
if strcmp(fcns.type, 'anonymous')
    t = anonFunctionHandleCallTime();
else
    t = simpleFunctionHandleCallTime();
end

t = max(t - emptyFunctionCallTime(), 0);

function t = simpleFunctionHandleCallTime
% Return the estimated time required to call a simple function handle to a
% function with an empty body.
%
% A simple function handle fh has the form @foo.

persistent sfhct
if ~isempty(sfhct)
    t = sfhct;
    return
end

num_repeats = 101;
% num_repeats chosen to take about 100 ms, assuming that
% timeFunctionHandleCall() takes about 1 ms.
times = zeros(1, num_repeats);

fh = @emptyFunction;

% Warm up fh().
fh();
fh();
fh();

for k = 1:num_repeats
   times(k) = functionHandleTimeExperiment(fh);
end

t = min(times);
sfhct = t;

function t = anonFunctionHandleCallTime
% Return the estimated time required to call an anonymous function handle that
% calls a function with an empty body.
%
% An anonymous function handle fh has the form @(arg_list) expression. For
% example:
%
%       fh = @(thetad) sin(thetad * pi / 180)

persistent afhct
if ~isempty(afhct)
    t = afhct;
    return
end

num_repeats = 101;
% num_repeats chosen to take about 100 ms, assuming that timeFunctionCall()
% takes about 1 ms.
times = zeros(1, num_repeats);

fh = @() emptyFunction();

% Warm up fh().
fh();
fh();
fh();

for k = 1:num_repeats
   times(k) = functionHandleTimeExperiment(fh);
end

t = min(times);
afhct = t;

function t = functionHandleTimeExperiment(fh)
% Call the function handle fh 2000 times and return the average time required.

% Record starting time.
tic();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();
fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh(); fh();

t = toc() / 2000;

function t = emptyFunctionCallTime()
% Return the estimated time required to call a function with an empty body.

persistent efct
if ~isempty(efct)
    t = efct;
    return
end

% Warm up emptyFunction.
emptyFunction();
emptyFunction();
emptyFunction();

num_repeats = 101;
% num_repeats chosen to take about 100 ms, assuming that timeFunctionCall()
% takes about 1 ms.
times = zeros(1, num_repeats);

for k = 1:num_repeats
   times(k) = emptyFunctionTimeExperiment();
end

t = min(times);
efct = t;

function t = emptyFunctionTimeExperiment()
% Call emptyFunction() 2000 times and return the average time required.

% Record starting time.
tic();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();
emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction(); emptyFunction();

t = toc() / 2000;

% Copyright (c) 2010, The MathWorks, Inc.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     * Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the distribution
%     * Neither the name of the The MathWorks, Inc. nor the names
%       of its contributors may be used to endorse or promote products derived
%       from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.


hydro = struct();

hydro = readWAMIT(hydro,'coer_comp.out',[]);
hydro = radiationIRF(hydro,10,[],[],[],[]);
hydro = radiationIRFSS(hydro,[],[]);
hydro = excitationIRF(hydro,10,[],[],[],[]);
writeBEMIOH5(hydro)
plotBEMIO(hydro)


function [MStitch, result] = RGBMain_Process(MStitch, W_box, H_box, bdown)

[MStitch, result] = Fun_StitchRGB(MStitch.im2, W_box, H_box, bdown, MStitch);


%CODEGENERATOR.FFINDREPLACE Find and replace all occurrences of string in a file.
%
%   CGen.ffindreplace(fName, oText, nText, varargin)
%   FNAME is the absolute or relative path to the file to replace the text in.
%   OTEXT is the text passage to replace.
%   NTEXT is the new text.
%
% Notes::
% The function opens and sweeps the text file FNAME. All occurrences of
% OTEXT are replaced by NTEXT.
%
% Authors::
%  Joern Malzahn, (joern.malzahn@tu-dortmund.de)
%
% See also CodeGenerator.finsertfront.

% Copyright (C) 1993-2012, by Peter I. Corke
% Copyright (C) 2012-2013, by Joern Malzahn
%
% This file is part of The Robotics Toolbox for Matlab (RTB).
%
% RTB 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.
%
% RTB 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 Leser General Public License
% along with RTB.  If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
%
% The code generation module emerged during the work on a project funded by
% the German Research Foundation (DFG, BE1569/7-1). The authors gratefully
% acknowledge the financial support.

function [ ] = ffindreplace(CGen, fName, oText, nText, varargin)

fid = fopen(fName,'r'); % open the file

% determine number of lines in the file
nLines = getNLines(fid);

% read all lines to cell
oldLines = cell(1,nLines);
newLines = {1,nLines};
for iLines = 1:nLines
    oldLines{iLines} = fgets(fid);
end

% close the file again
fclose(fid);

% replace all occurrences of oText by nText in each line
for iLines = 1:nLines
    newLines{iLines}= strrep(oldLines{iLines}, oText, nText);
end

% rewrite the file
fid = fopen(fName,'w');
for iLines = 1:nLines
   fprintf(fid,'%s',newLines{iLines});
end
fclose(fid);

end

function [nLines] = getNLines(fid)
nLines = 0;
while (~feof(fid))      % go through each line until eof is reached
    fgets(fid);
    nLines = nLines +1; % increment line counter
end
frewind(fid);           % set file indicator back to the beginning of the file
end


function options = bonminset(varargin)
%BONMINSET  Create or alter the options for Optimization with BONMIN
%
% options = bonminset('param1',value1,'param2',value2,...) creates an IPOPT
% options structure with the parameters 'param' set to their corresponding
% values in 'value'. Parameters not specified will be set to the IPOPT
% default.
%
% options = bonminset(oldopts,'param1',value1,...) creates a copy of the old
% options 'oldopts' and then fills in (or writes over) the parameters
% specified by 'param' and 'value'.
%
% options = bonminset() creates an options structure with all fields set to
% bonminset defaults.
%
% bonminset() prints a list of all possible fields and their function.
%
% See supplied BONMIN and IPOPT Documentation for further details of these options.

%   Copyright (C) 2011 Jonathan Currie (I2C2)

% Print out possible values of properties.
if((nargin == 0) && (nargout == 0))
    printfields();
    return
end

%Names and Defaults
Names = {'algorithm','var_lin','cons_lin','allowable_fraction_gap','allowable_gap','cutoff','cutoff_decr','enable_dynamic_nlp','iteration_limit',...
         'nlp_failure_behavior','node_comparison','num_cut_passes','num_cut_passes_at_root',...
         'number_before_trust','number_strong_branch','solution_limit','sos_constraints','tree_search_strategy',...
         'variable_selection','feasibility_pump_objective_norm','heuristic_RINS','heuristic_dive_MIP_fractional',...
         'heuristic_dive_MIP_vectorLength','heuristic_dive_fractional','heuristic_dive_vectorLength',...
         'heuristic_feasibility_pump','pump_for_minlp','candidate_sort_criterion','maxmin_crit_have_sol',...
         'maxmin_crit_no_sol','min_number_strong_branch','number_before_trust_list','number_look_ahead',...
         'number_strong_branch_root','setup_pseudo_frac','coeff_var_threshold','dynamic_def_cutoff_decr','first_perc_for_cutoff_decr',...
         'max_consecutive_infeasible','num_resolve_at_infeasibles',...
         'num_resolve_at_node','num_resolve_at_root','second_perc_for_cutoff_decr','milp_solver','milp_strategy','bb_log_interval',...
         'bb_log_level','lp_log_level','milp_log_level','nlp_log_level','nlp_log_at_root','oa_log_frequency','oa_log_level',...
         'max_consecutive_failures','max_random_point_radius','num_retry_unsolved_random_point','random_point_perturbation_interval',...
         'random_point_type','warm_start'}';
Defaults = {'B-BB',[],[],0,0,1e100,1e-5,'no',2^31-1,'stop','best-bound',1,20,8,20,2^31-1,'enable','probed-dive','osi-strong',1,'no',...
            'yes','no','no','no','no','no','best-ps-cost',0.1,0.7,0,0,0,2^31-1,0.5,0.1,'no',-0.02,0,0,0,0,-0.05,'Cbc_D','find_good_sol',...
            100,1,0,0,0,0,100,1,10,100000,0,1,'Jon','none'}';
%Get IPOPT Defaults if required
if(nargin == 2 && strcmpi(varargin{2},'noipopt'))
    varargin = varargin(1);
else
    iset = ipoptset('bonmin');
    Names = [Names;fieldnames(iset)];
    Defaults = [Defaults;struct2cell(iset)];
end

%Enter and check user args
try
    options = opticheckset(Names,Defaults,@checkfield,varargin{:});
catch ME
    throw(ME);
end

function checkfield(field,value)
%Check a field contains correct data type
switch lower(field)
    %Scalar double
    case {'first_perc_for_cutoff_decr','second_perc_for_cutoff_decr'}
        err = opticheckval.checkScalarDbl(value,field);
    %Scalar non negative double
    case {'min_number_strong_branch','number_look_ahead','number_strong_branch_root','coeff_var_threshold'}
        err = opticheckval.checkScalarNonNeg(value,field);
    %Scalar non zero double
    case {'oa_log_frequency','max_random_point_radius','random_point_perturbation_interval'}
        err = opticheckval.checkScalarGrtZ(value,field);
    %Scalar non negative integer
    case {'iteration_limit','num_cut_passes','num_cut_passes_at_root','number_before_trust','number_strong_branch',...
          'solution_limit','max_consecutive_infeasible','num_resolve_at_infeasibles','num_resolve_at_node',...
          'num_resolve_at_root','bb_log_interval','max_consecutive_failures','num_retry_unsolved_random_point'}
        err = opticheckval.checkScalarIntNonNeg(value,field);
    %Scalar double with bounds
    case 'cutoff_decr'
        err = opticheckval.checkScalarBoundLELE(value,field,-1e10,1e10);
    case {'allowable_fraction_gap','allowable_gap'}
        err = opticheckval.checkScalarBoundLELE(value,field,-1e20,1e20);
    case 'cutoff'
        err = opticheckval.checkScalarBoundLELE(value,field,-1e100,1e100);
    case {'maxmin_crit_have_sol','maxmin_crit_no_sol','setup_pseudo_frac'}
        err = opticheckval.checkScalarBoundLELE(value,field,0,1);
    case 'number_before_trust_list'
        err = opticheckval.checkScalarBoundLEL(value,field,-1,inf);
    %Scalar integer with bounds
    case 'feasibility_pump_objective_norm'
        err = opticheckval.checkScalarIntBoundLELE(value,field,1,2);
    case 'bb_log_level'
        err = opticheckval.checkScalarIntBoundLELE(value,field,0,5);
    case {'lp_log_level','milp_log_level'}
        err = opticheckval.checkScalarIntBoundLELE(value,field,0,4);
    case {'nlp_log_level','oa_log_level'}
        err = opticheckval.checkScalarIntBoundLELE(value,field,0,2);
    case 'nlp_log_at_root'
        err = opticheckval.checkScalarIntBoundLELE(value,field,0,12);
    %Vector integer 0/1
    case {'var_lin','cons_lin'}
        err = opticheckval.checkVector01(value,field);
    %Yes / No
    case {'heuristic_rins','heuristic_dive_mip_fractional','heuristic_dive_mip_vectorlength','heuristic_dive_fractional',...
          'heuristic_dive_vectorlength','heuristic_feasibility_pump','pump_for_minlp','dynamic_def_cutoff_decr'}
        err = opticheckval.checkYesNo(value,field);
    %Yes / No with Yes Crash
    case 'enable_dynamic_nlp'
        err = opticheckval.checkYesCrashNo(value,field);
    %Enable / Disable
    case 'sos_constraints'
        err = opticheckval.checkEnDis(value,field);
    %Misc String methods
    case 'algorithm'
        err = opticheckval.checkValidString(value,field,{'B-BB','B-OA','B-QG','B-Hyb','B-Ecp','B-iFP'});
    case 'nlp_failure_behavior'
        err = opticheckval.checkValidString(value,field,{'stop','fathom'});
    case 'node_comparison'
        err = opticheckval.checkValidString(value,field,{'best-bound','depth-first','breadth-first','dynamic','best-guess'});
    case 'tree_search_strategy'
        err = opticheckval.checkValidString(value,field,{'top-node','dive','probed-dive','dfs-dive','dfs-dive-dynamic'});
    case 'variable_selection'
        err = opticheckval.checkValidString(value,field,{'most-fractional','strong-branching','reliability-branching','qp-strong-branching',...
                                                         'lp-strong-branching','nlp-strong-branching','osi-simple','osi-strong','random'});
    case 'candidate_sort_criterion'
        err = opticheckval.checkValidString(value,field,{'best-ps-cost','worst-ps-cost','most-fractional','least-fractional'});
    case 'milp_solver'
        err = opticheckval.checkValidString(value,field,{'cbc_d','cplex'});
    case 'milp_strategy'
        err = opticheckval.checkValidString(value,field,{'find_good_sol','solve_to_optimality'});
    case 'random_point_type'
        err = opticheckval.checkValidString(value,field,{'jon','andreas','claudia'});
    case 'warm_start'
        err = opticheckval.checkValidString(value,field,{'none','fake_basis','optimum','interior_point'});

    %IPOPT or Unknown Parameter
    otherwise
        err = [];
        try
            %try for ipoptset arg
            iset = ipoptset(field,value);
        catch ME
            throw(ME);
        end
        if(isempty(iset))
            err = MException('OPTI:SetFieldError','Unrecognized parameter name ''%s''.', field);
        end
end
if(~isempty(err)), throw(err); end

function printfields()
%Print out fields with defaults
fprintf(' ----- BONMIN (MIP SOLVER) SETTINGS ----- \n');
fprintf('  BONMIN GENERAL SETTINGS:\n');
fprintf('                          algorithm: [ MIP Algorithm {''B-BB''}, ''B-OA'', ''B-QG'', ''B-Hyb'', ''B-Ecp'', ''B-iFP'' ] \n');
fprintf('                            var_lin: [ Decision Variable Linearity (0 - Nonlinear, 1 - Linear) {zeros(n,1)} ] \n');
fprintf('                           cons_lin: [ Constraint Linearity (0 - Nonlinear, 1 - Linear) {zeros(m,1)} ] \n');

fprintf('\n  BONMIN BRANCH & BOUND SETTINGS:\n');
fprintf('             allowable_fraction_gap: [ Value of Relative Gap when algorithm stops {0} ] \n');
fprintf('                      allowable_gap: [ Value of Absolute Gap when algorithm stops {0} ] \n');
fprintf('                             cutoff: [ The algorithm will only look for values better than cutoff {1e100} ] \n');
fprintf('                        cutoff_decr: [ Amount by which cutoff is decremented below a new best upper bound {1e-5} ] \n');
fprintf('                 enable_dynamic_nlp: [ Enable dynamic linear and quadratic rows addition in NLP ''yes'', {''no''} ] \n');
fprintf('                    iteration_limit: [ Cumulated maximum number of iterations in the algorithm to process continuous relaxations {2^31-1} ] \n');
fprintf('               nlp_failure_behavior: [ Action to take when an NLP is unsolved by IPOPT {''stop''}, ''fathom'' ] \n');
fprintf('                    node_comparison: [ Node selection strategy {''best-bound''}, ''depth-first'', ''breadth-first'', ''dynamic'', ''best-guess'' ] \n');
fprintf('                     num_cut_passes: [ Maximum number of cut passes at regular nodes {1} ] \n');
fprintf('             num_cut_passes_at_root: [ Maximum number of cut passes at root node {20} ] \n');
fprintf('                number_before_trust: [ Number of branches on a variable before its pseudo costs are to be believed in dynamic strong branching {8} ] \n');
fprintf('               number_strong_branch: [ Maximum number of variables considered for strong branching {20} ] \n');
fprintf('                     solution_limit: [ Abort after this many integer feasible solutions have been found (or 0 to deactivate) {2^31-1} ] \n');
fprintf('                    sos_constraints: [ Activate SOS Type 1 constraints {''enable''}, ''disable'' ] \n');
fprintf('               tree_search_strategy: [ Tree traversing strategy {''probed-dive''}, ''top-node'', ''dive'', ''dfs-dive'', ''dfs-dive-dynamic'' ] \n');
fprintf('                 variable_selection: [ Variable selection strategy {''osi-strong''}, ''most-fractional'', ''reliability-branching'', ''qp-strong-branching'', ''lp-strong-branching'', ''nlp-strong-branching'', ''osi-simple'', ''strong-branching'', ''random'' ] \n');

fprintf('\n  BONMIN MINLP HEURISTIC SETTINGS:\n');
fprintf('    feasibility_pump_objective_norm: [ Norm of feasibility pump objective norm {1}, 2 ] \n');
fprintf('                     heuristic_RINS: [ Use RINS heuristic {''no''}, ''yes'' ] \n');
fprintf('      heuristic_dive_MIP_fractional: [ Use Dive MIP fractional heuristic {''no''}, ''yes'' ] \n');
fprintf('    heuristic_dive_MIP_vectorLength: [ Use Dive MIP vectorLength heuristic {''no''}, ''yes'' ] \n');
fprintf('          heuristic_dive_fractional: [ Use Dive fractional heuristic ''no'', {''yes''} ] \n');
fprintf('        heuristic_dive_vectorLength: [ Use Dive vectorLength heuristic ''no'', {''yes''} ] \n');
fprintf('         heuristic_feasibility_pump: [ Use feasibility pump heuristic {''no''}, ''yes'' ] \n');
fprintf('                     pump_for_minlp: [ Use FP for MINLP {''no''}, ''yes'' ] \n');

fprintf('\n  BONMIN NON-CONVEX PROBLEM SETTINGS:\n');
fprintf('                coeff_var_threshold: [ Coefficient of variation threshold (for dynamic definition of cutoff_decr) {0.1} ] \n');
fprintf('            dynamic_def_cutoff_decr: [ Define cutoff_decr dynamically? {''no''}, ''yes'' ] \n');
fprintf('         first_perc_for_cutoff_decr: [ Percentage used when the coeff of variance is smaller than the threshold, to compute cutoff_decr dynamically {-0.02} ] \n');
fprintf('         max_consecutive_infeasible: [ Maximum number of consecutive infeasible subproblems before aborting a branch {0} ] \n');
fprintf('         num_resolve_at_infeasibles: [ Number of tries to resolve an infeasible node (other than the root) of the tree with different starting points {0} ] \n');
fprintf('                num_resolve_at_node: [ Number of tries to resolve a node (other than the root) of the tree with different starting points (like multi-start) {0} ] \n');
fprintf('                num_resolve_at_root: [ Number of tries to resolve the root node with different starting points {0} ] \n');
fprintf('        second_perc_for_cutoff_decr: [ Percentage used when the coeff of variance is greater than the threshold, to compute cutoff_decr dynamically {-0.05} ] \n');

fprintf('\n  BONMIN NLP SOLUTION ROBUSTNESS SETTINGS:\n');
fprintf('           max_consecutive_failures: [ Number of consecutive unsolved problems before aborting a branch of the tree {10} ] \n');
fprintf('            max_random_point_radius: [ Max value for coordinate of a random point {100000} ] \n');
fprintf('    num_retry_unsolved_random_point: [ Number of times that the algorithm will try to resolve an unsolved NLP with a random starting point (with new point) {0} ] \n');
fprintf(' random_point_perturbation_interval: [ Amount by which starting point is perturbed when choosing a random point {1} ] \n');
fprintf('                  random_point_type: [ Random starting point method {''Jon''}, ''Andreas'', ''Claudia'' ] \n');
fprintf('                         warm_start: [ Warm start method {''none''}, ''fake_basis'', ''optimum'', ''interior'' ] \n');

fprintf('\n  BONMIN STRONG BRANCHING SETTINGS:\n');
fprintf('           candidate_sort_criterion: [ Criterion used to choose candidates in strong-branching {''best-ps-cost''}, ''worst-ps-cost'', ''most-fractional'', ''least-fractional'' ] \n')
fprintf('               maxmin_crit_have_sol: [ Weight towards minimum of lower and upper branching estimates when a solution has been found {0.1} ] \n')
fprintf('                 maxmin_crit_no_sol: [ Weight towards minimum of lower and upper branching estimates when no solution has been found {0.7} ] \n')
fprintf('           min_number_strong_branch: [ Minimum number of variables for strong branching (overriding trust) {0} ] \n')
fprintf('           number_before_trust_list: [ Number of branches on a variable vbefore its pseudo costs are to be believed during setup of strong branching list {0} ] \n')
fprintf('                  number_look_ahead: [ Limit of look-ahead strong-branching trials {0} ] \n')
fprintf('          number_strong_branch_root: [ Maximum number of variables considered for strong branching at root node {2^31-1} ] \n')
fprintf('                  setup_pseudo_frac: [ Proportion of strong branching list that has to be taken from most integer infeasible list {0.5} ] \n')

fprintf('\n  BONMIN MILP SOLVER SETTINGS:\n');
fprintf('                        milp_solver: [ Subsolver to solve MILP problems in OA decompositions {''Cbc_D''}, ''Cplex'' (must be installed on your PC) ] \n');
fprintf('                      milp_strategy: [ MILP solving strategy {''find_good_sol''}, ''solve_to_optimality'' ] \n');

fprintf('\n  BONMIN OUPUT AND LOG LEVEL SETTINGS:\n');
fprintf('                    bb_log_interval: [ Interval at which node level output is printed (number of nodes) {100} ] \n');
fprintf('                       bb_log_level: [ Level of branch and bound log detail (0-5) {1} ] \n');
fprintf('                       lp_log_level: [ Level of LP solver log detail (0-4) {0} ] \n');
fprintf('                     milp_log_level: [ Level of MILP solver log detail (0-4) {0} ] \n');
fprintf('                      nlp_log_level: [ Level of NLP solver log detail (0-2) {0} ] \n');
fprintf('                    nlp_log_at_root: [ Level of NLP solver log detail at root node (0-12) {0} ] \n');
fprintf('                   oa_log_frequency: [ Frequency (in seconds) of OA log messages  {100} ] \n');
fprintf('                       oa_log_level: [ Level of OA decomposition log detail (0-2) {1} ] \n');

fprintf('\n ----- IPOPT (RELAXED SOLVER) SETTINGS ----- \n');
ipoptset('bonmin');


function tagS = general_series_module_tags_subset
%"general_series_module_tags_subset"
%   Return a subset of the tags specified in C.7.3.1 in PS3.3 of the 2006
%   DICOM specification required to identify different series'.
%
%   Tags are returned in a struct array with 3 fields:
%
%   Tag: String containing hex DICOM tag of a field.
%  Type: String describing type of field, with 5 options:
%         '1' Field must exist, data must exist and be valid.
%         '2' Field must exist, data can exist and be valid, or be NULL.
%         '3' Field is optional, if the field exists data can exist and be
%             valid or be NULL.
%         '1C' Field must exist under certain conditions and contain valid
%             data.
%         '2C' Field must exist under certain conditions and can contain
%             valid data or be NULL.
%Children: For sequences, a tagS with the same format as this struct array.
%
%AI 11/20/2020
%
%Usage:
%   tagS = general_series_module_tags_subset
%
% Copyright 2010, Joseph O. Deasy, on behalf of the CERR development team.
%
% This file is part of The Computational Environment for Radiotherapy Research (CERR).
%
% CERR development has been led by:  Aditya Apte, Divya Khullar, James Alaly, and Joseph O. Deasy.
%
% CERR has been financially supported by the US National Institutes of Health under multiple grants.
%
% CERR is distributed under the terms of the Lesser GNU Public License.
%
%     This version of CERR 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.
%
% CERR 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 General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with CERR.  If not, see <http://www.gnu.org/licenses/>.

tagC = {'00080060','0020000E'};

tagDecC = {524384,2097166};

fieldnameC = {'Modality','SeriesInstanceUID'};

typeC = {'1','1'};

childC = cell(1,length(tagC));

tagS = struct('tag',tagC,'fieldname',fieldnameC,'tagdec',tagDecC,'type',typeC,'children',childC);


function sbfmf_write_nframes(fp,version,nframes)

fseek(fp,4+length(version)+4+4,'bof');
fwrite(fp,nframes,'uint32');


function o2 = montage(obj, varargin)
% This function displays an atlas object on a standard slice montage
% Call with 'nosymmetric' or 'colors' ... to match colors with wedge plots
%
% :Usage:
% ::
%
%    montage(obj, [optional inputs])
%
% - takes all optional inputs to canlab_results_fmridisplay and addblobs method
%
% :Input:
%
%   **obj:**
%        a region object
%
% :Optional Inputs:  - see help canlab_results_fmridisplay
%
%   **o2***
%        An existing fmridisplay object, with no keyword strings
%
%   **symmetric** [default]
%       Mirror left/right blobs with same colors
%       See match_colors_left_right
%
%   **nosymmetric**
%       Standard color map, no L/R color-matching for symmetry
%       Call with 'nosymmetric' to match colors with wedge plots
%
%   **'noblobs':**
%        do not display blobs
%
%   **'nooutline':**
%        do not display blob outlines
%
%   **'addmontages':**
%        when entering existing fmridisplay obj, add new montages
%
%   **'noremove':**
%        do not remove current blobs when adding new ones
%
%   **'outlinecolor:**
%        followed by new outline color
%
%   **'splitcolor':**
%        followed by 4-cell new split colormap colors (help fmridisplay or edit code for defaults as example)
%
%   **'montagetype':**
%        'full' for full montages of axial and sagg slices.
%
%        'full hcp' for full montage, but with surfaces and volumes from
%        HCP data
%
%        'compact' [default] for single-figure parasagittal and axials slices.
%
%        'compact2': like 'compact', but fewer axial slices.
%
%        'multirow': followed by number of rows
%           e.g., o2 = canlab_results_fmridisplay([], 'multirow', 2);
%
%   **'noverbose':**
%        suppress verbose output, good for scripts/publish to html, etc.
%
%   **'overlay':**
%        specify anatomical image for montage (not surfaces), followed by
%        image name
%        e.g., o2 = canlab_results_fmridisplay([], 'overlay', 'icbm152_2009_symmetric_for_underlay.img')';
%
%         The default brain for overlays is based on Keuken et al. 2014
%         For legacy SPM8 single subject, enter as arguments:
%         'overlay', which('SPM8_colin27T1_seg.img')
%
% Other inputs to addblobs (fmridisplay method) are allowed, e.g., 'cmaprange', [-2 2], 'trans'
%
% See help fmridisplay and region/montage
% e.g., 'color', [1 0 0]
%
%
% Examples:
%
% o2 = canlab_results_fmridisplay([], 'noverbose');
% o2 = montage(r, o2);      % symmetric colors left/right
% o2 = removeblobs(o2);
% o2 = montage(r, o2, 'map');

% PROGRAMMERS' NOTES:
% Tor Wager, Feb 2018.
% This function simply invokes the region montage method.

r = atlas2region(obj);

o2 = montage(r, varargin{:});

end % function


function ui = mrViewAddOverlay(ui, mapNum);
%
% ui = mrViewAddOverlay([ui], [mapNum]);
%
% Initialize a new overlay for a mrViewer UI, by
% adding a new entry in the ui.overlays field and
% initializing a new overlay window (if the GUI is
% open).
%
%
% ras 07/05.
if ~exist('ui','var') | isempty(ui), ui = mrViewGet; end
if ishandle(ui), ui = get(ui, 'UserData'); end

% check that there are maps to use for the overlays:
% if not, load one:
if ~isfield(ui,'maps') | isempty(ui.maps)
    ui = mrViewLoad(ui,[],1);
end

% get index of new overlay
if ~isfield(ui,'overlays') | isempty(ui.overlays)
    o = 1;
else
    o = length(ui.overlays)+1;
end

if ~exist('mapNum','var') | isempty(mapNum)
    mapNum = min(o, length(ui.maps));
end

% preferred order of color maps according to overly #
prefOrder = {'hot' 'winter' 'greenred' 'cool' 'autumn' ...
             'jet' 'gray' 'red' 'green' 'blue'};

ui.overlays(o).hide = false; % logical indicator whether to hide overlay

ui.overlays(o).mapNum = mapNum;
for j = 1:3
    ui.overlays(o).thresholds(j).on = 0;
    ui.overlays(o).thresholds(j).mapNum = mapNum;
    ui.overlays(o).thresholds(j).min = 0;
    ui.overlays(o).thresholds(j).max = 1;
    ui.overlays(o).thresholds(j).autoScale = 0;
end
ui.overlays(o).cbar = cbarCreate(prefOrder{o});
ui.overlays(o).clim = 'auto';
ui.overlays(o).subVol = 1; % time / 4th-dimension index

% decide whether to dock based on 'dockFlag' preference variable
if ~ispref('VISTA', 'dockFlag'), setpref('VISTA', 'dockFlag', 0); end
dockFlag = getpref('VISTA', 'dockFlag');

if ishandle(ui.fig)
    ui = mrViewOverlayPanel(ui, dockFlag, o);
end

% Try to be predictive: initialize reasonable thresholds for some maps
if (strncmp(ui.maps(mapNum).dataUnits, '-log', 4) | ...
	strncmp(lower(ui.maps(mapNum).dataUnits), 't', 1)) & ...
        (ui.maps(mapNum).dataRange(1) < 2) & ...
        (ui.maps(mapNum).dataRange(2) > 2)
    % if the map appears to be a contrast map, initialize
	% the lower threshold to 2.0
    set(ui.overlays(o).threshCheck(1), 'Value', 1);
    mrvSliderSet(ui.overlays(o).threshMin(1), 'Value', 2);
    mrvSliderSet(ui.overlays(o).threshMin(1), 'Visible', 'on');
    mrvSliderSet(ui.overlays(o).threshMax(1), 'Visible', 'on');
    set(ui.overlays(o).threshMap(1), 'Visible', 'on');
    ui.overlays(o).thresholds(1).min = 2;
    ui.overlays(o).thresholds(1).on = 1;

elseif strncmp(ui.maps(mapNum).name, 'Coherence', 9)
	% Coherence map: initial lower thresh is 0.20
    set(ui.overlays(o).threshCheck(1), 'Value', 1);
    mrvSliderSet(ui.overlays(o).threshMin(1), 'Value', .2);
    mrvSliderSet(ui.overlays(o).threshMin(1), 'Visible', 'on');
    mrvSliderSet(ui.overlays(o).threshMax(1), 'Visible', 'on');
    set(ui.overlays(o).threshMap(1), 'Visible', 'on');
    ui.overlays(o).thresholds(1).min = .2;
    ui.overlays(o).thresholds(1).on = 1;

elseif strncmp(ui.maps(mapNum).name, 'meanMap', 7)
	% mean map: one 'click' up on the slider
	lo = ui.maps(mapNum).dataRange(1);
	hi = ui.maps(mapNum).dataRange(2);
	thresh = lo + .1 * (hi - lo);

    set(ui.overlays(o).threshCheck(1), 'Value', 1);
    mrvSliderSet(ui.overlays(o).threshMin(1), 'Value', thresh);
    mrvSliderSet(ui.overlays(o).threshMin(1), 'Visible', 'on');
    mrvSliderSet(ui.overlays(o).threshMax(1), 'Visible', 'on');
    set(ui.overlays(o).threshMap(1), 'Visible', 'on');
    ui.overlays(o).thresholds(1).min = thresh;
    ui.overlays(o).thresholds(1).on = 1;

end

ui = mrViewSetOverlay(ui, ui.panels.overlays(o));
% mrViewRefresh(ui);

return