11 namespace MatrixLibrary
13 #region "Exception in the Library"
21 base(
"To do this operation, matrix can not be null"){}
26 base(
"Dimension of the two matrices not suitable for this operation !"){}
31 base(
"To do this operation, matrix must be a square matrix !"){}
36 base(
"Determinent of matrix equals zero, inverse can't be found !"){}
41 base(
"Dimension of matrix must be [3 , 1] to do this operation !"){}
46 base(
"Matrix is singular this operation cannot continue !"){}
61 private double[,] in_Mat;
63 #region "Class Constructor and Indexer"
64 public Matrix(
int noRows,
int noCols)
72 this.in_Mat =
new double[noRows, noCols];
82 this.in_Mat = (
double[,])Mat.Clone();
88 public double this[
int Row,
int Col]
90 get{
return this.in_Mat[Row, Col];}
91 set{this.in_Mat[Row,Col] = value;}
95 #region "Public Properties"
103 get{
return this.in_Mat.GetUpperBound(0)+1;}
104 set{this.in_Mat =
new double[value, this.in_Mat.GetUpperBound(0)];}
114 get{
return this.in_Mat.GetUpperBound(1)+1;}
115 set{this.in_Mat =
new double[this.in_Mat.GetUpperBound(0),value];}
123 get{
return this.in_Mat;}
127 #region "Find Rows and Columns in a Matrix"
128 private static void Find_R_C(
double[] Mat, out
int Row)
130 Row = Mat.GetUpperBound(0);
133 private static void Find_R_C(
double[,] Mat, out
int Row, out
int Col)
135 Row = Mat.GetUpperBound(0);
136 Col = Mat.GetUpperBound(1);
140 #region "Change 1D array ([n]) to/from 2D array [n,1]"
155 try{Find_R_C(Mat,out Rows);}
158 double[,] Sol =
new double[Rows+1,1];
160 for (
int i=0; i<=Rows;i++)
182 try{Find_R_C(Mat,out Rows, out Cols);}
187 double[] Sol =
new double[Rows+1];
189 for (
int i=0; i<=Rows;i++)
197 #region "Identity Matrix"
198 public static double[,]
Identity(
int n)
205 double[,] temp =
new double[n,n];
206 for (
int i=0; i<n;i++) temp[i,i] = 1;
211 #region "Add Matrices"
212 public static double[,]
Add(
double[,] Mat1,
double[,] Mat2)
230 Find_R_C(Mat1,out Rows1, out Cols1);
231 Find_R_C(Mat2,out Rows2, out Cols2);
235 throw new MatrixNullException();
238 if (Rows1 != Rows2 || Cols1 != Cols2)
throw new MatrixDimensionException();
240 sol =
new double[Rows1+1,Cols1+1];
242 for (i = 0;i <= Rows1; i++)
243 for (j = 0; j<= Cols1; j++)
245 sol[i, j] = Mat1[i, j] + Mat2[i, j];
260 {
return new Matrix(
Add(Mat1.in_Mat,Mat2.in_Mat));}
271 {
return new Matrix(
Add(Mat1.in_Mat,Mat2.in_Mat));}
274 #region "Subtract Matrices"
284 public static double[,]
Subtract(
double[,] Mat1,
double[,] Mat2)
294 Find_R_C(Mat1,out Rows1, out Cols1);
295 Find_R_C(Mat2,out Rows2, out Cols2);
304 sol =
new double[Rows1+1,Cols1+1];
306 for (i = 0;i <= Rows1; i++)
307 for (j = 0; j<= Cols1; j++)
309 sol[i, j] = Mat1[i, j] - Mat2[i, j];
338 #region "Multiply Matrices"
348 public static double[,]
Multiply(
double[,] Mat1,
double[,] Mat2)
357 Find_R_C(Mat1, out Rows1, out Cols1);
358 Find_R_C(Mat2, out Rows2, out Cols2);
366 sol =
new double[Rows1+1, Cols2+1];
368 for (
int i=0; i<=Rows1; i++)
369 for (
int j = 0; j<=Cols2; j++)
371 for (
int l = 0; l<=Cols1; l++)
373 MulAdd = MulAdd + Mat1[i, l] * Mat2[l, j];
432 #region "Determinant of a Matrix"
433 public static double Det(
double[,] Mat)
445 double save, ArrayK, tmpDet;
450 DArray = (
double[,])Mat.Clone();
451 Find_R_C(Mat , out Rows, out Cols);
453 catch{
throw new MatrixNullException();}
455 if (Rows != Cols)
throw new MatrixNotSquare();
460 for (k = 0; k <= S; k++)
462 if (DArray[k, k] == 0)
465 while ((j < S) && (DArray[k, j] == 0)) j = j + 1;
466 if (DArray[k, j] == 0)
return 0;
469 for (i = k; i <= S; i++)
472 DArray[i, j] = DArray[i, k];
478 ArrayK = DArray[k, k];
479 tmpDet = tmpDet * ArrayK;
483 for (i = k1; i <= S; i++)
485 for (j = k1; j <= S; j++)
486 DArray[i, j] = DArray[i, j] - DArray[i, k] * (DArray[k, j] / ArrayK);
502 {
return Det(Mat.in_Mat);}
505 #region "Inverse of a Matrix"
506 public static double[,]
Inverse(
double[,] Mat)
520 int LL, LLM, L1, L2, LC, LCA, LCB;
524 Find_R_C(Mat, out Rows, out Cols);
525 Mat1 = (
double[,])Mat.Clone();
527 catch{
throw new MatrixNullException();}
529 if (Rows != Cols)
throw new MatrixNotSquare();
530 if (
Det(Mat) == 0)
throw new MatrixDeterminentZero();
534 AI =
new double[LL+1, LL+1];
536 for (L2 = 0; L2 <= LL; L2++)
538 for (L1 = 0; L1 <= LL; L1++) AI[L1, L2] = 0;
542 for (LC = 0; LC <= LL; LC++)
544 if (Math.Abs(Mat1[LC, LC]) < 0.0000000001)
546 for (LCA = LC + 1; LCA <= LL; LCA++)
548 if (LCA == LC)
continue;
549 if (Math.Abs(Mat1[LC, LCA]) > 0.0000000001)
551 for (LCB = 0; LCB <= LL; LCB++)
553 Mat1[LCB, LC] = Mat1[LCB, LC] + Mat1[LCB, LCA];
554 AI[LCB, LC] = AI[LCB, LC] + AI[LCB, LCA];
560 AIN = 1 / Mat1[LC, LC];
561 for (LCA = 0; LCA <= LL; LCA++)
563 Mat1[LCA, LC] = AIN * Mat1[LCA, LC];
564 AI[LCA, LC] = AIN * AI[LCA, LC];
567 for (LCA = 0; LCA <= LL; LCA++)
569 if (LCA == LC)
continue;
571 for (LCB = 0; LCB <= LL; LCB++)
573 Mat1[LCB, LCA] = Mat1[LCB, LCA] - AF * Mat1[LCB, LC];
574 AI[LCB, LCA] = AI[LCB, LCA] - AF * AI[LCB, LC];
594 #region "Transpose of a Matrix"
595 public static double[,]
Transpose(
double[,] Mat)
604 int i, j, Rows, Cols;
606 try {Find_R_C(Mat, out Rows, out Cols);}
607 catch{
throw new MatrixNullException();}
609 Tr_Mat =
new double[Cols+1, Rows+1];
611 for (i = 0; i<=Rows;i++)
612 for (j = 0; j<= Cols;j++) Tr_Mat[j, i] = Mat[i, j];
627 #region "Singula Value Decomposition of a Matrix"
628 public static void SVD(
double[,] Mat_, out
double[,] S_, out
double[,] U_, out
double[,] V_)
649 try {Find_R_C(Mat_, out Rows, out Cols);}
650 catch{
throw new MatrixNullException();}
671 A =
new double[m+1,n+1];
673 for (
int row = 1; row<=Rows+1;row++)
674 for(
int col = 1;col<=Cols+1;col++)
675 {A[row,col] = Mat_[row-1,col-1];}
677 const int NMAX = 100;
678 v =
new double[NP+1, NP+1];
679 w =
new double[NP+1];
682 int flag, i, its, j, jj;
684 double[,] U_temp, S_temp, V_temp;
685 double anorm, c, f, g, h, s, scale, x, y, z;
686 double [] rv1 =
new double[NMAX];
701 for (k=i;k<=m;k++) scale += Math.Abs(A[k,i]);
710 g = -Sign(Math.Sqrt(s),f);
717 for (s=0,k=i;k<=m;k++) s += A[k,i]*A[k,j];
719 for (k=i;k<=m;k++) A[k,j] += f*A[k,i];
722 for (k=i;k<=m;k++) A[k,i] *= scale;
727 if (i <= m && i != n)
729 for (k=l;k<=n;k++) scale += Math.Abs(A[i,k]);
738 g = -Sign(Math.Sqrt(s),f);
741 for (k=l;k<=n;k++) rv1[k]=A[i,k]/h;
746 for (s=0.0,k=l;k<=n;k++) s += A[j,k]*A[i,k];
747 for (k=l;k<=n;k++) A[j,k] += s*rv1[k];
750 for (k=l;k<=n;k++) A[i,k] *= scale;
753 anorm=Math.Max(anorm,(Math.Abs(w[i])+Math.Abs(rv1[i])));
762 v[j,i]=(A[i,j]/A[i,l])/g;
765 for (s=0.0,k=l;k<=n;k++) s += A[i,k]*v[k,j];
766 for (k=l;k<=n;k++) v[k,j] += s*v[k,i];
769 for (j=l;j<=n;j++) v[i,j]=v[j,i]=0.0;
780 for (j=l;j<=n;j++) A[i,j]=0.0;
788 for (s=0.0,k=l;k<=m;k++) s += A[k,i]*A[k,j];
790 for (k=i;k<=m;k++) A[k,j] += f*A[k,i];
793 for (j=i;j<=m;j++) A[j,i] *= g;
797 for (j=i;j<=m;j++) A[j,i]=0.0;
803 for (its=1;its<=30;its++)
809 if (Math.Abs(rv1[l])+anorm == anorm)
814 if (Math.Abs(w[nm])+anorm == anorm)
break;
823 if (Math.Abs(f)+anorm != anorm)
847 for (j=1;j<=n;j++) v[j,k]=(-v[j,k]);
851 if (its == 30) Console.WriteLine(
"No convergence in 30 SVDCMP iterations");
857 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
859 f=((x-z)*(x+z)+h*((y/(f+Sign(g,f)))-h))/x;
876 for (jj=1;jj<=n;jj++)
893 for (jj=1;jj<=m;jj++)
907 S_temp =
new double[NP,NP];
908 V_temp =
new double[NP, NP];
909 U_temp =
new double[MP, NP];
911 for (i = 1; i<= NP; i++) S_temp[i - 1, i - 1] = w[i];
915 for (i = 1; i<=NP;i++)
916 for (j = 1; j<=NP;j++) V_temp[i - 1, j - 1] = v[i, j];
920 for (i = 1; i<= MP;i++)
921 for (j = 1; j<=NP;j++) U_temp[i - 1, j - 1] = A[i, j];
926 private static double SQR(
double a)
931 private static double Sign (
double a,
double b)
933 if (b >= 0.0) {
return Math.Abs(a);}
934 else {
return -Math.Abs(a);}
937 private static double PYTHAG(
double a,
double b)
943 if (absa > absb)
return absa*Math.Sqrt(1.0+SQR(absb/absa));
944 else return (absb == 0.0 ? 0.0 : absb*Math.Sqrt(1.0+SQR(absa/absb)));
964 SVD(Mat.in_Mat, out s, out u, out v);
971 #region "LU Decomposition of a matrix"
972 public static void LU(
double[,]Mat , out
double[,]L, out
double[,] U, out
double[,] P)
990 int i,j,k, Rows, Cols;
994 A = (
double[,])Mat.Clone();
995 Find_R_C(Mat , out Rows, out Cols);
997 catch{
throw new MatrixNullException();}
999 if (Rows != Cols)
throw new MatrixNotSquare();
1001 int IMAX = 0, N = Rows;
1002 double AAMAX, Sum, Dum, TINY = 1E-20;
1004 int[] INDX =
new int[N+1];
1005 double[] VV =
new double[N*10];
1008 for (i = 0; i<= N;i++)
1011 for (j = 0; j<= N;j++)
1012 if (Math.Abs(A[i, j]) > AAMAX) AAMAX = Math.Abs(A[i, j]);
1013 if (AAMAX == 0.0)
throw new MatrixSingularException();
1014 VV[i] = 1.0 / AAMAX;
1016 for (j = 0; j<= N; j++)
1020 for (i = 0; i<= (j - 1); i++)
1025 for (k = 0; k<= (i - 1); k++)
1026 Sum = Sum - A[i, k] * A[k, j];
1032 for (i = j; i<= N; i++)
1037 for (k = 0; k<= (j - 1); k++)
1038 Sum = Sum - A[i, k] * A[k, j];
1041 Dum = VV[i] * Math.Abs(Sum);
1050 for (k = 0; k<= N; k++)
1053 A[IMAX, k] = A[j, k];
1062 if (A[j, j] == 0.0) A[j, j] = TINY;
1063 Dum = 1.0 / A[j, j];
1064 for (i = j + 1; i<=N; i++)
1065 A[i, j] = A[i, j] * Dum;
1070 if (A[N, N] == 0.0) A[N, N] = TINY;
1073 double[,] l =
new double[N+1,N+1];
1074 double[,] u =
new double[N+1,N+1];
1076 for (i = 0; i<=N; i++)
1078 for (j = 0; j<=count; j++)
1080 if (i!=0) l[i,j] = A[i,j];
1081 if (i==j) l[i,j] = 1.0;
1082 u[N-i,N-j] = A[N-i,N-j];
1093 SwapRows(P,i,INDX[i]);
1097 private static void SwapRows(
double[,] Mat,
int Row,
int toRow)
1099 int N = Mat.GetUpperBound(0);
1100 double[,] dumArray =
new double[1,N+1];
1101 for (
int i = 0; i <= N; i++)
1103 dumArray[0, i] = Mat[Row, i];
1104 Mat[Row, i] = Mat[toRow, i];
1105 Mat[toRow, i] = dumArray[0, i];
1127 LU(Mat.in_Mat, out l, out u, out p);
1134 #region "Solve system of linear equations"
1135 public static double[,]
SolveLinear(
double[,]MatA ,
double[,] MatB)
1154 int i,ii,j,k,ll, Rows, Cols;
1156 #region "LU Decompose"
1159 A = (
double[,])MatA.Clone();
1160 B = (
double[,])MatB.Clone();
1161 Find_R_C(A , out Rows, out Cols);
1163 catch{
throw new MatrixNullException();}
1165 if (Rows != Cols)
throw new MatrixNotSquare();
1166 if ((B.GetUpperBound(0)!=Rows) || (B.GetUpperBound(1)!=0))
1167 throw new MatrixDimensionException();
1169 int IMAX = 0, N = Rows;
1170 double AAMAX, Sum, Dum, TINY = 1E-20;
1172 int[] INDX =
new int[N+1];
1173 double[] VV =
new double[N*10];
1176 for (i = 0; i<= N;i++)
1179 for (j = 0; j<= N;j++)
1180 if (Math.Abs(A[i, j]) > AAMAX) AAMAX = Math.Abs(A[i, j]);
1181 if (AAMAX == 0.0)
throw new MatrixSingularException();
1182 VV[i] = 1.0 / AAMAX;
1184 for (j = 0; j<= N; j++)
1188 for (i = 0; i<= (j - 1); i++)
1193 for (k = 0; k<= (i - 1); k++)
1194 Sum = Sum - A[i, k] * A[k, j];
1200 for (i = j; i<= N; i++)
1205 for (k = 0; k<= (j - 1); k++)
1206 Sum = Sum - A[i, k] * A[k, j];
1209 Dum = VV[i] * Math.Abs(Sum);
1218 for (k = 0; k<= N; k++)
1221 A[IMAX, k] = A[j, k];
1230 if (A[j, j] == 0.0) A[j, j] = TINY;
1231 Dum = 1.0 / A[j, j];
1232 for (i = j + 1; i<=N; i++)
1233 A[i, j] = A[i, j] * Dum;
1237 if (A[N, N] == 0.0) A[N, N] = TINY;
1241 for ( i=0; i<=N; i++)
1248 for (j=ii; j<=i-1; j++) SUM=SUM-A[i,j]*B[j,0];
1250 else if (SUM!=0) ii=i;
1258 for (j=i+1; j<=N; j++) SUM=SUM-A[i,j]*B[j,0];
1283 #region "Rank of a matrix"
1284 public static int Rank(
double[,] Mat)
1297 Find_R_C(Mat , out Rows, out Cols);
1299 catch{
throw new MatrixNullException();}
1300 double EPS = 2.2204E-16;
1301 SVD(Mat, out S, out U, out V);
1303 for (
int i = 0; i<= S.GetUpperBound(0);i++)
1304 {
if (Math.Abs(S[i, i]) > EPS) r++;}
1316 {
return Rank(Mat.in_Mat);}
1319 #region "Pseudoinverse of a matrix"
1320 public static double[,]
PINV(
double[,] Mat)
1332 double[,] S, Part_I, Part_II;
1333 double EPS, MulAdd, Tol, Largest_Item=0;
1335 double[,] svdU, svdS, svdV;
1339 Matrix = (
double[,])Mat.Clone();
1340 Find_R_C(Mat , out m, out n);
1342 catch{
throw new MatrixNullException();}
1344 SVD(Mat, out svdS, out svdU, out svdV);
1350 Part_I =
new double[m,n];
1351 Part_II =
new double[m,n];
1352 S =
new Double[n,n];
1355 for (i = 0 ; i<= svdS.GetUpperBound(0);i++)
1357 if (i == 0) Largest_Item = svdS[0, 0];
1358 if (Largest_Item < svdS[i, i]) Largest_Item = svdS[i, i];
1361 if (m > n) Tol = m * Largest_Item * EPS;
1362 else Tol = n * Largest_Item * EPS;
1364 for (i = 0; i < n; i++) S[i, i] = svdS[i,i];
1366 for (i = 0;i < m; i++)
1368 for (j = 0; j < n; j++)
1370 for (l = 0; l< n; l++)
1372 if (S[l, j] > Tol) MulAdd += svdU[i, l] * (1 / S[l, j]);
1374 Part_I[i, j] = MulAdd;
1379 for (i = 0;i < m; i++)
1381 for (j = 0; j < n; j++)
1383 for (l = 0; l< n; l++)
1385 MulAdd += Part_I[i, l] * svdV[j, l];
1387 Part_II[i, j] = MulAdd;
1402 public static Matrix
PINV(Matrix Mat)
1406 #region "Eigen Values and Vactors of Symmetric Matrix"
1407 public static void Eigen(
double[,] Mat, out
double[,] d,out
double[,] v)
1428 Find_R_C(Mat, out Rows, out Cols);
1429 a = (
double[,])Mat.Clone();
1431 catch{
throw new MatrixNullException();}
1433 if (Rows != Cols)
throw new MatrixNotSquare();
1435 int j,iq,ip,i,n, nrot;
1436 double tresh,theta,tau,t,sm,s,h,g,c;
1440 d =
new double[n+1,1];
1441 v =
new double[n+1,n+1];
1445 for (ip=0;ip <= n;ip++)
1447 for (iq=0;iq <= n;iq++) v[ip,iq]=0.0;
1450 for (ip=0;ip <= n;ip++)
1452 b[ip]=d[ip,0]=a[ip,ip];
1457 for (i=0;i <= 50;i++)
1460 for (ip=0;ip <= n-1;ip++)
1462 for (iq=ip+1;iq <= n;iq++)
1463 sm += Math.Abs(a[ip,iq]);
1473 for (ip=0;ip <= n-1;ip++)
1475 for (iq=ip+1;iq <= n;iq++)
1477 g=100.0*Math.Abs(a[ip,iq]);
1478 if (i > 4 && (
double)(Math.Abs(d[ip,0])+g) == (
double)Math.Abs(d[ip,0])
1479 && (double)(Math.Abs(d[iq,0])+g) == (
double)Math.Abs(d[iq,0]))
1481 else if (Math.Abs(a[ip,iq]) > tresh)
1484 if ((
double)(Math.Abs(h)+g) == (
double)Math.Abs(h))
1488 theta=0.5*h/(a[ip,iq]);
1489 t=1.0/(Math.Abs(theta)+Math.Sqrt(1.0+theta*theta));
1490 if (theta < 0.0) t = -t;
1492 c=1.0/Math.Sqrt(1+t*t);
1501 for (j=0;j <= ip-1;j++)
1503 ROT(g,h,s,tau,a,j,ip,j,iq);
1505 for (j=ip+1;j <= iq-1;j++)
1507 ROT(g,h,s,tau,a,ip,j,j,iq);
1509 for (j=iq+1;j <= n;j++)
1511 ROT(g,h,s,tau,a,ip,j,iq,j);
1513 for (j=0;j <= n;j++)
1515 ROT(g,h,s,tau,v,j,ip,j,iq);
1521 for (ip=0;ip <= n;ip++)
1528 Console.WriteLine(
"Too many iterations in routine jacobi");
1531 private static void ROT(
double g ,
double h ,
double s ,
double tau,
1532 double[,] a ,
int i ,
int j ,
int k ,
int l)
1535 a[i,j]=g-s*(h+g*tau);
1536 a[k,l]=h+s*(g-h*tau);
1553 public static void Eigen(Matrix Mat, out Matrix d,out Matrix v)
1556 Eigen(Mat.in_Mat, out D, out V);
1562 #region "Multiply a matrix or a vector with a scalar quantity"
1563 public static double[,]
ScalarMultiply(
double Value,
double[,] Mat)
1573 int i, j, Rows, Cols;
1576 try {Find_R_C(Mat, out Rows, out Cols);}
1577 catch{
throw new MatrixNullException();}
1578 sol =
new double[Rows+1, Cols+1];
1580 for (i = 0; i<=Rows;i++)
1581 for (j = 0; j<=Cols;j++)
1582 sol[i, j] = Mat[i, j] * Value;
1627 #region "Divide a matrix or a vector with a scalar quantity"
1628 public static double[,]
ScalarDivide(
double Value,
double[,] Mat)
1638 int i, j, Rows, Cols;
1641 try {Find_R_C(Mat, out Rows, out Cols);}
1642 catch{
throw new MatrixNullException();}
1644 sol =
new double[Rows+1, Cols+1];
1646 for (i = 0; i<=Rows;i++)
1647 for (j = 0; j<=Cols;j++)
1648 sol[i, j] = Mat[i, j] / Value;
1678 #region "Vectors Cross Product"
1679 public static double[]
CrossProduct(
double[] V1,
double[] V2)
1690 double[] sol =
new double[2];
1696 Find_R_C(V1, out Rows1);
1697 Find_R_C(V2, out Rows2);
1699 catch{
throw new MatrixNullException();}
1701 if (Rows1 != 2)
throw new VectorDimensionException();
1703 if (Rows2 != 2)
throw new VectorDimensionException();
1705 i = V1[1] * V2[2] - V1[2] * V2[1];
1706 j = V1[2] * V2[0] - V1[0] * V2[2];
1707 k = V1[0] * V2[1] - V1[1] * V2[0];
1709 sol[0] = i ; sol[1] = j ; sol[2] = k;
1725 double[,] sol =
new double[3,1];
1731 Find_R_C(V1, out Rows1, out Cols1);
1732 Find_R_C(V2, out Rows2, out Cols2);
1740 i = V1[1, 0] * V2[2, 0] - V1[2, 0] * V2[1, 0];
1741 j = V1[2, 0] * V2[0, 0] - V1[0, 0] * V2[2, 0];
1742 k = V1[0, 0] * V2[1, 0] - V1[1, 0] * V2[0, 0];
1744 sol[0, 0] = i ; sol[1, 0] = j ; sol[2, 0] = k;
1761 #region "Vectors Dot Product"
1762 public static double DotProduct(
double[] V1,
double[] V2)
1777 Find_R_C(V1, out Rows1);
1778 Find_R_C(V2, out Rows2);
1780 catch{
throw new MatrixNullException();}
1782 if (Rows1 != 2)
throw new VectorDimensionException();
1784 if (Rows2 != 2)
throw new VectorDimensionException();
1786 return (V1[0] * V2[0] + V1[1] * V2[1] + V1[2] * V2[2]);
1804 Find_R_C(V1, out Rows1, out Cols1);
1805 Find_R_C(V2, out Rows2, out Cols2);
1813 return (V1[0,0] * V2[0,0] + V1[1,0] * V2[1,0] + V1[2,0] * V2[2,0]);
1828 #region "Magnitude of a Vector"
1839 try {Find_R_C(V, out Rows);}
1840 catch{
throw new MatrixNullException();}
1842 if (Rows != 2)
throw new VectorDimensionException();
1844 return Math.Sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2]);
1857 try {Find_R_C(V, out Rows, out Cols);}
1862 return Math.Sqrt(V[0, 0] * V[0, 0] + V[1, 0] * V[1, 0] + V[2, 0] * V[2, 0]);
1875 #region "Two Matrices are equal or not"
1876 public static bool IsEqual (
double[,] Mat1,
double[,] Mat2)
1892 Find_R_C(Mat1,out Rows1, out Cols1);
1893 Find_R_C(Mat2,out Rows2, out Cols2);
1897 throw new MatrixNullException();
1899 if (Rows1 != Rows2 || Cols1 != Cols2)
throw new MatrixDimensionException();
1901 for (
int i=0; i<=Rows1; i++)
1903 for (
int j=0; j<=Cols1; j++)
1905 if (Math.Abs(Mat1[i,j]-Mat2[i,j])>eps)
return false;
1918 public static bool IsEqual (Matrix Mat1, Matrix Mat2)
1919 {
return IsEqual(Mat1.in_Mat, Mat2.in_Mat);}
1929 {
return IsEqual(Mat1.in_Mat, Mat2.in_Mat);}
1939 {
return (!
IsEqual(Mat1.in_Mat, Mat2.in_Mat));}
1949 try {
return (
bool) (
this == (
Matrix) obj);}
1950 catch {
return false;}
1954 #region "Print Matrix"
1955 public static string PrintMat(
double[,] Mat)
1964 int N_Rows, N_Columns, k, i, j, m;
1966 int StrLen;
int[] Greatest;
1967 string LarString, OptiString, sol;
1970 try {Find_R_C(Mat, out N_Rows, out N_Columns);}
1973 throw new MatrixNullException();
1979 Greatest =
new int[N_Columns+1];
1981 for (i = 0;i<= N_Rows;i++)
1983 for (j = 0;j<= N_Columns;j++)
1988 for (m = 0;m<= N_Rows;m++)
1990 StrElem =Mat[m, j].ToString(
"0.0000");
1991 StrLen = StrElem.Length;
1992 if (Greatest[j] < StrLen)
1994 Greatest[j] = StrLen;
1995 LarString = StrElem;
1998 if (LarString.StartsWith(
"-")) Greatest[j] = Greatest[j];
2000 StrElem = Mat[i, j].ToString(
"0.0000");
2001 if (StrElem.StartsWith(
"-"))
2003 StrLen = StrElem.Length;
2004 if (Greatest[j] >= StrLen)
2006 for (k = 1;k<=(Greatest[j] - StrLen);k++) OptiString +=
" ";
2012 StrLen = StrElem.Length;
2013 if (Greatest[j] > StrLen)
2015 for (k = 1;k<= (Greatest[j] - StrLen);k++)
2021 OptiString +=
" " + (Mat[i, j].ToString(
"0.0000"));
2026 sol += OptiString +
"\n";