Any hope for complex matrices yet?

90 views
Skip to first unread message

Vic Putz

unread,
Jan 19, 2018, 4:47:42 PM1/19/18
to Numerical Clojure
I've a fun little project I'd love to try and implement in Clojure instead of returning once again to NumPy, but I need complex math... and having struggled for a few days to find something in Clojure, I'm still looking.  Neanderthal doesn't support complex numbers yet, and while I made minor progress with core.matrix.complex, any attempt at math using core.matrix operations results in "org.apache.commons.math3.complex.Complex cannot be cast to java.lang.Number".  I can't even seem to get mset!-type operations to work nicely.

I don't even need much that's too complex; basically maintaining a mutable vector and doing small matrix operations on various views.  But I'm just completely stymied and while I'm willing to even try and write something simple, I'm basically completely flummoxed at this point.

Any help?  I really want to use Clojure for this, but I'm severely struggling here.

Mike Anderson

unread,
Jan 21, 2018, 11:13:54 PM1/21/18
to Numerical Clojure
The core.matrix API is designed to support complex numbers (in fact it supports complexly arbitrary element types)

But if you want to use complex numbers "naturally" in core.matrix you can need to use an implementation that supports complex numbers as a numercial element type, because it needs to redefine addition, multiplication etc. This is why you are seeing the "org.apache.commons.math3.complex.Complex cannot be cast to java.lang.Number" error.

You may want to check out: 


This is pretty experimental, but you can probably get it to work.

Alan Forrester

unread,
Jan 22, 2018, 1:22:03 AM1/22/18
to numerica...@googlegroups.com
Try updating the complex dependency in project.clj to the latest version “0.1.11”. This eliminates some bugs from earlier versions.

Alan

Vic Putz

unread,
Jan 23, 2018, 1:45:47 PM1/23/18
to Numerical Clojure
Thanks, Alan (I am in fact using 0.1.11).  And Mike, thanks for the pointer--I just got confused about what implementations might support complex numbers.  I'll try that repository and see what I can learn.  Good stuff...

Is there a reliable clojurescript library for complex numbers?  May come into play eventually... I saw https://github.com/wbabic/complex but haven't had a go at it yet.

Truth be told, in this case I mostly just need to maintain a mutable vector of complex numbers and update them periodically with a few adds and multiplies (I don't need advanced matrix ops)--so I thought I'd look for the "approved solution" before trying to reinvent the wheel.  I'll flail about for a bit, though :)

Vic Putz

unread,
Jan 23, 2018, 3:54:38 PM1/23/18
to Numerical Clojure
blink, blink...

Oif!  So I did manage to compile and locally install core.matrix.complex, and eventually figured out that (m/set-current-implementation cm/canonical-complex-array) would have the m/array and m/matrix methods create complex arrays (requiring core.matrix as m and core.matrix.complex as cm).

...and basic adding works, and other things like traces work, and scaling works... but if I define cva as a vector and cmat as a matrix, (m/mmul cmat cva) gives the cast error but (m/inner-product cmat cva) seems to do what I want.

I suppose I'm at the point where this could be useful, but blimey, this is well confusing.

I'm bullheaded enough to blunder ahead, because I've got enough to at least muddle something, but I think it's safe to say at the present date that complex numbers in clojure are at the very least nonintuitive :)

How can I help?

Mike Anderson

unread,
Jan 23, 2018, 8:57:28 PM1/23/18
to numerica...@googlegroups.com
I think the main problem is that is just hasn't been sufficiently developed and tested.

core.matrix.complex was an experiment to show that:
a) complex numbers can be used with the core.matrix API - it was a design goal of core.matrix that arbitrary element types should be supported, and it was nice to confirm this
b) You can create complex matrices using, for example, two regular real matrices as the underlying storage and define efficient operations using these. This is interesting as it opens the possibility for stuff like e.g. GPU two accelerated arrays to "automatically" be treated as complex arrays.

In order to develop this to the point that this is generally useful, someone would need to:
1) complete the implementation (e.g. implement some protocol for things like inner-product to make mmul work) 
2) probably add some tests for things that are complex-specific.

There would also need to be some thought about what some of the API functions actually mean given complex arrays. Stuff like adding is obvious, but for some of the matrix decompositions etc. things could get complex (pun intended). This could however be deferred and supported later.

Vic, if you want to have a crack at any of this then I'd be delighted and happy to advise and answer questions, but haven't got much time to work directly on this right now (as I don't need complex numbers in my current work).




--
You received this message because you are subscribed to a topic in the Google Groups "Numerical Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/numerical-clojure/4RUXMHEMBqg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to numerical-clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vic Putz

unread,
Jan 24, 2018, 2:15:07 PM1/24/18
to Numerical Clojure
You know, I don't know if I'll make much headway, but what I'll do is fork it (again!  So many forks!  Okay, only a few on this one) and take a crack at a few things like that and poke a few things at documentation (not much, but for example the "default constructor", taking one array of real and one for imaginary, is the right choice for implementation but confused me at first).  It would be a good exercise for me and even if I didn't make enough headway for it to go mainstream I'd learn a ton (and there's enough in the existing core.matrix.complex for my project); one of those "sure can't hurt to try" projects.  May ask for pointers of course, but I don't need to take up any of your time.  Thanks for the work you've done already!
To unsubscribe from this group and all its topics, send an email to numerical-cloj...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages