<commit_before>package breeze.linalg

import breeze.storage.DenseStorage
import operators._
import org.netlib.util.intW
import org.netlib.lapack.LAPACK
import org.netlib.blas.{Dgemm, BLAS}
import breeze.util.ArrayUtil
import breeze.numerics.IntMath
import support._
import breeze.generic.CanMapValues

/**
 *
 * @author dlwh
 */

final class DenseMatrix[@specialized V](val data: Array[V],
                                        val rows: Int,
                                        val cols: Int,
                                        val majorStride: Int,
                                        val offset: Int = 0,
                                        val isTranspose: Boolean = false) extends StorageMatrix[V] with MatrixLike[V, DenseMatrix[V]] with DenseStorage[V] {
  def this(data: Array[V], rows: Int, cols: Int) = this(data, rows, cols, rows)
  def this(data: Array[V], rows: Int) = this(data, rows, {assert(data.length % rows == 0); data.length/rows})

  @inline final def apply(row: Int, col: Int) = {
    if(row < 0 || row > rows) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
    if(col < 0 || col > cols) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
      rawApply(linearIndex(row, col))
  }


  @inline final def linearIndex(row: Int, col: Int): Int = {
    if(isTranspose)
      offset + col + row * majorStride
    else
      offset + row + col * majorStride
  }

  @inline
  final def update(row: Int, col: Int, v: V) {
    if(row < 0 || row > rows) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
    if(col < 0 || col > cols) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
    rawUpdate(linearIndex(row, col), v)
  }

  def repr = this

  def activeIterator = iterator

  def activeValuesIterator = valuesIterator

  def activeKeysIterator = keysIterator

  def trace(implicit numeric: Numeric[V]) = diag(this).sum

  override def equals(p1: Any) = p1 match {
    case x: DenseMatrix[_] =>
      // todo: make this faster in obvious cases
      rows == x.rows && cols == x.cols && (valuesIterator sameElements x.valuesIterator )

    case _ => false
  }


  override def toString() = {
    // TODO:
    valuesIterator.mkString("DenseMatrix(",", ", ")")
  }
}

object DenseMatrix extends LowPriorityDenseMatrix
                           with DenseMatrixOps_Int
                           with DenseMatrixOps_Float
                           with DenseMatrixOps_Double with DenseMatrixMultiplyStuff {
  def zeros[V:ClassManifest](rows: Int, cols: Int) = {
    val data = new Array[V](rows * cols)
    new DenseMatrix(data, rows, cols)
  }

  /** Creates a dense matrix of the given value repeated of the requested size. */
  def fill[V:ClassManifest](rows: Int, cols: Int)(value: =>V) = {
    new DenseMatrix[V](Array.fill(rows * cols)(value), rows, cols)
  }

  def tabulate[V:ClassManifest](rows : Int, cols : Int)(fn : (Int, Int) => V) = {
    new DenseMatrix(Array.tabulate(rows * cols)(i => fn(i % rows, i / rows)), rows, cols)
  }

  /** Static constructor for a literal matrix. */
  def apply[R,V](rows : R*)(implicit rl : LiteralRow[R,V], man : ClassManifest[V]) = {
    val nRows = rows.length
    val ns = rl.length(rows(0))<commit_msg>DenseMAtrix.eye<commit_after>package breeze.linalg

import breeze.storage.DenseStorage
import operators._
import org.netlib.util.intW
import org.netlib.lapack.LAPACK
import org.netlib.blas.{Dgemm, BLAS}
import breeze.util.ArrayUtil
import breeze.numerics.IntMath
import support._
import breeze.generic.CanMapValues
import breeze.math.Semiring

/**
 *
 * @author dlwh
 */

final class DenseMatrix[@specialized V](val data: Array[V],
                                        val rows: Int,
                                        val cols: Int,
                                        val majorStride: Int,
                                        val offset: Int = 0,
                                        val isTranspose: Boolean = false) extends StorageMatrix[V] with MatrixLike[V, DenseMatrix[V]] with DenseStorage[V] {
  def this(data: Array[V], rows: Int, cols: Int) = this(data, rows, cols, rows)
  def this(data: Array[V], rows: Int) = this(data, rows, {assert(data.length % rows == 0); data.length/rows})

  @inline final def apply(row: Int, col: Int) = {
    if(row < 0 || row > rows) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
    if(col < 0 || col > cols) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
      rawApply(linearIndex(row, col))
  }


  @inline final def linearIndex(row: Int, col: Int): Int = {
    if(isTranspose)
      offset + col + row * majorStride
    else
      offset + row + col * majorStride
  }

  @inline
  final def update(row: Int, col: Int, v: V) {
    if(row < 0 || row > rows) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
    if(col < 0 || col > cols) throw new IndexOutOfBoundsException((row,col) + " not in [0,"+rows+") x [0," + cols+")")
    rawUpdate(linearIndex(row, col), v)
  }

  def repr = this

  def activeIterator = iterator

  def activeValuesIterator = valuesIterator

  def activeKeysIterator = keysIterator

  def trace(implicit numeric: Numeric[V]) = diag(this).sum

  override def equals(p1: Any) = p1 match {
    case x: DenseMatrix[_] =>
      // todo: make this faster in obvious cases
      rows == x.rows && cols == x.cols && (valuesIterator sameElements x.valuesIterator )

    case _ => false
  }


  override def toString() = {
    // TODO:
    valuesIterator.mkString("DenseMatrix(",", ", ")")
  }
}

object DenseMatrix extends LowPriorityDenseMatrix
                           with DenseMatrixOps_Int
                           with DenseMatrixOps_Float
                           with DenseMatrixOps_Double with DenseMatrixMultiplyStuff {
  def zeros[V:ClassManifest](rows: Int, cols: Int) = {
    val data = new Array[V](rows * cols)
    new DenseMatrix(data, rows, cols)
  }

  def eye[V: ClassManifest:Semiring](dim: Int) = {
    val r = zeros[V](dim, dim)
    diag(r) := implicitly[Semiring[V]].one
    r
  }

  /** Creates a dense matrix of the given value repeated of the requested size. */
  def fill[V:ClassManifest](rows: Int, cols: Int)(value: =>V) = {
    new DenseMatrix[V](Array.fill(rows * cols)(value), rows, cols)
  }

  def tabulate[V:ClassManifest](rows : Int, cols : Int)(fn : (Int, Int) => V) = {
    new DenseMatrix(Array.tabulate(rows * cols)(i => fn(i % rows, i / rows)), rows, cols)
  }

  /** Static constructor for a literal matrix. */
  def apply[R,V](rows : R*)(implicit rl : LiteralRow[R,V], man : ClassManifest[V]) = {
    val nRows = rows.length
    val ns = rl.length(rows(0))
{'fraction_non_alphanumeric': 0.13601868067717454, 'fraction_numerical': 0.0049620548744892, 'mean_word_length': 2.7083333333333335, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3133235', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2181, 'n_words': 610}