How to resolve the algorithm Matrix-exponentiation operator step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Matrix-exponentiation operator step by step in the Scala programming language

Table of Contents

Problem Statement

Most programming languages have a built-in implementation of exponentiation for integers and reals only.

Demonstrate how to implement matrix exponentiation as an operator.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Matrix-exponentiation operator step by step in the Scala programming language

Source code in the scala programming language

class Matrix[T](matrix:Array[Array[T]])(implicit n: Numeric[T], m: ClassManifest[T])
{
  import n._
  val rows=matrix.size
  val cols=matrix(0).size
  def row(i:Int)=matrix(i)
  def col(i:Int)=matrix map (_(i))

  def *(other: Matrix[T]):Matrix[T] = new Matrix(
    Array.tabulate(rows, other.cols)((row, col) =>
      (this.row(row), other.col(col)).zipped.map(_*_) reduceLeft (_+_)
  ))

  def **(x: Int)=x match {
    case 0 => createIdentityMatrix
    case 1 => this
    case 2 => this * this
    case _ => List.fill(x)(this) reduceLeft (_*_)
  }
	
  def createIdentityMatrix=new Matrix(Array.tabulate(rows, cols)((row,col) => 
    if (row == col) one else zero)
  )

  override def toString = matrix map (_.mkString("[", ", ", "]")) mkString "\n"
}

object MatrixTest {
  def main(args:Array[String])={
    val m=new Matrix[BigInt](Array(Array(3,2), Array(2,1)))
    println("-- m --\n"+m)
				
    Seq(0,1,2,3,4,10,20,50) foreach {x =>
      println("-- m**"+x+" --")
      println(m**x)
    }
  }
}


  

You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Arturo programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the Maxima programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the Maple programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Harbour programming language