Skip to content

Commit

Permalink
Nonlinear functions imported from ncclabcode
Browse files Browse the repository at this point in the history
  • Loading branch information
memming committed Jun 19, 2017
1 parent 40aaa90 commit c87eb52
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 0 deletions.
33 changes: 33 additions & 0 deletions matRegress/+nlfuns/logexp1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function [f,df,ddf] = logexp1(x)
% [f,df,ddf] = logexp1(x);
%
% Also known as the soft-rectifier
%
% Computes the function:
% f(x) = log(1+exp(x))
% and returns first and second derivatives

f = log(1+exp(x));

if nargout > 1
df = exp(x)./(1+exp(x));
end
if nargout > 2
ddf = exp(x)./(1+exp(x)).^2;
end

% Check for small values
if any(x(:)<-20)
iix = (x(:)<-20);
f(iix) = exp(x(iix));
df(iix) = f(iix);
ddf(iix) = f(iix);
end

% Check for large values
if any(x(:)>500)
iix = (x(:)>500);
f(iix) = x(iix);
df(iix) = 1;
ddf(iix) = 0;
end
19 changes: 19 additions & 0 deletions matRegress/+nlfuns/logexp2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function [f,df,ddf] = logexp2(x);
% [f,df,ddf] = logexp_pow(x);
%
% Implements the nonlinearity:
% f(x) = log(1+exp(x)).^pow;
% where pow=2,
% plus first and second derivatives

pow = 2;
f0 = log(1+exp(x));
f = f0.^pow;

if nargout > 1
df = pow*f0.^(pow-1).*exp(x)./(1+exp(x));
end
if nargout > 2
ddf = pow*f0.^(pow-1).*exp(x)./(1+exp(x)).^2 + ...
pow*(pow-1)*f0.^(pow-2).*(exp(x)./(1+exp(x))).^2;
end
18 changes: 18 additions & 0 deletions matRegress/+nlfuns/logexp3.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function [f,df,ddf] = logexp3(x);
% [f,df,ddf] = logexp3(x);
%
% Implements the nonlinearity:
% f(x) = log(1+exp(x)).^3;
% plus first and second derivatives


f0 = log(1+exp(x));
f = f0.^3;

if nargout > 1
df = 3*f0.^2.*exp(x)./(1+exp(x));
end
if nargout > 2
ddf = 6*f0.*(exp(x)./(1+exp(x))).^2 + ...
3*f0.^2.*exp(x)./(1+exp(x)).^2;
end
23 changes: 23 additions & 0 deletions matRegress/+nlfuns/logexp4.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function [f,df,ddf] = logexp4(x)
% [f,df,ddf] = logexp4(x);
%
% Implements the nonlinearity:
% f(x) = log(1+exp(x)).^4;
% plus first and second derivatives
%
% General formulas:
% f(x) = log(1+exp(x))^k
% f'(x) = k log(1+e^x)^(k-1) * e^x/(1+e^x);
% f"(x) = k(k-1) log(1+e^x)^(k-2) * (e^x/(1+e^x))^2
% + k log(1+e^x)^(k-1) * e^x/(1+e^x)^2

f0 = log(1+exp(x));
f = f0.^4;

if nargout > 1
df = 4*f0.^3.*exp(x)./(1+exp(x));
end
if nargout > 2
ddf = 12*f0.^2.*(exp(x)./(1+exp(x))).^2 + ...
4*f0.^3.*exp(x)./(1+exp(x)).^2;
end
23 changes: 23 additions & 0 deletions matRegress/+nlfuns/logexp5.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function [f,df,ddf] = logexp5(x);
% [f,df,ddf] = logexp4(x);
%
% Implements the nonlinearity:
% f(x) = log(1+exp(x)).^4;
% plus first and second derivatives
%
% General formulas:
% f(x) = log(1+exp(x))^k
% f'(x) = k log(1+e^x)^(k-1) * e^x/(1+e^x);
% f"(x) = k(k-1) log(1+e^x)^(k-2) * (e^x/(1+e^x))^2
% + k log(1+e^x)^(k-1) * e^x/(1+e^x)^2

f0 = log(1+exp(x));
f = f0.^5;

if nargout > 1
df = 5*f0.^4.*exp(x)./(1+exp(x));
end
if nargout > 2
ddf = 20*f0.^3.*(exp(x)./(1+exp(x))).^2 + ...
5*f0.^4.*exp(x)./(1+exp(x)).^2;
end
22 changes: 22 additions & 0 deletions matRegress/+nlfuns/logexp_pow.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function [f,df,ddf] = logexp_pow(x,pow);
% [f,df,ddf] = logexp_pow(x);
%
% Implements the nonlinearity:
% f(x) = log(1+exp(x)).^pow;
% plus first and second derivatives


f0 = log(1+exp(x));
f = f0.^pow;

if nargout > 1
df = pow*f0.^(pow-1).*exp(x)./(1+exp(x));
end
if nargout > 2
if pow == 1
ddf = pow*f0.^(pow-1).*exp(x)./(1+exp(x)).^2;
else
ddf = pow*f0.^(pow-1).*exp(x)./(1+exp(x)).^2 + ...
pow*(pow-1)*f0.^(pow-2).*(exp(x)./(1+exp(x))).^2;
end
end
11 changes: 11 additions & 0 deletions matRegress/+nlfuns/quadnl.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [f,df,ddf] = quadnl(x);
% [f,df,ddf] = quadnl(x);
%
% Implements the nonlinearity:
% f(x) = 1+x.^2;
% Where pow = 1;
% plus first and second derivatives

f = 1+x.^2;
df = 2.*x;
ddf = 2;
21 changes: 21 additions & 0 deletions matRegress/+nlfuns/scaledNlfun.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function [f, df, ddf] = scaledNlfun(x, fhandle, scale)
% [f, df, ddf] = scaledNlfun(x, fhandle, scale)
% Scale a nonlinear function so that its operating point is shifted.
%
% Evaluates: fhandle(x*scale) / scale
%
% For example, to make log(1+exp(x)) more linear, use scale of 1e3

if nargout > 2
[f, df, ddf] = fhandle(x * scale);
f = f / scale;
df = df;
ddf = ddf * scale;
elseif nargout > 1
[f, df] = fhandle(x * scale);
f = f / scale;
df = df;
else
[f] = fhandle(x * scale);
f = f / scale;
end
24 changes: 24 additions & 0 deletions matRegress/+nlfuns/threshLinear.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function [f, df, ddf] = threshLinear(x)
% Threshold linear, nonlinearity
% For numerical stability, the smallest value it returns is eps, not zero.
%
% /
% /
% /
% -----------'
%
% Consider using scaledNlfun with logexp1 instead (it's smoother)

f = zeros(size(x));
f(x > 0) = x(x > 0);
f(x <= 0) = eps;

if nargout > 1
df = zeros(size(x));
df(x > 0) = 1;
df(x == 0) = 0.2; % subgradient
end

if nargout > 2
ddf = zeros(size(x));
end

0 comments on commit c87eb52

Please sign in to comment.