本文整理汇总了Java中gov.sandia.cognition.math.matrix.Vector.setElement方法的典型用法代码示例。如果您正苦于以下问题:Java Vector.setElement方法的具体用法?Java Vector.setElement怎么用?Java Vector.setElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gov.sandia.cognition.math.matrix.Vector
的用法示例。
在下文中一共展示了Vector.setElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: diag
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Extract the diagonal elements as a vector
*
* @param mat
* the matrix to extract from
* @return the diagonal
*/
public static Vector diag(Matrix mat) {
Vector ret;
if (mat.getNumColumns() > mat.getNumRows()) {
ret = mat.getRow(0);
}
else {
ret = mat.getColumn(0);
}
final int rowcol = ret.getDimensionality();
for (int rc = 0; rc < rowcol; rc++) {
ret.setElement(rc, mat.getElement(rc, rc));
}
return ret;
}
示例2: testGetStateVector
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Test of getStateVector method, of class gov.sandia.isrc.cognition.framework.lite.SimplePatternRecognizerState.
*/
public void testGetStateVector()
{
SemanticLabel a = new DefaultSemanticLabel("a");
SemanticLabel b = new DefaultSemanticLabel("b");
ArrayList<SemanticLabel> givenLabels = new ArrayList<SemanticLabel>();
givenLabels.add(a);
givenLabels.add(b);
SimplePatternRecognizerState instance =
new SimplePatternRecognizerState(givenLabels);
Vector stateVector = instance.getStateVector();
assertNotNull(stateVector);
assertEquals(2, stateVector.getDimensionality());
assertEquals(0.0, stateVector.getElement(0));
assertEquals(0.0, stateVector.getElement(1));
stateVector.setElement(0, 1.0);
instance =
new SimplePatternRecognizerState(givenLabels, stateVector, false);
assertSame(stateVector, instance.getStateVector());
}
示例3: solve
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
@Override
final public Vector solve(
final Vector b)
{
checkSolveDimensions(b);
Vector result = b.clone();
for (int i = 0; i < diagonal.length; ++i)
{
if (diagonal[i] == 0)
{
if (result.get(i) != 0)
{
throw new UnsupportedOperationException("Unable to solve "
+ "Ax=b because b spans different space than A");
}
}
else
{
result.setElement(i, result.get(i) / diagonal[i]);
}
}
return result;
}
示例4: computePosteriorLogLikelihood
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Computes the posterior log likelihood of the Sample
* @param numObservations
* Number of observations in the Sample
* @param logConditional
* Log conditional likelihood of the data given the sample
* @return
* Posterior log likelihood
*/
public double computePosteriorLogLikelihood(
int numObservations,
double logConditional )
{
final int K = this.getNumClusters();
ChineseRestaurantProcess.PMF pmf = new ChineseRestaurantProcess.PMF(
this.getAlpha(), numObservations );
Vector counts = VectorFactory.getDefault().createVector(K);
for( int k = 0; k < K; k++ )
{
counts.setElement(k, this.clusters.get(k).getMembers().size() );
}
double logPrior = pmf.logEvaluate( counts );
double logPosterior = logPrior + logConditional;
return logPosterior;
}
示例5: testConvolve
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Test of convolve method, of class gov.sandia.isrc.math.MultivariateGaussian.
*/
public void testConvolve()
{
System.out.println( "convolve" );
Vector m1 = VectorFactory.getDefault().createVector( 1 );
m1.setElement( 0, 0.0 );
Matrix c1 = MatrixFactory.getDefault().createMatrix( 1, 1 );
c1.setElement( 0, 0, 0.1 );
MultivariateGaussian g1 = new MultivariateGaussian( m1, c1 );
Vector m2 = VectorFactory.getDefault().createVector( 1 );
m2.setElement( 0, 1.0 );
Matrix c2 = MatrixFactory.getDefault().createMatrix( 1, 1 );
c2.setElement( 0, 0, 0.1 );
MultivariateGaussian g2 = new MultivariateGaussian( m2, c2 );
MultivariateGaussian result = g1.convolve(g2);
assertEquals( g1.getMean().plus(g2.getMean()), result.getMean() );
assertEquals( g1.getCovariance().plus( g2.getCovariance()), result.getCovariance() );
}
示例6: testDistributionConvertToVector
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* convertToVector
*/
public void testDistributionConvertToVector()
{
System.out.println( "Distribution.convertToVector" );
ClosedFormUnivariateDistribution<NumberType> instance = this.createInstance();
// Should have at least one parameter
Vector x1 = instance.convertToVector();
assertNotNull( x1 );
assertTrue( x1.getDimensionality() > 0 );
// Should return the equal parameterization
Vector x2 = instance.convertToVector();
assertNotNull( x2 );
assertNotSame( x1, x2 );
assertEquals( x1.getDimensionality(), x2.getDimensionality() );
assertEquals( x1, x2 );
// Parameterization shouldn't be effected by changed returned parameters
x2.setElement( 0, x2.getElement( 0 ) + RANDOM.nextDouble() );
x2.scaleEquals( RANDOM.nextDouble() );
Vector x3 = instance.convertToVector();
assertNotNull( x3 );
assertFalse( x2.equals( x3 ) );
assertEquals( x1, x3 );
}
示例7: testComputeParameterGradient
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Test of computeParameterGradient method, of class gov.sandia.cognition.learning.util.function.PolynomialFunction.
*/
public void testComputeParameterGradient()
{
System.out.println( "computeParameterGradient" );
PolynomialFunction p2 = new PolynomialFunction( random.nextDouble() );
double x = random.nextDouble() * 20 - 10;
Vector g = VectorFactory.getDefault().createVector( 1 );
double v = Math.log( x ) * p2.evaluate( x );
g.setElement( 0, v );
final double EPS = 1e-5;
Vector ghat = p2.computeParameterGradient( x );
assertEquals( g.getDimensionality(), ghat.getDimensionality() );
assertTrue( g.equals( p2.computeParameterGradient( x ), EPS ) );
}
示例8: convertToVector
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Converts a given list of terms to a vector by counting the occurrence of
* each term.
*
* @param terms
* The terms to count.
* @param termIndex
* The term index to use to map terms to their vector indices.
* @param vectorFactory
* The vector factory to use to create the vector.
* @return
* The bag-of-words vector representation of the terms, which is the
* count of how many times each term occurs in the document.
*/
public static Vector convertToVector(
final Iterable<? extends Termable> terms,
final TermIndex termIndex,
final VectorFactory<?> vectorFactory)
{
// Create the vector to store the result.
final Vector result = vectorFactory.createVector(
termIndex.getTermCount());
for (Termable termable : terms)
{
final Term term = termable.asTerm();
int index = termIndex.getIndex(term);
if (index >= 0)
{
final double count = result.getElement(index);
result.setElement(index, count + 1);
}
// TODO: Ideally we would somehow handle all of the "unknown"
// elements also. Perhaps by using the first vector element for
// unknowns.
}
return result;
}
示例9: evaluate
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
public Vector evaluate(
final Vectorizable input)
{
if (input == null)
{
// Pass through null.
return null;
}
// Convert the input to a vector.
final Vector vector = input.convertToVector();
if (vector == null)
{
// Pass through null.
return null;
}
// The input vector has to be of the proper dimensionality.
vector.assertDimensionalityEquals(this.inputDimensionality);
// Create the sub-vector to fill in.
final int subDimensionality = this.subIndices.length;
final Vector subVector = this.getVectorFactory().createVector(
subDimensionality);
// Fill in the sub-vector.
for (int i = 0; i < subDimensionality; i++)
{
subVector.setElement(i, vector.getElement(this.subIndices[i]));
}
return subVector;
}
示例10: convertToVector
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Converts this function into its parameters, which consists of the
* threshold value
* @return one-element Vector consisting of the threshold value
*/
public Vector convertToVector()
{
Vector parameters = VectorFactory.getDefault().createVector(1);
parameters.setElement(0, this.getThreshold());
return parameters;
}
示例11: getDominance
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Gets the dominance weight (global weight) vector for all of the terms.
*
* @return
* The dominance weight (global weight) vector for all of the terms.
*/
public Vector getDominance()
{
// We cache the dominance.
if (this.dominance == null && this.termGlobalFrequencies != null)
{
// Need to update the dominance. Start by creating an empty vector to
// hold it.
final int dimensionality = this.getDimensionality();
final Vector newDominance = this.getVectorFactory().createVector(
dimensionality);
for (VectorEntry entry : this.termGlobalFrequencies)
{
final int index = entry.getIndex();
final double termEntropySum =
this.termEntropiesSum.getElement(index);
final double termOccurrences = entry.getValue();
// Calculate the actual dominance values.
double value = 0.0;
if (termOccurrences != 0.0)
{
value =
Math.exp(-(termEntropySum / termOccurrences
- Math.log(termOccurrences)))
/ this.documentCount;
}
newDominance.setElement(index, value);
}
this.setDominance(newDominance);
}
return this.dominance;
}
示例12: createRandom
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
@Override
protected Vector createRandom()
{
Vector v = super.createRandom();
if( v.getDimensionality() > 1 )
{
int index = RANDOM.nextInt(v.getDimensionality());
v.setElement(index, 0.0);
((SparseVector) v).compact();
}
return v;
}
示例13: testConvertFromVector
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
/**
* Test of convertFromVector method, of class MutableInteger.
*/
public void testConvertFromVector()
{
MutableInteger instance = new MutableInteger();
Vector vector = VectorFactory.getDefault().createVector(1);
int value = 0;
instance.convertFromVector(vector);
assertEquals(value, instance.getValue());
value = this.randomInt();
vector.setElement(0, value);
instance.convertFromVector(vector);
assertEquals(value, instance.getValue());
boolean exceptionThrown = false;
try
{
instance.convertFromVector(new Vector2());
}
catch (DimensionalityMismatchException e)
{
exceptionThrown = true;
}
finally
{
assertTrue(exceptionThrown);
}
}
示例14: stack
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
@Override
public final Vector stack(
final SparseVector other)
{
Vector result;
int len = values.length + other.getDimensionality();
int nnz = countNonZeros() + other.countNonZeros();
if (nnz > SparseVector.SPARSE_TO_DENSE_THRESHOLD * len)
{
result = new DenseVector(len);
}
else
{
result = new SparseVector(len);
}
for (int i = 0; i < values.length; ++i)
{
result.setElement(i, values[i]);
}
// NOTE: The below could be faster (and I could get rid of all of the
// "setElement"s if I wanted to write two versions of this method. As
// it's likely to be infrequently called, I don't want to increase code
// complexity for a minimal gain.
//
// The way to do this would be to move the below into the above if/elses.
// Then, you would only need to set the 0s in the dense vector class
// (as 0s are implicitly stored in sparse vectors). Moreover, you
// could call the package-private methods to get direct access to the
// entries of the sparse or dense vector instead of calling setElement.
other.compress();
int[] locs = other.getIndices();
double[] vals = other.getValues();
int idx = 0;
final int otherDimensionality = other.getDimensionality();
for (int i = 0; i < otherDimensionality; ++i)
{
if ((idx < locs.length) && (locs[idx] == i))
{
result.setElement(values.length + i, vals[idx]);
++idx;
}
else
{
result.setElement(values.length + i, 0);
}
}
return result;
}
示例15: update
import gov.sandia.cognition.math.matrix.Vector; //导入方法依赖的package包/类
@Override
public void update(
final DefaultKernelBinaryCategorizer<InputType> target,
final InputType input,
final boolean label)
{
// Predict the output as a double (negative values are false, positive
// are true).
final double prediction = target.evaluateAsDouble(input);
final double actual = label ? +1.0 : -1.0;
final double margin = prediction * actual;
if (!this.shouldUpdate(margin))
{
// Not an error, so no need to update.
return;
}
// Get the supports.
final int size = target.getExampleCount();
// Create the kernel matrix and kernel vector.
final Kernel<? super InputType> kernel = target.getKernel();
final Matrix K = MatrixFactory.getDenseDefault().createMatrix(size, size);
final Vector k = VectorFactory.getDenseDefault().createVector(size);
for (int i = 0; i < size; i++)
{
final InputType xI = target.get(i).getValue();
k.setElement(i, kernel.evaluate(input, xI));
K.setElement(i, i, kernel.evaluate(xI, xI));
// Loop over the upper-diagonal.
for (int j = i + 1; j < size; j++)
{
final InputType xJ = target.get(j).getValue();
final double value = kernel.evaluate(xI, xJ);
K.setElement(i, j, value);
K.setElement(j, i, value);
}
}
// TODO: Rather than performing the direct inverse on the kernel matrix, it
// is possible to incrementally compute the inverse of the kernel matrix as
// each item is added. This may make the algorithm faster to avoid the extra
// computation, however it will be extra book-keeping attached to the learned
// object. It should be done if the Projectron appears to perform well.
// - jdbasil (2011-04-11)
// Compute d by K^-1 * k.
final Vector d = K.inverse().times(k);
final double kernelInputInput = kernel.evaluate(input, input);
// Compute delta squared and then delta.
final double deltaSquared = kernelInputInput - k.dotProduct(d);
final double delta = Math.sqrt(Math.max(0.0, deltaSquared));
applyUpdate(target, input, actual, margin, kernelInputInput, delta, d);
}