Comments (12)
Sparse matrix support is at an early stage right now. We still need to do some design work to figure out the best way to add this to the API.
As far as implementations go:
- vectorz-clj (https://github.com/mikera/vectorz-clj) has some sparse matrix support, though you'll need to use some Java interop to create the right instances (via Matrixx.createSparse and friends). I haven't yet got round to exposing this functionality via a Clojure API
- core.matrix itself has some experimental support for treating a Clojure map as a sparse matrix (i.e. a map of index vector -> value), though it's not very efficient
from core.matrix.
P.S. Was there a particular use case you had in mind?
I've found that the actual overhead of sparse matrices often doesn't justify the theoretical benefits, unless your matrices are really big and very sparse.... (e.g. matrices representing connections in graphs with very many nodes and very few edges per node)
from core.matrix.
Generally, I'd tend to use a sparse implementation if:
(a) the matrix is large (e.g. bigger than 500 x 500), and
(b) the number of nonzero entries scales as O(N) (as opposed to the O(N^2) growth of a dense matrix) or some other power that's significantly sub-2.
For small matrices, I'd rather use a robust dense implementation in most circumstances. But at 100k+ rows, the memory footprint of a dense matrix-- e.g. 10 billion floats when you only have ~500k non-zero entries-- becomes unacceptable.
For example, convolutions (for smoothing) tend to involve "banded" matrices that are only nonzero near the diagonal, e.g. a tridiagonal smoothing matrix
| [3 1 0 0 0 ... 0 0] | [1 2 1 0 0 ... 0 0] | [0 1 2 1 0 ... 0 0] | [0 0 1 2 1 ... 0 0] | [. . . . . ... . .] | [0 0 0 0 0 ... 2 1] | [0 0 0 0 0 ... 1 3]
That fits the O(N) criteria, since the number of nonzeroes is 3*N-2. At N = 100k, you'd really want a sparse implementation.
Without that, you can usually avoid creating the matrix explicitly and fold the linear transformation into what it acts upon, but ideally a sparse matrix implementation would be faster than that (just as Lapack's offerings are typically orders of magnitude faster than hand-rolled matrix operations).
from core.matrix.
Great, thanks for clarifying your use cases.
The functions on specialised matrices can be supported by the regular core.matrix
protocols, so inner-product on diagonal matrices should be O(n) for example. This is of course dependent on the underlying implementation.
As mentioned, vectorz-clj already has support for various types of specialised matrices, including diagonal matrices, general sparse matrices and permutation matrices. No tridiagonal matrices yet, but those and other banded matrices should be fairly easy to add.
There are still a few other things that need to be done on the core.matrix
side though to get this all fully supported:
- Sparse matrix construction functions (maybe using index vector -> value maps?)
- Relevant predicates e.g.
sparse?
- Functions like
density
- Banded matrix functions:
upper-bandwidth
,band
etc.
Contributions in these areas are of course all very welcome!
from core.matrix.
Update: I've added some experimental banded matrix support in Vectorz. Should be available in the next release of vectorz-clj. See:
https://github.com/mikera/vectorz/blob/develop/src/main/java/mikera/matrixx/impl/BandedMatrix.java
from core.matrix.
I see there are some sparse matrix classes in Vectorz. Are you planning on adding them and other sparse matrix types to the KNOWN_IMPLEMENTATIONS map so that users can explicitly require a sparse implementation. AFAICT the only current options are: create a :persistent-map or create a non-sparse matrix and convert it with a call to sparse
.
Related to this, it'd be convenient to have a function for getting all the non-zero indices of a matrix.
Thanks for the work you've done on improving linear algebra support in Clojure.
from core.matrix.
"Related to this, it'd be convenient to have a function for getting all the non-zero indices of a matrix."
My current project could make use of this if it existed, fwiw.
from core.matrix.
Yes, I'm definitely planning to expose the sparse matrix support from Vectorz into core.matrix.
The work to be done is roughly as follows:
- Get the right API and protocols in core.matrix for creating and manipulating sparse matrices
- Hook up the right Vectorz classes in vectorz-clj
- Do lots of testing - sparse matrices have very different performance characteristics and there will be a lot of optimisations to do
Help with any of the above would be greatly appreciated (especially if you want to use Sparse matrices in the near future! It's much better to implement and test this stuff with a real use case)
from core.matrix.
@mars0i - regarding getting the non-zero indices, what format would you like these? A sequential list of [i j] pairs in row-major order?
Just trying to figure out the best API that is both useful and efficient...
from core.matrix.
@mattrepl - P.S. You don't actually need to add these classes to KNOWN_IMPLEMENTATIONS since these classes are already part of the :vectorz
implementation.
The tricky bit is constructing them - that's where we need a bit more thinking on the API
from core.matrix.
Thanks, I'll work on a branch and see what I come up with.
On Feb 19, 2014, at 21:49, Mike Anderson [email protected] wrote:
@mattrepl - P.S. You don't actually need to add these classes to KNOWN_IMPLEMENTATIONS since these classes are already part of the :vectorz implementation.
The tricky bit is constructing them - that's where we need a bit more thinking on the API
—
Reply to this email directly or view it on GitHub.
from core.matrix.
New issue for the non-zero index support:
from core.matrix.
Related Issues (20)
- index-max HOT 3
- `fill` does not preserve type
- Exception when `mmul`ing `fill`ed ndarrays
- Numeric types are unexpectedly coerced HOT 1
- TODO: Not yet implemented: for class clojure.lang.PersistentVector HOT 2
- Why is there no mmul! function ? HOT 2
- API documentation is out of date HOT 4
- Uninitialized object exists on backward branch 108 in clojurescript HOT 11
- Typo in persistent_vector.cljc (vector-dot)? HOT 2
- `pm` (pretty print) undefined in clojurescript HOT 1
- what is the purpose of 'unimplemented' function in utils HOT 2
- Matrix element wise multiplication by column vector HOT 1
- where is react native API document? HOT 1
- Warnings of clashes with Clojure 1.11.0 abs. HOT 6
- apache-commons-matrix not playing well with core.matrix? HOT 1
- New release HOT 2
- pm, get-shape are confused by cells containing sequences in NDArray
- Max size of ndarray matrices? HOT 2
- element-wise equality comparison for non-numbers HOT 2
- Feature request: reverse and rotate functions HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.matrix.