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 622089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:56:47+00:00 2026-05-13T18:56:47+00:00

I am very new to Scala. I want to implement a generic matrix class

  • 0

I am very new to Scala.

I want to implement a generic matrix class “class Matrix[T]”. The only constraint on T should be that T should implement a “+” and a “*” mothod/function. How do I go about doing this?

For example I want to be able to use both Int, Double, and my own defined types e.g. Complex

I was thinking something along the lines:

class Matrix[T <: MatrixElement[T]](data: Array[Array[T]]) {
   def *(that: Matrix) = ..// code that uses "+" and "*" on the elements
}
abstract class MatrixElement[T] {
    def +(that: T): T
    def *(that: T): T 
}
implicit object DoubleMatrixElement extends MatrixElement[Double]{
    def +(that: Double): Double = this + that
    def *(that: Double): Double = this * that 
}
implicit object ComplexMatrixElement extends MatrixElement[Complex]{
    def +(that: Complex): Complex = this + that
    def *(that: Complex): Complex = this * that 
}

Everything type checks but I still can’t instantiate a matrix. Am I missing an implicit constructor? How would I go about making that? Or am I completely wrong about my method?

Thanks in advance
Troels

  • 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-05-13T18:56:48+00:00Added an answer on May 13, 2026 at 6:56 pm

    Finally found the answer 🙂 I think I wasn’t that far off in my first try.
    Here it goes: (written for scala 2.8)

    trait MatrixElement[T] {
        def +(that: T): T
        def *(that: T): T 
    }
    
    object MatrixElement {
        implicit def intToMatrixElement(x : Int) = new MatrixElement[Int] {
            def +(y : Int) = x + y
            def *(y : Int) = x * y
        }
        implicit def doubleToMatrixElement(x : Double) = new MatrixElement[Double] {
            def +(y : Double) = x + y
            def *(y : Double) = x * y
        }
        implicit def complexToMatrixElement(x : Complex) = new MatrixElement[Complex] {
            def +(y : Complex) = x + y
            def *(y : Complex) = x * y
        }
    }
    
    class Matrix[T  <% MatrixElement[T] : ClassManifest ](d: Array[Array[T]]) {
        def *(that: Matrix) = ..// code that uses "+" and "*" on the elements
    }
    

    Now I can do stuff like:

    scala> new Matrix(Array(Array(1,0),Array(0,1)))
    res0: Matrix[Int] = 
    1 0 
    0 1 
    
    scala> new Matrix(Array(Array(new Complex(0),new Complex(1)),Array(new Complex(1),new Complex(0))))
    res9: Matrix[Complex] = 
    (0.0,0.0i) (1.0,0.0i) 
    (1.0,0.0i) (0.0,0.0i) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add new function extra to library class (orig for example). orig.extra(rich.Arg())
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Very new to XSL (and XML for that matter), but I need to step
I am very new to C++ programming and you will see why. I want
I have a list in scala called l : List[AType] that I want to
I'm very new to Scala and I'm still trying to get used to the
Scala implicits are very powerfull. I'm curious if they are a new/unique feature of
I am a very new bie for scala. So I download the source code
I'm very new to Scala, but from what I've read it seems the ideal
This a very simple scala sample, but it can't be compiled: abstract class Box[+A]

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.