-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
The best way to build a complex arithmetic package is probably to work atop `clojure.algo.generic`. I've done a pair of libraries (https://github.com/arrdem/imprecise, https://github.com/arrdem/meajure) based around extending algo.generic with custom datatypes and they've both worked out well
Clojure in general seems pretty loath to package "traditional" numerics facilities... note that we don't even have a built in square root operator and the common practice is to use java.lang.Math through interop so I wouldn't go holding your breath over adding irrational numbers to the language core. Also clojure.contrib is abandoned and deprecated in favor of "real" community maintained standalone libraries. The most likely place for an irrational type is probably either a user library like both of the above or possibly algo.generic itself.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
Basically... I think making this work both elegantly and efficiently is going to need compiler enhancements. Ideally there would be a mechanism to allow type hints / type inference or something similar that would optimise away the dynamic type-based dispatch checks in the case where the compiler knows or can infer the exact concrete type.
On Friday, 15 August 2014 23:24:24 UTC+8, Maik Schünemann wrote:Hi,is there a recommended way to do complex arithmetic in clojure ?I am interested to see clojure going forward for scientific computing purposes.There is already considerable effort going on with core.matrix, incanter, expresso etc.But something thad is oddly lacking is support for complex numbers. For some areas, this isa show stopper and it is very basic stuff in other languages and systems:- common-lisp, python etc all have a standard complex type as part of the language/standard library- see language support here http://en.wikipedia.org/wiki/Complex_data_typeIn Java, there are multiple incompatible complex number classes and I don't want that incompatibilitiesin clojure libraries that provide complex number manipulation.In my opinion we should strive as a community for a standard complex number datatype, that is agreed on by clojure libraries,and plays nicely with clojures other numeric types.Ideally, this would be provided as part of clojure.contrib or even clojure itself - like rationals are!
--
--
As far as I understand, your example with Math/abs would need to be part of Java.You cannot extend the arithmetic operators in Java, and you will not be able to extend static Java methods like Math/abs.What you may be able to do is make your custom type play nice with Clojure operators, like Clojure's rationals and bignums.I'd suggest looking at the code for these, and considering a Java class with two primitive attributes rather than a Clojure vector or map (which have a big overhead compared to a simple number). Depending on what you want to do, you may also want to consider implementing a "vector of complex" type, which could be a single object with two primitive arrays.
If you're targeting a performant implementation, you should probably cross post to numerical-clojure; people over there have a lot of experience about numerical optimizations in Clojure.
On Monday, 27 April 2015, endbegin <nkri...@gmail.com> wrote:
I have been thinking along the lines of mikera and Maik - and it seems like there is no further progress here? I would like to take a crack at creating a complex number type, but implemented as a library to Clojure. I am not sure where to start, and if anyone here has suggestions, I'd be happy to hear them.--A complex number could simply be a vector of two elements, or a map with :real and :imag keys (or something lightweight) - and I am not sure what it would require to make this type work happily with code arithmetic functions in Clojure and Java Math.It would also have to work with seq operations in Clojure - for instance:If I have a complex number c = {:real 3 :imag 4}, and a vector v = [1 -2 c], it would be nice to have the call 'map #(Math/abs %) v' produce (1 2 5).I am having trouble figuring out all the pieces that need to be implemented. Is it even possible to implement this as a library, or does this need to be a part of clojure.core?
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
--
....
What I would like is a complex type that plays well with Clojure's generic abstractions and functional style (much like Ratio), and is indistinguishable from other types - for example, the ability to work with (map/reduce/filter), the ability to be a part of seqs and use Clojure core functions on it, and so on. It might not as efficient as the complex type in, say C++, but depending on your definition of reasonable, it might be acceptable. I am willing to explore this further.
.....
--
Ideally math APIs would be cross-platform #ClojureScript
I'll just add that core matrix complex doesn't prevent you from using rationals, as most of the math happens on real/imaginary matrix pairs. However, any time you try to return a scalar value (not a matrix), it wraps the result using the alanforr library, so you'd be potentially restricted there, though I'm not sure if the alanforr library would automatically cast or error out. If someone came up with another complex library, it probably wouldn't be too hard to make that swappable, just as the underlying matrix implantations are presently (in core.matrix.complex).
Chris
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/lY1r6zw6LeU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris
Sent via mobile
I'll just add that core matrix complex doesn't prevent you from using rationals, as most of the math happens on real/imaginary matrix pairs. However, any time you try to return a scalar value (not a matrix), it wraps the result using the alanforr library, so you'd be potentially restricted there, though I'm not sure if the alanforr library would automatically cast or error out. If someone came up with another complex library, it probably wouldn't be too hard to make that swappable, just as the underlying matrix implantations are presently (in core.matrix.complex).
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.