📄 evalx.html
字号:
<HTML><HEAD><TITLE>Newmat09 - evaluation</TITLE></HEAD><BODY><H2>Evaluation of expressions - lazy evaluation</H2><A HREF="explode.html"> next</A> -<A HREF="explode.html"> skip</A> -<A HREF="design.html"> up</A> -<A HREF="index.html"> start</A><P>Consider the instruction<PRE> X = B - X;</PRE>A simple program will subtract <TT>X</TT> from <TT>B</TT>, store the result ina temporary <TT>T1</TT> and copy <TT>T1</TT> into <TT>X</TT>. It would be fasterif the program recognised that the result could be stored directly into<TT>X</TT>. This would happen automatically if the program could look atthe instruction first and mark <TT>X</TT> as temporary.<P>C programmers would expect to avoid the same problem with<PRE> X = X - B;</PRE>by using an operator <TT>-=</TT> <PRE> X -= B;</PRE>However this is an unnatural notation for non C users and itmay be nicer to write <TT>X = X - B</TT>; and know that theprogram will carry out the simplification.<P>Another example where this intelligent analysis of aninstruction is helpful is in<PRE> X = A.i() * B;</PRE>where <TT>i()</TT> denotes inverse. Numerical analysts know it isinefficient to evaluate this expression by carrying out theinverse operation and then the multiply. Yet it is a convenientway of writing the instruction. It would be helpful if theprogram recognised this expression and carried out the moreappropriate approach.<P>I regard this interpretation of <TT>A.i() * B</TT> as justproviding a convenient notation. The objective is not to correctthe errors of people who are unaware of the inefficiency of<TT>A.i() * B</TT> if interpreted literally.<P>There is a third reason for the two-stage evaluation ofexpressions and this is probably the most important one. In C++it is quite hard to return an expression from a functionsuch as (<TT>*</TT>, <TT>+</TT> etc) without a copy. This isparticularly the casewhen an assignment (<TT>=</TT>) is involved. The mechanismdescribed here provides one way for avoiding this in matrixexpressions.<P>To carry out this <I>intelligent</I> analysis of aninstruction matrix expressions are evaluated in two stages. Inthe the first stage a tree representation of the expression isformed. For example <TT>(A+B)*C</TT> is represented by a tree<PRE><TT> * / \ + C / \ A B</TT></PRE><P>Rather than adding <TT>A</TT> and <TT>B</TT> the <TT>+</TT> operatoryields an object ofa class <I>AddedMatrix</I> which is just a pair of pointers to<TT>A</TT> and <TT>B</TT>. Then the <TT>*</TT> operator yields a<I>MultipliedMatrix</I>which is a pair of pointers to the <I>AddedMatrix</I> and <TT>C</TT>.The tree is examined for any simplifications and then evaluated recursively.<P>Further possibilities not yet included are to recognise<TT>A.t()*A</TT> and <TT>A.t()+A</TT> as symmetric or to improve theefficiency ofevaluation of expressions like <TT>A+B+C</TT>, <TT>A*B*C</TT>,<TT>A*B.t()</TT> (<TT>t()</TT> denotes transpose).<P>One of the disadvantages of the two-stage approach is that thetypes of matrix expressions are determined at run-time. So thecompiler will not detect errors of the type<PRE> Matrix M; DiagonalMatrix D; ....; D = M;</PRE><P>We don't allow conversions using <TT>=</TT> when information would belost. Such errors will be detected when the statement isexecuted. <P><A HREF="explode.html"> next</A> -<A HREF="explode.html"> skip</A> -<A HREF="design.html"> up</A> -<A HREF="index.html"> start</A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -