operator overloading  
Author Message
Mark Thornton





PostPosted: 2008-5-11 2:34:00 Top

java-programmer, operator overloading Patricia Shanahan wrote:
> Although this syntax is better than the current prefix method call
> syntax, I don't think it is enough to make a Java Complex class usable
> for some of the expressions I've seen in scientific and engineering
> Fortran programs.

For complex it is more important that the operators be exactly the same
as those used for float and double. This is less valuable for other
types like matrices and vectors. The reason is that many expressions on
complex values are exactly the same (and have the same meaning) with the
complex values replaced by real values.
>
> Patricia
 
Mark Thornton





PostPosted: 2008-5-11 2:34:00 Top

java-programmer >> operator overloading Patricia Shanahan wrote:
> Although this syntax is better than the current prefix method call
> syntax, I don't think it is enough to make a Java Complex class usable
> for some of the expressions I've seen in scientific and engineering
> Fortran programs.

For complex it is more important that the operators be exactly the same
as those used for float and double. This is less valuable for other
types like matrices and vectors. The reason is that many expressions on
complex values are exactly the same (and have the same meaning) with the
complex values replaced by real values.
>
> Patricia
 
Mark Space





PostPosted: 2008-5-11 3:02:00 Top

java-programmer >> operator overloading Stefan Ram wrote:
> Patricia Shanahan <email***@***.com> writes:
>> Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.
>> This version is a very simple transformation of the mathematical notation.
>> Anyone up to writing this out in prefix method call notation?
>
> On can always use a program to transform an infix expression like
>
> BigDecimal( "2" )+ BigDecimal( "3" )* BigDecimal( "4" )
>
> to a prefix expression like
>
> ( new java.math.BigDecimal( "2" ) ).add( ( new java.math.BigDecimal( "3" ) ).multiply( new java.math.BigDecimal( "4" ) ) )


I was thinking the same thing. What is going in Java 7 is even more
support for scripting. I wonder if that could be used to translate from
infix to prefix.

import some.package.mathscript;

public class MatUtil {

public Matrix transform( Matrix a, Matrix b )
{
Matrix result;

@<mathscript>
result = (-a)*b;
@</mathscript>

return result;
}
}


Looks a bit like JSP. Hmm, I wonder if that's a good thing. The need
to make your own scripting engine should prevent the most cavalier abuse
of this type of construct, I think.


P.S. Does anyone else get surprised when their mail client doesn't
auto-indent? I always am.
 
 
Daniel Dyer





PostPosted: 2008-5-11 3:37:00 Top

java-programmer >> operator overloading On Sat, 10 May 2008 20:01:42 +0100, Mark Space <email***@***.com>
wrote:

> Stefan Ram wrote:
>> Patricia Shanahan <email***@***.com> writes:
>>> Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.
>>> This version is a very simple transformation of the mathematical
>>> notation.
>>> Anyone up to writing this out in prefix method call notation?
>> On can always use a program to transform an infix expression like
>> BigDecimal( "2" )+ BigDecimal( "3" )* BigDecimal( "4" )
>> to a prefix expression like
>> ( new java.math.BigDecimal( "2" ) ).add( ( new
>> java.math.BigDecimal( "3" ) ).multiply( new java.math.BigDecimal( "4" )
>> ) )
>
>
> I was thinking the same thing. What is going in Java 7 is even more
> support for scripting. I wonder if that could be used to translate from
> infix to prefix.

One option is to use Groovy. It uses BigDecimal by default for numeric
literals:

http://groovy.codehaus.org/Groovy+Math

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
 
Mark Thornton





PostPosted: 2008-5-11 4:18:00 Top

java-programmer >> operator overloading Daniel Dyer wrote:
>> I was thinking the same thing. What is going in Java 7 is even more
>> support for scripting. I wonder if that could be used to translate
>> from infix to prefix.
>
> One option is to use Groovy. It uses BigDecimal by default for numeric
> literals:
>
> http://groovy.codehaus.org/Groovy+Math
>
> Dan.
>

Unfortunately performance is terrible. Not exactly suitable for serious
maths.

Mark Thornton
 
 
ram





PostPosted: 2008-5-11 4:34:00 Top

