Пример #1
0
        private void TestTransponce()
        {
            MatrixSetUp provider = new MatrixSetUp(5, 3);

            double[,] U     = provider.U;
            double[,] trans = MyMatrix <double> .Transpose(U);

            Utils <double> .PrintMatrix(U);

            Console.WriteLine("trasponowana:\n");
            Utils <double> .PrintMatrix(trans);
        }
Пример #2
0
        public void TestAdding()
        {
            P = new double[, ] {
                { 0.1, 0.4, 0.7 }, { 0.2, 0.5, 0.8 }, { 0.3, 0.6, 0.9 }
            };
            U = new double[, ] {
                { 0.25, 0.35, 0.78 }, { 0.15, 0.45, 0.25 }, { 0.85, 0.1, 0.1 }
            };
            double[,]  Result = MyMatrix <double> .Add(U, P);

            Console.WriteLine("\n wynik dodowania");
            Utils <double> .PrintMatrix(Result);
        }
Пример #3
0
        private void TestMultiplication()
        {
            double[,] m1 = { { 0.4, 0.5, 0.6 } };
            double[,] m2 = { { 0.4 }, { 0.5 }, { 0.6 } };
            Utils <double> .PrintMatrix(m2);

            Utils <double> .PrintMatrix(m1);

            //double[,] sol = MyMatrix<double>.Multiplication(m2, m1);
            double[,] sol = MyMatrix <double> .Multiplication(m1, m2);

            Console.WriteLine("roziwzanie: \n");
            Utils <double> .PrintMatrix(sol);
        }
Пример #4
0
        private void TestColumnFromMatrix()
        {
            P = new double[, ] {
                { 0.93119636, 0.01215318, 0.82254304, 0.92704314, 0.72097256,
                  0.1119594, 0.05907673, 0.27337659, 0.51578453, 0.47299487 },
                { 0.1671686, 0.02328032, 0.64793332, 0.46310597, 0.98508579,
                  0.23390272, 0.34862754, 0.29751156, 0.81994987, 0.32293732 },
                { 0.72302848, 0.91165485, 0.70980305, 0.20125138, 0.33071352,
                  0.40941998, 0.6984816, 0.94986196, 0.52719633, 0.66722182 }
            };

            List <int> myL = new List <int>();

            myL.Add(4);
            myL.Add(6);
            double[,] M = MyMatrix <double> .GetMatrixFromOtherMatrixColumns(P, myL);

            Utils <double> .PrintMatrix(M);
        }
Пример #5
0
        public static double Calculate(int[,] Ratings, double[,] U, double[,] P, int d, double lambda)
        {
            double S_k = 0,       // Sum of (rating – U_u.T * P_p) ^ 2
                    S_u = 0,       // Sum of U columns squared norm
                    S_p = 0,       // Sum of P columns squared norm
                    result;

            double[,] U_T = MyMatrix<double>.Transpose(U);
            double[,] ratingsCalculated = MyMatrix<double>.Multiplication(U_T, P);
            for (int u = 0; u < U.GetLength(1); u++) //Ratings.GetLength(0)
            {
                for (int p = 0; p < P.GetLength(1); p++) //Ratings.GetLenght(1)
                {
                    if (Ratings[u, p] != 0)
                    {
                        S_k += Math.Pow((Ratings[u, p] - ratingsCalculated[u, p]), 2);
                    }
                }
            }

            for (int j = 0; j < P.GetLength(1); j++)
            {
                for (int i = 0; i < P.GetLength(0); i++)
                {
                    S_p += Math.Pow(P[i, j], 2);
                }
            }

            for (int j = 0; j < U.GetLength(1); j++)
            {
                for (int i = 0; i < U.GetLength(0); i++)
                {
                    S_u += Math.Pow(U[i, j], 2);
                }
            }
            result = S_k + lambda * (S_u + S_p);
            Console.WriteLine($"\n funkcja celu: {result}");
            return result;
        }
Пример #6
0
        private void StepForU(int u)
        {
            List <int> _I_u = FlatNonZeroInRow(u, Ratings);

            double[,] _regE = EyeMulDouble(d);
            MyMatrix <double> gauss = new MyMatrix <double>(d);

            //Liczymy _P_I_u_(czyli kolumny z macierzy P o indeksach w _I_u_)
            double[,] _P_I_u = MyMatrix <double> .GetMatrixFromOtherMatrixColumns(P, _I_u);

            double[,] _P_I_u_T = MyMatrix <double> .Transpose(_P_I_u);

            double[,] _A_u = MyMatrix <double> .Add(
                MyMatrix <double> .Multiplication(_P_I_u, _P_I_u_T),
                _regE);

            double[] _V_u = Count_V_u(_I_u, P, Ratings, u);
            //Jak już mamy wszystko policzone, możemy podstawić A_u oraz V_u do gaussa:
            gauss.A = _A_u;
            gauss.B = _V_u;
            gauss.ComputePG();
            double[] GaussSolution = gauss.Xgauss;
            U = InsertGaussColumn(u, U, GaussSolution);
        }
Пример #7
0
        private void StepForP(int p)
        {
            List <int> _I_p = FlatNonZeroInColumn(p, Ratings);

            double[,] _regE = EyeMulDouble(d);
            MyMatrix <double> gauss = new MyMatrix <double>(d);

            double[,] _U_I_p = MyMatrix <double> .GetMatrixFromOtherMatrixColumns(U, _I_p);

            double[,] _U_I_p_T = MyMatrix <double> .Transpose(_U_I_p);

            double[,] _B_u =
                MyMatrix <double> .Add(
                    MyMatrix <double> .Multiplication(_U_I_p, _U_I_p_T),
                    _regE
                    );

            double[] _W_u = Count_W_u(_I_p, U, Ratings, p);
            gauss.A = _B_u;
            gauss.B = _W_u;
            gauss.ComputePG();
            double[] GaussSolution = gauss.Xgauss;
            P = InsertGaussColumn(p, P, GaussSolution);
        }