本文整理汇总了Java中cern.colt.matrix.DoubleMatrix2D.assign方法的典型用法代码示例。如果您正苦于以下问题:Java DoubleMatrix2D.assign方法的具体用法?Java DoubleMatrix2D.assign怎么用?Java DoubleMatrix2D.assign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cern.colt.matrix.DoubleMatrix2D
的用法示例。
在下文中一共展示了DoubleMatrix2D.assign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: logMatrixMult
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
public static void logMatrixMult(DoubleMatrix2D result, DoubleMatrix2D A, DoubleMatrix2D B, DoubleMatrix1D ri, boolean noMatrixMult) {
DoubleDoubleFunction sumFunc = new SumFunc();
if (noMatrixMult)
result.assign(B);
else {
for (int i = 0; i < A.rows(); i++) {
for (int j = 0; j < B.columns(); j++) {
double value = LOG0;
for (int k = 0; k < B.rows(); k++) {
value = logSumExp(value, A.get(i,k)+B.get(k, j));
}
result.set(i, j, value);
}
}
}
for (int i = 0; i < A.rows(); i++) {
result.viewRow(i).assign(ri, sumFunc);
}
}
示例2: doubleTest8
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
*/
public static void doubleTest8() {
int rows = 2;
int columns = 3; // make a 4*5 matrix
DoubleMatrix2D master = Factory2D.ascending(rows,columns);
//master.assign(1); // set all cells to 1
System.out.println("\n"+master);
//master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
//System.out.println("\n"+master);
DoubleMatrix2D view1 = master.viewDice();
System.out.println("view1="+view1);
DoubleMatrix2D view2 = view1.viewDice();
System.out.println("view2="+view2);
view2.assign(-1);
System.out.println("master replaced"+master);
System.out.println("flip1 replaced"+view1);
System.out.println("flip2 replaced"+view2);
}
示例3: doubleTest7
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
*/
public static void doubleTest7() {
int rows = 4;
int columns = 5; // make a 4*5 matrix
DoubleMatrix2D master = Factory2D.ascending(rows,columns);
//master.assign(1); // set all cells to 1
System.out.println("\n"+master);
//master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
//System.out.println("\n"+master);
int[] rowIndexes = {0,1,3,0};
int[] columnIndexes = {0,2};
DoubleMatrix2D view1 = master.viewSelection(rowIndexes,columnIndexes);
System.out.println("view1="+view1);
DoubleMatrix2D view2 = view1.viewPart(0,0,2,2);
System.out.println("view2="+view2);
view2.assign(-1);
System.out.println("master replaced"+master);
System.out.println("flip1 replaced"+view1);
System.out.println("flip2 replaced"+view2);
}
示例4: addRandom
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
public static void addRandom(DoubleMatrix2D in, DoubleMatrix2D out, double temperature) {
out.assign(in);
int randomIndexX = (int) (Math.random() * in.columns());
int randomIndexY = (int) (Math.random() * in.rows());
out.setQuick(randomIndexX, randomIndexY, in.getQuick(randomIndexX, randomIndexY) + ((Math.random() - 0.5) * temperature));
}
示例5: checkBuildMatrix
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/** Tests matrix construction for a supplied graph and matrix builder.
* Since it messes up the configuration of the graph, it has to be run at the end of every test method rather than multiple times.
*/
protected void checkBuildMatrix(LearnerGraph gr, DoubleMatrix2D expectedAx, DoubleMatrix1D expectedB)
{
GDLearnerGraph ndGraph = new GDLearnerGraph(gr,LearnerGraphND.ignoreRejectStates, false);
LSolver solver = ndGraph.buildMatrix(threadNumber);
DoubleMatrix2D Ax=solver.toDoubleMatrix2D();
Assert.assertEquals(getExpectedMatrix2DSlowly(gr),Ax);
if (expectedAx != null) Assert.assertEquals(expectedAx, Ax);
DoubleMatrix1D b=solver.toDoubleMatrix1D();
if (expectedB != null) Assert.assertEquals(expectedB, b);Assert.assertEquals(getExpectedMatrix1DSlowly(gr),b);
solver.solveExternally(1);// check if we have a solution, just in case it fails.
// Now check consistency.
gr.config.setAttenuationK_testOnly(1);DoubleMatrix2D Ax1 = ndGraph.buildMatrix(threadNumber).toDoubleMatrix2D();
gr.config.setAttenuationK(0);DoubleMatrix2D Ax0 = ndGraph.buildMatrix(threadNumber).toDoubleMatrix2D();
DoubleMatrix1D one = DoubleFactory1D.dense.make(Ax1.rows(), 1), a=DoubleFactory1D.dense.make(Ax.rows(), 0);
// check A(1)*one >= 0
Ax1.zMult(one, a);for(int i=0;i<a.size();++i) Assert.assertTrue(a.getQuick(i)>=0);
// check (A(1)-A(0))*one = b
Ax1.assign(Ax0, cern.jet.math.Functions.minus);
Ax1.zMult(one, a);for(int i=0;i<a.size();++i) Assert.assertTrue(a.getQuick(i) == -b.getQuick(i));
// Finally, we check that neither states are incompatible (there are no reject states so there should not be any)
int pairNumber [] = new int[gr.getStateNumber()*(gr.getStateNumber()+1)/2];
ndGraph.findIncompatiblePairs(pairNumber, threadNumber);GDLearnerGraph.numberNonNegativeElements(pairNumber);
for(int i=0;i<pairNumber.length;++i) pairNumber[i]=i;
}
示例6: calcTerms
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
private void calcTerms(DoubleMatrix2D terms){
terms.assign(0.0);
for(int x=0;x<windowLength;++x){
for(int y=0;y<featureLength;++y){
for(int i=0;i<k;++i){
for(int j=0;j<l;++j){
terms.set(featureLength*x+y,l*i+j,Math.pow(x,i)*Math.pow(y,j));
}
}
}
}
}
示例7: doubleTest3
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
*/
public static void doubleTest3() {
int rows = 4;
int columns = 5; // make a 4*5 matrix
DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
System.out.println(master);
master.assign(1); // set all cells to 1
System.out.println("\n"+master);
master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
System.out.println("\n"+master);
DoubleMatrix2D flip1 = master.viewColumnFlip();
System.out.println("flip around columns="+flip1);
DoubleMatrix2D flip2 = flip1.viewRowFlip();
System.out.println("further flip around rows="+flip2);
flip2.viewPart(0,0,2,2).assign(3);
System.out.println("master replaced"+master);
System.out.println("flip1 replaced"+flip1);
System.out.println("flip2 replaced"+flip2);
/*
DoubleMatrix2D copyPart = master.copyPart(2,1,2,3);
copyPart.assign(3); // modify an independent copy
copyPart.set(0,0,4);
System.out.println("\n"+copyPart); // has changed
System.out.println("\n"+master); // master has not changed
DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
System.out.println("\n"+view1);
System.out.println("\n"+view2);
*/
}
示例8: doubleTest4
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
*/
public static void doubleTest4() {
int rows = 4;
int columns = 5; // make a 4*5 matrix
DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
System.out.println(master);
master.assign(1); // set all cells to 1
DoubleMatrix2D view = master.viewPart(2,0,2,3).assign(2);
System.out.println("\n"+master);
System.out.println("\n"+view);
Transform.mult(view,3);
System.out.println("\n"+master);
System.out.println("\n"+view);
/*
DoubleMatrix2D copyPart = master.copyPart(2,1,2,3);
copyPart.assign(3); // modify an independent copy
copyPart.set(0,0,4);
System.out.println("\n"+copyPart); // has changed
System.out.println("\n"+master); // master has not changed
DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
System.out.println("\n"+view1);
System.out.println("\n"+view2);
*/
}
示例9: calcTerms
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
private void calcTerms(DoubleMatrix2D terms){
terms.assign(0.0);
for(int x=0;x<windowLength;++x){
for(int y=0;y<featureLength;++y){
for(int i=0;i<k;++i){
for(int j=0;j<l;++j){
terms.set(l*i+j,featureLength*x+y,Math.pow(x,i)*Math.pow(y,j));
}
}
}
}
}
示例10: zdemo7
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
* Demonstrates sorting with precomputation of aggregates, comparing mergesort with quicksort.
*/
public static void zdemo7(int rows, int columns, boolean print) {
// for reliable benchmarks, call this method twice: once with small dummy parameters to "warm up" the jitter, then with your real work-load
System.out.println("\n\n");
System.out.println("now initializing... ");
final cern.jet.math.Functions F = cern.jet.math.Functions.functions;
DoubleMatrix2D A = cern.colt.matrix.DoubleFactory2D.dense.make(rows,columns);
A.assign(new cern.jet.random.engine.DRand()); // initialize randomly
DoubleMatrix2D B = A.copy();
double[] v1 = A.viewColumn(0).toArray();
double[] v2 = A.viewColumn(0).toArray();
System.out.print("now quick sorting... ");
cern.colt.Timer timer = new cern.colt.Timer().start();
quickSort.sort(A,0);
timer.stop().display();
System.out.print("now merge sorting... ");
timer.reset().start();
mergeSort.sort(A,0);
timer.stop().display();
System.out.print("now quick sorting with simple aggregation... ");
timer.reset().start();
quickSort.sort(A,v1);
timer.stop().display();
System.out.print("now merge sorting with simple aggregation... ");
timer.reset().start();
mergeSort.sort(A,v2);
timer.stop().display();
}
示例11: addRandom
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
public static void addRandom(DoubleMatrix2D in, DoubleMatrix2D out, double temperature) {
out.assign(in);
int randomIndexX = (int) (Math.random() * in.columns());
int randomIndexY = (int) (Math.random() * in.rows());
out.setQuick(randomIndexX, randomIndexY, in.getQuick(randomIndexX, randomIndexY) + ((Math.random() - 0.5) * temperature));
}
示例12: CalculatePortfolioStandardDeviation
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
* Compute the stock portfolio return standard deviation (sigma, a number).
*
* A. Compute the stock portfolio covariance matrix:
* 1. Calculate log-returns: aR(t) = log(p(t)/p(t-1)).
* 2. Estimate the log-returns: maR(t) = d*aR(t) + (1-d)*maR(t-1) where d is memory.
* 3. Estimate the covariance matrix:
* Cov(t) = d*(outer(aR(t)-maR(t),aR(t)-maR(t)) + (1-d)*Cov(t-1).
*
* B. Compute the deviation of portfolio returns:
* 1. Variance sigma**2 of the stock portfolio : sigma**2 = w**T.Cov.w
* 2. Assume, for simplicity, that loans have same variance.
* 3. Return sqrt(sigma**2 + (1-sum(w))**2*s**2).
*/
static double CalculatePortfolioStandardDeviation(
int numberOfFirmsToInvestIn,
List<Double> firmMarketValuesNow,
List<Double> firmMarketValuesLast,
double[] firmStockWeights,
double covarianceAdaptationRate,
DoubleMatrix1D meanLogStockReturns,
DoubleMatrix2D meanStockCovarianceMatrix
) {
Algebra algebra = new Algebra();
DenseDoubleMatrix1D logOfStockReturns = new DenseDoubleMatrix1D(numberOfFirmsToInvestIn);
for (int i = 0; i < numberOfFirmsToInvestIn; ++i) {
double presentOverLastValue = (firmMarketValuesNow.get(i) / firmMarketValuesLast.get(i));
if (presentOverLastValue > 0.)
logOfStockReturns.set(i, Math.log(presentOverLastValue));
else logOfStockReturns.set(i, 0.);
}
meanLogStockReturns.assign(
logOfStockReturns.copy().assign(Functions.mult(covarianceAdaptationRate))
.assign(meanLogStockReturns.copy().assign(Functions.mult(1. - covarianceAdaptationRate)),
Functions.plus));
logOfStockReturns.assign(meanLogStockReturns, Functions.minus);
DoubleMatrix2D newCovMatrix =
DoubleFactory2D.dense.make(numberOfFirmsToInvestIn, numberOfFirmsToInvestIn);
algebra.multOuter(logOfStockReturns, logOfStockReturns, newCovMatrix);
meanStockCovarianceMatrix.assign(
newCovMatrix.copy().assign(Functions.mult(covarianceAdaptationRate))
.assign(meanStockCovarianceMatrix.copy().assign(
Functions.mult(1. - covarianceAdaptationRate)), Functions.plus));
DoubleMatrix1D stockWeightsVector =
new DenseDoubleMatrix1D(firmStockWeights);
final double
loanWeight = (1. - stockWeightsVector.zSum()),
sigmaSquared = algebra.mult(stockWeightsVector,
algebra.mult(meanStockCovarianceMatrix, stockWeightsVector));
return Math.sqrt(sigmaSquared * (1. + loanWeight * loanWeight));
}
示例13: doubleBenchmarkMult
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
/**
* Runs a bench on matrices holding double elements.
*/
public static void doubleBenchmarkMult(int runs, int rows, int columns, String kind, boolean print, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
System.out.println("benchmarking double matrix");
// certain loops need to be constructed so that the jitter can't optimize them away and we get fantastic numbers.
// this involves primarly read-loops
cern.colt.Timer timer1 = new cern.colt.Timer();
cern.colt.Timer timer2 = new cern.colt.Timer();
long size = (((long)rows)*columns)*runs;
DoubleMatrix2D matrix = null;
if (kind.equals("sparse")) matrix = new SparseDoubleMatrix2D(rows,columns,initialCapacity,minLoadFactor,maxLoadFactor);
else if (kind.equals("dense")) matrix = new DenseDoubleMatrix2D(rows,columns);
//else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(rows,columns);
else throw new RuntimeException("unknown kind");
System.out.println("\nNow multiplying...");
matrix.assign(1);
//if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
for (int i=0; i<runs; i++) {
timer1.start();
cern.colt.matrix.doublealgo.Transform.mult(matrix, 3);
timer1.stop();
}
timer1.display();
System.out.println(size / timer1.seconds() +" elements / sec");
if (print) {
System.out.println(matrix);
}
/*
if (kind.equals("sparse")) {
int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
System.out.println("hashCollisions="+hashCollisions);
System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
}
*/
System.out.println("\nNow multiplying2...");
matrix.assign(1);
//if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
for (int i=0; i<runs; i++) {
timer2.start();
cern.colt.matrix.doublealgo.Transform.mult(matrix,3);
timer2.stop();
}
timer2.display();
System.out.println(size / timer2.seconds() +" elements / sec");
if (print) {
System.out.println(matrix);
}
/*
if (kind.equals("sparse")) {
int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
System.out.println("hashCollisions="+hashCollisions);
System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
}
*/
System.out.println("bye bye.");
}
示例14: normalize
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
public void normalize() {
// System.out.println(adjacencyMatrix);
int adjacencyMatrixSize = adjacencyMatrix.columns();
if(adjacencyMatrixSize < 1)
return;
DoubleMatrix2D selector = new SparseDoubleMatrix2D(1, adjacencyMatrixSize);
selector.set(0, 0, 1.0);
// System.out.println(selector);
// get vertices reachable from V0
int cardinality = selector.cardinality();
int lastCardinality = 0;
DoubleDoubleFunction merge = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return (a > 0 || b > 0) ? 1 : 0;
}
};
while(cardinality != lastCardinality) {
lastCardinality = cardinality;
// selector = Algebra.DEFAULT.mult(selector, adjacencyMatrix);
selector.assign(Algebra.DEFAULT.mult(selector, adjacencyMatrix), merge);
// System.out.println(selector);
cardinality = selector.cardinality();
}
// System.out.println(selector);
if(cardinality == adjacencyMatrixSize) {
return;
}
// IntArrayList nonZeros = new IntArrayList();
// IntArrayList unusedInt = new IntArrayList();
// DoubleArrayList unusedDouble = new DoubleArrayList();
// selector.getNonZeros(unusedInt, nonZeros, unusedDouble);
// SparseDoubleMatrix2D reduced = new SparseDoubleMatrix2D(adjacencyMatrix.viewSelection(nonZeros.elements(), nonZeros.elements()).toArray());
SparseDoubleMatrix2D reduced = new SparseDoubleMatrix2D(cardinality, cardinality);
int iCurrentVertex = 0;
for(int i=0; i<adjacencyMatrixSize; ++i) {
if(selector.get(0, i) != 0.0) {
int jCurrentVertex = 0;
for(int j=0; j<adjacencyMatrixSize; ++j) {
if(selector.get(0, j) != 0.0){
reduced.set(iCurrentVertex, jCurrentVertex, adjacencyMatrix.get(i, j));
++jCurrentVertex;
}
}
++iCurrentVertex;
}
}
// System.out.println(reduced);
// System.out.println("=======");
adjacencyMatrix = reduced;
vertexCount = adjacencyMatrix.columns();
edgeCount = adjacencyMatrix.cardinality();
}
示例15: dcopy
import cern.colt.matrix.DoubleMatrix2D; //导入方法依赖的package包/类
public void dcopy(DoubleMatrix2D A, DoubleMatrix2D B) {
B.assign(A);
}