Пример #1
0
        public static DoubleDenseMatrix Mul(DoubleDenseMatrix left, DoubleDenseMatrix right)
        {
            // <preconditions>
            if (left == (DoubleDenseMatrix)null)
            {
                throw new ArgumentNullException("left");
            }
            if (right == (DoubleDenseMatrix)null)
            {
                throw new ArgumentNullException("right");
            }
            // </preconditions>
            if (left.ColumnCount != right.RowCount)
            {
                throw new MatrixSizeMistmatchException();
            }

            DoubleDenseMatrix m = new DoubleDenseMatrix(left.RowCount, right.ColumnCount);

            for (int i = 0; i < left.RowCount; ++i)
            {
                for (int j = 0; j < left.ColumnCount; ++j)
                {
                    for (int k = 0; k < right.ColumnCount; ++k)
                    {
                        m[i, k] += left[i, j] * right[j, k];
                    }
                }
            }

            return(m);
        }
 public MatrixSizeMistmatchException(
     DoubleDenseMatrix left,
     DoubleDenseMatrix right)
     : this(String.Format("Matrix size ({0}x{1}) does not match ({2}x{3})",
         left.RowCount,left.ColumnCount,right.RowCount, right.ColumnCount))
 {
 }
Пример #3
0
        public static DoubleDenseMatrix Identity(int count)
        {
            DoubleDenseMatrix m = new DoubleDenseMatrix(count, count);

            for (int i = 0; i < count; ++i)
            {
                m[i, i] = 1;
            }
            return(m);
        }
Пример #4
0
        public DoubleDenseMatrix Sub(DoubleDenseMatrix matrix)
        {
            VerifySize(matrix);

            for (int i = 0; i < data.Length; ++i)
            {
                this.data[i] -= matrix.data[i];
            }

            return(this);
        }
Пример #5
0
 private void VerifySize(DoubleDenseMatrix matrix)
 {
     if (this.RowCount != matrix.RowCount)
     {
         throw new MatrixSizeMistmatchException();
     }
     if (this.ColumnCount != matrix.ColumnCount)
     {
         throw new MatrixSizeMistmatchException();
     }
 }
Пример #6
0
        public static DoubleDenseMatrix Add(DoubleDenseMatrix left, DoubleDenseMatrix right)
        {
            // <preconditions>
            if (left == (DoubleDenseMatrix)null)
            {
                throw new ArgumentNullException("left");
            }
            // </preconditions>
            DoubleDenseMatrix m = Create(left);

            return(m.Add(right));
        }
Пример #7
0
        public DoubleDenseMatrix Transpose()
        {
            DoubleDenseMatrix m = new DoubleDenseMatrix(this.ColumnCount, this.RowCount);

            for (int i = 0; i < this.RowCount; ++i)
            {
                for (int j = 0; j < this.ColumnCount; ++j)
                {
                    m[j, i] = this[i, j];
                }
            }
            return(m);
        }
Пример #8
0
        public DoubleDenseMatrix Trace()
        {
            if (this.RowCount != this.ColumnCount)
            {
                throw new MatrixSizeMistmatchException();
            }
            DoubleDenseMatrix m = new DoubleDenseMatrix(this.RowCount, 1);

            for (int i = 0; i < this.RowCount; ++i)
            {
                m.data[i] = this[i, i];
            }

            return(m);
        }
Пример #9
0
        public static DoubleDenseMatrix Similarity(
            DoubleDenseMatrix A,
            DoubleDenseMatrix B,
            double tolerance
            )
        {
            // <preconditions>
            if (A == (DoubleDenseMatrix)null)
            {
                throw new ArgumentNullException("A");
            }
            if (B == (DoubleDenseMatrix)null)
            {
                throw new ArgumentNullException("B");
            }
            // </preconditions>
            DoubleDenseMatrix AT    = A.Transpose();
            DoubleDenseMatrix BT    = B.Transpose();
            DoubleDenseMatrix Zk    = DoubleDenseMatrix.Create(B.RowCount, A.RowCount, 1.0 / (A.RowCount * B.RowCount));
            DoubleDenseMatrix Zk1   = null;
            DoubleDenseMatrix ZkOld = null;

            int iteration = 0;

            do
            {
                Zk1 = B * Zk * AT + BT * Zk * A;
                Zk1.Div(Zk1.GetNorm2());

                ZkOld = Zk;
                Zk    = B * Zk1 * AT + BT * Zk1 * A;
                Zk.Div(Zk.GetNorm2());

                Console.WriteLine(iteration);
                Zk.WriteMatrix(Console.Out);
                Console.WriteLine((Zk - ZkOld).GetNorm2());

                if (iteration++ > 100)
                {
                    throw new InvalidOperationException();
                }
            } while ((Zk - ZkOld).GetNorm2() > tolerance);

            return(Zk);
        }
Пример #10
0
 private void VerifySize(DoubleDenseMatrix matrix)
 {
     if (this.RowCount != matrix.RowCount)
         throw new MatrixSizeMistmatchException();
     if (this.ColumnCount != matrix.ColumnCount)
         throw new MatrixSizeMistmatchException();
 }
Пример #11
0
 public static DoubleDenseMatrix Mul(DoubleDenseMatrix left, double factor)
 {
     DoubleDenseMatrix m = Create(left);
     return m.Mul(factor);
 }
