here it is:

(defrecord imaginary [real complex])

(defmethod + [imaginary Number] [i r]
           (imaginary. (+ (:real i) r) (:complex i)))
(defmethod + [Number imaginary] [r i]
           (+ i r))
(defmethod + [imaginary imaginary] [i1 i2]
           (imaginary. (+ (:real i1) (:real i2))
                       (+ (:complex i1) (:complex i2))))


(defmethod * [imaginary Number] [i r]
           (imaginary. (* r (:real i)) (* r (:complex i))))
(defmethod * [Number imaginary] [r i]
           (* i r))
(defmethod * [imaginary imaginary] [i1 i2]
           (let [[a1 b1] [(:real i1) (:complex i1)]
                 [a2 b2] [(:real i2) (:complex i2)]]
             (imaginary. (- (* a1 a2) (* b1 b2))
                         (+ (* a1 b2) (* a2 b1)))))

(defmethod / imaginary [i]

           (cond (and (not= (:real i) 0) (not= (:complex i) 0))
                 (let [[a b] [(:real i) (:complex i)]
                       d (- (/ (/ a) (+ (/ a b) (/ b a))))
                       c (/ (+ 1 (* b d)) a)]
                   (imaginary. c d))
                 (= (:real i) 0)
                 (imaginary. 0 (- (/ (:complex i))))
                 (= (:complex i) 0)
                 (imaginary. (/ (:real i)) 0)))
                
        
(defmethod / [imaginary imaginary] [i1 i2]
           (* i1 (/ i2)))

(defmethod / [imaginary Number] [i r]
           (* i (/ r)))

(defmethod / [Number imaginary] [r i]
           (* r (/ i)))

(defmulti norm class)
(defmethod norm Number [r] r)
(defmethod norm imaginary [i]
           (sqrt (+ (sq (:real i) ) (sq (:complex i)))))
        



(def i (imaginary. 0 1))


It's a very basic complex number implementation.
It uses clojure.generic Math operators instead of clojure.core
Maybe it can help you out.
I'd be interested in hearing your protocol implementation.

--Robert McIntyre


On Mon, Dec 13, 2010 at 10:27 PM, Sunil S Nandihalli
<sunil.nandiha...@gmail.com> wrote:
> Robert would love to see your implementation.. The reason I am going for
> protocols .. is performance...
> Sunil.
>
> On Tue, Dec 14, 2010 at 8:52 AM, Robert McIntyre <r...@mit.edu> wrote:
>>
>> It's not a protocol, but you may want to take a look at
>> clojure.contrib.generic.arithmetic for inspiration.
>>
>> I wish you luck in your complex number implementation!  I've done my
>> own using clojure.contrib.generic and can post if it anyone's
>> interested.
>>
>> thanks,
>> --Robert McIntyre
>>
>> On Mon, Dec 13, 2010 at 9:55 PM, Sunil S Nandihalli
>> <sunil.nandiha...@gmail.com> wrote:
>> > Thanks David .. but is there a protocol that contains +,*,/,-
>> > operations? If
>> > there is one I can simply add those operations...
>> > Sunil.
>> > On Tue, Dec 14, 2010 at 7:07 AM, David Nolen <dnolen.li...@gmail.com>
>> > wrote:
>> >>
>> >> On Mon, Dec 13, 2010 at 8:13 PM, Sunil S Nandihalli
>> >> <sunil.nandiha...@gmail.com> wrote:
>> >>>
>> >>> Hello Everybody,
>> >>> I see that Konrad-Hinsen's complex-library in the contrib is a very
>> >>> good
>> >>> fit.. But I am afraid about the performance since the basic operations
>> >>> are
>> >>> being dispatched using multi-methods... Does anybody have any say on
>> >>> this?
>> >>> Sunil.
>> >>
>> >> Contribute a new library that uses protocols and deftype/record ? :)
>> >> David
>> >>
>> >> --
>> >> 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 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 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 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 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

Reply via email to