java-programmer >> operator overloading Mark Thornton <email***@***.com> writes:
>Unfortunately performance is terrible. Not exactly suitable for
>serious maths.

Another possibility would be an IDE that can display certain
Java prefix expression as an infix expression in an edit box.

It knows about BigDecimal and can learn about the operators
and precedence rules for other classes from annotations.

This has been done before: Some word processors have an
internal language for mathematical terms and contain WYSIWYG
formula editors for them.

 
 
Lew





PostPosted: 2008-5-11 5:51:00 Top

java-programmer >> operator overloading Patricia Shanahan wrote:
> Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.
> This version is a very simple transformation of the mathematical notation.
>
> Anyone up to writing this out in prefix method call notation?

/ * n + * 2 a1 * - n 1 d 2

result =
divide( multiply( n,
add( multiply( 2, a1 ),
multiply( subtract( n, 1 ),
d ),
2 );

I cheated. I used static methods.

--
Lew
 
 
Lew





PostPosted: 2008-5-11 5:53:00 Top

java-programmer >> operator overloading Patricia Shanahan wrote:
>> Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.
>> This version is a very simple transformation of the mathematical
>> notation.
>>
>> Anyone up to writing this out in prefix method call notation?

Mark Space wrote:
> n.minus(1).times(d).plus(a1.times(2)).times(n).dividedBy(2)
^ ^
This might break down if the methods mutate n; not if if they create new
objects for the results.

--
Lew
 
 
Patricia Shanahan





PostPosted: 2008-5-11 7:56:00 Top

java-programmer >> operator overloading Mark Space wrote:
...
> import some.package.mathscript;
>
> public class MatUtil {
>
> public Matrix transform( Matrix a, Matrix b )
> {
> Matrix result;
>
> @<mathscript>
> result = (-a)*b;
> @</mathscript>
>
> return result;
> }
> }
>
>
> Looks a bit like JSP. Hmm, I wonder if that's a good thing. The need
> to make your own scripting engine should prevent the most cavalier abuse
> of this type of construct, I think.
...

What forms of abuse does this prevent that would not also be prevented
by mapping e.g. a+b to a.add(b)?

Patricia
 
 
Patricia Shanahan





PostPosted: 2008-5-11 8:01:00 Top

java-programmer >> operator overloading Arne Vajh鴍 wrote:
> Mark Space wrote:
>> Tom Anderson wrote:
>>>> Matrix x = y .add. z;
>>>>
>>>> ?
>>>
>>> Mark, 1957 just called, FORTRAN wants its operators back.
>>
>> I new I'd seen those somewhere before. Thanks!
>
> Actually Fortran only used that form for comparison
> operators.

and it was relatively harmless in Fortran because the only uses of "."
were inside Hollarith constants and in real and double literals. Also,
Fortran was a hopeless case for single token lookahead parsing.

Patricia
 
 
Mark Space





PostPosted: 2008-5-11 9:22:00 Top

java-programmer >> operator overloading Patricia Shanahan wrote:

>
> What forms of abuse does this prevent that would not also be prevented
> by mapping e.g. a+b to a.add(b)?
>
> Patricia

I'm thinking "lazy people." People who think that typing is a lot of
work and anything at all that can be done to cut down on programmer
typing (as opposed to the maintainer's effort) is a good thing. Or at
least it's kewl.

So any class that has an "add" method will get overloaded versions of +
just in the name of kewl.

List myList = new List();
myList + "List Item 1";

instead of

myList.add( "List Item 1" );


Then they'll overload - for remove() because if one is kewl then two are
even kewler. And they'll overload ^ for contains() and % for toArray()
and that'll be sooperkewl. And then someone will overload << for
toString() because some *other* language they saw on the *internet* has
it and then it'll all have gone to s--t.

And I mean individual programmers will sub-class existing (API) classes,
and writing their own classes. I'm sure Sun would never stoop to this.
But there's a lot of really, really lazy programmers out there, and I
don't mean that in a good "laziness is a virtue" way.


Some one else on this thread said that the Java community was composed
of traumatized C++ programmers. And say that's a good thing. I like to
learn from my mistakes, not repeat them.
 
 
Patricia Shanahan





PostPosted: 2008-5-11 9:29:00 Top

java-programmer >> operator overloading Mark Space wrote:
...
> And I mean individual programmers will sub-class existing (API) classes,
> and writing their own classes. I'm sure Sun would never stoop to this.
> But there's a lot of really, really lazy programmers out there, and I
> don't mean that in a good "laziness is a virtue" way.
...

Sun stooped to the misuse of "+" for String concatenation. I think it
should be reserved for addition.

Patricia
 
 
John W Kennedy





PostPosted: 2008-5-11 11:39:00 Top

java-programmer >> operator overloading Tom Anderson wrote:
> On Fri, 9 May 2008, Mark Space wrote:
>> ...but what if you could call it like this:
>>
>> Matrix x = y .add. z;
>>
>> ?
>
> Mark, 1957 just called, FORTRAN wants its operators back.

1962, actually. Logical operators first appeared in FORTRAN IV.
--
John W. Kennedy
"Information is light. Information, in itself, about anything, is light."
-- Tom Stoppard. "Night and Day"
 
 
Arne Vajh鴍





PostPosted: 2008-5-11 11:42:00 Top

java-programmer >> operator overloading John W Kennedy wrote:
> Tom Anderson wrote:
>> On Fri, 9 May 2008, Mark Space wrote:
>>> ...but what if you could call it like this:
>>>
>>> Matrix x = y .add. z;
>>>
>>> ?
>>
>> Mark, 1957 just called, FORTRAN wants its operators back.
>
> 1962, actually. Logical operators first appeared in FORTRAN IV.

This is before I was born, but I find it difficult to see how
they could use Fortran for much without .eq., .ne. etc..

Arne

 
 
Mark Thornton





PostPosted: 2008-5-11 17:27:00 Top

java-programmer >> operator overloading Stefan Ram wrote:
> Mark Thornton <email***@***.com> writes:
>> Unfortunately performance is terrible. Not exactly suitable for
>> serious maths.
>
> Another possibility would be an IDE that can display certain
> Java prefix expression as an infix expression in an edit box.
>
> It knows about BigDecimal and can learn about the operators
> and precedence rules for other classes from annotations.
>
> This has been done before: Some word processors have an
> internal language for mathematical terms and contain WYSIWYG
> formula editors for them.
>

It has been suggested many times. One concern is that without the
special IDE the resulting code may be unreadable.

Mark Thornton
 
 
Mark Thornton





PostPosted: 2008-5-11 17:32:00 Top

java-programmer >> operator overloading Arne Vajh鴍 wrote:
> John W Kennedy wrote:
>> Tom Anderson wrote:
>>> On Fri, 9 May 2008, Mark Space wrote:
>>>> ...but what if you could call it like this:
>>>>
>>>> Matrix x = y .add. z;
>>>>
>>>> ?
>>>
>>> Mark, 1957 just called, FORTRAN wants its operators back.
>>
>> 1962, actually. Logical operators first appeared in FORTRAN IV.
>
> This is before I was born, but I find it difficult to see how
> they could use Fortran for much without .eq., .ne. etc..
>
> Arne
>

They had an arithmetic if statement (from memory)

if(expr)labelNeg,labelZero,labelPos

The target label depended on the value of the expression. If negative
the first label was used, zero the second and positive values used the
third target. All the labels were of course integers.

Mark Thornton
 
 
Andreas Leitgeb





PostPosted: 2008-5-11 17:44:00 Top

java-programmer >> operator overloading Mark Space <email***@***.com> wrote:
> I'm thinking "lazy people." People who think that typing is a lot of
> work and anything at all that can be done to cut down on programmer
> typing (as opposed to the maintainer's effort) is a good thing. Or at
> least it's kewl.

Almost correct, except that those people think that their cutting
down on typing effort actually *HELPS* also those who maintain
the code later.

--
"Too many notes" a former austrian emperor about some work of W.A.Mozart
 
 
ram





PostPosted: 2008-5-11 19:50:00 Top

java-programmer >> operator overloading Patricia Shanahan <email***@***.com> writes:
>http://en.wikipedia.org/wiki/Arithmetic_progression#Sum_.28arithmetic_series.29
>Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.

The transcription of such term sometimes matters:

籎uly 28, 1962 -- Mariner I space probe. A bug in the
flight software for the Mariner 1 causes the rocket to
divert from its intended path on launch. Mission control
destroys the rocket over the Atlantic Ocean. The
investigation into the accident discovers that a formula
written on paper in pencil was improperly transcribed into
computer code? 

http://www.wired.com/software/coolapps/news/2005/11/69355

 
 
Tom Anderson





PostPosted: 2008-5-11 20:09:00 Top

java-programmer >> operator overloading On Sat, 10 May 2008, Lew wrote:

> Patricia Shanahan wrote:
>>> Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.
>>> This version is a very simple transformation of the mathematical notation.
>>>
>>> Anyone up to writing this out in prefix method call notation?
>
> Mark Space wrote:
>> n.minus(1).times(d).plus(a1.times(2)).times(n).dividedBy(2)
> ^ ^
> This might break down if the methods mutate n; not if if they create new
> objects for the results.

Yes. Also, the arithmetic operation would also break if arithmetic
operators mutate their operands, rather than creating new values.

Personally, i read it as implicit in both cases that they didn't.

tom

--
I DO IT WRONG!!!
 
 
Tom Anderson





PostPosted: 2008-5-11 20:10:00 Top

java-programmer >> operator overloading On Sat, 10 May 2008, John W Kennedy wrote:

> Tom Anderson wrote:
>> On Fri, 9 May 2008, Mark Space wrote:
>>> ...but what if you could call it like this:
>>>
>>> Matrix x = y .add. z;
>>>
>>> ?
>>
>> Mark, 1957 just called, FORTRAN wants its operators back.
>
> 1962, actually. Logical operators first appeared in FORTRAN IV.

Dammit! When i posted that, the back of my mind was saying "ah, but did
the original fortran have those operators?", but i dismissed it as the
kind of thing that's unlikely to be the case, and even less likely to get
caught if it is. Wrongly!

tom

--
I DO IT WRONG!!!
 
 
Tom Anderson





PostPosted: 2008-5-11 20:18:00 Top

java-programmer >> operator overloading On Sat, 10 May 2008, Mark Space wrote:

> Patricia Shanahan wrote:
>
>> What forms of abuse does this prevent that would not also be prevented
>> by mapping e.g. a+b to a.add(b)?
>
> I'm thinking "lazy people." People who think that typing is a lot of work
> and anything at all that can be done to cut down on programmer typing (as
> opposed to the maintainer's effort) is a good thing. Or at least it's kewl.
>
> So any class that has an "add" method will get overloaded versions of + just
> in the name of kewl.
>
> List myList = new List();
> myList + "List Item 1";
>
> instead of
>
> myList.add( "List Item 1" );
>
> Then they'll overload - for remove() because if one is kewl then two are even
> kewler. And they'll overload ^ for contains() and % for toArray() and
> that'll be sooperkewl. And then someone will overload << for toString()
> because some *other* language they saw on the *internet* has it and then
> it'll all have gone to s--t.

As i said before, and will keep on saying: THIS HAS NOT HAPPENED IN ANY
LANGUAGE WITH OPERATOR OVERLOADING OTHER THAN C++. Not in python, not in
smalltalk, not in ada (that i've heard), not anywhere. Why do you think
it'll happen in java when it hasn't happened in those languages?

> And I mean individual programmers will sub-class existing (API) classes, and
> writing their own classes. I'm sure Sun would never stoop to this. But
> there's a lot of really, really lazy programmers out there, and I don't mean
> that in a good "laziness is a virtue" way.

Do you think that java programmers are, on average, more stupid than
python, smalltalk or ada programmers?

Actually, i could believe that. Java being the big industrial language
that every idiot who doesn't fancy a career in McDonald's or Iraq is
learning, and the others being a bit more niche and sophisticated.

> Some one else on this thread said that the Java community was composed
> of traumatized C++ programmers. And say that's a good thing. I like to
> learn from my mistakes, not repeat them.

Agreed. So let's learn from our mistakes and use operator overloading
without abusing it. Not having operator overloading is like giving up cake
because you once ate so much you were sick - don't give up cake, just
practice moderation in consuming it. I don't see why we shouldn't have our
cake and eat it too.

tom

--
I DO IT WRONG!!!