uncomplicate / neanderthal

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

vector addition #123

Closed cgdeboer closed 2 years ago

cgdeboer commented 2 years ago

@blueberry or others, I'm new to neanderthal (but not to scientific programming) and am considering it for replacing a python/numpy service I currently maintain. One thing that has plagued me so far is access to (or ability to locate) some basic vector management functions. e.g

These are certainly easily solvable in plain clojure but wanted to see if there was an opportunity to keep the vector math a little speedier. Am I not seeing these in the docs, or are those simpler sorts of vector manipulation not included in this lib.

Thanks for the help !

kxygk commented 2 years ago

There are several issues:

  1. Adding a constant to every entry in a vector is not typically a sensible operation.
  2. You are thinking in terms of napkin math and not in BLAS terms. The BLAS API doesn't really match 1-to-1 to textbook linear algebra. It's very well thought out and ensures you have good use of both memory, CPU cycles and cache locality. If you find you are fighting the BLAS API then that's a code smell and typically means you should rethink your algorithm

If you really need to add a constant then you should use axpy. Just use your favorite search engine and look for "add a constant to a vector in BLAS". The first result is:

https://stackoverflow.com/questions/14051064/add-scalar-to-vector-in-blas-cublas-cuda

You'll see examples in C++ but doing the same in neanderthal will be a million times easier

blueberry commented 2 years ago

If you really need to add a constant then you should use axpy. Just use your favorite search engine and look for "add a constant to a vector in BLAS". The first result is:

I would suggest an even faster way that does not require additional identity matrix: use linear-frac! from the vect-math namespace.

kxygk commented 2 years ago

Thanks for the tip :) Maybe typo? This? https://neanderthal.uncomplicate.org/codox/uncomplicate.neanderthal.vect-math.html#var-linear-frac

blueberry commented 2 years ago

Yes, linear-frac.

cgdeboer commented 2 years ago

Thanks folks !