uncomplicate / neanderthal

Fast Clojure Matrix Library
http://neanderthal.uncomplicate.org
Eclipse Public License 1.0
1.06k stars 56 forks source link

ev with double lower diagonal fails with abstract method error #120

Closed cnuernber closed 2 years ago

cnuernber commented 2 years ago

I am computing PCA via the covariance method which boils down to a triangular matrix:

tech.v3.dataset.tensor> (def ld (fit-pca-neanderthal! (dtype/clone test-tens) {:method :cov}))
#'tech.v3.dataset.tensor/ld
tech.v3.dataset.tensor> ld
#RealUploMatrix[double, type:tr, mxn:3x3, layout:column, offset:0]
   ▥       ↓       ↓       ↓       ┓    
   →       2.32    *       *            
   →       1.61    2.50    *            
   →      -0.43   -1.28    7.88         
   ┗                               ┛    
tech.v3.dataset.tensor> (linalg/ev ld)
Execution error (AbstractMethodError) at uncomplicate.neanderthal.linalg/ev! (linalg.clj:605).
uncomplicate.neanderthal.internal.host.mkl.DoubleTREngine.ev(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;

The actual transformation is here. I would like to use view-tr and then ev on that as opposed to ev on the entire covariance matrix.

blueberry commented 2 years ago

Hi Chris,

In this particular ev invocation, the problem is that the ev method is not supported for triangular matrices. However, this is not the real problem in this code, since you intend to call ev on the covariance matrix, which should be symmetrical instead of triangular. I know, technically they may look similar, but there is a big difference in semantics: the ld matrix in your code (being triangular) implies zero values where asterisks are displayed, which obviously is not what you intended to do.

In short, the ld matrix should either be a symmetrical matrix (SY), or a GE matrix in which you make sure that upper and lower triangles are the same.

Also, it may be interesting (and useful) to you that there is a whole dedicated chapter on implementing and optimizing PCA in my book Numerical Linear Algebra for Programmers (pages 195-211) https://aiprobook.com/numerical-linear-algebra-for-programmers/

cnuernber commented 2 years ago

view-sy worked like a charm, thanks. Closing issue.

I am working on a java API for tmd which includes exposing neanderthal. Neanderthal itself I think could have a fairly straight forward java API and that may allow you access to more users as it is a much better library in nearly all respects than dl4j.

blueberry commented 2 years ago

as it is a much better library in nearly all respects than dl4j.

Thank you!