Beispiel #1
0
 public static GenTensor <T, TWrapper> MatrixDivide(GenTensor <T, TWrapper> a, GenTensor <T, TWrapper> b)
 {
     #if ALLOW_EXCEPTIONS
     if (!a.IsSquareMatrix || !b.IsSquareMatrix)
     {
         throw new InvalidShapeException("Both should be square matrices");
     }
     if (a.Shape != b.Shape)
     {
         throw new InvalidShapeException("Given matrices should be of the same shape");
     }
     #endif
     var fwd = b.Forward();
     fwd.InvertMatrix();
     return(MatrixMultiplication <T, TWrapper> .Multiply(a, fwd));
 }
Beispiel #2
0
        public static GenTensor <T, TWrapper> MatrixPower(GenTensor <T, TWrapper> m, int power, Threading threading)
        {
            #if ALLOW_EXCEPTIONS
            if (!m.IsSquareMatrix)
            {
                throw new InvalidShapeException("Square matrix required");
            }
            #endif
            if (power == 0)
            {
                return(Constructors <T, TWrapper> .CreateIdentityMatrix(m.Shape.shape[0]));
            }
            if (power < 0)
            {
                m = m.Forward();
                m.InvertMatrix();
                power *= -1;
            }
            if (power == 1)
            {
                return(m);
            }
            if (power == 2)
            {
                return(MatrixMultiplication <T, TWrapper> .Multiply(m, m, threading));
            }
            var half   = power / 2;
            var m1     = MatrixPower(m, half, threading);
            var dotted = MatrixMultiplication <T, TWrapper> .Multiply(m1, m1, threading);

            if (power % 2 == 0)
            {
                return(dotted);
            }
            else
            {
                return(MatrixMultiplication <T, TWrapper> .Multiply(dotted, m, threading));
            }
        }