You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: penalty_function.m
    path of file: ./repos/TensorReg/private
    
    the code of the file until where you have to start completion: function [pen,d1pen,d2pen,dpendlambda] = ...
    penalty_function(beta,lambda,pentype,penparam)
% PENALTY_FUNCTION The penalty value and derivatives of penalties
%   The outputs have same size as beta   
%
% INPUT:
%   beta - regression coefficients
%   lambda - penalty constant    
%   penname - 'enet'|'log'|'mcp'|'power'|'scad'
%   penargs - index parameter for penalty function penname; allowed range
%       enet [1,2] (1 by default), log (0,inf) (1 by default), mcp (0,inf) 
%       (1 by default), power (0,2] (1 by default), scad (2,inf) (3.7 by default)
%
% Output:
%   pen - penalty values
%   d1pen - first derivatives
%   d2pen - second derivatives
%   dpendlambda - second mixed derivatives
%
% COPYRIGHT: North Carolina State University
% AUTHOR: Hua Zhou (hua_zhou@ncsu.edu), Artin Armagan

% check arguments
if (lambda<0)
    error('penalty constant lambda should be nonnegative');

    
function [pen,d1pen,d2pen,dpendlambda] = ...
    penalty_function(beta,lambda,pentype,penparam)
% PENALTY_FUNCTION The penalty value and derivatives of penalties
%   The outputs have same size as beta   
%
% INPUT:
%   beta - regression coefficients
%   lambda - penalty constant    
%   penname - 'enet'|'log'|'mcp'|'power'|'scad'
%   penargs - index parameter for penalty function penname; allowed range
%       enet [1,2] (1 by default), log (0,inf) (1 by default), mcp (0,inf) 
%       (1 by default), power (0,2] (1 by default), scad (2,inf) (3.7 by default)
%
% Output:
%   pen - penalty values
%   d1pen - first derivatives
%   d2pen - second derivatives
%   dpendlambda - second mixed derivatives
%
% COPYRIGHT: North Carolina State University
% AUTHOR: Hua Zhou (hua_zhou@ncsu.edu), Artin Armagan

% check arguments
if (lambda<0)
    error('penalty constant lambda should be nonnegative');
end
pentype = upper(pentype);
if (strcmp(pentype,'ENET'))
    if (isempty(penparam))
        penparam = 1;   % lasso by default
    elseif (penparam<1 || penparam>2)
        error('index parameter for ENET penalty should be in [1,2]');
    end