Re: complex numbers in clojure

2015-04-27 Thread endbegin
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 clojure@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+unsubscr...@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 the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: complex numbers in clojure

2015-04-27 Thread endbegin
This is what I suspected. Of course, this is not just for abs, but really 
for any mathematical operation. Sqrt, Trig operations etc.

I will study the Ratio type more closely, but note that something like 
(Math/abs (/ 22 7)) does not work. 


On Monday, April 27, 2015 at 12:45:32 PM UTC-4, Gary Verhaegen wrote:

 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 javascript: 
 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 clojure@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+unsubscr...@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 the Google Groups 
 Clojure group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to clojure+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@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 the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: complex numbers in clojure

2015-04-27 Thread endbegin
As far as I know, Java Math does not have a complex number type. Some 
implementations of a complex number exist (such as Apache Commons Math), 
but they create a Complex class that implements Serializeable and a Commons 
specific interface.  

Modifying clojure.core itself is a bit daunting, and like you said, it 
would need to be a priority for the core team.



On Monday, April 27, 2015 at 12:45:46 PM UTC-4, Andy Fingerhut wrote:

 Unless the Java Math library handles complex types already, I don't know 
 of any way to extend it in a way that would let you get the answer you want 
 from (Math/abs my-complex-number) in Clojure.

 If you want code that gives the correct answers, a library using vectors 
 or maps for complex numbers can get you there.  Memory footprint and 
 performance should be better with Clojure records or a new class written 
 directly in Java.  With a library, I don't know of any way to be able to 
 mix Clojure's arithmetic operations like + - * / etc. with its existing 
 numeric types, and your new library-implemented complex types.

 Support of Clojure's arithmetic operations on complex numbers might only 
 be achievable by modifying Clojure itself.  Depending upon your goals, the 
 down side of that approach is that it will not become part of the normal 
 Clojure distribution unless the Clojure core team wants that addition, 
 which I would guess would be very low on their priority list (just a guess 
 -- I have no inside knowledge there).

 Andy

 On Mon, Apr 27, 2015 at 8:39 AM, endbegin nkri...@gmail.com javascript:
  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 
 javascript:
 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 javascript:
 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+u...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/d/optout.




-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@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 the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: IllegalArgumentException when running core.async example

2014-07-09 Thread endbegin
Thanks!! That did it.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@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 the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


IllegalArgumentException when running core.async example

2014-07-08 Thread endbegin
Hi

I was trying to teach myself core.async and tried the rock, paper, scissors 
example found here:
http://tech.puredanger.com/2013/07/10/rps-core-async/

See a gist here:
https://gist.github.com/endbegin/b10be6d7a3ba5f6c29db

Really, the main difference is in the :require statement at the top, and 
whenever I use a function from the core.async library, I prepend it with 
async. For instance, async/go or async/! and so on. 

I am using CIDER + Emacs, and Clojure 1.5.1, core.async 
0.1.303.0-886421-alpha. I created a new app with Lein, and when I do a 
CTRL+C+k, the file compiles just fine. 

However, when I try to play the game, I do a
 (def game (init))
 (play game)

and get the error:

IllegalArgumentException No implementation of method: :take! of protocol: 
#'clojure.core.async.impl.protocols/ReadPort found for class: 
clojure.core.async$chan  clojure.core/-cache-protocol-fn 
(core_deftype.clj:541)

*What does this mean?!?*

*P.S.* If I replace the :require at the top with 
(:require [clojure.core.async :refer :all]))

I can't even get it to compile. I get an error about there being a conflict 
between the partition-by in clojure.core and in core.async. 

Thanks for the help!

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@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 the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: IllegalArgumentException when running core.async example

2014-07-08 Thread endbegin


 Just tried it with Clojure 1.6.0. Still no luck!


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@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 the Google Groups 
Clojure group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: can Clojure 1.3 code always be made as fast as Java for numeric computations?

2012-03-28 Thread endbegin
On Tuesday, March 27, 2012 2:51:41 PM UTC-4, Jay Fields wrote:

 I recently ran into some code** that was in Java, and ran in single
 digit microseconds (not millis). I converted it to clojure, and got it
 running at about the same speed... though it did take me a day to
 figure out all the tweaks.

 It can be done, if you're willing to invest the time and learn the tricks.

 Cheers, Jay


 That is good to know. Is there a place where the tricks involved were 
documented? As in, here is what you should think about to get Java-like 
performance. I've seen fragments in different places, and a lot of them 
applied to Clojure 1.2 or earlier. Based on what I have seen on Stack 
Overflow, it seems like if you want Java performance, then you have dig 
down into direct Java interop, and use things like Java arrays.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: How to add a new type of collection?

2012-01-24 Thread endbegin
I know this isn't exactly what the OP is looking for, but the Incanter 
project has done something for dense matrices that might work. The Matrix 
type has been imported (which is a java class), and a whole bunch of 
functions have been implemented on that class.
See here
https://github.com/liebke/incanter/blob/master/modules/incanter-core/src/incanter/core.clj

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

extracting the execution time of a function ...

2011-12-27 Thread endbegin
Hi,

I am new to learning clojure, and I am hoping there is a solution to
something that is not obvious to me ... I have a function that I want
to run multiple times, measure the time it takes to execute each
function, and put those numbers in a vector. I would like to process
that vector further by doing things like max, min and average, and I
think I can do that fairly easily, but I'm having trouble generating
such a vector in the first place.

For instance when I do this:

= (dorun (repeatedly 3 myfunc))

I get

Elapsed time: 1253.764 msecs
Elapsed time: 20003.997 msecs
Elapsed time: 653.919 msecs
nil

I would like to get something like
[1253.764 23.997 653.919], which I can post-process.

I tried a

= (conj [] (dorun (repeatedly 3 myfunc)))

but that just gives me a [nil].

Can anyone help?

Thanks ...

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: extracting the execution time of a function ...

2011-12-27 Thread endbegin
Thanks Cedric. I did something along the lines of what you suggested
(created a function instead of a macro).

Here's what I did to get time in seconds:

(defn run-myfunc []
  (let [starttime (System/nanoTime)]
(myfunc)
(/ (- (System/nanoTime) starttime) 1e9)))

and then to produce timings across 10 runs,
(vec (repeatedly 10 run-myfunc))

produces the desired vector.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en