<commit_before>package breeze.linalg
/*
 Copyright 2016 @author claydonkey (<NAME>)

 Licensed 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.
*/
import breeze.numerics._
import breeze.math._
import breeze.linalg.Helper._
import breeze.generic.UFunc

/* This class represents a Jacobi or Givens rotation.
 * This is a 2D rotation in the plane \c J of angle \f$ \theta \f$ defined by
 * its cosine \c c and sine \c s as follow:
 * \f$ J = \left ( \begin{array}{cc} c & \overline s \\ -s  & \overline c \end{array} \right ) \f$
 *
 * You can apply the respective counter-clockwise rotation to a column vector \c v by
 * applying its adjoint on the left: \f$ v = J^* v \f$
 */
object jacobi extends UFunc {

  def apply(M: DenseMatrix[Complex]): Jacobi = new Jacobi(M)

  class Jacobi(val M: DenseMatrix[Complex]) {
    object GivensType {
      sealed trait EnumVal
      case object Undefined extends EnumVal
      case object Right extends EnumVal
      case object Left extends EnumVal
      val givensType = Seq(Right, Left)
    }
    var cRot: JRotation = new JRotation(null, null, null)
    var vPos: Int = 0
    var hPos: Int = 0
    var typeRot: GivensType.EnumVal = GivensType.Undefined

//mutating (speedier)
    def rotateL(jrot: JRotation) = { cRot = new JRotation(conj(jrot.m_c), -(jrot.m_s), jrot.rot); jacobi.applyRotationinPlane(M(0, ::).t, M(1, ::).t, cRot) }
    def rotateR(jrot: JRotation) = { cRot = new JRotation((jrot.m_c), -conj(jrot.m_s), jrot.rot); jacobi.applyRotationinPlane(M(::, 0), M(::, 1), cRot) }

    //non-mutating
    def rotateL(jrot: JRotation, vpos: Int, hpos: Int) = { typeRot = GivensType.Left; vPos = vpos; hPos = hpos; cRot = jrot; getGivens(cRot, vPos, hPos) * M }
    def rotateR(jrot: JRotation, vpos: Int, hpos: Int) = { typeRot = GivensType.Right; vPos = vpos; hPos = hpos; cRot = jrot; getGivens(cRot, vPos, hPos) * M }

    //non-mutating using Givens (p,q)
    def rotateL(p: Complex, q: Complex, vPos: Int, hPos: Int): DenseMatrix[Complex] = rotateL(makeGivens(p, q), vPos, hPos)
    def rotateR(p: Complex, q: Complex, vPos: Int, hPos: Int): DenseMatrix[Complex] = rotateR(makeGivens(p, q), vPos, hPos)

    /* Method fo Givens QR decomposition

    hPos defines the column where the Givens is extracted from
    vPos defines both the diag the Givens rotation is placed, and the row the Givens is extracted from

    RotateR
    Example 1:

    [x x x x]
    [x x x x]
    [i x x x]
    [j x x x]   <= index = (0,0)

    Gives Givens:
    [1 0 0 0]
    [0 1 0 0]
    [0 0 c -s]
    [0 0 s c]

    RotateR
    Example 2:

    [x i x x]
    [x j x x]
    [x x x x]
    [x x x x]   <= index = (2,1)

    Gives Givens:
    [c -s 0 0]
    [s c -s 0]
    [0 0 1  0]
    [0 0 0 1]
*/
    def rotateL(vpos: Int = 0, hpos: Int = 0) :DenseMatrix[Complex] = { typeRot = GivensType.Left; vPos = vpos; hPos = hpos; cRot = makeGivens(M(vPos, hPos), M(vPos + 1, hPos)); getGivens(cRot, vPos, hPos) * M }
    def rotateR(vpos: Int = 0, hpos: Int = 0)  :DenseMatrix[Complex]  = { typeRot = GivensType.Right; vPos = vpos; hPos = hpos; cRot = makeGivens(M(M.rows - (vPos + 2), hPos), M(M.rows - (vpos + 1), hPos)); getGivens(cRot, vPos, hPos) * M }

       def rotate2L(vpos: Int = 0, hpos: Int = 0) : ( DenseMatrix[Complex] ,DenseMatrix[Complex] ) = { typeRot = GivensType.Left; vPos = vpos; hPos = hpos; cRot = makeGivens(M(vPos, hPos), M(vPos + 1, hPos)); (getGivens(cRot, vPos, hPos) * M, getGivens(cRot, vPos, hPos) )}
    def rotate2R(vpos: Int = 0, hpos: Int = 0)  : ( DenseMatrix[Complex] ,DenseMatrix[Complex] ) = { typeRot = GivensType.Right; vPos = vpos; hPos = hpos; cRot = makeGivens(M(M.rows - (vPos + 2), hPos), M(M.rows - (vpos + 1), hPos)); getGivens(cRot, vPos, hPos) * M }


//semi static method
    /* returns the Givens Matrix G used in Rotation */
    def getGivens(jrot: JRotation, vpos: Int, hpos: Int): DenseMatrix[Complex] = {

      vPos = vpos
      hPos = hpos
      cRot = jrot

      typeRot match {
        case GivensType.Right =>
          val m = DenseMatrix.eye[Complex](M.rows)
          m((m.cols - (vPos + 2)) to (m.cols - (vPos + 1)), (m.cols - (vPos + 2)) to (m.cols - (vPos + 1))) := DenseMatrix((jrot.m_c, -conj(jrot.m_s)), (jrot.m_s, jrot.m_c))
          m
        case GivensType.Left =>
          val m = DenseMatrix.eye[Complex](M.cols)
          m(vPos to (vPos + 1), vPos to (vPos + 1)) := DenseMatrix((jrot.m_c, -conj(jrot.m_s)), (jrot.m_s, jrot.m_c))
          m
        case _ => throw new IllegalArgumentException("Rotation Type not defined")
    }
      }
  }

  private def applyRotationinPlane(_x: DenseVector[Complex], _y: DenseVector[Complex], j: JRotation) = {

    if (j.m_c == 1 && j.m_s == 0)
      DenseMatrix.zeros[Complex](_x.size, 2)

    val x1 = DenseVector.tabulate[Complex](_x.length) { (i) => j.m_c * _x(i) + conj(j.m_s) * _y(i) }
    val y1 = DenseVector.tabulate[Complex](_y.length) { (i) => -j.m_s * _x(i) + conj(j.m_c) * _y(i) }
    val res = DenseMatrix.vertcat(x1.t, y1.t)
    val res1 = DenseVector.horzcat(x1, y1)

    val i = 0

    for (i <- 0 to _x.length - 1) {
      val xi = _x(i)
      val yi = _y(i)
      _x(i) = j.m_c * xi + conj(j.m_s) * yi
      _y(i) = -j.m_s * xi + conj(j.m_c) * yi<commit_msg>added givens methods<commit_after>package breeze.linalg
/*
 Copyright 2016 @author claydonkey (<NAME>)

 Licensed 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.
*/
import breeze.numerics._
import breeze.math._
import breeze.linalg.Helper._
import breeze.generic.UFunc

/* This class represents a Jacobi or Givens rotation.
 * This is a 2D rotation in the plane \c J of angle \f$ \theta \f$ defined by
 * its cosine \c c and sine \c s as follow:
 * \f$ J = \left ( \begin{array}{cc} c & \overline s \\ -s  & \overline c \end{array} \right ) \f$
 *
 * You can apply the respective counter-clockwise rotation to a column vector \c v by
 * applying its adjoint on the left: \f$ v = J^* v \f$
 */
object jacobi extends UFunc {

  def apply(M: DenseMatrix[Complex]): Jacobi = new Jacobi(M)

  class Jacobi(val M: DenseMatrix[Complex]) {
    object GivensType {
      sealed trait EnumVal
      case object Undefined extends EnumVal
      case object Right extends EnumVal
      case object Left extends EnumVal
      val givensType = Seq(Right, Left)
    }
    var cRot: JRotation = new JRotation(null, null, null)
    var vPos: Int = 0
    var hPos: Int = 0
    var typeRot: GivensType.EnumVal = GivensType.Undefined

    //mutating (speedier)
    def rotateL(jrot: JRotation) = { cRot = new JRotation(conj(jrot.m_c), -(jrot.m_s), jrot.rot); jacobi.applyRotationinPlane(M(0, ::).t, M(1, ::).t, cRot) }
    def rotateR(jrot: JRotation) = { cRot = new JRotation((jrot.m_c), -conj(jrot.m_s), jrot.rot); jacobi.applyRotationinPlane(M(::, 0), M(::, 1), cRot) }

    //non-mutating
    def rotateL(jrot: JRotation, vpos: Int, hpos: Int) = { typeRot = GivensType.Left; vPos = vpos; hPos = hpos; cRot = jrot; getGivens(cRot, vPos, hPos) * M }
    def rotateR(jrot: JRotation, vpos: Int, hpos: Int) = { typeRot = GivensType.Right; vPos = vpos; hPos = hpos; cRot = jrot; getGivens(cRot, vPos, hPos) * M }

    //non-mutating using Givens (p,q)
    def rotateL(p: Complex, q: Complex, vPos: Int, hPos: Int): DenseMatrix[Complex] = rotateL(makeGivens(p, q), vPos, hPos)
    def rotateR(p: Complex, q: Complex, vPos: Int, hPos: Int): DenseMatrix[Complex] = rotateR(makeGivens(p, q), vPos, hPos)

    /* Method fo Givens QR decomposition

    hPos defines the column where the Givens is extracted from
    vPos defines both the diag the Givens rotation is placed, and the row the Givens is extracted from

    RotateR
    Example 1:

    [x x x x]
    [x x x x]
    [i x x x]
    [j x x x]   <= index = (0,0)

    Gives Givens:
    [1 0 0 0]
    [0 1 0 0]
    [0 0 c -s]
    [0 0 s c]

    RotateR
    Example 2:

    [x i x x]
    [x j x x]
    [x x x x]
    [x x x x]   <= index = (2,1)

    Gives Givens:
    [c -s 0 0]
    [s c -s 0]
    [0 0 1  0]
    [0 0 0 1]
*/
    def rotateL(vpos: Int = 0, hpos: Int = 0): DenseMatrix[Complex] = { typeRot = GivensType.Left; vPos = vpos; hPos = hpos; cRot = makeGivens(M(vPos, hPos), M(vPos + 1, hPos)); getGivens(cRot, vPos, hPos) * M }
    def rotateR(vpos: Int = 0, hpos: Int = 0): DenseMatrix[Complex] = { typeRot = GivensType.Right; vPos = vpos; hPos = hpos; cRot = makeGivens(M(M.rows - (vPos + 2), hPos), M(M.rows - (vpos + 1), hPos)); getGivens(cRot, vPos, hPos) * M }
    /*returns tuple of Matrix A transformed and Givens Rotation Matrix (useful for QR decomp) */
    def rotate2L(vpos: Int = 0, hpos: Int = 0): (DenseMatrix[Complex], DenseMatrix[Complex]) = { typeRot = GivensType.Left; vPos = vpos; hPos = hpos; cRot = makeGivens(M(vPos, hPos), M(vPos + 1, hPos)); (getGivens(cRot, vPos, hPos) * M, getGivens(cRot, vPos, hPos)) }
    /*returns tuple of Matrix A transformed and Givens Rotation Matrix (useful for QR decomp) */
    def rotate2R(vpos: Int = 0, hpos: Int = 0): (DenseMatrix[Complex], DenseMatrix[Complex]) = { typeRot = GivensType.Right; vPos = vpos; hPos = hpos; cRot = makeGivens(M(M.rows - (vPos + 2), hPos), M(M.rows - (vpos + 1), hPos)); (getGivens(cRot, vPos, hPos) * M, getGivens(cRot, vPos, hPos)) }

    //semi static method
    /* returns the Givens Matrix G used in Rotation */
    def getGivens(jrot: JRotation, vpos: Int, hpos: Int): DenseMatrix[Complex] = {

      vPos = vpos
      hPos = hpos
      cRot = jrot

      typeRot match {
        case GivensType.Right =>
          val m = DenseMatrix.eye[Complex](M.rows)
          m((m.cols - (vPos + 2)) to (m.cols - (vPos + 1)), (m.cols - (vPos + 2)) to (m.cols - (vPos + 1))) := DenseMatrix((jrot.m_c, -conj(jrot.m_s)), (jrot.m_s, jrot.m_c))
          m
        case GivensType.Left =>
          val m = DenseMatrix.eye[Complex](M.cols)
          m(vPos to (vPos + 1), vPos to (vPos + 1)) := DenseMatrix((jrot.m_c, -conj(jrot.m_s)), (jrot.m_s, jrot.m_c))
          m
        case _ => throw new IllegalArgumentException("Rotation Type not defined")
    }
      }

    def getGivens(vpos: Int, hpos: Int): DenseMatrix[Complex] = getGivens(cRot, vpos, hpos)
    def getGivens: DenseMatrix[Complex] = getGivens(cRot, vPos, hPos)
  }

  private def applyRotationinPlane(_x: DenseVector[Complex], _y: DenseVector[Complex], j: JRotation) = {

    if (j.m_c == 1 && j.m_s == 0)
      DenseMatrix.zeros[Complex](_x.size, 2)

    val x1 = DenseVector.tabulate[Complex](_x.length) { (i) => j.m_c * _x(i) + conj(j.m_s) * _y(i) }
    val y1 = DenseVector.tabulate[Complex](_y.length) { (i) => -j.m_s * _x(i) + conj(j.m_c) * _y(i) }
    val res = DenseMatrix.vertcat(x1.t, y1.t)
    val res1 = DenseVector.horzcat(x1, y1)

    val i = 0

    for (i <- 0 to _x.length - 1) {
      val xi = _x(i)
      val yi = _y(i)
      _x(i) = j.m_c * xi + conj(j.m_s) * yi
      _y(i) = -j.m_s * xi + conj(j.m_c) * yi
{'fraction_non_alphanumeric': 0.13934913756478884, 'fraction_numerical': 0.013425099838558927, 'mean_word_length': 2.965633423180593, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984007', 'n_tokens_mistral': 4731, 'n_tokens_neox': 4527, 'n_words': 1407}