Hello,
I’ve written a little program that solves 3 equations with 3 unknowns using Cramer’s rule. I’m perplexed that the |= operator seems to increment one of its arguments. I’m seeing this in Opera and Chrome.
Also, I thought there was a |== operator that wouldn’t try to do type coercion, similar to the === operator. However, if I replace my |= operators with |== both Opera and Chrome choke, is |== only a legend.
Here’s my entire source code:
[CODE]
<html>
<head>
<title>Solve 3 Equations with 3 Unknowns using Cramer’s Rule</title>
<script>
/*
matrix[0][0]*x+matrix[1][0]*y+matrix[2][0]=A
matrix[0][1]*x+matrix[1][1]*y+matrix[2][1]=B
matrix[0][2]*x+matrix[1][2]*y+matrix[2][2]=B
*/
var inputMatrix3x4 = [];
inputMatrix3x4[0]=[];
inputMatrix3x4[1]=[];
inputMatrix3x4[2]=[];
inputMatrix3x4[3]=[];
var xMatrix = [];
xMatrix[0]=[];
xMatrix[1]=[];
xMatrix[2]=[];
var yMatrix = [];
yMatrix[0]=[];
yMatrix[1]=[];
yMatrix[2]=[];
var zMatrix = [];
zMatrix[0]=[];
zMatrix[1]=[];
zMatrix[2]=[];
var bottom = [];
bottom[0]=[];
bottom[1]=[];
bottom[2]=[];
var x;
var y;
var z;
alert(“hello”);
/*
for (var i=0;i<=3;i++) {
for (var j=0;j<=2;j++) {
if (i<3) {
inputMatrix3x4[i][j] = prompt(“Enter the matrix for i = ” + i + ” and j = ” + j + “.”,””);
}
else {
inputMatrix3x4[i][j] = prompt(“Enter the answer for row j = ” + j + “.”,””);
}
}
}
*/
inputMatrix3x4[0][0]=2;
inputMatrix3x4[1][0]=3;inputMatrix3x4[2][0]=1;inputMatrix3x4[3][0]=10;
inputMatrix3x4[0][1]=1;inputMatrix3x4[1][1]=-1;inputMatrix3x4[2][1]=1;inputMatrix3x4[3][1]=4;
inputMatrix3x4[0][2]=4;inputMatrix3x4[1][2]=-1;inputMatrix3x4[2][2]=-5;inputMatrix3x4[3][2]=-8;
bottom = replaceWithAnswers(3,inputMatrix3x4);
//alert3x3Matrix(bottom);
xMatrix=replaceWithAnswers(0,inputMatrix3x4);
//alert3x3Matrix(xMatrix);
yMatrix=replaceWithAnswers(1,inputMatrix3x4);
//alert3x3Matrix(yMatrix);
zMatrix=replaceWithAnswers(2,inputMatrix3x4);
//alert3x3Matrix(zMatrix);
x=get3x3Determinant(xMatrix)/get3x3Determinant(bottom);
y=get3x3Determinant(yMatrix)/get3x3Determinant(bottom);
z=get3x3Determinant(zMatrix)/get3x3Determinant(bottom);
alert(“x = ” + x);
alert(“y = ” + y);
alert(“z = ” + z);
function alert3x3Matrix(my3x3Matrix) {
for (var i=0;i<=2;i++) {
for (var j=0;j<=2;j++) {
alert(“matrix (” + i + “, ” + j + ” ) = ” + my3x3Matrix[i][j]);
}
}
}
function replaceWithAnswers(col,my3x4Matrix) {
var my3x3Matrix = [];
my3x3Matrix[0]=[];
my3x3Matrix[1]=[];
my3x3Matrix[2]=[];
for (var i=0;i<=2;i++) {
for (var j=0;j<=2;j++) {
if (i===col) {
//Replace it with the answer
my3x3Matrix[i][j] = my3x4Matrix[3][j];
}
else {
//Just add it to the 3x3matrix
my3x3Matrix[i][j] = my3x4Matrix[i][j];
}
}
}
return my3x3Matrix;
}
function get3x3Determinant(matrix) {
/*
matrix[0][0] matrix[1][0] matrix[2][0]
matrix[0][1] matrix[1][1] matrix[2][1]
matrix[0][2] matrix[1][2] matrix[2][2]
| + – + |
| – + – |
| + – + |
*/
document.write(“<table>”)
document.write(“<tr>”)
document.write(“<td>” + matrix[0][0] + “</td><td>” + matrix[1][0] + “</td><td>” + matrix[2][0] + “</td>”)
document.write(“</tr>”)
document.write(“<tr>”)
document.write(“<td>” + matrix[0][1] + “</td><td>” + matrix[1][1] + “</td><td>” + matrix[2][1] + “</td>”)
document.write(“</tr>”)
document.write(“<tr>”)
document.write(“<td>” + matrix[0][2] + “</td><td>” + matrix[1][2] + “</td><td>” + matrix[2][2] + “</td>”)
document.write(“</tr>”)
document.write(“</table>”)
//Return the determinant
return matrix[0][0]*get2x2Determinant(get2x2Matrix(0,0,matrix))
-matrix[1][0]*get2x2Determinant(get2x2Matrix(1,0,matrix))
+matrix[2][0]*get2x2Determinant(get2x2Matrix(2,0,matrix));
}
function get2x2Matrix(x,y,matrix3x3) {
//Get the 2×2 matrix by “covering up” the row and column
var ii=0; //x-axis for the 2×2 matrix you pass back
var matrix2x2 = []; //2×2 matrix you pass back
matrix2x2[0]=[];
matrix2x2[1]=[];
for (var i=0;i<=2;i++) {
document.write(“before i|=x, i = ” + i + “<br/>”);
if (i|=x) {
document.write(“after i|=x, i = ” + i + “<br/>”);
//You’re not in the column you’re covering up
var jj=0; //y-axis for the 2×2 matrix you pass back
for (var j=0;j<=2;j++) {
if( j |= y) {
//You’re not in the row you’re covering up, add this one to the 2×2 matrix
matrix2x2[ii][jj]=matrix3x3[i][j];
jj++; //Increment the y-axis counter for the 2×2 matrix
}
}
ii++; //Increment the y-axis counter for the 2×2 matrix
}
}
return matrix2x2; //Return the 2×2 matrix
}
function get2x2Determinant(matrix2x2) {
/*
matrix[0][0] matrix[1][0]
matrix[0][1] matrix[1][1]
*/
//Return the determinant;
return matrix2x2[0][0]*matrix2x2[1][1]-matrix2x2[0][1]*matrix2x2[1][0];
}
</script>
</head>
<body>
<h1>Solve 3 Equations with 3 Unknowns using Cramer’s Rule</h1>
</body>
</html>