示例#1
0
            protected override void doExecute()
            {
                mw.TB_nROW.Text              = m_nROW;
                mw.TB_nCOL.Text              = m_nCOL;
                mw.TB_nZero.Text             = m_nZero;
                mw.TB_maxRnd.Text            = m_maxRnd;
                mw.rightBorder               = m_right;
                mw.leftBorder                = m_left;
                mw.CheckBox_BORDER.IsChecked = bBorder;
                switch (bridge)
                {
                case 0:
                    mw.DT_WPF.IsChecked = true;
                    break;

                case 1:
                    mw.DT_CONSOLE.IsChecked = true;
                    break;
                }
                if (m_mx == null)
                {
                    mw.ApplyMatrix(m_mx);
                }
                else
                {
                    mw.ApplyMatrix(m_mx.Clone());
                }
            }
示例#2
0
文件: MainForm.cs 项目: batuZ/Samples
        void scaleCalculate(IModelPoint oldMP, IModelPoint newMP,
                            out double scaleX, out double scaleY, out double scaleZ,
                            out double centerX, out double centerY, out double centerZ)
        {
            M0 = oldMP.AsMatrix();
            M1 = newMP.AsMatrix();
            IMatrix M0_Inverse = M0.Clone();

            M0_Inverse.Inverse();
            IMatrix  MS     = MultiplyMatrix(M0_Inverse, M1);
            IVector3 scaleV = MS.GetScale();

            scaleX  = scaleV.X;
            scaleY  = scaleV.Y;
            scaleZ  = scaleV.Z;
            centerX = MS.M41 / (1.0 - scaleV.X);
            centerY = MS.M42 / (1.0 - scaleV.Y);
            centerZ = MS.M43 / (1.0 - scaleV.Z);
            if (scaleV.X == 1)
            {
                centerX = MS.M41;
            }
            if (scaleV.Y == 1)
            {
                centerY = MS.M42;
            }
            if (scaleV.Z == 1)
            {
                centerZ = MS.M43;
            }
        }
示例#3
0
            void _Update(IMatrix error, INeuralNetworkLayerUpdater w, INeuralNetworkLayerUpdater u, INeuralNetworkUpdateAccumulator updateAccumulator)
            {
                var deltaW = _input.TransposeThisAndMultiply(error);
                var deltaU = _pc.TransposeThisAndMultiply(error);

                updateAccumulator.Record(w, error, deltaW);
                updateAccumulator.Record(u, error.Clone(), deltaU);
            }
示例#4
0
 public DiagonalPreconditioner(IMatrix matr)
 {
     matrix = matr.Clone() as IMatrix;
     if (matrix.Diagonal.ContainZero())
     {
         throw new slae_project.Matrix.MatrixExceptions.LUFailException("Для использования диагонального предобуславливания главная диагональ исходной матрицы не должна содержать нулевые элементы.");
     }
 }
示例#5
0
文件: Matrix.cs 项目: wnf0000/Eto
 /// <summary>
 /// Multiply the specified <paramref name="matrix"/> and <paramref name="matrices"/>.
 /// </summary>
 /// <returns>A new matrix with the product of multiplying each of the specified matrix and matrices</returns>
 /// <param name="matrix">Matrix to multiply with</param>
 /// <param name="matrices">Matrices to append</param>
 public static IMatrix Multiply(IMatrix matrix, params IMatrix[] matrices)
 {
     matrix = matrix.Clone();
     for (int i = 0; i < matrices.Length; i++)
     {
         matrix.Append(matrices[i]);
     }
     return(matrix);
 }
示例#6
0
 public LUPreconditioner(IMatrix matr)
 {
     m = matr.Clone() as IMatrix;
     m.MakeLU();
     if (m.Diagonal.ContainZero())
     {
         throw new slae_project.Matrix.MatrixExceptions.LUFailException();
     }
 }
示例#7
0
        public IGraphicsPath Clone()
        {
            var handler = new GraphicsPathHandler();

            handler._commands.AddRange(_commands);
            handler._data.AddRange(_data);
            handler._transform         = _transform?.Clone();
            handler._firstFigureClosed = _firstFigureClosed;
            handler._isFirstFigure     = _isFirstFigure;
            return(handler);
        }
示例#8
0
        public IGraphicsPath Clone()
        {
            var handler = new GraphicsPathHandler();

            handler.commands.AddRange(commands);
            handler.transform = transform != null?transform.Clone() : null;

            handler.firstFigureClosed = firstFigureClosed;
            handler.isFirstFigure     = isFirstFigure;
            return(handler);
        }
