/** * Creates a new matrix by copying the given set of column vectors. * * @param columns The column vectors to create a matrix from. Must all be the same dimensionality. * @return A new matrix whose columns are equal to the given set of columns. */ public MatrixType copyColumnVectors(final Collection<? extends Vectorizable> columns) { // Create the matrix. final int numRows = VectorUtil.safeGetDimensionality(CollectionUtil.getFirst(columns)); final int numColumns = columns.size(); final MatrixType result = this.createMatrix(numRows, numColumns); // Fill in the matrix with the columns. int columnIndex = 0; for (Vectorizable column : columns) { result.setColumn(columnIndex, column.convertToVector()); columnIndex++; } return result; }
/** * Creates a new matrix by copying the given set of row vectors. * * @param rows The row vectors to create a matrix from. Must all be the same dimensionality. * @return A new matrix whose rows are equal to the given set of rows. */ public MatrixType copyRowVectors(final Collection<? extends Vectorizable> rows) { // Create the matrix. final int numRows = rows.size(); final int numColumns = VectorUtil.safeGetDimensionality(CollectionUtil.getFirst(rows)); final MatrixType result = this.createMatrix(numRows, numColumns); // Fill in the matrix with the rows. int rowIndex = 0; for (Vectorizable row : rows) { result.setRow(rowIndex, row.convertToVector()); rowIndex++; } return result; }
/** * Creates a new square matrix whose number of rows and columns match the dimensionality of the * given vector. It also places the values of the vector on the diagonal of the matrix. * * @param diagonal The vector of diagonal values. * @return A new, square matrix with the diagonal elements equal to the elements of the given * vector. */ public MatrixType createDiagonal(final Vectorizable diagonal) { final Vector vector = diagonal.convertToVector(); final int dimensionality = vector.getDimensionality(); // Create the matrix. final MatrixType result = this.createMatrix(dimensionality, dimensionality); // Set the diagonal values. for (VectorEntry entry : vector) { final int i = entry.getIndex(); result.setElement(i, i, entry.getValue()); } return result; }