i was tryng to write a js script to calculate the determinant of a matrix
to do so i created a function to give me a matrix of a specific dimension filled with a certain value.
as i was tryng to debug my unfunctional code i run into a paradox, wich you can experience at line 118.
while creating a 4×4 of zeors js creates a 4×4 whith every rows that starts with a 1.
i tried changing the nature of the variable, the name, and running the same piece of code in other parts of the script, i just wanted a 4×4 filled with zeros.
as you can see in the last line this doesn’t happen in any other part of the code.
can someone please explain what am i doing worng? thanks in advance and sorry for my poor english.
/* implement matrix visualizer
implement fast matrix creation
implement matrix multiplication
implement matrix inversion
convert a letter in a column vector with te one */
function matrix_visualizer(M){
n=M.length;
M_str='';
for(let i=0; i<n; i++){
m=M[i].length;
for(let j=0; j<m; j++){
M_str+=M[i][j].toString()+' ';
}
M_str+='<br> '; /* <br> è lo n degli output innerHTML */
}
document.getElementById("matrix").innerHTML=M_str;
return
} /* put a preatty matrix visualization */
function M_of_a(n,m,a){
a=parseInt(a);
M=[];
for(let i=0; i<n; i++){
M.push(Array(m).fill(a));
}
return M;
} /* creates an n x m matrix full of a elements */
function eye(n){
M=M_of_a(n,n,0);
for(let i=0; i<n; i++){
M[i][i]=1;
}
return M;
} /* creates an identy square matrix */
function matrix_product(A, B){
/* check if both the matrix have consistent dimension */
/* A */
let nA=A.length;
let mA=A[0].length;
for(let i=1; i<nA;i++){
if(A[i].length!=mA){
alert('matrix A not well written');
return [0];
}
}
/* B */
let nB=B.length;
let mB=B[0].length;
for(let i=1; i<nB;i++){
if(B[i].length!=mB){
alert('matrix B not well written');
return [0];
}
}
/* check if the matrix are compatible for product */
if(mA!=nB){
alert('matrix size non compatible for multiplication');
return [0];
}
/* do the actual product */
let C=M_of_a(nA,mB, 0);
for(let i=0; i<nA; i++){
for (let j=0; j<mB; j++){
for (let k=0; k<mA; k++){
C[i][j]=parseInt(C[i][j]);
C[i][j]+=parseInt(A[i][k])*parseInt(B[k][j]);
}
}
}
return C;
} /* product of the A and B matrix */
function matrix_det(M){
/* check if M is a consistent squared matrix */
console.log(M);
let det=0;
n=M.length;
m=M[0].length;
for (let i=1; i<n; i++){
if(M[i].length!=m){
alert('matrix not consistent');
console.log('matrix not consistent');
return 0;
}
}
if(n!=m){
alert('not squared matrix');
console.log('not squared matrix');
return 0;
}
/* easy determinats */
/* n=1 */
if(n==1){
det=M[0][0];
return det;
} else if(n==2){
det=(parseInt(M[0][0])*parseInt(M[1][1]))-(parseInt(M[1][0])*parseInt(M[1][0]));
return det;
} else{
for(let k=0; k<n; k++){
if(parseInt(M[0][k])!=0){
console.log(parseInt(M[0][k]));
/* this should optimize the process by a lot */
let Sub_matrix=M_of_a(n-1,n-1,0);
console.log(Sub_matrix);
console.log(M_of_a(n-1,n-1,0));
/* WHAT IS HAPPENING? */
/* buld A */
for(i=1; i<n; i++){
for(j=0; j<n; j++){
let jj=0;
if(j!=k){
Sub_matrix[i-1][jj]+=parseInt(M[i][j]);
jj++;
}
}
}
det+=parseInt(M[1][k])*matrix_det(Sub_matrix);
}
}
}
return det;
}
console.log(matrix_det(eye(5)));
let A=M_of_a(4,4,0);
console.log(A);
Giovanni Roncoroni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.