示例#9
0
 void Prepend(IMatrix matrix)
 {
     if (Current != null)
     {
         pop();
         Current.Prepend(matrix);
     }
     else
     {
         Current = matrix.Clone();
     }
     push(Current);
 }
示例#10
0
		void Prepend(IMatrix matrix)
		{
			if (Current != null)
			{
				pop();
				Current.Prepend(matrix);
			}
			else
			{
				Current = matrix.Clone();
			}
			push(Current);
		}
示例#11
0
 public BackupCommand(MainWindow m, IMatrix <int> mx, string nROW, string nCOL, string nZero, string maxRnd, bool Border, int Bridge, string right, string left)
 {
     mw       = m;
     m_mx     = mx == null ? null : mx.Clone();
     m_nROW   = nROW;
     m_nCOL   = nCOL;
     m_nZero  = nZero;
     m_maxRnd = maxRnd;
     bBorder  = Border;
     bridge   = Bridge;
     m_right  = right;
     m_left   = left;
 }
示例#12
0
        public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
        {
            var t = context.CurrentEpoch;

            using var deltaSquared = delta.PointwiseMultiply(delta);
            _cache.AddInPlace(delta, _decayRate, 1 - _decayRate);
            _cache2.AddInPlace(deltaSquared, _decayRate2, 1 - _decayRate2);
            using var mb = _cache.Clone();
            using var vb = _cache2.Clone();
            mb.Multiply(1f / (1f - Convert.ToSingle(Math.Pow(_decayRate, t))));
            vb.Multiply(1f / (1f - Convert.ToSingle(Math.Pow(_decayRate2, t))));
            using var vbSqrt = vb.Sqrt();
            using var delta2 = mb.PointwiseDivide(vbSqrt);
            _updater.Update(source, delta2, context);
        }
示例#13
0
        void _TestNode(INode node, IMatrix forwardInput, IMatrix expectedForwardOutput, IMatrix backwardInput, IMatrix expectedBackwardOutput)
        {
            var context = new TestingContext(_cpu);
            var matrix  = forwardInput.AsIndexable();

            context.Data = matrix.AsGraphData();
            node.ExecuteForward(context, 0);

            var output       = context.Forward.First();
            var outputMatrix = output.Item1.Data.GetMatrix();

            FloatingPointHelper.AssertEqual(outputMatrix.AsIndexable(), expectedForwardOutput.AsIndexable());

            output.Item2.Backward(null, backwardInput.Clone().AsGraphData(), context, new[] { node });
            var bpOutput = context.Backward.First().Item1.GetMatrix();

            FloatingPointHelper.AssertEqual(bpOutput.AsIndexable(), expectedBackwardOutput.AsIndexable());
        }
示例#14
0
        /// <summary>
        /// Given A which is a n x n symmetric matrix, we want to find U and T
        /// such that:
        ///   1. A = U * T * U.transpose
        ///   2. U is a n x n matrix whose columns are eigen vectors of A (A * x = lambda * x, where lambda is the eigen value, and x is the eigen vector)
        ///   3. T is a diagonal matrix whose diagonal entries are the eigen values
        ///
        /// The method works in the following manner:
        ///   1. intialze A_0 = A, U_0 = I
        ///   2. iterate for k = 1, ... K, K is termination criteria
        ///   3. In each iteration k:
        ///      3.1 QR factorization to find Q_k and R_k such that A_{k-1}=Q_k * R_k
        ///      3.2 Let A_k = R_k * Q_k
        ///      3.3 Let U_k = U_{k-1} * Q_k
        ///   4. Set T = A_K, U = U_K
        ///
        /// Note that U is an orthogonal matrix if A is a symmetric matrix, in other words, if A.transpose = A, then U.inverse = U.transpose
        /// </summary>
        /// <param name="A">The matrix to be factorized</param>
        /// <param name="K">maximum number of iterations</param>
        /// <param name="T">T is a diagonal matrix whose diagonal entries are the eigen values</param>
        /// <param name="U">U is a n x n matrix whose columns are eigen vectors of A</param>
        public static void Factorize(IMatrix A, out IMatrix T, out IMatrix U, int K = 100, double epsilon = 1e-10)
        {
            Debug.Assert(A.RowCount == A.ColCount);

            int     n   = A.RowCount;
            IMatrix A_k = A.Clone();
            IMatrix U_k = A.Identity(n);

            IMatrix Q_k, R_k;

            for (int k = 1; k <= K; ++k)
            {
                QR.Factorize(A_k, out Q_k, out R_k);
                A_k = R_k.Multiply(Q_k);
                U_k = U_k.Multiply(Q_k);

                double sum = 0;
                foreach (IVector rowVec in A_k.NonEmptyRows)
                {
                    int rowId = rowVec.ID;
                    foreach (int key in rowVec.NonEmptyKeys)
                    {
                        if (key == rowId)
                        {
                            continue;
                        }
                        sum += System.Math.Abs(rowVec[key]);
                    }
                }
                if (sum <= epsilon)
                {
                    break;
                }
            }

            T = new SparseMatrix(n, n, A.DefaultValue);

            for (int i = 0; i < n; ++i)
            {
                T[i, i] = A_k[i, i];
            }

            U = U_k;
        }
