function [A,bv] = pivot(A,bv)
%
% This is for the simplex method.  You provide a matrix A and a vector
% that corresponds to the basic variables.
% The program returns the new matrix A and the new basic variables
%
% This file was written by Mary Pugh, mpugh@math.toronto.edu, in
% February 2002.  Please do not use this without her consent and 
% without attributing her.

a = input('what is the incoming variable?  ','s');
n = length(a);
b = a(2:n);
% find the index of the incoming variable
[m,n] = size(A);
for j=1:n
int2str(j);
  if int2str(j) == b
    j_in = j;
  end
end

a = input('what is the outgoing variable?  ','s');
n = length(a);
b = a(2:n);
n = length(bv);
% find the index of the outgoing variable
for i=1:n
  if int2str(bv(i)) ==  b
    i_out = i;  
  end
end
    
[m,n] = size(A);
if A(i_out,j_in) == 0
  disp('you cannot pivot with your choice of incoming and outgoing variables');
end

for i=1:i_out-1
  A(i,:) = A(i,:) - A(i_out,:)*A(i,j_in)/A(i_out,j_in); 
end
for i=i_out+1:m
  A(i,:) = A(i,:) - A(i_out,:)*A(i,j_in)/A(i_out,j_in); 
end
A(i_out,:) = A(i_out,:)/A(i_out,j_in);

% change the label corresponding to the i-out row to the j-in variable...
bv(i_out) = j_in;

LP_disp;
