Ejemplo n.º 1
0
        /*
         * Display
         */

        public static void Print(Matrix m)
        {
            for (int l = 0; l < m.Height(); l++)
            {
                Console.Write("[ " + centeredString(m.get(l, 0).ToString(), 6));
                for (int c = 1; c < m.Width(); c++)
                {
                    Console.Write(", " + centeredString(m.get(l, c).ToString(), 6));
                }
                Console.Write("]\n");
            }
            Console.Write("\n");
        }
Ejemplo n.º 2
0
        public static Matrix GibbsSampling(Matrix GoalFunc, int tryn)
        {
            Random r = new Random();

            int[] sample = new int[tryn];
            int point = r.Next(0, 19);
            int newpoint;

            for (int i = 0; i < tryn; i++)
            {
                sample[i] = point;
                newpoint = JumpPointGibbs(point, r, GoalFunc);
                double ttt = GoalFunc.get(0, newpoint) / (GoalFunc.get(0, newpoint) + GoalFunc.get(0, point));
                point = newpoint;
            }
            double[,] sumnum = new double[1, 20];
            for (int i = 0; i < 20; i++)
            {
                sumnum[0, i] = 0;
            }

            for (int i = 0; i < tryn; i++)
            {
                if (sample[i] == -1)
                {

                }
                else
                {
                    sumnum[0, sample[i]]++;
                }
            }

            Matrix getdist = new Matrix(sumnum);
            getdist.toProbabilty();

            return getdist;
        }
Ejemplo n.º 3
0
        public static Matrix Transpose(Matrix a)
        {
            Matrix result = new Matrix(a.Width(), a.Height());

            for (int i = 0; i < a.Height(); i++)
            {
                for (int j = 0; j < a.Width(); j++)
                {
                    result.set(j, i, a.get(i, j));
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static Matrix Map(Matrix m, Func <double, double> func)
        {
            Matrix result = new Matrix(m.Height(), m.Width());

            for (int i = 0; i < m.Height(); i++)
            {
                for (int j = 0; j < m.Width(); j++)
                {
                    result.set(i, j, func(m.get(i, j)));
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Add another matrix to the instance
        /// </summary>
        /// <param name="m">Matrix to add</param>
        /// <exception cref="MatrixException">Dimension not egual</exception>
        public void Add(Matrix m)
        {
            if (Width() != m.Width() || Height() != m.Height())
            {
                throw new MatrixException("Dimension not egual");
            }

            for (int c = 0; c < Width(); c++)
            {
                for (int l = 0; l < Height(); l++)
                {
                    _matrix[l, c] += m.get(l, c);
                }
            }
        }
Ejemplo n.º 6
0
        public static double Dot(Matrix a, Matrix b)
        {
            if (!(a.Height() == 1 && b.Width() == 1 && a.Width() == b.Height()))
            {
                throw new MatrixException("Dot product incompatible");
            }

            double result = 0;

            for (int i = 0; i < a.Width(); i++)
            {
                result += a.get(0, i) * b.get(i, 0);
            }

            return(result);
        }
Ejemplo n.º 7
0
        public static Matrix Sub(Matrix a, Matrix b)
        {
            if (a.Width() != b.Width() || a.Height() != b.Height())
            {
                throw new MatrixException("Dimension not egual");
            }

            Matrix m = new Matrix(a.Height(), a.Width());

            for (int l = 0; l < a.Height(); l++)
            {
                for (int c = 0; c < a.Width(); c++)
                {
                    m.set(l, c, a.get(l, c) - b.get(l, c));
                }
            }

            return(m);
        }
Ejemplo n.º 8
0
        public static Matrix Mult(Matrix a, Matrix b)
        {
            if (a.Width() != b.Height())
            {
                throw new MatrixException("Width is not egual to the Height (" + a.Width() + ", " + b.Height() + ")");
            }

            Matrix tmp = new Matrix(a.Height(), b.Width());

            for (int l = 0; l < tmp.Height(); l++)
            {
                for (int c = 0; c < tmp.Width(); c++)
                {
                    for (int k = 0; k < b.Height(); k++)
                    {
                        tmp.set(l, c, tmp.get(l, c) + a.get(l, k) * b.get(k, c));
                    }
                }
            }

            return(tmp);
        }
Ejemplo n.º 9
0
        public static int JumpPointGibbs(int point, Random r, Matrix GoalFunc)
        {
            RandomLi rl = new RandomLi(r);

            int[] gfsize = GoalFunc.getSize();
            double[] varprob = new double[gfsize[1]];

            for (int i = 0; i < gfsize[1]; i++)
            {
                varprob[i] = GoalFunc.get(new int[] { 0, i });
            }

            return rl.NextfromProbMass(varprob);
        }
Ejemplo n.º 10
0
        public static Matrix RejectionSampling(Matrix GoalFunc, int tryn)
        {
            Random r = new Random();

            int[] sample = new int[tryn];
            double rejectionprob;
            int point;

            for (int i = 0; i < tryn; i++)
            {
                point = r.Next(0, 19);
                rejectionprob = r.NextDouble();

                if (rejectionprob > GoalFunc.get(0, point))
                {
                    sample[i] = -1;
                }
                else
                {
                    sample[i] = point;
                }
            }

            double[,] sumnum = new double[1,20];
            for (int i = 0; i < 20; i++)
            {
                sumnum[0,i] = 0;
            }

            for (int i = 0; i < tryn; i++)
            {
                if (sample[i] == -1)
                {

                }
                else
                {
                    sumnum[0,sample[i]]++;
                }
            }

            Matrix getdist = new Matrix(sumnum);
            getdist.toProbabilty();

            return getdist;
        }