% Test that fancy routine matches the behavior of the obvious Matlab routine % (when numerics aren't a problem) over a wide range of weird ND-array sizes % (This is really a test of ndhelper.m) rand('state', 0); ref_fn = @(A, B) log(bsxfun(@plus, exp(A), exp(B))); sizes = {[1 3], [5 1], [10 3], [0], [0 9], [3 0], [3 1 4], [1 2 3], [1 1 1 4], [1 1 0 5]}; for ii = 1:numel(sizes) sz = sizes{ii}; A = rand(sz); B = rand(sz); testclose(logplusexp(A, B), ref_fn(A, B)); for dim = 1:length(sz) sz2 = sz; sz2(dim) = 1; B = rand(sz2); if ~testclose(logplusexp(A, B), ref_fn(A, B)); sz keyboard end end end % Test that answers are ok when numerics are horrible by comparing to LOGSUMEXP A = [-Inf -Inf -3000 -3000 -Inf -600 -599 -10 5 1000 999 Inf Inf 1000 Inf NaN 5]; B = [-Inf -3000 -3000 -Inf -600 -599 -10 5 1000 999 Inf Inf 1000 Inf NaN 5 2]; testclose(logplusexp(A, B), logsumexp([A; B], 1)); fprintf('A warning (but not failure) on the previous line about NaN''s was expected.\n'); test_exit