下面列出了com.google.common.io.PatternFilenameFilter#no.uib.cipr.matrix.DenseVector 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see AbstractSolver#solve(AbstractMatrix, AbstractVector)
*/
@Override
public AbstractVector solve(AbstractMatrix m, AbstractVector b) {
if (m instanceof MtjMatrix && b instanceof MtjVector) {
DenseVector result = new DenseVector(m.getRowDimension());
no.uib.cipr.matrix.AbstractMatrix systemMatrix = ((MtjMatrix) m).getMatrix();
DenseVector bMtj = ((MtjVector) b).getVector();
CG cg = new CG(result);
try {
cg.solve(systemMatrix, bMtj, result);
} catch (IterativeSolverNotConvergedException e) {
throw new RuntimeException(e.getMessage());
}
return new MtjVector(result);
}
throw new UnsupportedOperationException();
}
@Test
public void testMultiply() {
double[][] tmpMatrix = { { 1, 2, 3 }, { 4, 5, 6 } };
double[] tmpVector = { 1, 2, 3 };
double[] resultExpected = { 14, 32 };
DenseMatrix denseMatrix = new DenseMatrix(tmpMatrix);
DenseVector denseVector = new DenseVector(tmpVector);
AbstractMatrix abstractMatrix = new MtjMatrix(denseMatrix);
AbstractVector abstractVector = new MtjVector(denseVector);
AbstractVector resultActual = abstractMatrix.multiply(abstractVector);
assertEquals(resultExpected.length, resultActual.getDimension());
for (int i = 0; i < resultActual.getDimension(); i++) {
assertEquals(resultExpected[i], resultActual.getEntry(i), Helper.TOLERANCE);
}
}
protected double calculateLogFieldLikelihood() {
makeIntervalsKnown();
double currentLike = 0;
DenseVector diagonal1 = new DenseVector(fieldLength);
DenseVector currentGamma = new DenseVector(popSizeParameter.getParameterValues());
SymmTridiagMatrix currentQ = getScaledWeightMatrix(precisionParameter.getParameterValue(0), lambdaParameter.getParameterValue(0));
currentQ.mult(currentGamma, diagonal1);
// currentLike += 0.5 * logGeneralizedDeterminant(currentQ) - 0.5 * currentGamma.dot(diagonal1);
currentLike += 0.5 * (fieldLength - 1) * Math.log(precisionParameter.getParameterValue(0)) - 0.5 * currentGamma.dot(diagonal1);
if (lambdaParameter.getParameterValue(0) == 1) {
currentLike -= (fieldLength - 1) / 2.0 * LOG_TWO_TIMES_PI;
} else {
currentLike -= fieldLength / 2.0 * LOG_TWO_TIMES_PI;
}
return currentLike;
}
protected double calculateLogFieldLikelihood() {
makeIntervalsKnown();
double currentLike = 0;
DenseVector diagonal1 = new DenseVector(fieldLength);
DenseVector currentGamma = new DenseVector(popSizeParameter.getParameterValues());
SymmTridiagMatrix currentQ = getScaledWeightMatrix(precisionParameter.getParameterValue(0), lambdaParameter.getParameterValue(0));
currentQ.mult(currentGamma, diagonal1);
// currentLike += 0.5 * logGeneralizedDeterminant(currentQ) - 0.5 * currentGamma.dot(diagonal1);
currentLike += 0.5 * (fieldLength - 1) * Math.log(precisionParameter.getParameterValue(0)) - 0.5 * currentGamma.dot(diagonal1);
if (lambdaParameter.getParameterValue(0) == 1) {
currentLike -= (fieldLength - 1) / 2.0 * LOG_TWO_TIMES_PI;
} else {
currentLike -= fieldLength / 2.0 * LOG_TWO_TIMES_PI;
}
return currentLike;
}
double getLogFieldLikelihood() {
checkIntervals(); // TODO Is this really necessary? Computation below does not appear to depend on intervals.
DenseVector diagonal1 = new DenseVector(fieldLength);
DenseVector currentGamma = getMeanAdjustedGamma();
double currentLike = handleMissingValues();
SymmTridiagMatrix currentQ = getScaledWeightMatrix(precisionParameter.getParameterValue(0), lambdaParameter.getParameterValue(0));
currentQ.mult(currentGamma, diagonal1);
currentLike += 0.5 * (fieldLength - 1) * Math.log(precisionParameter.getParameterValue(0)) - 0.5 * currentGamma.dot(diagonal1);
if (lambdaParameter.getParameterValue(0) == 1) {
currentLike -= (fieldLength - 1) / 2.0 * LOG_TWO_TIMES_PI;
} else {
currentLike -= fieldLength / 2.0 * LOG_TWO_TIMES_PI;
}
return currentLike;
}
public double computeEdgesWeight(Community c, List<Node> nodeList){
double totalWeight =0;
if(c.getChild1() == null){
DenseVector nodesC = c.getVertexIndexes();
for(VectorEntry ve : nodesC){
int id = (int)ve.get();
Set<Edge<Node>> edges = structure.getEdges(nodeList.get(id));
for(Edge e:edges){
if(e.getSource().equals(nodeList.get(id)))
totalWeight += e.getWeight();
}
}
}else{
computeEdgesWeight(c.getChild1(),nodeList);
computeEdgesWeight(c.getChild2(),nodeList);
}
return totalWeight;
}
public void explore(Community c){
if(c.getChild1() == null){
DenseVector nodesC = c.getVertexIndexes();
EDCoWEvent event = new EDCoWEvent();
for(VectorEntry ve : nodesC){
int id = (int)ve.get();
event.keywords.add(nodeList.get(id).getName());
event.setEpsylon(computeE(c));
event.setStartSlice(startSlice);
event.setEndSlice(endSlice);
}
events.add(event);
}else{
explore(c.getChild1());
explore(c.getChild2());
}
}
/**
* Sets up the preconditioner for the given matrix
*
* @param LU
* Matrix to use internally. For best performance, its non-zero
* pattern should conform to that of the system matrix
* @param tau
* Drop tolerance
* @param p
* Number of entries to keep on each row in of the factored
* matrix. This is in addition to the entries of the original
* matrix
*/
public ILUT(FlexCompRowMatrix LU, double tau, int p) {
if (!LU.isSquare())
throw new IllegalArgumentException(
"ILU only applies to square matrices");
this.LU = LU;
this.tau = tau;
this.p = p;
int n = LU.numRows();
lower = new ArrayList<IntDoubleEntry>(n);
upper = new ArrayList<IntDoubleEntry>(n);
y = new DenseVector(n);
diagind = new int[n];
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = 0; i < numRows; ++i) {
// Get row i
SparseVector row = LU.getRow(i);
int[] index = row.getIndex();
double[] data = row.getData();
// xi = bi - sum[j<i] Lij * xj
double sum = 0;
for (int j = 0; j < diagind[i]; ++j)
sum += data[j] * xd[index[j]];
xd[i] = bd[i] - sum;
}
return x;
}
@Override
public Vector transSolve(Vector b, Vector x) {
if (!(x instanceof DenseVector))
return super.transSolve(b, x);
x.set(b);
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i) {
// Get row i
SparseVector row = LU.getRow(i);
int[] index = row.getIndex();
double[] data = row.getData();
// At this stage, x[i] is known, so move it over to the right
// hand side for the remaining equations
for (int j = 0; j < diagind[i]; ++j)
xd[index[j]] -= data[j] * xd[i];
}
return x;
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i) {
// Get row i
SparseVector row = LU.getRow(i);
int[] index = row.getIndex();
int used = row.getUsed();
double[] data = row.getData();
// xi = (bi - sum[j>i] Uij * xj) / Uii
double sum = 0;
for (int j = diagind[i] + 1; j < used; ++j)
sum += data[j] * xd[index[j]];
xd[i] = (bd[i] - sum) / data[diagind[i]];
}
return x;
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = 0; i < numRows; ++i) {
// xi = bi - sum[j<i] Lij * xj
double sum = 0;
for (int j = rowptr[i]; j < diagind[i]; ++j)
sum += data[j] * xd[colind[j]];
xd[i] = bd[i] - sum;
}
return x;
}
@Override
public Vector transSolve(Vector b, Vector x) {
if (!(x instanceof DenseVector))
return super.transSolve(b, x);
x.set(b);
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i)
// At this stage, x[i] is known, so move it over to the right hand
// side for the remaining equations
for (int j = rowptr[i]; j < diagind[i]; ++j)
xd[colind[j]] -= data[j] * xd[i];
return x;
}
@Override
public Vector transMultAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMultAdd(alpha, x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData(), yd = ((DenseVector) y)
.getData();
// y = 1/alpha * y
y.scale(1. / alpha);
// y = A'x + y
for (int i = 0; i < numRows; ++i) {
SparseVector v = rowD[i];
int[] index = v.getIndex();
double[] data = v.getData();
int length = v.getUsed();
for (int j = 0; j < length; ++j)
yd[index[j]] += data[j] * xd[i];
}
// y = alpha*y = alpha * A'x + y
return y.scale(alpha);
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData(), yd = ((DenseVector) y)
.getData();
// y = 1/alpha * y
y.scale(1 / alpha);
// y = A*x + y
for (int i = 0; i < numColumns; ++i)
for (int j = columnPointer[i]; j < columnPointer[i + 1]; ++j)
yd[rowIndex[j]] += data[j] * xd[i];
// y = alpha*y = alpha*A*x + y
return y.scale(alpha);
}
@Override
public Vector transMult(Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMult(x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < numColumns; ++i) {
double dot = 0;
for (int j = columnPointer[i]; j < columnPointer[i + 1]; ++j)
dot += data[j] * xd[rowIndex[j]];
yd[i] = dot;
}
return y;
}
@Override
public Vector transMultAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMultAdd(alpha, x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < numColumns; ++i) {
double dot = 0;
for (int j = columnPointer[i]; j < columnPointer[i + 1]; ++j)
dot += data[j] * xd[rowIndex[j]];
yd[i] += alpha * dot;
}
return y;
}
@Override
public Vector solve(Vector b, Vector x) {
if (!(b instanceof DenseVector) || !(x instanceof DenseVector))
return super.solve(b, x);
double[] bd = ((DenseVector) b).getData();
double[] xd = ((DenseVector) x).getData();
for (int i = numRows - 1; i >= 0; --i) {
// xi = (bi - sum[j>i] Uij * xj) / Uii
double sum = 0;
for (int j = diagind[i] + 1; j < rowptr[i + 1]; ++j)
sum += data[j] * xd[colind[j]];
xd[i] = (bd[i] - sum) / data[diagind[i]];
}
return x;
}
@Override
public Vector transSolve(Vector b, Vector x) {
if (!(x instanceof DenseVector))
return super.transSolve(b, x);
x.set(b);
double[] xd = ((DenseVector) x).getData();
for (int i = 0; i < numRows; ++i) {
// Solve for the current entry
xd[i] /= data[diagind[i]];
// Move this known solution over to the right hand side for the
// remaining equations
for (int j = diagind[i] + 1; j < rowptr[i + 1]; ++j)
xd[colind[j]] -= data[j] * xd[i];
}
return x;
}
@Override
public Vector mult(Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.mult(x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
y.zero();
for (int i = 0; i < ind.length; ++i) {
int row = ind[i] < 0 ? -ind[i] : 0;
int column = ind[i] > 0 ? ind[i] : 0;
double[] locDiag = diag[i];
for (int j = 0; j < locDiag.length; ++j, ++row, ++column)
yd[row] += locDiag[j] * xd[column];
}
return y;
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < ind.length; ++i) {
int row = ind[i] < 0 ? -ind[i] : 0;
int column = ind[i] > 0 ? ind[i] : 0;
double[] locDiag = diag[i];
for (int j = 0; j < locDiag.length; ++j, ++row, ++column)
yd[row] += alpha * locDiag[j] * xd[column];
}
return y;
}
@Override
public Vector transMultAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.transMultAdd(alpha, x, y);
checkTransMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
for (int i = 0; i < ind.length; ++i) {
int row = ind[i] < 0 ? -ind[i] : 0;
int column = ind[i] > 0 ? ind[i] : 0;
double[] locDiag = diag[i];
for (int j = 0; j < locDiag.length; ++j, ++row, ++column)
yd[column] += alpha * locDiag[j] * xd[row];
}
return y;
}
@Override
public Vector multAdd(double alpha, Vector x, Vector y) {
if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
return super.multAdd(alpha, x, y);
checkMultAdd(x, y);
double[] xd = ((DenseVector) x).getData();
double[] yd = ((DenseVector) y).getData();
// y = 1/alpha * y
y.scale(1. / alpha);
// y = A*x + y
for (int i = 0; i < numColumns; ++i) {
SparseVector v = colD[i];
int[] index = v.getIndex();
double[] data = v.getData();
int length = v.getUsed();
for (int j = 0; j < length; ++j)
yd[index[j]] += data[j] * xd[i];
}
// y = alpha*y = alpha * A'x + y
return y.scale(alpha);
}
@Test
public void testBug27() {
double[] tfVector = {0.0, 0.5, 0.0, 0.4, 0.0};
DenseVector dense = new DenseVector(tfVector, false);
SparseVector vectorTF = new SparseVector(dense);
vectorTF.compact();
assertTrue(vectorTF.getUsed() == 2); // vectorTF.getUsed() returns 5
for (Iterator<VectorEntry> it = vectorTF.iterator(); it.hasNext();) {
VectorEntry ve = it.next();
int index = ve.index();
double value = ve.get();
assertTrue(tfVector[index] == value);
}
}
/**
* @see AbstractMatrix#multiply(AbstractVector)
*/
@Override
public AbstractVector multiply(AbstractVector vector) {
if (vector.getDimension() != this.matrix.numColumns()) {
throw new IllegalArgumentException("Dimension mismatch");
}
if (vector instanceof MtjVector) {
DenseVector v = ((MtjVector) vector).getVector();
DenseVector result = new DenseVector(this.matrix.numRows());
this.matrix.mult(v, result);
return new MtjVector(result);
}
throw new UnsupportedOperationException();
}
/**
* @see ApacheVector#subtract(AbstractVector)
*/
@Override
public AbstractVector subtract(AbstractVector v) {
if (v instanceof MtjVector) {
DenseVector tmp = ((MtjVector) v).getVector();
tmp.scale(-1);
tmp.add(this.vector);
return new MtjVector(tmp);
} else {
throw new UnsupportedOperationException();
}
}
/**
* @see ApacheVector#getSubVector(int, int)
*/
@Override
public AbstractVector getSubVector(int index, int n) {
DenseVector subVector = new DenseVector(n);
for (int i = index, j = 0; i < index + n; i++, j++) {
double value = this.vector.get(i);
subVector.set(j, value);
}
return new MtjVector(subVector);
}
@Test
public void testGetSubVector() {
double[] x = new double[] { 0, 1, 2, 3, 4, 5, 6 };
double[] expected = new double[] { 4, 5, 6 };
DenseVector vector = new DenseVector(x);
AbstractVector v = new MtjVector(vector);
AbstractVector resultActual = v.getSubVector(4, 3);
assertEquals(resultActual.getDimension(), expected.length);
for (int i = 0; i < resultActual.getDimension(); i++) {
assertEquals(expected[i], resultActual.getEntry(i), Helper.TOLERANCE);
}
}
@Test
public void testSetSubVector() {
double[] x = new double[] { 1, 2 };
double[] expected = new double[] { 0, 1, 2, 0 };
AbstractVector vActual = new MtjVector(4);
DenseVector tmp = new DenseVector(x);
AbstractVector subVector = new MtjVector(tmp);
vActual.setSubVector(1, subVector);
assertEquals(expected.length, vActual.getDimension());
for (int i = 0; i < 4; i++) {
assertEquals(expected[i], vActual.getEntry(i), Helper.TOLERANCE);
}
}
public SplineBasis(String name, Variable<Double> knotLocations, Variable<Double> knotValues, int degree) {
super(name);
this.knotLocations = knotLocations;
this.knotValues = knotValues;
addVariable(knotLocations);
addVariable(knotValues);
this.degree = degree;
updateBasis = true;
n = knotValues.getSize();
h = new double[n - 1];
deltaY = new double[n - 1];
hMatrix = new BandMatrix(n, 1, 1);
yByH = new DenseVector(n);
z = new DenseVector(n);
calculateBasis();
StringBuilder buffer = new StringBuilder();
buffer.append("Constructing spline basis:\n");
buffer.append("\tDegree: ").append(degree).append("\n");
buffer.append("\tRange: [").append(getLowerBound()).append(", ").append(getUpperBound()).append("\n");
Logger.getLogger("dr.math").info(buffer.toString());
}