Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!uunet!odi!jack From: jack@odi.com (Jack Orenstein) Newsgroups: comp.lang.c++ Subject: Re: puzzle/problem/yet another new feature Message-ID: <1989Oct26.135247.20151@odi.com> Date: 26 Oct 89 13:52:47 GMT References: <32164@ucbvax.BERKELEY.EDU> Reply-To: jack@odi.com (Jack Orenstein) Organization: Object Design Inc., Burlington, MA Lines: 33 In article <32164@ucbvax.BERKELEY.EDU> othar@ernie.Berkeley.EDU (Othar Hansson) writes: >Imagine that I had a really nice matrix class definition, and wrote >the following code: > >#include "reallynicematrixclass.h" >matrix A,B,C,D,E; > >... // give the matrices bounds, and fill them with values > >matrix ANSWER = A * B * C * D * E; // use matrix::operator*(matrix&, matrix&) > >Now, if I remember, C++ and C will parenthesize this expression from >left to right, evaluating A*B first, etc. However, one of the first >things we learn in a class on algorithms is the dynamic programming >example of optimizing the order of matrix multiplications. Depending >on the matrix bounds, this might be the parenthesization which runs >the fastest: > >ANSWER = (A * B) * ((C * D) * E) >... >In this case, where the bounds are run-time defined, the programmer >can't parenthesize the expression in advance. In C++ version 8.0, how >are we going to allow the code to decide order of evaluation at >run-time? Probably the same as in 1.2 or 2.0. Define matrix::operator+, operator*, etc. to construct a tree representing the expression. Then, when the value of the expression is needed, e.g. in assignment to ANSWER above, or in a comparison, optimize and evaluate. Jack Orenstein Object Design, Inc.