if (entity_type !=0 && entity_type != 1)
throw new IllegalArgumentException("entity type must be either USER or ITEM, not " + entity_type);
ArrayList<ArrayList<Integer>> ratings_by_other_entity = (entity_type == 0) ? ratings.ByItem() : ratings.ByUser();
SparseMatrix freqs = new SparseMatrix(num_entities, num_entities);
SparseMatrix_d i_sums = new SparseMatrix_d(num_entities, num_entities);
SparseMatrix_d j_sums = new SparseMatrix_d(num_entities, num_entities);
SparseMatrix_d ij_sums = new SparseMatrix_d(num_entities, num_entities);
SparseMatrix_d ii_sums = new SparseMatrix_d(num_entities, num_entities);
SparseMatrix_d jj_sums = new SparseMatrix_d(num_entities, num_entities);
for(int i1=0;i1<ratings_by_other_entity.size();i1++){
ArrayList<Integer> other_entity_ratings = ratings_by_other_entity.get(i1);
for (int i = 0; i < other_entity_ratings.size(); i++)
{
Integer index1 = other_entity_ratings.get(i);
int x = (entity_type == 0) ? ratings.GetUsers().get(index1) : ratings.GetItems().get(index1);
// update pairwise scalar product and frequency
for (int j = i + 1; j < other_entity_ratings.size(); j++)
{
Integer index2 = other_entity_ratings.get(j);
int y = (entity_type == 0) ? ratings.GetUsers().get(index2) : ratings.GetItems().get(index2);
double rating1 = ratings.GetValues(index1);
double rating2 = ratings.GetValues(index2);
// update sums
if (x < y)
{
freqs.setLocation(x, y, freqs.getLocation1(x, y)+1);
i_sums.setLocation(x, y,i_sums.getLocation1(x, y)+rating1);
j_sums.setLocation(x, y, j_sums.getLocation1(x, y)+rating2);
ij_sums.setLocation(x, y,ij_sums.getLocation1(x, y)+rating1*rating2);
ii_sums.setLocation(x, y, ii_sums.getLocation1(x, y)+rating1*rating1);
jj_sums.setLocation(x, y, jj_sums.getLocation1(x, y)+rating2*rating2);
}
else
{
freqs.setLocation(y, x, freqs.getLocation1(y, x)+1);
i_sums.setLocation(y, x, i_sums.getLocation1(y, x)+rating1);
j_sums.setLocation(y, x, j_sums.getLocation1(y, x)+rating2);
ij_sums.setLocation(y, x, ij_sums.getLocation1(y, x)+rating1*rating2);
ii_sums.setLocation(y, x, ii_sums.getLocation1(y, x)+rating1*rating1);
jj_sums.setLocation(y, x, jj_sums.getLocation1(y, x)+rating2*rating2);
}
}
}
}
for (int i = 0; i < num_entities; i++)
this.setLocation(i, i, 1);
List<Tupel<Integer,Integer>> elementi=freqs.NonEmptyEntryIDs();
// fill the entries with interactions
for (int i1=0;i1<elementi.size();i1++)
{
Tupel<Integer,Integer> par=elementi.get(i1);
int i=par.getFirst();
int j=par.getSecond();
int n = freqs.getLocation(i, j);
if (n < 2)
{
this.setLocation(i, j, 0);