function y = ammoniteifs(x,p1,p2,p3)
% AMMONITEIFS is a map of IFS ammonite map for a single step.
% y = ammoniteifs(x,p1,p2,p3)
% p1, p2, p3 and p4 are probabilities for selecting
% corresponding maps W1, W2, W3 and W4.
%
% defaults: p1=0.06124, p2=0.022236, p3=0.916524

% Copyright(C) 2004 Tohru Ikeguchi, All Rights Reserved

if nargin<1, x = randn(2,1); end

if nargin<2 | isempty(p1), p1=0.06124;  end
if nargin<3 | isempty(p2), p2=0.022236; end
if nargin<4 | isempty(p3), p3=0.916524; end

if p1+p2+p3 ~= 1, 
   p1+p2+p3
   error('sum of p is larger than 1');
end

if size(x,1) == 1 & size(x',1) == 1
  x(2) = randn;
end

if size(x,1) > 2 & size(x,2) >2
  size(x)
  error('Should not happen');
  return
end

pb = rand;

if pb<=p1
   y(1) = -0.289993.*x(1) - 0.001347.*x(2) + 0.593333;
   y(2) =  0.001986.*x(1) - 0.196662.*x(2) - 0.32;
elseif pb<=(p1+p2)
   y(1) = -0.073058.*x(1) - 0.024834.*x(2) + 0.793333;
   y(2) = -0.006353.*x(1) + 0.285589.*x(2) - 0.056667;
else
   y(1) =  0.939186.*x(1) - 0.218787.*x(2) - 0.046667;
   y(2) =  0.214337.*x(1) + 0.958685.*x(2) + 0.01;
end

return
