Well, I don't find the question to be irrelevant. It makes me think
about a couple issues:

1.) Should Complex and Imaginary extend java.lang.Number?
2.) What do we do with the standard Java math functions?
3.) Is 'i' by itself, valid input as a number?

First, I do not propose that just "i" or "j" is enough to indicate the
imaginary number, no more than just 'd' means the number 1.0d (the
latter is correct way to write a double in Java and in clojure).

That said, I would expect the same result that clojure gives now:

user=> (Math/pow (Math/E (* 2/5 2 Math/PI i)))
java.lang.Exception: Unable to resolve symbol: i in this context
(NO_SOURCE_FILE:4)

If you use: 1.0i

user=> (Math/pow (Math/E (* 2/5 2 Math/PI 1.0i)))
java.lang.IllegalArgumentException: No matching method: E
(NO_SOURCE_FILE:1)

Oops! You have an extra set of parenthesis!

If you meant (Math/pow Math/E (* 2/5 2 Math/PI 1.0i)), then the answer
depends on whether Imaginary extends Number. If it does logical (but
perhaps confusing) answer is:

user=> (Math/pow (Math/E (* 2/5 2 Math/PI 1.0i))
1.0

Why? Because you're forcing java.lang.Math.pow(double, double). Java
will then force the cast of the Imaginary number to a double by
calling doubleValue() on "2.51327412i" which has no real component,
thus, you're finding E^0.0 = 1.0. This is the exact same behavior you
would have in Java if you were working with a custom Complex Java type
which extends Number.

If, however, we choose the route Commons Math chooses (to not extend
Number), we should get something like we get now if we pass a non-
Number type to Math/pow:

user=> (Math/pow Math/E "cat")
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Number (NO_SOURCE_FILE:0)

So, I would expect something like:

user=> (Math/pow Math/E 5i)
java.lang.ClassCastException: clojure.lang.Imaginary cannot be cast to
a java.lang.Number (NO_SOURCE_FILE:0)

To get something useful, one would have to use a library (a standard
clojure math library) which supports the complex type, just as it is
in Java. This seems to be the preferable approach.

Thanks for the skepticism and the question!

-Travis

On Jun 8, 11:03 am, Mike Meyer <mwm-keyword-googlegroups.
620...@mired.org> wrote:
> On Tue, 8 Jun 2010 10:27:28 -0700 (PDT)
>
> Steven Devijver <steven.devij...@gmail.com> wrote:
> > On 8 jun, 16:38, Mike Meyer <mwm-keyword-googlegroups.
> > 620...@mired.org> wrote:
>
> > > Why? It isn't supported for rationals or exponents. Or are you
> > > claiming that because we support "3/4" we should also support
>
> > > (* (my-complicated-algo val)/(my-other-complicated-algo exp)
> > >    1/(another-complicated-algo exp2))
>
> > > with similar problems because we support "1e3"?
>
> > What would (Math/pow (Math/E (* 2/5 2 Math/PI i))) return?
>
> Why is this relevant to a discussion of whether or not support for
> complex literals is desirable? All but "i" is involved with the
> semantics of complex values, not the literal.
>
>         <mike
> --
> Mike Meyer <m...@mired.org>          http://www.mired.org/consulting.html
> Independent Network/Unix/Perforce consultant, email for more information.
>
> O< ascii ribbon campaign - stop html mail -www.asciiribbon.org

-- 
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