Numerical Computing in Java? 196
Nightshade queries: "I work for a department in a big financial company that uses equal amounts of C++ and Java. For a variety of reasons, we've decided that Java is the future of the group because of all the benefits of the language (it's so easy to use compared to C++, we can use Eclipse, Ant, jUnit, etc). The problem is that we do a lot of numerical computing and Java has no operator overloading! Languages like C# have operator overloading and because of this company's like CenterSpace have popped up with some nice looking numerical libraries. Try to find numerical packages for Java and it'll be pretty tough. What have people done in terms of numerical computing in Java? We currently use the Jama and Colt libraries for matrices and complex numbers, but these have awkward interfaces without operator overloading and are incomplete (no support for things like symmetric matrices) so we're looking for better solutions. So should we bite the bullet and switch to C#? Should we use a pre-processor like JFront? What have other people done?"
No Operator Overloading is a BAD THING (Score:5, Insightful)
All it does is make us have to type more and take a few hundred milliseconds more time to look at a line of code like
CrazyObjectNumber a = new CrazyObjectNumber(...);
CrazyObjectNumber b = new CrazyObjectNumber(...);
CrazyObjectNumber c = (a * b) + 53;
Which line 3 ends up being:
CrazyObjectNumber c = ((a = a.multiply(b)).add(53)).clone();
Which one is easier to read?
Use the right tools for the right job... (Score:5, Insightful)
I hate overloading (Score:3, Insightful)
I also dislike "virtual" inheritance for the same reason.
I just don't think OO programming is the greatest thing since sliced bread. That's a very unpopular view.
These are not the languages you are looking for (Score:5, Insightful)
Go get Matlab (or Mathematica or Mathcad/Maple). Matlab has a powerful scripting language that does exactly what you need, and you can download thousands of functions written for it. Or just hire me and I'll write a translator from Matlab to your favorite language. Oh wait: translators already exist, so nevermind.
Also, why are you trying to confuse yourself (and future maintainers) with operator overloading in C++? It's just a Bad Idea (TM). Don't do it.
Re:No Operator Overloading is a BAD THING (Score:1, Insightful)
c = (a.multiply(b)).add(53);
As long as you're writing methods, might as well have them return sensible objects.
Re:Can you elaborate? (Score:4, Insightful)
a = sqrt(abs((b + c) * (d / e)));
vs
a = Math.sqrt(Math.abs((b.add(c).multiply(d.divide(e)
for the small cases (such as this one) it doesn't make as much difference, but for complex equations it adds up quickly.
Re:Java Hurt (Score:1, Insightful)
Fortran do not have that, but many manufacturers have libraries or own compiler directives to do that. Well, well, I have used vendor library calls.
For speed of Fortran..
There are many things speed is depending on.
I work with "supercomputers" vectorcomputers down to clusters, and Fortran compilers are superior simply because they have been around longer. Ok, generalisation, there are no 100% answers. It is depending on code, machine, librarys, compiler, the weather of the day and if you are wearing your lucky shirt...
Many engineeringcodes were started with Fortran and therefor there are a lot of library support for Matrixes etc.
90% of the code I work with is Fortran. 10% C.
We actually got a benchmark using Java. Was a good laugh..
But sure, use whatever language float your boat.
Just make sure to program with accuracy in mind (Kahan) and with performance in mind (use librarys, constructs that the compiler can optimize. Unfortunatly is the nice objectoriented hiding hampering the compilers.. )
Re:Jython (Score:2, Insightful)
Other than that, I can't think of any reason why I'd use it, especially for numeric computation. Why be 2 versions behind in the language when you can have some very useful and elegant features like generator expressions now?
Re:java is the future (Score:2, Insightful)
From Java's perspective, the two are tied together.
You (currently) can't have Java without C, while you can have C, without Java.
It's safe to assume that until another language comes around that can do things as well as C/C++, that Java will continue to be written in C.
So to say that Java is the future, while condemning C/C++ to the past is short-sighted at best, and ignorant at worst.
Where would all your lovely new "Java" versions come from, if not from dedicated, hard working C/C++ programmers?
Horses for courses (Score:4, Insightful)
Any competent group generally needs to be able to handle a mix of languages, from C/C++/Java, Perl/Python/Ruby/etc, and the myriad of narrow languages (SQL, templating, shell & batch, HTML, lua, etc., etc.).
Perhaps you should use C, C++ or FORTRAN for the numerical portions and native Java for the general purpose portions.
- Barrie
Operator Overloading is evil, evil, evil (Score:5, Insightful)
But dude, have you ever programmed in C++? Used STL? Blech! Blech^2! I know there are people who love these things, but the readability is unforgivable. Only a Perl code could make it look good. Operator overloading brings out the worst in developers, encourages them to be waaaay to clever for anybody's good. In C++, the evil started with (what the hell were they thinking?!) and went downhill from there. Once you open the door to crap like that, the crap will come.
Years ago, I was at a forum with Josh Bloch and Gilad Bracha where a Java numerics guy berated them for not having overloading and asked them to add it. Bracha basically said "over my cold, dead body." I'm with him on that. The greater cause of readable Java trumps the minor benefits of overloading.
Try JNI (Score:3, Insightful)
This will allow you to make use of a lot of pre-existing C++ code, and to write code in C++ when it turns out to be better at a particular problem, while still using Java for the majority of your application.
I've used JNI extensively for graphics applications (which are heavy on math), where it's either much faster in C++ (yes yes, java is much faster than it used to be, but sometimes much faster still isn't quite fast enough), or just much easier to solve a given problem in C++, even though Java is the best choice for most of the application.
Re:No Operator Overloading is a BAD THING (Score:3, Insightful)
Sure it may not be easy to figure out which is broken but that's better than figuring whether the 10 lines of Java are correct or not (and which 10 lines to focus on) (if they're wrong you don't have a quick "checksum").
No religion wars, please (Score:2, Insightful)
The statement "since it is so easy to misuse" doesn't count: I'd like to know WHY it is so easy to misuses.
The statement "you'd better use other languages for mathematical calculus" doesn't apply either: I'm in a financial project and we use Java, and there are some pretty complicate expression even in economics.
The statement "I used it in C++ and it was a mess" is also not appropriate as an answer to my question: if Java will ever consider this feature, there's no reason why it should copy the C++ style.
On the other side (the operator overloading fans):
the statement "I'm not going to" doesn't apply; your colleague could do and you would kill him after tracing a bug
The statement "The expressive power of this feature is more important than the possible misuses" doesn't apply either: Java tries to avoid misuses by forcing programmers to behave properly and we should respect this philosophy (not meaning I'm against the feature.. only I'd like to have it without the major cons)
My opinion:
"Why it is so easy to misuse and mantain?"
1) At first glance you could not realize if the symbol "+" is a simple primitive "sum" or a more elaborate object operator
2) sometimes the notation is simply "out of this world" (ehm... meaning "not natural"
Example: (let me write in pseudo-Java)
Vector v1=new Vector();
Vector v2=new Vector();
Vector v3=v1+v2;
Vector v4=v2-v1;
3) if we choose a C++-like implementation we could have a "operator+" (-/*) method that has its own implementation (possibly different from add() or any other method in the class)
4) if we choose a C++-like implementation we wouldn't have just one place to look at to understand the meaning of operators (they could be overloaded twice or more times in the class hierarchy)
Any other reason in your opinion?
Then when we have all the reasons listed we could consider if there could be a way (compatible with "Java guidelines") to add this feature without incurring in all this misuse problems.
If we (or Sun
Last thing:
you can vote for this feature (or stand against it ) at this URL (registration needed) http://bugs.sun.com/bugdatabase/view_bug.do?bug_i
No, operator overloading is a GOOD THING! (Score:3, Insightful)
Here are a few reasons not to do it.
We use high level languages instead of assembler because they let us work at varying levels of abstraction, keeping what we're doing relatively simple at each level and delegating the details to the levels below. That makes for more readable, less error-prone code. What you're advocating is the very antithesis of this approach; if you're going to be that clumsy, you might as well write in assembler. In fact, on reflection, that would be neater...
cout << "Design error"; (Score:3, Insightful)
The problem with using << for C++ I/O streams isn't really the use of operator overloading, it's the fact that it puts into code what should be data: the order of the terms to be output. As anyone who's worked with internationalised code much can tell you, that's a "D'oh!" mistake.
As for readability, I write serious maths software using C++. We already use complex matrix multiplication expressions and the like, which are hard enough to read already when you're constrained to a textual representation. From a numerical programmer's perspective, you can have my overloaded (and highly readable) operators over my code, dead body.
Re:Okay, mom.... (Score:2, Insightful)
http://www.google.com/search?hl=en&lr=&ie=UTF-8&o
Re:No Operator Overloading is a BAD THING (Score:3, Insightful)
And that is why I never, ever comment my code.
That was moderated as funny, but in reality it is an excellent idea. People still have to understand what your code does, so if you write you code in such a way that it is perfectly clear what you are doing, and with variable, class and method names that clearly indicate thier function, there is absolutly no reason to comment code and every reason not to.
Comments can very easily grow out of date but the code itself NEVER can. That is the nature of code, after all.
not just operator overloading (Score:2, Insightful)
The C# language is considerably better for numerical computing than Java. However, C# implementations are still a bit behind Java implementations (although they seem to be catching up fast).
I would recommend sticking with C++ for now and waiting another year to switch to something else. C# will probably mature to the point where it is a reasonable choice.
I disagree completely with this statement (Score:2, Insightful)
From a mathematical point of view, the prefix notation represented by a function with arguments makes much more sense than the infix notation represented by operator overloading.
Operator overloading only makes sense in a small number of cases where the class you are developing only provides binary and unary operations. There are many more cases where a function should be tertiary or more. In these cases, you have to abandon operator overloading and use the same functional notation anyway. Also, sometimes operator overloading doesn't even make sense. E.g. How do I overload the * operator for vectors?
In the end, when developing a library like that using operator overloading is going to have to use inconsistent representation for operations - which is just ugly - imo.