Java源码示例:org.apache.commons.math3.util.OpenIntToDoubleHashMap
示例1
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws DimensionMismatchException if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v)
throws DimensionMismatchException {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例2
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例3
/**
* Compute the sum of this matrix and {@code m}.
*
* @param m Matrix to be added.
* @return {@code this} + {@code m}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
public OpenMapRealMatrix add(OpenMapRealMatrix m) {
// safety check
MatrixUtils.checkAdditionCompatible(this, m);
final OpenMapRealMatrix out = new OpenMapRealMatrix(this);
for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
iterator.advance();
final int row = iterator.key() / columns;
final int col = iterator.key() - row * columns;
out.setEntry(row, col, getEntry(row, col) + iterator.value());
}
return out;
}
示例4
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例5
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例6
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws DimensionMismatchException if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v)
throws DimensionMismatchException {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例7
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例8
/**
* {@inheritDoc}
*
* @throws NumberIsTooLargeException if {@code m} is an
* {@code OpenMapRealMatrix}, and the total number of entries of the product
* is larger than {@code Integer.MAX_VALUE}.
*/
@Override
public RealMatrix multiply(final RealMatrix m)
throws DimensionMismatchException, NumberIsTooLargeException {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
示例9
/**
* {@inheritDoc}
*
* @throws NumberIsTooLargeException if {@code m} is an
* {@code OpenMapRealMatrix}, and the total number of entries of the product
* is larger than {@code Integer.MAX_VALUE}.
*/
@Override
public RealMatrix multiply(final RealMatrix m)
throws DimensionMismatchException, NumberIsTooLargeException {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
示例10
/**
* {@inheritDoc}
*
* @throws NumberIsTooLargeException if {@code m} is an
* {@code OpenMapRealMatrix}, and the total number of entries of the product
* is larger than {@code Integer.MAX_VALUE}.
*/
@Override
public RealMatrix multiply(final RealMatrix m)
throws DimensionMismatchException, NumberIsTooLargeException {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
示例11
/** {@inheritDoc} */
@Override
public RealMatrix multiply(final RealMatrix m) {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
// safety check
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
示例12
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws DimensionMismatchException if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v)
throws DimensionMismatchException {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例13
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws DimensionMismatchException if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v)
throws DimensionMismatchException {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例14
/**
* Optimized method to add two OpenMapRealVectors.
* It copies the larger vector, then iterates over the smaller.
*
* @param v Vector to add.
* @return the sum of {@code this} and {@code v}.
* @throws DimensionMismatchException if the dimensions do not match.
*/
public OpenMapRealVector add(OpenMapRealVector v)
throws DimensionMismatchException {
checkVectorDimensions(v.getDimension());
boolean copyThis = entries.size() > v.entries.size();
OpenMapRealVector res = copyThis ? this.copy() : v.copy();
Iterator iter = copyThis ? v.entries.iterator() : entries.iterator();
OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries;
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (randomAccess.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
示例15
/** {@inheritDoc} */
@Override
public RealMatrix multiply(final RealMatrix m) {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
// safety check
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
示例16
/**
* {@inheritDoc}
*
* @throws NumberIsTooLargeException if {@code m} is an
* {@code OpenMapRealMatrix}, and the total number of entries of the product
* is larger than {@code Integer.MAX_VALUE}.
*/
@Override
public RealMatrix multiply(final RealMatrix m)
throws DimensionMismatchException, NumberIsTooLargeException {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
示例17
public LeastAggregator(AggregationFunction function) {
this.sourceAttribute = function.getSourceAttribute();
mappingSize = sourceAttribute.getMapping().size();
if (mappingSize > AggregationFunction.MAX_MAPPING_SIZE) {
frequenciesMap = new OpenIntToDoubleHashMap();
} else {
frequencies = new double[mappingSize];
}
}
示例18
/**
* Optimized method to compute the dot product with an OpenMapRealVector.
* It iterates over the smallest of the two.
*
* @param v Cector to compute the dot product with.
* @return the dot product of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public double dotProduct(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean thisIsSmaller = entries.size() < v.entries.size();
Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator();
OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries;
double d = 0;
while(iter.hasNext()) {
iter.advance();
d += iter.value() * larger.get(iter.key());
}
return d;
}
示例19
/**
* Create from an array, specifying zero tolerance.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key];
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例20
/**
* Create from an array.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(Double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key].doubleValue();
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例21
/**
* Generic copy constructor.
*
* @param v Instance to copy from.
*/
public OpenMapRealVector(RealVector v) {
virtualSize = v.getDimension();
entries = new OpenIntToDoubleHashMap(0.0);
epsilon = DEFAULT_ZERO_TOLERANCE;
for (int key = 0; key < virtualSize; key++) {
double value = v.getEntry(key);
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例22
/**
* Postmultiply this matrix by {@code m}.
*
* @param m Matrix to postmultiply by.
* @return {@code this} * {@code m}.
* @throws MatrixDimensionMismatchException
* if the number of rows of {@code m} differ from the number of columns
* of this matrix.
*/
public OpenMapRealMatrix multiply(OpenMapRealMatrix m) {
// Safety check.
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
OpenMapRealMatrix out = new OpenMapRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
final int rightKey = m.computeKey(k, j);
if (m.entries.containsKey(rightKey)) {
final int outKey = out.computeKey(i, j);
final double outValue =
out.entries.get(outKey) + value * m.entries.get(rightKey);
if (outValue == 0.0) {
out.entries.remove(outKey);
} else {
out.entries.put(outKey, outValue);
}
}
}
}
return out;
}
示例23
/**
* Create from an array, specifying zero tolerance.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key];
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例24
/**
* Build a sparse matrix with the supplied row and column dimensions.
*
* @param rowDimension Number of rows of the matrix.
* @param columnDimension Number of columns of the matrix.
* @throws NotStrictlyPositiveException if row or column dimension is not
* positive.
* @throws NumberIsTooLargeException if the total number of entries of the
* matrix is larger than {@code Integer.MAX_VALUE}.
*/
public OpenMapRealMatrix(int rowDimension, int columnDimension)
throws NotStrictlyPositiveException, NumberIsTooLargeException {
super(rowDimension, columnDimension);
long lRow = rowDimension;
long lCol = columnDimension;
if (lRow * lCol >= Integer.MAX_VALUE) {
throw new NumberIsTooLargeException(lRow * lCol, Integer.MAX_VALUE, false);
}
this.rows = rowDimension;
this.columns = columnDimension;
this.entries = new OpenIntToDoubleHashMap(0.0);
}
示例25
/**
* Create from an array.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(Double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key].doubleValue();
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例26
/**
* Generic copy constructor.
*
* @param v Instance to copy from.
*/
public OpenMapRealVector(RealVector v) {
virtualSize = v.getDimension();
entries = new OpenIntToDoubleHashMap(0.0);
epsilon = DEFAULT_ZERO_TOLERANCE;
for (int key = 0; key < virtualSize; key++) {
double value = v.getEntry(key);
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例27
/**
* Optimized method to compute the dot product with an OpenMapRealVector.
* It iterates over the smallest of the two.
*
* @param v Cector to compute the dot product with.
* @return the dot product of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public double dotProduct(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean thisIsSmaller = entries.size() < v.entries.size();
Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator();
OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries;
double d = 0;
while(iter.hasNext()) {
iter.advance();
d += iter.value() * larger.get(iter.key());
}
return d;
}
示例28
/**
* Create from an array, specifying zero tolerance.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key];
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
示例29
/**
* Optimized method to compute the dot product with an OpenMapRealVector.
* It iterates over the smallest of the two.
*
* @param v Cector to compute the dot product with.
* @return the dot product of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public double dotProduct(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean thisIsSmaller = entries.size() < v.entries.size();
Iterator iter = thisIsSmaller ? entries.iterator() : v.entries.iterator();
OpenIntToDoubleHashMap larger = thisIsSmaller ? v.entries : entries;
double d = 0;
while(iter.hasNext()) {
iter.advance();
d += iter.value() * larger.get(iter.key());
}
return d;
}
示例30
/**
* Generic copy constructor.
*
* @param v Instance to copy from.
*/
public OpenMapRealVector(RealVector v) {
virtualSize = v.getDimension();
entries = new OpenIntToDoubleHashMap(0.0);
epsilon = DEFAULT_ZERO_TOLERANCE;
for (int key = 0; key < virtualSize; key++) {
double value = v.getEntry(key);
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}