All Downloads are FREE. Search and download functionalities are using the official Maven repository.

datasets.DenseMatrixSet Maven / Gradle / Ivy

The newest version!
package datasets;

import datastructs.I2DDataSet;
import datastructs.IVector;
import datastructs.RowType;
import datastructs.RowBuilder;
import parallel.partitioners.IPartitionPolicy;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.Row;
import tech.tablesaw.api.Table;

import java.util.*;

/**
 * Represents a dense matrix set.
 */
public class DenseMatrixSet implements I2DDataSet> {

    /**
     * Constructor
     *
     * @param rowType The row type
     * @param rowBuilder The row builder
     */
    public DenseMatrixSet(RowType.Type rowType, RowBuilder rowBuilder){
        this.rowType = rowType;
        this.rowBuilder = rowBuilder;
    }

    /**
     * Constructor
     *
     * @param rowType The row type
     * @param builder The row builder
     * @param n N
     * @param m M
     * @param val The value
     */
    public DenseMatrixSet(RowType.Type rowType, RowBuilder builder, int m, int n, T val){
        this(rowType, builder);
        this.createInternal(m ,n);
        for(int i=0; i other){
        this.initializeFrom(other);
    }

    /**
     * Copy this matrix
     */
    @Override
    public I2DDataSet> copy(){

        return new DenseMatrixSet(this);
    }

    /**
     * Create a new matrix
     */
    @Override
    public I2DDataSet> create(int m, int n){

        //DenseMatrix matrix = new DenseMatrix();
        this.createInternal(m, n);
        return this;
    }

    /**
     * How many rows the dataset has
     */
    @Override
    public final int m(){return this.data.size();}

    /**
     * How many columns the dataset has
     */
    @Override
    public final int n(){return this.data.get(0).size();}

    /**
     * Initialize the matrix from the given Table data set
     *
     * @param table A data set in tablesaw format
     */
    public void initializeFrom(final Table table){

        if(table == null){
            throw new IllegalArgumentException("Input Table should not be null");
        }

        // how many rows and columns
        int m = table.rowCount();
        int n = table.columnCount();

        this.createInternal(m, n);

        int rowCounter = 0;
        for (Row row: table ) {

            IVector vecRow = this.rowBuilder.build(this.rowType, row.columnCount()); //).create(row.columnCount()); //new Vector(row.columnCount());
            vecRow.set(row);
            this.set(rowCounter++, vecRow);
        }
    }

    /**
     * Initialize the matrix from the given DenseMatrix
     *
     * @param other The given dense matrix
     */
    public void initializeFrom(final DenseMatrixSet other){

        if(other == null){
            throw new IllegalArgumentException("Input DenseMatrix should not be null");
        }

        this.createInternal(other.m(), other.n());

        for (int rowIdx = 0; rowIdx < other.m(); rowIdx++) {


            IVector vecRow = this.rowBuilder.build(this.rowType, other.n()); //).create(other.n());  //new Vector(other.n());
            vecRow.set(other.getRow(rowIdx));
            this.set(rowIdx, vecRow);
        }
    }

    /**
     * Given the number of columns to include and the column indices
     * create a submatrix that has all the rows and columns specified
     */
    @Override
    public final  void getSubMatrix(E[][] subMatix, int numColsToInclude, int... includeCols){

        //T[][] subMatrix = new T[this.m()][numColsToInclude];

        for(int i=0; i col = getColumn(column);

        for(int i=0; i vec = this.data.get(i);
            vec.resize(vec.size() + 1);

            vec.set(vec.size()-1, col.get(i));
        }
    }

    /**
     * Set the (i,j) entry of the matrix
     *
     * @param i The i-th entry
     * @param j The j-th entry
     * @param value The value of the entry
     */
    public final void set(int i, int j, T value){

        if( i >= m() || i < 0 ){
            throw new IllegalArgumentException("Invalid row index");
        }

        if( j >= n() || j < 0 ){
            throw new IllegalArgumentException("Invalid column index");
        }

        this.data.get(i).set(j, value);
    }

    /**
     * Set the i-th row
     *
     * @param i The number of the row
     * @param value The value of the vector
     */
    public final void set(int i, IVector value){

        if( i >= m() || i < 0 ){
            throw new IllegalArgumentException("Invalid row index");
        }

        if(value.size() != this.n()){
            throw new IllegalArgumentException("Invalid number of columns");
        }

        this.data.get(i).set(value);
    }

    /**
     * Set the i-th row
     *
     * @param i The number of the row
     * @param value The values
     */
    public final void set(int i, Double... value){

       this.set(i, (IVector) this.rowBuilder.build(this.rowType, this.m(), value)); //.create(this.m() , value)); //new Vector(value));
    }

    /**
     * Set the c-th column from the DoubleColumn data
     *
     * @param c The number of the column
     * @param col The data
     */
    public final void setColumn(int c, DoubleColumn col){

        if(col.size() != this.m()){
            throw new IllegalArgumentException("Column size not equal to the number of rows");
        }

        for(int i=0; i row =  this.data.get(i);
            for(int j=0; j col){

        if(col.size() != this.m()){
            throw new IllegalArgumentException("Column size not equal to the number of rows");
        }

        for(int i=0; i getColumn(int column){

        if(column <0 || column >= this.n()){
            throw new IllegalArgumentException("Invalid column index: "+column+" should be in [0,"+this.n()+")");
        }

        IVector columnVals = this.rowBuilder.build(this.rowType, this.m()); //.create(this.m() , 0.0); //new Vector(this.m(), 0.0);

        for(int i=0; i getRow(int r){

        if( r >= m() || r < 0 ){
            throw new IllegalArgumentException("Invalid row index");
        }

        return this.data.get(r);
    }

    public final T getEntry(int i, int j){

        if( i >= m() || i < 0 ){
            throw new IllegalArgumentException("Invalid row index");
        }

        if( j >= n() || j < 0 ){
            throw new IllegalArgumentException("Invalid column index");
        }

        return this.data.get(i).get(j);
    }

    private final void createInternal(int m, int n){

        if(m <= 0 || n<= 0){
            throw new IllegalArgumentException("Cannot create a matrix with zero rows or columns");
        }

        this.data = new ArrayList>(m);

        for(int i=0; i row = this.rowBuilder.build(this.rowType, n); //.create(n ,val); //new Vector(n, val);
            this.data.add(row);
        }
    }

    /**
     * Exchange the i-th row with the j-th row
     */
    @Override
    public void exchangeRows(int i, int k){

        if( (i>=this.m() || k>=this.m()) || (i < 0 || k < 0)){
            throw new IllegalArgumentException("Invalid row index given");
        }

        // exchange
        IVector tmp = this.data.get(i);
        IVector next = this.data.get(k);
        this.data.set(i, next);
        this.data.set(k, tmp);
    }

    /**
     * Set the partition policy for this matrix
     *
     * @param policy The policy
     */
    public void setPartitionPolicy(IPartitionPolicy policy){
        this.partitionePolicy = policy;
    }

    /**
     * Returns the partition policy
     */
    @Override
    public IPartitionPolicy getPartitionPolicy(){
        return this.partitionePolicy;
    }

    RowType.Type rowType;
    RowBuilder rowBuilder;
    private ArrayList> data = null;
    IPartitionPolicy partitionePolicy = null;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy