Exemplo n.º 1
0
        //operator *
        public static SqrMatrix operator *(SqrMatrix left, SqrMatrix right)
        {
            if (left.N != right.N)
                return null;

            SqrMatrix c = new SqrMatrix(left.N);

            for (int i = 0; i < left.N; i++)
            {
                for (int j = 0; j < left.N; j++)
                {
                    c[i, j] = 0;
                    for (int k = 0; k < left.N; k++)
                        c[i, j] = c[i, j] + left[i, k] * right[k, j];
                }
            }
            return c;
        }
Exemplo n.º 2
0
        // operator -
        public static SqrMatrix operator -(SqrMatrix left, SqrMatrix right)
        {
            if (left.N != right.N)
                return null;

            SqrMatrix c = new SqrMatrix(left.N);

            for (int i = 0; i < left.N; i++)
                for (int j = 0; j < left.N; j++)
                    c[i, j] = left[i, j] - right[i, j];

            return c;
        }
Exemplo n.º 3
0
        // removing complete to exp of 2
        private static SqrMatrix deleteComplete(SqrMatrix mat,int origN)
        {
            SqrMatrix temp = new SqrMatrix(origN);

            for (int j = 0; j < origN; j++)
                for (int k = 0; k < origN; k++)
                {
                    temp[j, k] = mat[j, k];
                }

            return temp;
        }
Exemplo n.º 4
0
        // complete values to exp of 2
        private static SqrMatrix completeMatrix(SqrMatrix mat)
        {
            int i=mat.N;
            while (!IsPowerOfTwo((ulong)(i)))
            {
                i++;
            }

            SqrMatrix temp = new SqrMatrix(i);

            for(int j=0;j<i;j++)
              temp[j,j]=1;

            for(int j=0;j<mat.N;j++)
              for(int k=0;k<mat.N;k++)
            {
                temp[j, k] = mat[j, k];
            }

            return temp;
        }
Exemplo n.º 5
0
        // make the invertion
        public static SqrMatrix inv(SqrMatrix source)
        {
            if (!IsPowerOfTwo((ulong)source.N)) // if the dimension is not a pow of 2
            {
                SqrMatrix result1;
               result1= inv( completeMatrix(source));
               return deleteComplete(result1,source.N);

            }

             //   source.print();

            SqrMatrix result;
            if (source.N == 1) // break condition when reaching 1
            {

                result = new SqrMatrix(1);
                result[0,0]=1/source[0,0];
                return result;

            }
               SqrMatrix A = new SqrMatrix(source.N / 2);
               SqrMatrix B = new SqrMatrix(source.N / 2);
               SqrMatrix C = new SqrMatrix(source.N / 2);
               SqrMatrix D = new SqrMatrix(source.N / 2);
               SqrMatrix iA,iB,iC,iD,invDCAB,invA;
               for (int i = 0; i < A.N; i++) // putting values back to matrixes A,B,C,D
                   for (int j = 0; j < A.N; j++)
                   {
                       A[i, j] = source[i, j];
                       B[i, j] = source[i, j + B.N];
                       C[i, j] = source[i + C.N, j];
                       D[i, j] = source[i + D.N, j + D.N];
                   }

              // according to the formula
               invA = inv(A);
               invDCAB = inv(D-C*invA*B);

               iA = invA + invA*B*invDCAB*C*invA;
               iB = -invA*B*invDCAB;
               iC = -invDCAB*C*invA;
               iD = invDCAB;

               result = new SqrMatrix(source.N);
            for(int i=0;i<iA.N;i++)   // putting values to a bigger matrix
                for(int j =0;j<iA.N;j++)
                {
                    result[i,j]=iA[i,j];
                    result[i, j + iB.N] = iB[i, j];
                    result[i + iC.N, j] = iC[i, j];
                    result[i + iD.N, j + iD.N] = iD[i, j];
                }

            return result;
        }
Exemplo n.º 6
0
        //unary operator -
        public static SqrMatrix operator -(SqrMatrix left)
        {
            SqrMatrix c = new SqrMatrix(left.N);

            for (int i = 0; i < left.N; i++)
                for (int j = 0; j < left.N; j++)
                    c[i, j] = -left[i, j] ;

            return c;
        }