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.util.Map;

Doubly sparse matrix. Implemented as a Map of RandomAccessSparseVector rows
 
 public class SparseMatrix extends AbstractMatrix {
 
   private OpenIntObjectHashMap<VectorrowVectors;
  
  
Construct a matrix of the given cardinality with the given row map

Parameters:
rows a Map<Integer, RandomAccessSparseVector> of rows
columns
rowVectors
 
   public SparseMatrix(int rowsint columnsMap<IntegerRandomAccessSparseVectorrowVectors) {
     super(rowscolumns);
     this. = new OpenIntObjectHashMap<Vector>();
     for (Map.Entry<IntegerRandomAccessSparseVectorentry : rowVectors.entrySet()) {
       this..put(entry.getKey(), entry.getValue().clone());
     }
   }
  
  
Construct a matrix with specified number of rows and columns.
 
   public SparseMatrix(int rowsint columns) {
     super(rowscolumns);
     this. = new OpenIntObjectHashMap<Vector>();
   }
 
   @Override
   public Matrix clone() {
     SparseMatrix clone = (SparseMatrixsuper.clone();
     clone.rowVectors = .clone();
     for (int i = 0; i < numRows(); i++) {
       clone.rowVectors.put(i.get(i).clone());
     }
     return clone;
   }
 
   @Override
   public Iterator<MatrixSliceiterator() {
     final IntArrayList keys = new IntArrayList(.size());
     .keys(keys);
     return new AbstractIterator<MatrixSlice>() {
       private int slice;
       @Override
       protected MatrixSlice computeNext() {
         if ( >= .size()) {
           return endOfData();
         }
         int i = keys.get();
         Vector row = .get(i);
         ++;
         return new MatrixSlice(rowi);
       }
     };
   }
   
   @Override
   public double getQuick(int rowint column) {
     Vector r = .get(row);
     return r == null ? 0.0 : r.getQuick(column);
   }
   
   @Override
   public Matrix like() {
     return new SparseMatrix(rowSize(), columnSize());
   }
   
   @Override
   public Matrix like(int rowsint columns) {
     return new SparseMatrix(rowscolumns);
   }
  
  public void setQuick(int rowint columndouble value) {
    Vector r = .get(row);
    if (r == null) {
      r = new RandomAccessSparseVector(columnSize());
      .put(rowr);
    }
    r.setQuick(columnvalue);
  }
  
  public int[] getNumNondefaultElements() {
    int[] result = new int[2];
    result[] = .size();
    for (Vector vectorEntry : .values()) {
      result[] = Math.max(result[], vectorEntry
          .getNumNondefaultElements());
    }
    return result;
  }
  
  public Matrix viewPart(int[] offsetint[] size) {
    if (offset[] < 0) {
      throw new IndexException(offset[], rowSize());
    }
    if (offset[] + size[] > rowSize()) {
      throw new IndexException(offset[] + size[], rowSize());
    }
    if (offset[] < 0) {
      throw new IndexException(offset[], columnSize());
    }
    if (offset[] + size[] > columnSize()) {
      throw new IndexException(offset[] + size[], columnSize());
    }
    return new MatrixView(thisoffsetsize);
  }
  
  public Matrix assignColumn(int columnVector other) {
    if (rowSize() != other.size()) {
      throw new CardinalityException(rowSize(), other.size());
    }
    if (column < 0 || column >= columnSize()) {
      throw new IndexException(columncolumnSize());
    }
    for (int row = 0; row < rowSize(); row++) {
      double val = other.getQuick(row);
      if (val != 0.0) {
        Vector r = .get(row);
        if (r == null) {
          r = new RandomAccessSparseVector(columnSize());
          .put(rowr);
        }
        r.setQuick(columnval);
      }
    }
    return this;
  }
  
  public Matrix assignRow(int rowVector other) {
    if (columnSize() != other.size()) {
      throw new CardinalityException(columnSize(), other.size());
    }
    if (row < 0 || row >= rowSize()) {
      throw new IndexException(rowrowSize());
    }
    .put(rowother);
    return this;
  }
  
  public Vector viewRow(int row) {
    if (row < 0 || row >= rowSize()) {
      throw new IndexException(rowrowSize());
    }
    Vector res = .get(row);
    if (res == null) {
      res = new RandomAccessSparseVector(columnSize());
      .put(rowres);
    }
    return res;
  }
  
New to GrepCode? Check out our FAQ X