Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7880439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:08:42+00:00 2026-06-03T04:08:42+00:00

What would be the best way to extend this class to have power method

  • 0

What would be the best way to extend this class to have power method that would return the matrix raised to the nth power.

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

What I am trying to do is to calculate large Fibonacci numbers using matrices.

From wikipdia

These are the changes I have made so far to the original code. I am receiving StackOverflow exception.

File 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)
}

File 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
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:08:43+00:00Added an answer on June 3, 2026 at 4:08 am

    Extending a case class—like RichMatrix here—is a bad idea, and is deprecated in recent versions of Scala. You can, however, follow the author’s pattern and use the pimp-my-library approach:

    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
      }
    }
    

    Now List(List(2.0, 0.0), List(0.0, 2.0)).power(3) for example should give the following:

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

    You’d be better off using a serious Scala or Java matrix library, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to detect newline return method in PHP. CR,
I have an abstract class, Foo, that has a non-abstract method called Bar. I
I'm trying to figure out what the best way to tackle this would be.
I have a class Game that uses a class TCPServer . I would like
I want to make a code snippet database web application. Would the best way
What would be the best way to version a rails application? We want to
What would be the best way to properly concatenate string with an url inside?
What would be the best way and more idiomatic to break a string into
What would be the best way to convert a 50-digit String to a BigInteger
What would be the best way in Python to parse out chunks of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.