Created
March 15, 2017 10:22
-
-
Save myui/17c56e7ff4e367c2d8f511504dc68f1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package hivemall.matrix.sparse; | |
import hivemall.matrix.AbstractMatrix; | |
import hivemall.matrix.VectorProcedure; | |
import hivemall.matrix.builders.MatrixBuilder; | |
import hivemall.utils.lang.ArrayUtils; | |
import java.util.Arrays; | |
import javax.annotation.Nonnegative; | |
import javax.annotation.Nonnull; | |
public final class COOMatrix extends AbstractMatrix { | |
private boolean rowMajor; | |
@Nonnull | |
private final int[] indicies; | |
@Nonnull | |
private final int[] rows; | |
@Nonnull | |
private final int[] cols; | |
@Nonnull | |
private final double[] values; | |
@Nonnegative | |
private final int numRows; | |
@Nonnegative | |
private final int numColumns; | |
@Nonnegative | |
private final int nnz; | |
public COOMatrix(boolean rowMajor, @Nonnull int[] indicies, @Nonnull int[] rows, | |
@Nonnull int[] cols, @Nonnull double[] values, @Nonnegative int numRows, | |
@Nonnegative int numColumns) { | |
super(); | |
this.rowMajor = rowMajor; | |
this.indicies = indicies; | |
this.rows = rows; | |
this.cols = cols; | |
this.values = values; | |
this.numRows = numRows; | |
this.numColumns = numColumns; | |
this.nnz = values.length; | |
} | |
@Override | |
public boolean isSparse() { | |
return true; | |
} | |
@Override | |
public boolean isRowMajorMatrix() { | |
return rowMajor; | |
} | |
@Override | |
public boolean isColumnMajorMatrix() { | |
return rowMajor == false; | |
} | |
@Override | |
public boolean readOnly() { | |
return true; | |
} | |
@Override | |
public boolean swappable() { | |
return false; | |
} | |
@Override | |
public int nnz() { | |
return nnz; | |
} | |
@Override | |
public int numRows() { | |
return numRows; | |
} | |
@Override | |
public int numColumns() { | |
return numColumns; | |
} | |
@Override | |
public int numColumns(int row) { | |
checkRowIndex(row, numRows); | |
final int firstColIndex = indicies[row]; | |
final int numCols; | |
if ((row + 1) < numRows) {// has next row | |
numCols = indicies[row + 1] - firstColIndex; | |
} else { | |
numCols = values.length - firstColIndex; | |
} | |
return numCols; | |
} | |
@Override | |
public double[] getRow(final int index) { | |
int numCols = numColumns(index); | |
final double[] row = new double[numCols]; | |
for (int i = indicies[index], j = 0; j < numCols; i++, j++) { | |
row[j] = values[i]; | |
} | |
return row; | |
} | |
@Override | |
public double[] getRow(final int index, @Nonnull final double[] dst) { | |
Arrays.fill(dst, 0.d); | |
int numCols = numColumns(index); | |
final int size = Math.min(dst.length, numCols); | |
for (int i = indicies[index], j = 0; j < size; i++, j++) { | |
dst[j] = values[i]; | |
} | |
return dst; | |
} | |
@Override | |
public double get(final int row, final int col, final double defaultValue) { | |
checkIndex(row, col, numRows, numColumns); | |
final int firstColIndex = indicies[row]; | |
final int numCols; | |
if ((row + 1) < numRows) {// has next row | |
numCols = indicies[row + 1] - firstColIndex; | |
} else { | |
numCols = values.length - firstColIndex; | |
} | |
final int index = ArrayUtils.indexOf(cols, col, firstColIndex, endIndex); | |
return 0; | |
} | |
@Override | |
public void set(final int row, final int col, final double value) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public double getAndSet(final int row, final int col, final double value) { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public void swap(final int row1, final int row2) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachInRow(final int row, @Nonnull final VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachNonZeroInRow(final int row, @Nonnull final VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachInColumn(final int col, @Nonnull final VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachInNonZeroColumn(final int col, @Nonnull final VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public MatrixBuilder builder() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package hivemall.matrix.builders; | |
import hivemall.matrix.sparse.COOMatrix; | |
import hivemall.utils.collections.lists.FloatArrayList; | |
import hivemall.utils.collections.lists.IntArrayList; | |
import hivemall.utils.lang.ArrayUtils; | |
import java.util.Arrays; | |
import javax.annotation.Nonnegative; | |
import javax.annotation.Nonnull; | |
public final class COOMatrixBuilder extends MatrixBuilder { | |
@Nonnull | |
private final IntArrayList rows; | |
@Nonnull | |
private final IntArrayList cols; | |
@Nonnull | |
private final FloatArrayList values; | |
private final boolean rowMajor; | |
private int row; | |
private int maxNumColumns; | |
public COOMatrixBuilder() { | |
this(true); | |
} | |
public COOMatrixBuilder(boolean rowMajor) { | |
super(); | |
this.rows = new IntArrayList(1024); | |
this.cols = new IntArrayList(1024); | |
this.values = new FloatArrayList(1024); | |
this.rowMajor = rowMajor; | |
this.row = 0; | |
this.maxNumColumns = 0; | |
} | |
@Override | |
public MatrixBuilder nextRow() { | |
row++; | |
return this; | |
} | |
@Override | |
public MatrixBuilder nextColumn(@Nonnegative final int col, final double value) { | |
rows.add(row); | |
cols.add(col); | |
values.add((float) value); | |
this.maxNumColumns = Math.max(col + 1, maxNumColumns); | |
return this; | |
} | |
@Override | |
public COOMatrix buildMatrix() { | |
if (rows.isEmpty() || cols.isEmpty()) { | |
throw new IllegalStateException("No element in the matrix"); | |
} | |
final int[] rowsArray = rows.toArray(true); | |
final int[] colsArray = cols.toArray(true); | |
final float[] valuesArray = values.toArray(true); | |
final int index[]; | |
if (rowMajor) { | |
index = new int[row + 1]; | |
Arrays.fill(index, -1); | |
int prevRow = rowsArray[0]; | |
index[prevRow] = 0; | |
for (int i = 1; i < rowsArray.length; i++) { | |
int currRow = rowsArray[i]; | |
if (currRow != prevRow) { | |
index[currRow] = i; | |
} | |
prevRow = currRow; | |
} | |
index[row] = rowsArray.length; | |
} else { | |
// convert to column major | |
ArrayUtils.sort(colsArray, rowsArray, valuesArray); | |
index = new int[maxNumColumns + 1]; | |
Arrays.fill(index, -1); | |
int prevCol = colsArray[0]; | |
index[prevCol] = 0; | |
for (int j = 0; j < colsArray.length; j++) { | |
int currCol = colsArray[j]; | |
if (currCol != prevCol) { | |
index[currCol] = j; | |
} | |
prevCol = currCol; | |
} | |
index[maxNumColumns] = colsArray.length; | |
} | |
// sort by row/column major | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment