Click or drag to resize

IndexPartitionCreate Method (DoubleMatrix)

Creates a partition of positions in a collection of Double elements by aggregating those positions occupied by a same element.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static IndexPartition<double> Create(
	DoubleMatrix elements
)

Parameters

elements
Type: Novacta.AnalyticsDoubleMatrix
The collection of elements whose positions are to be partitioned.

Return Value

Type: IndexPartitionDouble
The partition of element positions in the specified collection.
Exceptions
ExceptionCondition
ArgumentNullExceptionelements is null.
Examples

In the following example, the row indexes of a matrix are partitioned by the contents of its first column. Each part is identified by a value, the part identifier, and contains the indexes of the rows in which the identifier is positioned in the first column.

Partitioning the rows of a matrix by the contents of one of its columns
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class IndexPartitionExample0  
    {
        public void Main()
        {
            // Create a matrix.
            var data = new double[18] {
                0,0,1,
                0,0,1,
                0,1,0,
                0,1,0,
                1,0,0,
                1,0,0
            }; 
            var matrix = DoubleMatrix.Dense(6, 3, data, StorageOrder.RowMajor);

            // Partition the matrix row indexes by the contents of column 0:
            // a part is created for each distinct value in column 0.
            var partition = IndexPartition.Create(matrix[":", 0]);

            // Each part is identified by its corresponding value and contains
            // the indexes of the rows in which the identifier
            // is positioned in column 0.
            Console.WriteLine();
            foreach (var identifier in partition.Identifiers) {
                Console.WriteLine("Part identifier: {0}", identifier);
                Console.WriteLine("     indexes: {0}", partition[identifier]);
                Console.WriteLine();
            }
        }
    }
}

// Executing method Main() produces the following output:
// 
// 
// Part identifier: 0
//      indexes: 0, 1, 2, 3
// 
// Part identifier: 1
//      indexes: 4, 5

In the following example, the linear indexes of a matrix are partitioned by the sign of its entries. The part corresponding to zero entries is identified by zero, the part corresponding to positive entries by 1, and the part of negative entries by -1.

Partitioning the linear indexes of a matrix by the sign of its entries
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class IndexPartitionExample2  
    {
        public void Main()
        {
            // Create a matrix.
            var data = new double[8] {
                0, 1,-2,-3,
                0,-1, 2, 3
            };
            var matrix = DoubleMatrix.Dense(2, 4, data, StorageOrder.RowMajor);

            // Check the sign of its entries.
            var signs = DoubleMatrix.Dense(matrix.NumberOfRows, matrix.NumberOfColumns);
            for (int i = 0; i < matrix.Count; i++) {
                signs[i] = Math.Sign(matrix[i]);
            }

            // Partition the matrix linear indexes by the sign of each entry.
            var partition = IndexPartition.Create(signs);

            // The partition contains three parts, the zero part, identified by 0,
            // the negative part (identified by -1), and the positive one 
            // (identified by 1).
            Console.WriteLine();
            foreach (var identifier in partition.Identifiers) {
                Console.WriteLine("Part identifier: {0}", identifier);
                Console.WriteLine("     indexes: {0}", partition[identifier]);
                Console.WriteLine();
            }
        }
    }
}

// Executing method Main() produces the following output:
// 
// 
// Part identifier: -1
//      indexes: 3, 4, 6
// 
// Part identifier: 0
//      indexes: 0, 1
// 
// Part identifier: 1
//      indexes: 2, 5, 7

See Also