Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to find the norm of a matrix #803

Open
jimka2001 opened this issue Apr 7, 2021 · 4 comments
Open

how to find the norm of a matrix #803

jimka2001 opened this issue Apr 7, 2021 · 4 comments

Comments

@jimka2001
Copy link

I'm trying to call the norm function, as documented in the cheat sheet.
https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet

Here is the code I'm trying to compile, and the compilation error is shown below.
What is the correct way to find out if two matrices of the same dimensions are equal within a given epsilon? Am I doing it wrong?

test("breeze"){
  import breeze.linalg._
  import scala.util.Random
  for{dim <- 1 to 4} {
    val dm1 = DenseMatrix.tabulate(dim, dim)((_,_)=>Random.between(-10.0,10.0))
    val svd.SVD(u, s, vt) = svd(dm1)
    val dm2 = u * diag(s) * vt.t
    val dist:Double = norm(dm2 - dm1) /// <--- this fails to compile
    assert(dist < 0.001, s"dist=$dist dm1=[$dm1] dm2=[$dm2]")
  }
}

But I get a compilation error about a missing implicit. I don't see information in the cheat sheet about how to get the implicits.

/Users/jnewton/Repos/scalain_e/scalain-e-course-code/src/test/scala/TranscendentalsTestSuite.scala:493:29
could not find implicit value for parameter impl: breeze.linalg.norm.Impl[breeze.linalg.DenseMatrix[Double],VR]
      val dist:Double = norm(dm2 - dm1)
@dlwh
Copy link
Member

dlwh commented Apr 7, 2021 via email

@jimka2001
Copy link
Author

jimka2001 commented Apr 8, 2021

For testing whether two matrices are equal within epsilon, I think any norm works.
How do people normally check whether two matrices are almost equal?
Is flatting to a vector, and using the vector norm, the usual way?
Or is there some sort of fold/reduce function I should be using, which should be faster and more memory efficient anyway?

@darrenjw
Copy link
Contributor

darrenjw commented Apr 8, 2021

For testing purposes (eg. in unit and property based tests), I tend to just use sum(abs(A)). But I think it would make sense to have the Frobenious norm in Breeze. Flattening and applying the vector norm should be quite efficient, I think.

@jimka2001
Copy link
Author

I'm using max(abs(...))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants