2

このクラスを拡張して、行列を n 乗して返す power メソッドを持たせる最善の方法は何でしょうか。

https://github.com/eobrain/scalaclass/blob/master/matrix/src/main/scala/org/eamonn/published_matrix/PublishedMatrix.scala

私がやろうとしているのは、行列を使用して大きなフィボナッチ数を計算することです。

ウィキペディアより

これらは、これまでに元のコードに加えた変更です。StackOverflow 例外が発生しています。

ファイル Row.scala

/*
* Copyright (c) 2010 Eamonn O'Brien-Strain, eob@well.com
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which is available at http://www.eclipse.org/legal/epl-v10.html
*/

package org.eamonn.published_matrix

import Row._

/** Methods that are added to List[BigInt] by an implicit conversion */
case class RichRow(v:Row){

  /** dot product */
  def *(that:RichRow) = dotProd( this.v, that.v )

  /** vector addition */
  def add(that:RichRow) = vPlusV( this.v, that.v )

  /** convert to column vector */
  def T = v.map{ List(_) }

  /** As row matrix */
  def asMatrix = List( v )
}

object Row{

  /** A convenient alias */
  type Row = List[BigInt]

  def dotProd(v1:Row,v2:Row) = 
              v1.zip( v2 ).map{ t:(BigInt,BigInt) => t._1 * t._2 }.reduceLeft(_ + _)

  def vPlusV(v1:Row,v2:Row) = 
             v1.zip( v2 ).map{ t:(BigInt,BigInt) => t._1 + t._2 }

  /** effectively add RichRow methods to List[Double] */
  implicit def pimp(v:Row) = new RichRow(v)
}

ファイル Matrix.scala

/*
* Copyright (c) 2010 Eamonn O'Brien-Strain, eob@well.com
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which is available at http://www.eclipse.org/legal/epl-v10.html
*/

package org.eamonn.published_matrix

import Matrix._
import Row._
import Seq.Projection

/** Methods that are added to List[List[BigInt]] by an implicit conversion */
case class RichMatrix(m:Matrix){

  def T = transpose(m)

  def *(that:RichMatrix) = mXm( this.m, that.m )

  def power (exp:Int) = recPower(this.m, exp)

  def recPower(m:Matrix, exp:BigInt) : Matrix = 
         if (exp == 1) m else mXm(m, recPower(m, exp - 1))

  def apply(i:Int,j:Int) = m(i)(j)

  def rowCount = m.length
  def colCount = m.head.length

  def toStr = "\n" + m.map { _.map{"\t" + _}.reduceLeft(_ + _) + "\n" }.reduceLeft(_ + _)
}

object Matrix{

  /** A convenient alias */
  type Matrix = List[Row]

  def apply( rowCount:Int, colCount:Int )( f:(Int,Int) => BigInt ) = (
     for(i <- 1 to rowCount) yield
        ( for( j <- 1 to colCount) yield f(i,j) ).toList
     ).toList

  def transpose(m:Matrix):Matrix =
      if(m.head.isEmpty) Nil else m.map(_.head) :: transpose(m.map(_.tail))

  def mXv(m:Matrix, v:Row) = m.map{ dotProd(_,v) } reduceLeft ( _ + _ )

  def mXm( m1:Matrix, m2:Matrix ) =
     for( m1row <- m1 ) yield
        for( m2col <- transpose(m2) ) yield
           dotProd( m1row, m2col )

  def rowCount(m:Matrix) = m.length
  def colCount(m:Matrix) = m.head.length

  /** effectively add RichMatrix methods to List[List[BigInt]] */
  implicit def pimp1(m:Matrix) = new RichMatrix(m)
  implicit def pimp2(m:List[Projection[BigInt]]) = new RichMatrix(m.map{_.toList})
  implicit def pimp1(m:Projection[List[BigInt]]) = new RichMatrix(m.toList)
  implicit def pimp2(m:Projection[Projection[BigInt]]) = new RichMatrix(m.map{_.toList}.toList)

  // Suggested by Travis Brown - Not working
  //  implicit def toRichMatrixWithPower(m: Matrix) = new {
  //    val matrix = new RichMatrix(m)
  //    def power(n: Int) = {
  //      require(matrix.rowCount == matrix.colCount)
  //      Iterator.iterate(matrix)(_ * matrix).drop(n - 1).next
  //    }
  //  }

  def main(args: Array[String]): Unit =
  {
    val m = List(List[BigInt](1, 1), List[BigInt](1, 0))

    println((m power 9999)(0)(1)) //java.lang.StackOverflowError
  }
}
4

3 に答える 3

4

ここのようにケースクラスを拡張することRichMatrixは悪い考えであり、最近のバージョンのScalaでは非推奨になっています。ただし、作成者のパターンに従って、pimp-my-libraryアプローチを使用することはできます。

import org.eamonn.published_matrix._
import org.eamonn.published_matrix.Matrix._

implicit def toRichMatrixWithPower(m: Matrix) = new {
  val matrix = new RichMatrix(m)
  def power(n: Int) = {
    require(matrix.rowCount == matrix.colCount)
    Iterator.iterate(matrix)(_ * matrix).drop(n - 1).next
  }
}

List(List(2.0, 0.0), List(0.0, 2.0)).power(3)たとえば、次のようになります。

RichMatrix(List(List(8.0, 0.0), List(0.0, 8.0)))

ただし、本格的なScalaまたはJavaマトリックスライブラリを使用する方がよいでしょう。

于 2012-05-04T23:27:59.917 に答える
0

これが最終的な作業コードです。ありがとう@travis-brown

ファイルMatrix.scala

/*
* Copyright (c) 2010 Eamonn O'Brien-Strain, eob@well.com
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which is available at http://www.eclipse.org/legal/epl-v10.html
*/

package org.eamonn.published_matrix

import Matrix._
import Row._

/** Methods that are added to List[List[Double]] by an implicit conversion */
case class RichMatrix(m:Matrix){

  def T = transpose(m)

  def *(that:RichMatrix) = mXm( this.m, that.m )

  def apply(i:Int,j:Int) = m(i)(j)

  def rowCount = m.length
  def colCount = m.head.length

  def toStr = "\n" + m.map { _.map{"\t" + _}.reduceLeft(_ + _) + "\n" }.reduceLeft(_ + _)
}

object Row{

  /** A convenient alias */
  type Row = List[BigInt]

  def dotProd(v1:Row,v2:Row) = v1.zip( v2 ).map{ t:(BigInt,BigInt) => t._1 * t._2 }.reduceLeft(_ + _)
}

object Matrix{

  /** A convenient alias */
  type Matrix = List[Row]

  def transpose(m:Matrix):Matrix =
    if(m.head.isEmpty) Nil else m.map(_.head) :: transpose(m.map(_.tail))

  def mXm( m1:Matrix, m2:Matrix ) =
    for( m1row <- m1 ) yield
      for( m2col <- transpose(m2) ) yield
        dotProd( m1row, m2col )

  def rowCount(m:Matrix) = m.length
  def colCount(m:Matrix) = m.head.length


  /** effectively add RichMatrix methods to List[List[Double]] */
  implicit def pimp1(m:Matrix) = new RichMatrix(m)

  implicit def matrixPower(m: Matrix) = new {
    //val matrix = new RichMatrix(m)
    def power(n: Int) = {
      require(m.rowCount == m.colCount)
      Iterator.iterate(m)(_ * m).drop(n - 1).next
    }
  }


  def main(args: Array[String]): Unit =
  {
    val m = List(List[BigInt](1, 1), List[BigInt](1, 0))

    println(m.power(9)(1)(0))
  }
}
于 2012-05-05T10:53:21.497 に答える