当前位置: 首页>>代码示例>>Java>>正文


Java RealVector类代码示例

本文整理汇总了Java中org.apache.commons.math.linear.RealVector的典型用法代码示例。如果您正苦于以下问题:Java RealVector类的具体用法?Java RealVector怎么用?Java RealVector使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


RealVector类属于org.apache.commons.math.linear包,在下文中一共展示了RealVector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createAvgProductVector

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
public double[] createAvgProductVector(List<Long> recentitemInteractions,Map<Long,float[]> productFeatures)
  {
  	int numLatentFactors = productFeatures.values().iterator().next().length;
  	double[] userFeatures = new double[numLatentFactors];
  	for (Long item : recentitemInteractions) 
{
  		float[] productFactors = productFeatures.get(item);
  		if (productFactors != null)
  		{
  			for (int feature = 0; feature < numLatentFactors; feature++) 
  			{
  				userFeatures[feature] += productFactors[feature];
  			}
  		}
}
  	RealVector  userFeaturesAsVector = new ArrayRealVector(userFeatures);
  	RealVector normalised =  userFeaturesAsVector.mapDivide(userFeaturesAsVector.getL1Norm());

  	return normalised.getData();
  }
 
开发者ID:SeldonIO,项目名称:seldon-server,代码行数:21,代码来源:RecentMfRecommender.java

示例2: testNewSample2

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Verifies that setting X, Y and covariance separately has the same effect as newSample(X,Y,cov).
 */
