Java Code Examples for org.apache.commons.math.linear.RealVector#getEntry()

The following examples show how to use org.apache.commons.math.linear.RealVector#getEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractStableDistributionFunction.java    From datafu with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: LSHPigTest.java    From datafu with Apache License 2.0 5 votes vote down vote up
@Test
public void testSparseVectors() throws IOException, ParseException
{
  RandomGenerator rg = new JDKRandomGenerator();
  rg.setSeed(0);
  RandomData rd = new RandomDataImpl(rg);
  int n = 20;
  List<RealVector> vectors = LSHTest.getVectors(rd, 1000, n);
  PigTest test = createPigTestFromString(sparseVectorTest);
  writeLinesToFile("input", getSparseLines(vectors));
  test.runScript();
  List<Tuple> neighbors = this.getLinesForAlias(test, "PTS");
  Assert.assertEquals(neighbors.size(), n);
  int idx = 0;
  for(Tuple t : neighbors)
  {
    Assert.assertTrue(t.get(0) instanceof DataBag);
    Assert.assertEquals(t.size(), 1);
    RealVector interpreted = DataTypeUtil.INSTANCE.convert(t, 3);
    RealVector original = vectors.get(idx);
    Assert.assertEquals(original.getDimension(), interpreted.getDimension());
    for(int i = 0;i < interpreted.getDimension();++i)
    {
      double originalField = original.getEntry(i);
      double interpretedField = interpreted.getEntry(i);
      Assert.assertTrue(Math.abs(originalField - interpretedField) < 1e-5);
    }

    idx++;
  }
}
 
Example 3
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Uses back substitution to solve the system</p>
 * 
 * <p>coefficients X = constants</p>
 * 
 * <p>coefficients must upper-triangular and constants must be a column 
 * matrix.  The solution is returned as a column matrix.</p>
 * 
 * <p>The number of columns in coefficients determines the length
 * of the returned solution vector (column matrix).  If constants
 * has more rows than coefficients has columns, excess rows are ignored.
 * Similarly, extra (zero) rows in coefficients are ignored</p>
 * 
 * @param coefficients upper-triangular coefficients matrix
 * @param constants column RHS constants vector
 * @return solution matrix as a column vector
 * 
 */
private static RealVector solveUpperTriangular(RealMatrix coefficients,
                                               RealVector constants) {
    checkUpperTriangular(coefficients, 1E-12);
    int length = coefficients.getColumnDimension();
    double x[] = new double[length];
    for (int i = 0; i < length; i++) {
        int index = length - 1 - i;
        double sum = 0;
        for (int j = index + 1; j < length; j++) {
            sum += coefficients.getEntry(index, j) * x[j];
        }
        x[index] = (constants.getEntry(index) - sum) / coefficients.getEntry(index, index);
    } 
    return new ArrayRealVector(x);
}
 
Example 4
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Uses back substitution to solve the system</p>
 * 
 * <p>coefficients X = constants</p>
 * 
 * <p>coefficients must upper-triangular and constants must be a column 
 * matrix.  The solution is returned as a column matrix.</p>
 * 
 * <p>The number of columns in coefficients determines the length
 * of the returned solution vector (column matrix).  If constants
 * has more rows than coefficients has columns, excess rows are ignored.
 * Similarly, extra (zero) rows in coefficients are ignored</p>
 * 
 * @param coefficients upper-triangular coefficients matrix
 * @param constants column RHS constants vector
 * @return solution matrix as a column vector
 * 
 */
private static RealVector solveUpperTriangular(RealMatrix coefficients,
                                               RealVector constants) {
    checkUpperTriangular(coefficients, 1E-12);
    int length = coefficients.getColumnDimension();
    double x[] = new double[length];
    for (int i = 0; i < length; i++) {
        int index = length - 1 - i;
        double sum = 0;
        for (int j = index + 1; j < length; j++) {
            sum += coefficients.getEntry(index, j) * x[j];
        }
        x[index] = (constants.getEntry(index) - sum) / coefficients.getEntry(index, index);
    } 
    return new ArrayRealVector(x);
}
 
Example 5
Source File: OLSMultipleLinearRegression.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Uses back substitution to solve the system</p>
 * 
 * <p>coefficients X = constants</p>
 * 
 * <p>coefficients must upper-triangular and constants must be a column 
 * matrix.  The solution is returned as a column matrix.</p>
 * 
 * <p>The number of columns in coefficients determines the length
 * of the returned solution vector (column matrix).  If constants
 * has more rows than coefficients has columns, excess rows are ignored.
 * Similarly, extra (zero) rows in coefficients are ignored</p>
 * 
 * @param coefficients upper-triangular coefficients matrix
 * @param constants column RHS constants vector
 * @return solution matrix as a column vector
 * 
 */
private static RealVector solveUpperTriangular(RealMatrix coefficients,
                                               RealVector constants) {
    checkUpperTriangular(coefficients, 1E-12);
    int length = coefficients.getColumnDimension();
    double x[] = new double[length];
    for (int i = 0; i < length; i++) {
        int index = length - 1 - i;
        double sum = 0;
        for (int j = index + 1; j < length; j++) {
            sum += coefficients.getEntry(index, j) * x[j];
        }
        x[index] = (constants.getEntry(index) - sum) / coefficients.getEntry(index, index);
    } 
    return new ArrayRealVector(x);
}