OptimizeExpression further enhanced?
- To: mathgroup at smc.vnet.net
- Subject: [mg25975] OptimizeExpression further enhanced?
- From: Adalbert Hanssen <hanssen at Zeiss.de>
- Date: Tue, 14 Nov 2000 03:47:08 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi, MathGroup, has anybody tried to further enhance OptimizeExpression.m ? Example: the intersection of a line starting at {px,py,pz} going in the direction {rx,ry,rz} with the sphere centered at {0,0,0} with radius r may be calculated as Solve[{px,py,pz}+d*{rx,ry,rz}=={x,y,Sqrt[r^2-x^2-y^2]},{x,y,d}] If we put the result through OptimizeExpression[#,OptimizeLevel->-1]& we get: OptimizedExpression [Modude[{$1, $2, $3, $4, $5, $6, $7, $8, $9, $10 ,$11, $12, $13, $14, $15, $16, $17, $18, $19, $20 ,$21, $22, $23, $24, $25, $26, $27, $28 } ,$1 = rx^2; $2 = ry^2; $3 = rz^2; $4 = $1 + $2 + $3; $5 = $4^(-1); $6 = -(px*$1*$5); $7 = -(py*rx*ry*$5); $8 = -(pz*rx*rz*$5); $9 = 2*px*rx; $10 = 2*py*ry; $11 = 2*pz*rz; $12 = $10 + $11 + $9; $13 = $12^2; $14 = px^2; $15 = py^2; $16 = pz^2; $17 = r^2; $18 = -$17; $19 = $14 + $15 + $16 + $18; $20 = -4*$19*$4; $21 = $13 + $20; $22 = Sqrt[$21]; $23 = -(px*rx*ry*$5); $24 = -(py*$2*$5); $25 = -(pz*ry*rz*$5); $26 = -2*px*rx; $27 = -2*py*ry; $28 = -2*pz*rz; {{x -> px - (rx*$22*$5)/2 + $6 + $7 + $8 ,y -> py + $23 + $24 + $25 - (ry*$22*$5)/2 ,d -> ((-$22 + $26 + $27 + $28)*$5)/2 } ,{x -> px + (rx*$22*$5)/2 + $6 + $7 + $8 ,y -> py + $23 + $24 + $25 + (ry*$22*$5)/2 ,d -> (($22 + $26 + $27 + $28)*$5)/2 } } ] ] The two solutions only differ in the sign of a common subexpressions, e.g. for x in the sign before (rx*$22*$5)/2 etc. Why does OptimizeExpression not take notice of this? If OptimizeExpression would work completely, it would use an extra variable for the subexpression, e.g. $30 = (rx*$22*$5)/2 Since the factor $22*$5 literally appears several times, it might be assigned to another variable, e.g. $29 = $22*$5/2; $39 = rx*$29; In the same way, several sums may also be computed only once, (if we look at sums with the same terms adjacent to each other, $6+$7+$8 and $23+$24+$25 and $26+$27+$28 might be assigned to intermediate variables, looking closer at it, even px+$6+$7+$8 might be unified to one term, also py + $23 + $24 + $25 to another one). Of course, fine tuning OptimizeExpression-results by hand, one also finds out, that $26,$27,$28 are only used in a sum and all of them contain a comman factor -2. The sum would better be done first and the factor -2 applied later. Of course, the number of intermediate variables can be reduced by using one for them: $26=-2*(px*rx+py*ry+pz*rz); Is anybody out there, who has tought OptimizeExpression to handle this and similar cases? kind regards Dipl.-Math. Adalbert Hanszen