示例#15
0
 public void Transform(IMatrix matrix)
 {
     if (matrix != null)
     {
         if (transform != null)
         {
             transform.Prepend(matrix);
         }
         else
         {
             transform = matrix.Clone();
         }
     }
     else
     {
         transform = null;
     }
     control = null;
 }
示例#16
0
        public override void Update(IMatrix biasDelta, IMatrix weightDelta, ITrainingContext context)
        {
            var t = context.CurrentEpoch;

            using (var deltaSquared = weightDelta.PointwiseMultiply(weightDelta)) {
                _cache.AddInPlace(weightDelta, _decay, 1 - _decay);
                _cache2.AddInPlace(deltaSquared, _decay2, 1 - _decay2);

                using (var mb = _cache.Clone())
                    using (var vb = _cache2.Clone()) {
                        mb.Multiply(1f / (1f - Convert.ToSingle(Math.Pow(_decay, t))));
                        vb.Multiply(1f / (1f - Convert.ToSingle(Math.Pow(_decay2, t))));
                        using (var vbSqrt = vb.Sqrt(1e-8f))
                            using (var delta = mb.PointwiseDivide(vbSqrt)) {
                                _layerUpdater.Update(biasDelta, delta, context);
                            }
                    }
            }
        }
示例#17
0
        public IMatrix Mul(IMatrix mat)
        {
            int    m, n, r;
            double row;

            System.Console.WriteLine("in Mul!");
            IMatrix tmp = (IMatrix)mat.Clone();

            tmp.SizeY = SizeY;

            for (m = 0; m < tmp.SizeX; m++)
            {
                for (n = 0; n < SizeY; n++)
                {
                    row = 0;
                    for (r = 0; r < SizeX; r++)
                    {
                        row += GetNum(r, n) * mat.GetNum(m, r);
                    }
                    tmp.SetNum(m, n, row);
                }
            }
            return(tmp);
        }
示例#18
0
 public override lab_matrix_bridge.ICommand Clone() => new MatrixCommand(mw, mx.Clone(), m_Append);
示例#19
0
		/// <summary>
		/// Multiply the specified <paramref name="matrix"/> and <paramref name="matrices"/>.
		/// </summary>
		/// <returns>A new matrix with the product of multiplying each of the specified matrix and matrices</returns>
		/// <param name="matrix">Matrix to multiply with</param>
		/// <param name="matrices">Matrices to append</param>
		public static IMatrix Multiply (IMatrix matrix, params IMatrix[] matrices)
		{
			matrix = matrix.Clone ();
			for (int i = 0; i < matrices.Length; i++)
				matrix.Append (matrices [i]);
			return matrix;
		}
示例#20
0
文件: Matrix.cs 项目: wnf0000/Eto
 /// <summary>
 /// Creates an inverted copy of the specified matrix.
 /// </summary>
 /// <param name="matrix">Matrix to invert.</param>
 public static IMatrix Inverse(this IMatrix matrix)
 {
     matrix = matrix.Clone();
     matrix.Invert();
     return(matrix);
 }
示例#21
0
		public void Transform(IMatrix matrix)
		{
			if (matrix != null)
			{
				if (transform != null)
					transform.Prepend(matrix);
				else
					transform = matrix.Clone();
			}
			else
				transform = null;
			control = null;
		}
示例#22
0
 public MatrixCommand(MainWindow m, IMatrix <int> matr, int append = 0)
 {
     mw       = m;
     mx       = matr.Clone();
     m_Append = append;
 }
示例#23
0
文件: MaPack.cs 项目: abladon/canvas
            public IMatrix Solve(IMatrix rhs)
            {
                if (rhs.Rows != _l.Rows)
                {
                    throw new ArgumentException("Matrix dimensions do not match.");
                }
                if (!_isSymmetric)
                {
                    throw new InvalidOperationException("Matrix is not symmetric.");
                }
                if (!_isPositiveDefinite)
                {
                    throw new InvalidOperationException("Matrix is not positive definite.");
                }

                int dimension = _l.Rows;
                int count = rhs.Columns;

                IMatrix b = rhs.Clone();
                double[][] l = _l.Array;

                // Solve L*Y = B;
                for (int k = 0; k < _l.Rows; k++)
                {
                    for (int i = k + 1; i < dimension; i++)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            b[i, j] -= b[k, j]*l[i][k];
                        }
                    }

                    for (int j = 0; j < count; j++)
                    {
                        b[k, j] /= l[k][k];
                    }
                }

                // Solve L'*X = Y;
                for (int k = dimension - 1; k >= 0; k--)
                {
                    for (int j = 0; j < count; j++)
                    {
                        b[k, j] /= l[k][k];
                    }

                    for (int i = 0; i < k; i++)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            b[i, j] -= b[k, j]*l[k][i];
                        }
                    }
                }

                return b;
            }
示例#24
0
文件: MaPack.cs 项目: abladon/canvas
            public IMatrix Solve(IMatrix rhs)
            {
                if (rhs.Rows != _qr.Rows) throw new ArgumentException("Matrix row dimensions must agree.");
                if (!IsFullRank) throw new InvalidOperationException("Matrix is rank deficient.");

                // Copy right hand side
                int count = rhs.Columns;
                IMatrix x = rhs.Clone();
                int m = _qr.Rows;
                int n = _qr.Columns;
                double[][] qr = _qr.Array;

                // Compute Y = transpose(Q)*B
                for (int k = 0; k < n; k++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        double s = 0.0;
                        for (int i = k; i < m; i++)
                            s += qr[i][k]*x[i, j];
                        s = -s/qr[k][k];
                        for (int i = k; i < m; i++)
                            x[i, j] += s*qr[i][k];
                    }
                }

                // Solve R*X = Y;
                for (int k = n - 1; k >= 0; k--)
                {
                    for (int j = 0; j < count; j++)
                        x[k, j] /= _rdiag[k];

                    for (int i = 0; i < k; i++)
                        for (int j = 0; j < count; j++)
                            x[i, j] -= x[k, j]*qr[i][k];
                }

                return x.Submatrix(0, n - 1, 0, count - 1);
            }
        /// The method works by using Gaussian elimination to covert the matrix A to the echelon form of A
        /// The computational Complexity is O(n^3)
        /// </summary>
        /// <param name="A">The original matrix</param>
        /// <param name="M">An invertiable matrix, which originally starts as an identity matrix and arrived by undergoing the same elementary operations as A</param>
        /// <param name="numRowExOperations">The number of elementary row exchange operations during the Gaussian elimination</param>
        /// <returns>The echelon form of the original matrix, which is M*A</returns>
        public static IMatrix GetEchelonForm(IMatrix A, out IMatrix M, out int numRowExOperations)
        {
            IMatrix B        = A.Clone();
            int     rowCount = B.RowCount;
            int     colCount = B.ColCount;

            M = A.Identity(rowCount);

            numRowExOperations = 0;

            List <int>    newRows       = new List <int>();
            HashSet <int> remainingRows = new HashSet <int>();

            List <int> oldRows = B.RowKeys.ToList();

            foreach (int k in oldRows)
            {
                remainingRows.Add(k);
            }


            foreach (int c in B.ColKeys)
            {
                List <int> nonZeroRows = GetRowsWithNonZeroColumnEntry(B, remainingRows, c);
                if (nonZeroRows.Count > 0)
                {
                    int pivot = GetPivot(B, nonZeroRows, c);

                    newRows.Add(pivot);
                    remainingRows.Remove(pivot);

                    foreach (int r in nonZeroRows)
                    {
                        double multiplier = B[r][c] / B[pivot][c];

                        B[r] = B[r].Minus(B[pivot].Multiply(multiplier));
                        M[r] = M[r].Minus(M[pivot].Multiply(multiplier));
                    }
                }
            }

            foreach (int r in remainingRows)
            {
                newRows.Add(r);
            }

            for (int i = 0; i < newRows.Count; ++i)
            {
                int newRow = newRows[i];
                int oldRow = oldRows[i];

                if (!newRow.Equals(oldRow))
                {
                    IVector temp = B[newRow];
                    B[newRow] = B[oldRow];
                    B[oldRow] = temp;

                    temp      = M[newRow];
                    M[newRow] = M[oldRow];
                    M[oldRow] = temp;

                    int newRowIndex = i;
                    int oldRowIndex = newRows.IndexOf(oldRow);

                    Swap(newRows, newRowIndex, oldRowIndex);

                    numRowExOperations++;
                }
            }

            return(B);
        }