public static NuGenMatrix ScaleMultiply(decimal scalar, NuGenMatrix M) { decimal[,] A = M.Data; int ro = A.GetLength(0); int co = A.GetLength(1); decimal[,] B = new decimal[ro, co]; for (int p = 0; p < ro; p++) { for (int q = 0; q < co; q++) { B[p, q] = scalar * A[p, q]; } } return(new NuGenMatrix(B)); }
public static NuGenMatrix RowSwitch(NuGenMatrix M, int i, int j) { NuGenMatrix MS = NuGenMatrix.DeepCopy(M); decimal[,] a = MS.Data; int ro = a.GetLength(0); int co = a.GetLength(1); decimal temp = 0; for (int q = 0; q < co; q++) { temp = a[i, q]; a[i, q] = a[j, q]; a[j, q] = temp; } return(new NuGenMatrix(a)); }
public static NuGenMatrix DeepCopy(NuGenMatrix M) { return ScaleMultiply(1, M); }
public static NuGenMatrix ScaleMultiply(decimal scalar, NuGenMatrix M) { decimal[,] A = M.Data; int ro = A.GetLength(0); int co = A.GetLength(1); decimal[,] B = new decimal[ro,co]; for(int p=0;p<ro;p++) { for(int q=0;q<co;q++) { B[p,q]= scalar*A[p,q]; } } return(new NuGenMatrix(B)); }
public static NuGenMatrix RowSwitch (NuGenMatrix M ,int i , int j) { NuGenMatrix MS = NuGenMatrix.DeepCopy(M); decimal[,] a = MS.Data; int ro = a.GetLength(0); int co = a.GetLength(1); decimal temp =0; for (int q=0;q<co;q++) { temp=a[i,q]; a[i,q]=a[j,q]; a[j,q]=temp; } return(new NuGenMatrix(a)); }
/* public void display() { int r1 = this.Data.GetLength(0);int c1 = this.Data.GetLength(1); for (int i=0;i<r1;i++) { for (int j=0;j<c1;j++) { Console.Write(this.Data[i,j].ToString("N2")+" " ); } Console.WriteLine(); } Console.WriteLine(); } */ public static NuGenMatrix INV (NuGenMatrix M ) { decimal[,] a = M.Data; int ro = a.GetLength(0); int co = a.GetLength(1); if (ro!=co) { throw new System.ArgumentException("Cannot find inverse for an non square matrix!"); } int q; decimal[,] b = new decimal[ro,co]; decimal[,] I = Identity(ro).Data; for(int p=0;p<ro;p++){for(q=0;q<co;q++){b[p,q]=a[p,q];}} int i;decimal det=1; if (a[0,0]==0) { i=1; while (i<ro) { if (a[i,0]!=0) { NuGenMatrix.interrow(a,0,i); NuGenMatrix.interrow(I,0,i); det *= -1; break; } i++; } } det*= a[0,0]; NuGenMatrix.rowdiv(I,0,a[0,0]); NuGenMatrix.rowdiv(a,0,a[0,0]); for (int p=1;p<ro;p++) { q=0; while(q<p) { NuGenMatrix.rowsub(I,p,q,a[p,q]); NuGenMatrix.rowsub(a,p,q,a[p,q]); q++; } if(a[p,p]!=0) { det*=a[p,p]; NuGenMatrix.rowdiv (I,p,a[p,p]); NuGenMatrix.rowdiv (a,p,a[p,p]); } if(a[p,p]==0) { for(int j=p+1;j<co;j++) { if(a[p,j]!=0) { throw new System.Exception("Unable to determine the Inverse!"); } } } } for (int p=ro-1;p>0;p--) { for(q=p-1;q>=0;q--) { NuGenMatrix.rowsub (I,q,p,a[q,p]); NuGenMatrix.rowsub (a,q,p,a[q,p]); } } for(int p=0;p<ro;p++) { for(q=0;q<co;q++) { a[p,q]=b[p,q]; } } return(new NuGenMatrix(I)); }
public static NuGenMatrix DeepCopy(NuGenMatrix M) { return(ScaleMultiply(1, M)); }
/* * * public void display() * { * int r1 = this.Data.GetLength(0);int c1 = this.Data.GetLength(1); * * for (int i=0;i<r1;i++) * { * * for (int j=0;j<c1;j++) * { * Console.Write(this.Data[i,j].ToString("N2")+" " ); * } * Console.WriteLine(); * } * Console.WriteLine(); * } */ public static NuGenMatrix INV(NuGenMatrix M) { decimal[,] a = M.Data; int ro = a.GetLength(0); int co = a.GetLength(1); if (ro != co) { throw new System.ArgumentException("Cannot find inverse for an non square matrix!"); } int q; decimal[,] b = new decimal[ro, co]; decimal[,] I = Identity(ro).Data; for (int p = 0; p < ro; p++) { for (q = 0; q < co; q++) { b[p, q] = a[p, q]; } } int i; decimal det = 1; if (a[0, 0] == 0) { i = 1; while (i < ro) { if (a[i, 0] != 0) { NuGenMatrix.interrow(a, 0, i); NuGenMatrix.interrow(I, 0, i); det *= -1; break; } i++; } } det *= a[0, 0]; NuGenMatrix.rowdiv(I, 0, a[0, 0]); NuGenMatrix.rowdiv(a, 0, a[0, 0]); for (int p = 1; p < ro; p++) { q = 0; while (q < p) { NuGenMatrix.rowsub(I, p, q, a[p, q]); NuGenMatrix.rowsub(a, p, q, a[p, q]); q++; } if (a[p, p] != 0) { det *= a[p, p]; NuGenMatrix.rowdiv(I, p, a[p, p]); NuGenMatrix.rowdiv(a, p, a[p, p]); } if (a[p, p] == 0) { for (int j = p + 1; j < co; j++) { if (a[p, j] != 0) { throw new System.Exception("Unable to determine the Inverse!"); } } } } for (int p = ro - 1; p > 0; p--) { for (q = p - 1; q >= 0; q--) { NuGenMatrix.rowsub(I, q, p, a[q, p]); NuGenMatrix.rowsub(a, q, p, a[q, p]); } } for (int p = 0; p < ro; p++) { for (q = 0; q < co; q++) { a[p, q] = b[p, q]; } } return(new NuGenMatrix(I)); }
public static NuGenMatrix operator/(decimal D, NuGenMatrix M) { return(ScaleMultiply(D, NuGenMatrix.INV(M))); }