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

  • Home
  • SEARCH
  • 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 6867383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:18:10+00:00 2026-05-27T03:18:10+00:00

I’d like to implement a matrix dot product in Scala in the following way:

  • 0

I’d like to implement a “matrix dot product” in Scala in the following way:

type Real = Double
type Row = Array[Real]
type Matrix = Array[Row]

def dot[T](f: (T,T) => Real)(as: Iterable[T], bs: Iterable[T]): Real =
  (for ((a, b) <- as zip bs) yield f(a, b)) sum

def rowDot(r1: Row, r2: Row) = dot(_*_)(r1, r2)
def matDot(m1: Matrix, m2: Matrix) = dot(rowDot)(m1, m2)

However, the definition of rowDot doesn’t work. Scala needs explicit type annotations for the anonymous function (_*_), so instead I must write

def rowDot(r1: Row, r2: Row) = dot((x:Real, y: Real) => x*y)(r1, r2)

or

def rowDot = dot((x:Real, y: Real) => x*y) _

Is there some way to change the definition of dot so that the shorthand (_*_) can be used?

Edit: Another confusion: matDot also gives type errors in certain circumstances. It fails with Arrays of Arrays, but not with Lists of Arrays

scala> matDot(Array(Array(1.0,2.0)), Array(Array(1.0,2.0,3.0)))
<console>:27: error: type mismatch;
 found   : Array[Array[Double]]
 required: Iterable[Iterable[Real]]
              matDot(Array(Array(1.0,2.0)), Array(Array(1.0,2.0,3.0)))
                          ^

scala> matDot(List(Array(1.0,2.0)), List(Array(1.0,2.0,3.0)))
res135: Real = 5.0

What’s the difference?

  • 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-27T03:18:11+00:00Added an answer on May 27, 2026 at 3:18 am

    specifying dot[Real] explicitly should work too.

    def rowDot(r1: Row, r2: Row) = dot[Real](_*_)(r1, r2)
    

    EDIT

    replying to your edit: I think the issue is that the implicit conversion from Array to WrappedArray is not applied recursively when you have a Array[Array].

    Array[Int] is not an Iterable[Int]; normally, when you assign it to a Iterable, an Array[Int] is implicitly converted to a WrappedArray[Int] (where WrappedArray is a Iterable[Int]). This is what happens when you use List[Array[Int]] (you get a List[WrappedArray[Int]] implicitly).

    However, as I said, the implicit conversion is not applied recursively, so an Array[Array[Int]] is not implicitly converted to WrappedArray[WrappedArray[Int]].

    Here’s a REPL session that demonstrates the problem:

    A List[Array[Int]] can be assigned to Iterable[Iterable[Int]] (note that Array is converted to WrappedArray)

    scala> val i : Iterable[Iterable[Int]] = List(Array(1,2), Array(1,2,3))
    i: Iterable[Iterable[Int]] = List(WrappedArray(1, 2), WrappedArray(1, 2, 3))
    

    An Array[Array[Int]] does not work automatically (as you discovered)

    scala> val j : Iterable[Iterable[Int]] = Array(Array(1,2), Array(1,2,3))
    <console>:9: error: type mismatch;
     found   : Array[Array[Int]]
     required: Iterable[Iterable[Int]]
           val j : Iterable[Iterable[Int]] = Array(Array(1,2), Array(1,2,3))
                                                  ^
    

    However, with some hand-holding (converting manually the inner Arrays to WrappedArrays) everything works again:

        scala> import scala.collection.mutable.WrappedArray
        import scala.collection.mutable.WrappedArray
    
        scala> val k : Iterable[Iterable[Int]] = Array(WrappedArray.make(Array(1,2)),
     WrappedArray.make(Array(1,2,3)))
        k: Iterable[Iterable[Int]] = WrappedArray(WrappedArray(1, 2), WrappedArray(1, 2,
         3))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I

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.