function wolfram(r,s)
% WOLFRAM  Cellular automata, "A New Kind of Science", Chapter 3. 
% WOLFRAM(r,s) is s steps of rule r.
% Defaults are "Rule 30", r = 30, and s = 128.
% Wolfram LLC may have some intellectual property claims on this program.

shg
set(gcf,'doublebuffer','on', ...
    'pos',[12 64 1000 611])

if ~isequal(get(gcf,'tag'),'wolfram')
   shg
   clf reset
   uicontrol( ...
      'position',[470 40 35 32], ...
      'string','<','fontsize',14,'fontweight','bold', ...
      'callback','wolfram(''<'')');
   uicontrol( ...
      'position',[530 40 35 32], ...
      'string','>','fontsize',14,'fontweight','bold', ...
      'callback','wolfram(''>'')');
   set(gcf,'tag','wolfram');
end

if nargin == 0
   r = 30;
elseif isequal(r,'<')
   r = get(gcf,'userdata') - 1;
elseif isequal(r,'>')
   r = get(gcf,'userdata') + 1;
end
if r < 1, r = 1; end
if r > 256, r = 256; end
set(gcf,'userdata',r)

if nargin < 2, s = 128; end
A = zeros(s,2*s+1);
A(1,s+1) = 1;
d = fliplr(rem(floor(r*2.^(-7:0)),2));
j = 1:2*s+1;
jm = max(j-1,1);
jp = min(j+1,2*s+1);
for k = 1:s-1
   A(k+1,j) = d(4*A(k,jm) + 2*A(k,j) + A(k,jp) + 1);
end
spy(A)
title(num2str(r))
