function g = mixgrad_em(w, net, x, t , post, errstr, gradstr, varargin)
%mixgrad_em  Evaluate the error gradient for an expert or a gating network when training a mixture of experts (or gated experts) model with EM
%
%	G = MIXGRAD_EM(W, NET, X, T, POST, ERRSTR, GRADSTR) 
%       takes a network data structure NET (either one of the expert 
%       networks or the gating network) and its corresponding weight
%       vector, together with the matrix X of input vectors and the matrix 
%       T of target vectors, and returns the gradient of the error function 
%       evaluated at W. 
%       MIXGRAD_EM mainly serves as a helper function that passes on its
%       arguments to the function specified in the string GRADSTR which
%       can be:
%          'mixgrad_exp'  = calculates the error gradient for a GLM expert network.
%          'mixgrad_gate' = calculates the error gradient for a GLM gating network.
%          'mixmlpgrad_exp'  = calculates the error gradient for a MLP expert network.
%          'mixmlpgrad_gate' = calculates the error gradient for a MLP gating network.
%
%       The posterior probabilities POST are also passed on to GRADSTR. 
%
%       G = MIXGRAD_EM(W, NET, X, T, POST, ERRSTR, GRADSTR, SIGMA) if the 
%       conditional  density for the experts is an isotropic Gaussian (with 
%       one variance parameter for each expert), SIGMA contains these
%       variances. 
%
%       See also
%       mixerr_em, mixgrad_exp, mixgrad_gate, mixem, mixopt
%
%       Copyright: Perry Moerland

unpakstr = [net.type, 'unpak'];

net = feval(unpakstr, net, w);

g = feval(gradstr, net, x, t, post, varargin{:});
