function D=genmean(a,b,s,w)

% D=GENMEAN(A,B,S,W)    
%       Generalized mean of at least two elements.
%       S is the index of optimism. W is a vector of weight 
%       factors for weighted mean. If W is empty, equal weights are 
%       assumed. When A and B are both defined, W has two elements.
%
%       If A and B are both row or column vectors, then D is also a row
%       or column vector. If A is a column vector and B a row vector, 
%       then a matrix D is returned, representing the Cartesian product
%       A x B.
%
%       GENMEAN(A,S,W) calculates the generalized mean of the rows of A.
%       In this case, S may also be a vector.
%
%       See also GENCRIT, GENDIST, DUALMEAN.

% 
% (c)   Uzay Kaymak (1994)

if nargin > 3,        % B is also supplied
  if isempty(w), 
    w=[1;1];
  end;  
  am=size(a,1); bn=size(b,2);
  if ( am > 1 & bn > 1),
    a=a*ones(1,bn); b=ones(am,1)*b;
  end;  
  if s > 100,         % if S is too large, calculate maximum
    D = max(a,b);
  elseif s < -100,    % if S is too small, calculate minimum
    D = min(a,b);
  elseif s==0,        % if S = 0, calculate geometric mean
      D = (a.^w(1)).*(b.^w(2));
      D = D.^(1/sum(w));
  else  
    D = (((w(1)*(a.^s))+(w(2)*(b.^s)))/sum(w)).^(1/s);
  end;  
else                  % parameter B is not supplied 
  if isempty(s),      % assign correct weights to W
    w = ones(size(a,1),1);
  else
    w = s(:);         % make sure w is a column vector
  end;
  s = b;
  for j = length(s) : -1 : 1,
    if s(j) > 100,       % if S is too large, calculate maximum
      D(j,:) = max(a);
    elseif s(j) < -100,  % if S is too small, calculate minimum
      D(j,:) = min(a);
    elseif s(j)==0,      % if S = 0, calculate geometric mean
      D(j,:) = prod([ones(size(a(1,:)));a.^(w*ones(size(a(1,:))))]);
      D(j,:) = D(j,:).^(1/sum(w));
    else  
      D(j,:) = ((w'*(a.^s(j)))/sum(w)).^(1/s(j));
    end;  
  end;    
end;  

