Sparse representation of a boolean matrix, using binary search (memory efficient)
Fast row-wise access is possible. Indexes are zero-based.
Inheritance: IBooleanMatrix
        public void TestCreateMatrix()
        {
            var matrix = new SparseBooleanMatrixStatic();

            var other_matrix = matrix.CreateMatrix(2, 2);
            Assert.IsInstanceOfType(matrix.GetType(), other_matrix);
        }
		/// <summary>Get the transpose of the matrix, i.e. a matrix where rows and columns are interchanged</summary>
		/// <returns>the transpose of the matrix</returns>
		public IMatrix<bool> Transpose()
		{
			var transpose = new SparseBooleanMatrixStatic();
			for (int i = 0; i < row_list.Count; i++)
				foreach (int j in this[i])
					transpose[j, i] = true;
			return transpose;
		}
        public void TestGetEntriesByRow()
        {
            var matrix = new SparseBooleanMatrixStatic();
            var row = new int[] { 1, 4 };
            for (int i = 0; i < 5; i++)
                if (i != 2 && i !=3)
                    matrix[i] = row;

            Assert.AreEqual(2, matrix.GetEntriesByRow(0).Count);
            Assert.AreEqual(2, matrix.GetEntriesByRow(1).Count);
            Assert.AreEqual(0, matrix.GetEntriesByRow(2).Count);
            Assert.AreEqual(0, matrix.GetEntriesByRow(3).Count);
            Assert.AreEqual(2, matrix.GetEntriesByRow(4).Count);
        }
        public void TestIsSymmetric()
        {
            var matrix = new SparseBooleanMatrixStatic();
            Assert.IsTrue(matrix.IsSymmetric);

            var row0  = new int[0];
            var row1  = new int[] { 1 };
            var row12 = new int[] { 1, 2 };

            matrix[1] = row1;
            Assert.IsTrue(matrix.IsSymmetric);

            matrix[2] = row1;
            Assert.IsFalse(matrix.IsSymmetric);

            matrix[1] = row12;
            Assert.IsTrue(matrix.IsSymmetric);

            matrix[2] = row0;
            Assert.IsFalse(matrix.IsSymmetric);
        }
        public void TestTranspose()
        {
            // contruct the matrix
            var matrix = new SparseBooleanMatrixStatic();
            var row14 = new int[] { 1, 4 };
            for (int i = 0; i < 7; i++)
                if (i != 2 && i != 4)
                    matrix[i] = row14;
            var row25 = new int[] { 2, 5 };
            var row3 = new int[] { 3 };
            matrix[2] = row25;
            matrix[4] = row3;

            // transpose the matrix
            var transposed_matrix = (IBooleanMatrix) matrix.Transpose();
            // test the transposed matrix
            Assert.IsTrue(transposed_matrix[1,0]);
            Assert.IsTrue(transposed_matrix[4, 6]);
            Assert.IsFalse(transposed_matrix[3, 1]);
            Assert.IsFalse(transposed_matrix[5, 4]);
        }
        public void TestOverlapCount()
        {
            var row25 = new int[] { 2, 5 };
            var row36 = new int[] { 3, 6 };
            var row15 = new int[] { 1, 5 };
            var matrix = new SparseBooleanMatrixStatic();
            matrix[2] = row25;
            matrix[4] = row36;
            matrix[5] = row15;

            var overlapMatrix = new SparseBooleanMatrix();
            overlapMatrix[2, 1] = true;
            overlapMatrix[2, 5] = true; // same entry
            overlapMatrix[4, 4] = true;
            overlapMatrix[4, 6] = true; // same entry
            overlapMatrix[5, 2] = true;
            overlapMatrix[5, 5] = true; // same entry

            Assert.AreEqual(3, matrix.Overlap(overlapMatrix));
        }
        public void TestNumEntriesByColumn()
        {
            var matrix = new SparseBooleanMatrixStatic();
            var row = new int[] { 1, 4 };
            for (int i = 0; i < 5; i++)
                if (i != 2 && i !=3)
                    matrix[i] = row;

            Assert.AreEqual(0, matrix.NumEntriesByColumn(0));
            Assert.AreEqual(3, matrix.NumEntriesByColumn(1));
            Assert.AreEqual(0, matrix.NumEntriesByColumn(2));
            Assert.AreEqual(0, matrix.NumEntriesByColumn(3));
            Assert.AreEqual(3, matrix.NumEntriesByColumn(4));
        }
 public void TestNumberOfRows()
 {
     var matrix = new SparseBooleanMatrixStatic();
     var row = new int[] { 1, 4 };
     for (int i = 0; i < 5; i++)
         if (i != 2 && i !=3)
             matrix[i] = row;
     Assert.AreEqual(5, matrix.NumberOfRows);
 }
        public void TestNonEmptyRows()
        {
            var matrix = new SparseBooleanMatrixStatic();
            var row = new int[] { 1, 4 };
            for (int i = 0; i < 5; i++)
                if (i != 2 && i !=3)
                    matrix[i] = row;

            Assert.IsTrue(matrix[0, 1]);
            var nonEmptyRows = matrix.NonEmptyRows;
            Assert.AreEqual(3, nonEmptyRows.Count);
        }
        public void TestNonEmptyRowIDs()
        {
            var matrix = new SparseBooleanMatrixStatic();
            var row = new int[] { 1, 4 };
            for (int i = 0; i < 5; i++)
                if (i != 2 && i !=3)
                    matrix[i] = row;

            Assert.AreEqual(3, matrix.NonEmptyRowIDs.Count);

            var rowIDs = matrix.NonEmptyRowIDs;
            var rowIDsEnum = rowIDs.GetEnumerator();
            rowIDsEnum.MoveNext();
            Assert.AreEqual(0, rowIDsEnum.Current);
            rowIDsEnum.MoveNext();
            Assert.AreEqual(1, rowIDsEnum.Current);
            rowIDsEnum.MoveNext();
            Assert.AreEqual(4, rowIDsEnum.Current);
            Assert.IsFalse(rowIDsEnum.MoveNext());
        }
        public void TestNonEmptyColumnIDs()
        {
            var matrix = new SparseBooleanMatrixStatic();
            var row = new int[] { 1, 4 };
            for (int i = 0; i < 5; i++)
                if (i != 2 && i !=3)
                    matrix[i] = row;

            Assert.AreEqual(2, matrix.NonEmptyColumnIDs.Count);
        }
 /// <summary>Get the transpose of the matrix, i.e. a matrix where rows and columns are interchanged</summary>
 /// <returns>the transpose of the matrix</returns>
 public IMatrix<bool> Transpose()
 {
     var transpose = new SparseBooleanMatrixStatic();
     for (int i = 0; i < row_list.Count; i++)
         foreach (int j in this[i])
             transpose[j, i] = true;
     return transpose;
 }
Beispiel #13
0
    static IPosOnlyFeedback CreateFeedback(IRatings ratings, double threshold)
    {
        SparseBooleanMatrixStatic user_item_matrix = new SparseBooleanMatrixStatic();

        for (int u = 0; u <= ratings.MaxUserID; u++)
        {
            var items = new List<int>();

            foreach (int index in ratings.ByUser[u])
                if (ratings[index] >= threshold)
                    items.Add(ratings.Items[index]);

            user_item_matrix[u] = items.ToArray();
        }
        var feedback = new PosOnlyFeedback<SparseBooleanMatrixStatic>(user_item_matrix);

        Console.Error.WriteLine("{0} ratings > {1}", feedback.Count, threshold);

        return feedback;
    }