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); }
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; }
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); }
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); }