Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 14, 2001 Operator overloading. | ||||
---|---|---|---|---|
| ||||
Quote from the specification for D: Operator overloading. The only practical applications for operator overloading seem to be implementing a complex floating point type, a string class, and smart pointers. D provides the first two natively, smart pointers are irrelevant in a garbage collected language. Another quote: D has many features to directly support features needed by numerics programmers, like direct support for the complex data type and defined behavior for NaN's and infinities. Comment: For numerical computing it is convenient to define classes e.g. vectors, matrices and other entities beyond complex numbers, such as quaternions. For this overloading of operators such as +, -, +=, etc means that top level code can be easily written and readable. John Fletcher |
August 14, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | I agree that operator overloading can be used for matrix and vector classes, in fact, the first user of my C++ compiler used it to develop a matrix class! The results were disappointing, though. It's amazingly difficult to write a good class that overloads operators. What's a quaternion? 4 reals? -Walter John Fletcher wrote in message <3B78E154.BC62E006@aston.ac.uk>... >Quote from the specification for D: > >Operator overloading. The only practical applications for operator overloading seem to be implementing a complex floating point type, a string class, and smart pointers. D provides the first two natively, smart pointers are irrelevant in a garbage collected language. > >Another quote: > > D has many features to directly support features needed by numerics >programmers, like direct support for the complex data type and defined behavior for NaN's and infinities. > > >Comment: > >For numerical computing it is convenient to define classes e.g. vectors, matrices and other entities beyond complex numbers, such as quaternions. For this overloading of operators such as +, -, +=, etc means that top level code can be easily written and readable. > >John Fletcher > > |
August 15, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > I agree that operator overloading can be used for matrix and vector classes, in fact, the first user of my C++ compiler used it to develop a matrix class! The results were disappointing, though. It's amazingly difficult to write a good class that overloads operators. > > What's a quaternion? 4 reals? > > -Walter > A quaternion is an object which has four components. One is real. The other three can be viewed as three mutually perpendicular imaginary numbers. They were invented by Hamilton in the 1850's or thereabouts (before vectors) and now have a lot of use calculating rotations in three dimensions. See for example "SymbolicC++, An introduction to Computer Algebra using object oriented programming" T.K. Shi & W.H.Steeb http://issc.rau.ac.za/symbolic/symbolic.html who define a quaternion class. I use this software with DM compiler, making extensive use of templates and operator overloading, so that the top level program resembles as far as possible the algebra which is written down. You can see some the results of this in http://www.ceac.aston.ac.uk/clifford/paper25/index.htm It is difficult to write operator overloading classes well. I have used as a basis the work of Coplien. Some of the issues are around the creation of temporary copies along the way. I quite frequently have to extend the stack space to large values to get the programs to run. That is the attraction of a language with garbage collection. John Fletcher |
August 15, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | Wow! I see what you're doing now. "John Fletcher" <J.P.Fletcher@aston.ac.uk> wrote in message news:3B7A3FCB.DD2550E7@aston.ac.uk... > > > Walter wrote: > > > I agree that operator overloading can be used for matrix and vector classes, > > in fact, the first user of my C++ compiler used it to develop a matrix class! The results were disappointing, though. It's amazingly difficult to > > write a good class that overloads operators. > > > > What's a quaternion? 4 reals? > > > > -Walter > > > > A quaternion is an object which has four components. One is real. The other > three can be viewed as three mutually perpendicular imaginary numbers. They > were invented by Hamilton in the 1850's or thereabouts (before vectors) and now > have a lot of use calculating rotations in three dimensions. > > See for example "SymbolicC++, An introduction to Computer Algebra using object > oriented programming" T.K. Shi & W.H.Steeb > > http://issc.rau.ac.za/symbolic/symbolic.html > > who define a quaternion class. > > I use this software with DM compiler, making extensive use of templates and > operator overloading, so that the top level program resembles as far as possible > the algebra which is written down. You can see some the results of this in > http://www.ceac.aston.ac.uk/clifford/paper25/index.htm > > It is difficult to write operator overloading classes well. I have used as a > basis the work of Coplien. Some of the issues are around the creation of temporary copies along the way. I quite frequently have to extend the stack > space to large values to get the programs to run. That is the attraction of a > language with garbage collection. > > John Fletcher > > |
August 16, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter |
Walter wrote:
> Wow! I see what you're doing now.
>
It was a related program which had the bug you found for me a little while ago.
John
|
August 16, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Whether it is easy to write good overloaded operators or not doesn't remove the handiness of their existance. How do you suggest matrix algebra be done in D? Matrix a = matrixMult(matrixAdd(c, d), e); or Matrix a = c.add(d).mult(e); ??? Neither one of these is especially usefull for complex expresssions. Also, another use for operator overloading is arbitrary presision Integers/Floats. If I want a 128-bit or a 256-bit integer, I want to be able to use it in normal mathematical expressions. -Rolf Campbell "Walter" <walter@digitalmars.com> wrote in message news:9lbhub$20ib$1@digitaldaemon.com... > I agree that operator overloading can be used for matrix and vector classes, > in fact, the first user of my C++ compiler used it to develop a matrix class! The results were disappointing, though. It's amazingly difficult to write a good class that overloads operators. > > What's a quaternion? 4 reals? > > -Walter > > John Fletcher wrote in message <3B78E154.BC62E006@aston.ac.uk>... > >Quote from the specification for D: > > > >Operator overloading. The only practical applications for operator overloading seem to be implementing a complex floating point type, a string class, and smart pointers. D provides the first two natively, smart pointers are irrelevant in a garbage collected language. > > > >Another quote: > > > > D has many features to directly support features needed by numerics > >programmers, like direct support for the complex data type and defined behavior for NaN's and infinities. > > > > > >Comment: > > > >For numerical computing it is convenient to define classes e.g. vectors, matrices and other entities beyond complex numbers, such as quaternions. For this overloading of operators such as +, -, +=, etc means that top level code can be easily written and readable. > > > >John Fletcher |
August 16, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote:
> I agree that operator overloading can be used for matrix and vector classes, in fact, the first user of my C++ compiler used it to develop a matrix class! The results were disappointing, though. It's amazingly difficult to write a good class that overloads operators.
Just because something is difficult is no reason to exclude it from the language. If we never allowed folks to attempt things that are difficult we'd never make any progress. Operator overloading should definitely be in the language!
--
Jonathan Cano
Member of Technical Staff
MMC Networks, Inc.
|
August 17, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | John Fletcher wrote: > Comment: > > For numerical computing it is convenient to define classes e.g. vectors, matrices and other entities beyond complex numbers, such as quaternions. For this overloading of operators such as +, -, +=, etc means that top level code can be easily written and readable. > > John Fletcher Gotta agree wholeheartedly with this. If you are doing any numerical computation, then using infix operators is an enormous convenience. This is one (only one, there are others:-) of the problems with Java IMO. -- Cheers, Brendan. ---------------------------------------------------------------------------- Brendan McCane Email: mccane@cs.otago.ac.nz Department of Computer Science Phone: +64 3 479 8588/8578. University of Otago Fax: +64 3 479 8529 Box 56, Dunedin, New Zealand. There's only one catch - Catch 22. |
August 17, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brendan McCane | Brendan McCane wrote: > John Fletcher wrote: > > > Comment: > > > > For numerical computing it is convenient to define classes e.g. vectors, matrices and other entities beyond complex numbers, such as quaternions. For this overloading of operators such as +, -, +=, etc means that top level code can be easily written and readable. > > > > John Fletcher > > Gotta agree wholeheartedly with this. If you are doing any numerical computation, then using infix operators is an enormous convenience. This is one (only one, there are others:-) of the problems with Java IMO. Remember, from a distant perspective *all* "operators" are "syntactic sugar"! (A "convenience" as mentioned above.) How's that? Well, let's look at an arbitrary complex expression in C, one with several operators: this.that.pointer_to->something_else = z**(2*pi) / (x ^ 0xff); Let's move it all to functions: memberOf(deRef(memberOf(memberOf(this, that),pointer_to)), something_else) = div(pow(z, times(2, pi)), xor(x, 0xff)); Not a single operator in the line, and nearly completely unreadable. (Should I have used RPN instead?) Where to go from here? Well, the isomorphism is visible, at the very least. And that's the key. Though I'm a D newbie, one resolution path comes from the separatiion of the syntactic and semantic elements of the language. While pre-processing is not a "feature" of D, I'm sure some entreprising individuals will hack the parsers in a way that will allow arbitrary operator creation (and overloading), done under the guise of a simple mapping operation. Of course, a tool such as M4 may be easier to use, or even cpp or sed! For example, create the file "myoperators.d" with the following contents: import math; import operatorLib; Operator("**", <double LHS>, <double RHS>, "pow(LHS,RHS)"); Overload("^", <double LHS>, <double RHS>, "pow(LHS,RHS)"); Then, in D code, do an "import myoperators". The first definition would create the new operator "**", and the second would overload the "^" (XOR) operator for doubles. (The assignment versions "**=" and "^=" could be implicitly defined whenever the LHS and RHS are of the same type.) This would map expressions of the form "A**B" and "A^B" to the string "pow(A,B)" when A and B are doubles (where "LHS" and "RHS" are placeholder tokens for each side of the operator). Once the code is generated, the compiler would catch type mismatch errors as usual. Ideally, any such errors would be mapped back to the original text, and not to the replacement (a common problem with C macros). Hence the need to hack the parsers (especially if things like operator precedence are to be implemented simply). [Actually, I'd vote to eliminate multiple levels of operator precedence! Let's make everything Left-to-Right, and use parens as needed to make precedence explicit.] This could possibly be done via an enhanced module interface, allowing us to avoid many problems by simply forbidding operator definitions to be used within the files containing their definition. I feel the many of the problems with (and objections to) C++ operator creation and overloading appear to reduce to management issues. By making the process simpler and explicit (via implementation as a true dynamic language extension, rather than as a textual substitution) we may be able to control and manage the beast. I'm not yet familiar enough with D and its parsers to make a specific recommendation. Help? Of course, Lisp and Forth adherents will point out the difficulties with "operator explosion", where so many custom operators are implemented that the language morphs into something quite implementation-specific, and completely impenetrable even to language experts. Let's not make operator creation and overloading too easy! But, within reasonable restrictions, it should be made possible. How can we get what we need from operator creation and overloading with minimal fuss, safety, readability and reliability? The C++ way has well documented difficulties and is easy to misuse, but it can be made to work. Remember, D already provides overloading for the common math operators over the intrinsic numeric types. Can we allow the D programmer to extend this on their own, or must the whole concept be scrapped? If there really is no "suitable" way to do all this within D itself, then at least we'll always have functions and M4... -BobC |
August 17, 2001 Re: Operator overloading. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Cano | Jonathan Cano wrote: > > Just because something is difficult is no reason to exclude it from the language. If we never allowed folks to attempt things that are difficult we'd never make any progress. Operator overloading should definitely be in the language! I think the problem here is to be able to balance the complexity you are inserting with the amount of real-life problems you are solving. Operator overloading implementation as a problem has been solved many times already. I personally agree with Walter's decision: operators mean something very precise for me. For me, adding fancy meanings to them would just add to the confusion of the result. Carlo -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu) |
Copyright © 1999-2021 by the D Language Foundation