Example #1
0
        /// <summary>
        /// Q-R Decomposition
        /// </summary>
        /// <param name="A">Source matrix</param>
        /// <returns>{Q,R} in array form</returns>
        public static RealMatrix[] QR(RealMatrix A)
        {
            int m = A.Height;
            int n = A.Width;

            var Q = RealMatrix.I(m);

            var R = new RealMatrix(A);

            for (int k = 0; k < n; k++)
            {
                var x = new RVector(R.Submatrix(k, k, 0, 1));
                var e = RVector.Zeros(x.Length);
                e[0] = 1;
                var u = -Math.Sign(x[0]) * x.Norm2 * e + x;
                u = u / u.Norm2;
                var qn = RealMatrix.I(u.Length) - 2 * u.Outer(u);

                //var r = R.Submatrix(k, k) - 2 * u.Outer(u) * R.Submatrix(k, k);
                var q = RealMatrix.I(Q.Height);
                q.Update(k, k, qn);
                Q = Q * q;
                R = Q.Transpose * A;
            }

            return(new[] { Q, R });
        }
Example #2
0
        public static double Dot(this RVector v1, RVector v2)
        {
            double r = 0;

            Parallel.For(0, v1.Length, i =>
            {
                r += v1[i] * v2[i];
            });
            return(r);
        }
Example #3
0
        public static RVector operator >(RVector c1, RVector c2)
        {
            var result = new RVector(c1.Length);

            Parallel.For(0, c1.Length, i =>
            {
                result[i] = Convert.ToDouble(c1[i] > c2[i]);
            });
            return(result);
        }
Example #4
0
        public static RVector operator -(RVector u, RVector v)
        {
            var x = new RVector(u.Length);

            Parallel.For(0, u.Length, i =>
            {
                x[i] = u[i] - v[i];
            });
            return(x);
        }
Example #5
0
        public static RVector Logistic(RVector vector)
        {
            var result = new RVector(vector.Length);

            for (int i = 0; i < vector.Length; i++)
            {
                result[i] = Logistic(vector[i]);
            }
            return(result);
        }
Example #6
0
        /// <summary>
        /// Uniform Random Vector
        /// </summary>
        /// <param name="numElements"></param>
        /// <returns></returns>
        public static RVector UniformRandromVector(int numElements)
        {
            var vector = new RVector(numElements);

            Parallel.For(0, numElements, i =>
            {
                vector[i] = GetRandomDouble();
            });
            return(vector);
        }
Example #7
0
        public static RVector operator *(double scalar, RVector v)
        {
            var newVector = new RVector(v.Length);

            Parallel.For(0, v.Length, i =>
            {
                newVector[i] = v[i] * scalar;
            });

            return(newVector);
        }
Example #8
0
        public static RealMatrix Outer(this RVector u, RVector v)
        {
            var A = new RealMatrix(u.Length, v.Length);

            Parallel.For(0, u.Length, i =>
                         Parallel.For(0, v.Length, j =>
            {
                A[i, j] = u[i] * v[j];
            }));
            return(A);
        }
Example #9
0
        public static RVector Ones(int size)
        {
            var v = new RVector(size);

            for (int i = 0; i < size; i++)
            {
                v[i] = 1;
            }

            return(v);
        }
Example #10
0
        /// <summary>
        /// Update a selection with a source vector
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="src"></param>
        /// <param name="length"></param>
        /// <param name="isVertical"></param>
        /// <param name="mPos"></param>
        /// <param name="nPos"></param>
        public static void Update(this RealMatrix matrix, RVector src, int length = 0, bool isVertical = true,
                                  int mPos = 0, int nPos = 0)
        {
            length = length == 0 ? src.Length : length;


            for (int i = 0; i < length; i++)
            {
                if (isVertical)
                {
                    matrix[mPos + i, nPos] = src[i];
                }
                else
                {
                    matrix[mPos, nPos + i] = src[i];
                }
            }
        }
Example #11
0
        /// <summary>
        /// Day dream - Reconstruct a randrom matrix (An interesting way of seeing strong features the machine has learnt)
        /// </summary>
        /// <param name="numberOfSamples">How many images/dreams</param>
        /// <returns>Array of Reconstructed dreams</returns>
        public double[][] DayDream(int numberOfSamples)
        {
            var data = RealMatrix.Ones(numberOfSamples, m_numVisibleElements + 1);

            data.Update(0, 1, Distributions.UniformRandromMatrixBool(1, m_numVisibleElements), 1);

            for (int i = 0; i < numberOfSamples; i++)
            {
                var visible            = data.Submatrix(i, 0, 1).ToVector();
                var hidden_activations = (visible * m_weights).ToVector();
                var hidden_probs       = ActivationFunctions.Logistic(hidden_activations);
                var hidden_states      = hidden_probs > RVector.Random(m_numHiddenElements + 1);
                hidden_states[0] = 1;

                var visible_activations = (hidden_states * m_weights.Transpose).ToVector();
                var visible_probs       = ActivationFunctions.Logistic(visible_activations);
                var visible_states      = visible_probs > RVector.Random(m_numVisibleElements + 1);
                data.Update(visible_states, 0, false, i, 0);
            }

            return(data.Submatrix(0, 1).ToArray());
        }