Пример #12
0
 public static DoubleDenseMatrix Identity(int count)
 {
     DoubleDenseMatrix m = new DoubleDenseMatrix(count, count);
     for (int i = 0; i < count; ++i)
         m[i, i] = 1;
     return m;
 }
Пример #13
0
        public static DoubleDenseMatrix Mul(DoubleDenseMatrix left, DoubleDenseMatrix right)
        {
            // <preconditions>
            if (left == (DoubleDenseMatrix)null)
                throw new ArgumentNullException("left");
            if (right == (DoubleDenseMatrix)null)
                throw new ArgumentNullException("right");
            // </preconditions>
            if (left.ColumnCount != right.RowCount)
                throw new MatrixSizeMistmatchException();

            DoubleDenseMatrix m = new DoubleDenseMatrix(left.RowCount, right.ColumnCount);
            for (int i = 0; i < left.RowCount; ++i)
            {
                for (int j = 0; j < left.ColumnCount; ++j)
                {
                    for (int k = 0; k < right.ColumnCount; ++k)
                    {
                        m[i,k] += left[i,j] * right[j,k];
                    }
                }
            }

            return m;
        }
Пример #14
0
 public static DoubleDenseMatrix Mul(double factor, DoubleDenseMatrix right)
 {
     DoubleDenseMatrix m = Create(right);
     return m.Mul(factor);
 }
Пример #15
0
        public static DoubleDenseMatrix Mul(DoubleDenseMatrix left, double factor)
        {
            DoubleDenseMatrix m = Create(left);

            return(m.Mul(factor));
        }
Пример #16
0
        public static DoubleDenseMatrix Mul(double factor, DoubleDenseMatrix right)
        {
            DoubleDenseMatrix m = Create(right);

            return(m.Mul(factor));
        }
Пример #17
0
        public static DoubleDenseMatrix Similarity(
            DoubleDenseMatrix A,
            DoubleDenseMatrix B,
            double tolerance
            )
        {
            // <preconditions>
            if (A == (DoubleDenseMatrix)null)
                throw new ArgumentNullException("A");
            if (B == (DoubleDenseMatrix)null)
                throw new ArgumentNullException("B");
            // </preconditions>
            DoubleDenseMatrix AT = A.Transpose();
            DoubleDenseMatrix BT = B.Transpose();
            DoubleDenseMatrix Zk = DoubleDenseMatrix.Create(B.RowCount, A.RowCount, 1.0/(A.RowCount*B.RowCount));
            DoubleDenseMatrix Zk1 = null;
            DoubleDenseMatrix ZkOld = null;

            int iteration = 0;
            do
            {
                Zk1 = B * Zk * AT + BT * Zk * A;
                Zk1.Div(Zk1.GetNorm2());

                ZkOld = Zk;
                Zk = B * Zk1 * AT + BT * Zk1 * A;
                Zk.Div(Zk.GetNorm2());

                Console.WriteLine(iteration);
                Zk.WriteMatrix(Console.Out);
                Console.WriteLine((Zk - ZkOld).GetNorm2());

                if (iteration++ > 100)
                    throw new InvalidOperationException();

            } while ((Zk - ZkOld).GetNorm2() > tolerance);

            return Zk;
        }
Пример #18
0
        public static DoubleDenseMatrix Sub(DoubleDenseMatrix left, DoubleDenseMatrix right)
        {
            DoubleDenseMatrix m = Create(left);

            return(m.Sub(right));
        }
Пример #19
0
 public static DoubleDenseMatrix Sub(DoubleDenseMatrix left, DoubleDenseMatrix right)
 {
     DoubleDenseMatrix m = Create(left);
     return m.Sub(right);
 }
Пример #20
0
 public static DoubleDenseMatrix Create(DoubleDenseMatrix matrix)
 {
     return new DoubleDenseMatrix(matrix.RowCount, matrix.ColumnCount, matrix.GetData());
 }
Пример #21
0
        public DoubleDenseMatrix Sub(DoubleDenseMatrix matrix)
        {
            VerifySize(matrix);

            for (int i = 0; i < data.Length; ++i)
                this.data[i] -= matrix.data[i];

            return this;
        }
Пример #22
0
 public DoubleDenseMatrix Transpose()
 {
     DoubleDenseMatrix m = new DoubleDenseMatrix(this.ColumnCount, this.RowCount);
     for (int i = 0; i < this.RowCount; ++i)
         for (int j = 0; j < this.ColumnCount; ++j)
             m[j, i] = this[i, j];
     return m;
 }
Пример #23
0
        public DoubleDenseMatrix Trace()
        {
            if (this.RowCount != this.ColumnCount)
                throw new MatrixSizeMistmatchException();
            DoubleDenseMatrix m = new DoubleDenseMatrix(this.RowCount,1);
            for (int i = 0; i < this.RowCount; ++i)
                m.data[i] = this[i, i];

            return m;
        }
Пример #24
0
 public static DoubleDenseMatrix Create(DoubleDenseMatrix matrix)
 {
     return(new DoubleDenseMatrix(matrix.RowCount, matrix.ColumnCount, matrix.GetData()));
 }
Пример #25
0
 public static DoubleDenseMatrix Add(DoubleDenseMatrix left, DoubleDenseMatrix right)
 {
     // <preconditions>
     if (left == (DoubleDenseMatrix)null)
         throw new ArgumentNullException("left");
     // </preconditions>
     DoubleDenseMatrix m = Create(left);
     return m.Add(right);
 }