Commit 068b33b9 authored by KSHITIJ GARG's avatar KSHITIJ GARG

adding lab01

parent 5d5d6867
# function to solve a given initial lights out problem matrix
# expects a single matrix as an argument
# solves Ax + b = c for x
# here, c is the vector describing final state of the matrix, consisting entirely of zeros
function ret = backward_solve(input)
if(nargin ~= 1),
usage("The function backward_solve expects only one argument");
end;
b = mattovec(input);
sz = size(input);
m = sz(1);
n = sz(2);
# constructing matrix A, where each entry aij contains whether the cell i is inverted or not by pressing the cell j
A = zeros(m*n, m*n);
for x= 1:m*n,
for y= 1:m*n,
# pressing the button flips its own configuration
if x == y,
A(x, y) = 1;
end;
# pressing the button flips the configuration of the button just before it, given it exists
if mod(x, n) ~= 1 && x - y == 1,
A(x, y) = 1;
end;
# pressing the button flips the configuration of the button just after it, given it exists
if mod(x, n) ~= 0 && y - x == 1,
A(x, y) = 1;
end;
# pressing the button flips the configuration of the button just above it, given it exists
if x > n && x - y == n,
A(x, y) = 1;
end;
# pressing the button flips the configuration of the button just below it, given it exists
if x < m*n - n + 1 && y - x == n,
A(x, y) = 1;
end;
end;
end;
de = det(A);
de = floor(de);
if mod(de, 2) == 0,
de = de + 1;
end;
# multiplying by an odd number does not change the value of Ax modulo 2
A = floor(A);
# the process requires taking an inverse, in some cases a psuedo inverse is taken, which might not
# have integral values, thus taking the floor
b = b*de;
# the determinant value of inverse is reciprocal of the determinant value of given matrix
x = (A)\b;
x = mod(x + 0.01, 2);
# according to our observations, octave represents numbers very slightly less than two to two.
# in a few testcases that we tried, the mod function did the right thing, yet the display was misleading
x = floor(x);
# finally representing the strategy in matrix form
ret = vectomat(x,m,n);
endfunction
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment