Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.mahout.math;
 
 
 import java.io.File;
 import java.util.List;

Provides a way to get data from a file and treat it as if it were a matrix, but avoids putting all that data onto the Java heap. Instead, the file is mapped into non-heap memory as a DoubleBuffer and we access that instead.
 
 public final class FileBasedMatrix extends AbstractMatrix {
   private final int rowsPerBlock;
   private final List<DoubleBuffercontent = Lists.newArrayList();

  
Constructs an empty matrix of the given size.

Parameters:
rows The number of rows in the result.
columns The number of columns in the result.
 
   public FileBasedMatrix(int rowsint columns) {
     super(rowscolumns);
     long maxRows = ((1L << 31) - 1) / (columns * 8);
     if (rows > maxRows) {
        = (intmaxRows;
     } else {
        = rows;
     }
   }
 
   private void addData(DoubleBuffer content) {
     this..add(content);
   }
 
   public void setData(File fboolean loadNowthrows IOException {
     Preconditions.checkArgument(f.length() ==  *  * 8L, "File " + f + " is wrong length");
 
     for (int i = 0; i < ( +  - 1) / i++) {
       long start = i *  *  * 8L;
       long size =  *  * 8L;
                                                                      Math.min(f.length() - startsize));
       if (loadNow) {
         buf.load();
       }
       addData(buf.asDoubleBuffer());
     }
   }
 
   public static void writeMatrix(File fMatrix mthrows IOException {
     Preconditions.checkArgument(f.canWrite(), "Can't write to output file");
     FileOutputStream fos = new FileOutputStream(f);
     try {
       ByteBuffer buf = ByteBuffer.allocate(m.columnSize() * 8);
       for (MatrixSlice row : m) {
         buf.clear();
         for (Vector.Element element : row.vector().all()) {
           buf.putDouble(element.get());
         }
         buf.flip();
         fos.write(buf.array());
       }
     } finally {
       fos.close();
     }
   }

  
Assign the other vector values to the column of the receiver

Parameters:
column the int row to assign
other a Vector
Returns:
the modified receiver
Throws:
CardinalityException if the cardinalities differ
  public Matrix assignColumn(int columnVector other) {
    throw new UnsupportedOperationException("Default operation");
  }

  
Assign the other vector values to the row of the receiver

Parameters:
row the int row to assign
other a Vector
Returns:
the modified receiver
Throws:
CardinalityException if the cardinalities differ
  public Matrix assignRow(int rowVector other) {
    throw new UnsupportedOperationException("Default operation");
  }

  
Return the value at the given indexes, without checking bounds

Parameters:
row an int row index
column an int column index
Returns:
the double at the index
  public double getQuick(int rowint column) {
    int block = row / ;
    return .get(block).get((row % ) *  + column);
  }

  
Return an empty matrix of the same underlying class as the receiver

Returns:
a Matrix
  public Matrix like() {
    throw new UnsupportedOperationException("Default operation");
  }

  
Returns an empty matrix of the same underlying class as the receiver and of the specified size.

Parameters:
rows the int number of rows
columns the int number of columns
  public Matrix like(int rowsint columns) {
    return new DenseMatrix(rowscolumns);
  }

  
Set the value at the given index, without checking bounds

Parameters:
row an int row index into the receiver
column an int column index into the receiver
value a double value to set
  public void setQuick(int rowint columndouble value) {
    throw new UnsupportedOperationException("Default operation");
  }

  
Return a view into part of a matrix. Changes to the view will change the original matrix.

Parameters:
offset an int[2] offset into the receiver
size the int[2] size of the desired result
Returns:
a matrix that shares storage with part of the original matrix.
Throws:
CardinalityException if the length is greater than the cardinality of the receiver
IndexException if the offset is negative or the offset+length is outside of the receiver
  public Matrix viewPart(int[] offsetint[] size) {
    throw new UnsupportedOperationException("Default operation");
  }
New to GrepCode? Check out our FAQ X