@Test
public void testNewSample2() throws Exception {
    double[] y = new double[] {1, 2, 3, 4}; 
    double[][] x = new double[][] {
      {19, 22, 33},
      {20, 30, 40},
      {25, 35, 45},
      {27, 37, 47}   
    };
    double[][] covariance = MatrixUtils.createRealIdentityMatrix(4).scalarMultiply(2).getData();
    GLSMultipleLinearRegression regression = new GLSMultipleLinearRegression();
    regression.newSampleData(y, x, covariance);
    RealMatrix combinedX = regression.X.copy();
    RealVector combinedY = regression.Y.copy();
    RealMatrix combinedCovInv = regression.getOmegaInverse();
    regression.newXSampleData(x);
    regression.newYSampleData(y);
    assertEquals(combinedX, regression.X);
    assertEquals(combinedY, regression.Y);
    assertEquals(combinedCovInv, regression.getOmegaInverse());
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:25,代码来源:GLSMultipleLinearRegressionTest.java

示例3: equationFromString

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Converts a test string to a {@link LinearConstraint}.
 * Ex: x0 + x1 + x2 + x3 - x12 = 0
 */
private LinearConstraint equationFromString(int numCoefficients, String s) {
    Relationship relationship;
    if (s.contains(">=")) {
        relationship = Relationship.GEQ;
    } else if (s.contains("<=")) {
        relationship = Relationship.LEQ;
    } else if (s.contains("=")) {
        relationship = Relationship.EQ;
    } else {
        throw new IllegalArgumentException();
    }

    String[] equationParts = s.split("[>|<]?=");
    double rhs = Double.parseDouble(equationParts[1].trim());

    RealVector lhs = new ArrayRealVector(numCoefficients);
    String left = equationParts[0].replaceAll(" ?x", "");
    String[] coefficients = left.split(" ");
    for (String coefficient : coefficients) {
        double value = coefficient.charAt(0) == '-' ? -1 : 1;
        int index = Integer.parseInt(coefficient.replaceFirst("[+|-]", "").trim());
        lhs.setEntry(index, value);
    }
    return new LinearConstraint(lhs, relationship, rhs);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:30,代码来源:SimplexSolverTest.java

示例4: cosine

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
public static double cosine(RealVector vec1, RealVector vec2) throws Exception
{
    final double norm1 = vec1.getNorm();
    final double norm2 = vec2.getNorm();

    if (norm1 == 0 || norm2 == 0) 
        throw new Exception("Division by zero");
    return vec1.dotProduct(vec2) / (norm1 * norm2);
}
 
开发者ID:sinantie,项目名称:Generator,代码行数:10,代码来源:Utils.java

示例5: getInnerProduct

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public double getInnerProduct(final Matrix<?> m1, final Matrix<?> m2) {
  Validate.notNull(m1, "m1");
  Validate.notNull(m2, "m2");
  if (m1 instanceof DoubleMatrix1D && m2 instanceof DoubleMatrix1D) {
    final RealVector t1 = CommonsMathWrapper.wrap((DoubleMatrix1D) m1);
    final RealVector t2 = CommonsMathWrapper.wrap((DoubleMatrix1D) m2);
    return t1.dotProduct(t2);
  }
  throw new IllegalArgumentException("Can only find inner product of DoubleMatrix1D; have " + m1.getClass() + " and " + m2.getClass());
}
 
开发者ID:DevStreet,项目名称:FinanceAnalytics,代码行数:15,代码来源:CommonsMatrixAlgebra.java

示例6: getOuterProduct

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public DoubleMatrix2D getOuterProduct(final Matrix<?> m1, final Matrix<?> m2) {
  Validate.notNull(m1, "m1");
  Validate.notNull(m2, "m2");
  if (m1 instanceof DoubleMatrix1D && m2 instanceof DoubleMatrix1D) {
    final RealVector t1 = CommonsMathWrapper.wrap((DoubleMatrix1D) m1);
    final RealVector t2 = CommonsMathWrapper.wrap((DoubleMatrix1D) m2);
    return CommonsMathWrapper.unwrap(t1.outerProduct(t2));
  }
  throw new IllegalArgumentException("Can only find outer product of DoubleMatrix1D; have " + m1.getClass() + " and " + m2.getClass());
}
 
开发者ID:DevStreet,项目名称:FinanceAnalytics,代码行数:15,代码来源:CommonsMatrixAlgebra.java

示例7: assertRealVectorEquals

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
private void assertRealVectorEquals(final RealVector v1, final DoubleMatrix1D v2) {
  final int n = v1.getDimension();
  assertEquals(n, v2.getNumberOfElements());
  for (int i = 0; i < n; i++) {
    assertEquals(v1.getEntry(i), v2.getEntry(i), EPS);
  }
}
 
开发者ID:DevStreet,项目名称:FinanceAnalytics,代码行数:8,代码来源:SVDecompositionCommonsResultTest.java

示例8: vecStatsNonZeros

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * 
 * Computes useful statistics for the vector on the non-zero elements only.
 * These include the average, the absolute value average, positive values only average, 
 * negative values only average, max positive value, max negative value, num of positive values,
 * num of negative values, sparsity, L1-norm, count
 * @return 
 */
@Override
public double[] vecStatsNonZeros()
{
    double sumNonZero = 0;
    double positiveSum = 0, negativeSum = 0, minNegative = 0, maxPositive = 0;
    int counter = 0, positiveCounter = 0, negativeCounter = 0;
    double l1Norm = 0;
    for(Iterator<RealVector.Entry> it = counts.sparseIterator(); it.hasNext();)
    {
        double value = it.next().getValue();
        sumNonZero += value;
        l1Norm += Math.abs(value);
        if(value > 0)
        {
            positiveSum += value;
            positiveCounter++;
            if(value > maxPositive)
                maxPositive = value;
        }
        else
        {
            negativeSum += value;
            negativeCounter++;
            if(value < minNegative)
                minNegative = value;
        }
        counter++;
    }
    return new double[] {sumNonZero / (double) counter, l1Norm / (double) counter, 
        positiveSum / (double) positiveCounter, negativeSum / (double) negativeCounter,
        maxPositive, minNegative, 
        (double) counter / (double) size(), l1Norm, positiveCounter, negativeCounter, counter};
}
 
开发者ID:sinantie,项目名称:PLTAG,代码行数:42,代码来源:SparseVec.java

示例9: getInvertedCoeffiecientSum

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Get the -1 times the sum of all coefficients in the given array.
 * @param coefficients coefficients to sum
 * @return the -1 times the sum of all coefficients in the given array.
 */
protected static double getInvertedCoeffiecientSum(final RealVector coefficients) {
    double sum = 0;
    for (double coefficient : coefficients.getData()) {
        sum -= coefficient;
    }
    return sum;
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:13,代码来源:SimplexTableau.java

示例10: testEigenvectors

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/** test eigenvectors */
public void testEigenvectors() {
    EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
    for (int i = 0; i < matrix.getRowDimension(); ++i) {
        double lambda = ed.getRealEigenvalue(i);
        RealVector v  = ed.getEigenvector(i);
        RealVector mV = matrix.operate(v);
        assertEquals(0, mV.subtract(v.mapMultiplyToSelf(lambda)).getNorm(), 1.0e-13);
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:11,代码来源:EigenDecompositionImplTest.java

示例11: calculateBeta

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Calculates beta by GLS.
 * <pre>
 *  b=(X' Omega^-1 X)^-1X'Omega^-1 y
 * </pre>
 * @return beta
 */
@Override
protected RealVector calculateBeta() {
    RealMatrix OI = getOmegaInverse();
    RealMatrix XT = X.transpose();
    RealMatrix XTOIX = XT.multiply(OI).multiply(X);
    RealMatrix inverse = new LUDecompositionImpl(XTOIX).getSolver().getInverse();
    return inverse.multiply(XT).multiply(OI).operate(Y);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:16,代码来源:GLSMultipleLinearRegression.java

示例12: getObsPoint

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
public RealVector getObsPoint() {
if (hemisphere) {
    return new ArrayRealVector(new double[] { 0, 0, 1 });
} else {
    return new ArrayRealVector(new double[] { 0, 0, -1 });
}
   }
 
开发者ID:langurmonkey,项目名称:celestial-pole-motion,代码行数:8,代码来源:Parameters.java

示例13: store

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Store the illumination and index of the brightest sample.
 * @param illuminationFromSample illumination received from sample
 * @param sample current sample illuminating the element
 */
void store(final double illuminationFromSample,
           final Map.Entry<RealVector, Double> sample) {
    if (illuminationFromSample > this.brightestIllumination) {
        this.brightestIllumination = illuminationFromSample;
        this.brightestSample = sample;
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:13,代码来源:MicrosphereInterpolatingFunction.java

示例14: apply

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Compute the LSH for a given vector.
 */
public long apply(RealVector vector)
{
  /*
   * The hash is just floor(<v, a>/w)
   */
   double ret = b;
  
   for(int i = 0;i < dim;++i)
   {
      ret += vector.getEntry(i)*a[i];
   }
   return (long)Math.floor(ret/w);
}
 
开发者ID:apache,项目名称:incubator-datafu,代码行数:17,代码来源:AbstractStableDistributionFunction.java

示例15: testNewSample2

import org.apache.commons.math.linear.RealVector; //导入依赖的package包/类
/**
 * Verifies that setting X and Y separately has the same effect as newSample(X,Y).
 */
@Test
public void testNewSample2() throws Exception {
    double[] y = new double[] {1, 2, 3, 4}; 
    double[][] x = new double[][] {
      {19, 22, 33},
      {20, 30, 40},
      {25, 35, 45},
      {27, 37, 47}   
    };
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(y, x);
    RealMatrix combinedX = regression.X.copy();
    RealVector combinedY = regression.Y.copy();
    regression.newXSampleData(x);
    regression.newYSampleData(y);
    assertEquals(combinedX, regression.X);
    assertEquals(combinedY, regression.Y);
    
    // No intercept
    regression.setNoIntercept(true);
    regression.newSampleData(y, x);
    combinedX = regression.X.copy();
    combinedY = regression.Y.copy();
    regression.newXSampleData(x);
    regression.newYSampleData(y);
    assertEquals(combinedX, regression.X);
    assertEquals(combinedY, regression.Y);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:32,代码来源:OLSMultipleLinearRegressionTest.java


注:本文中的org.apache.commons.math.linear.RealVector类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。