       Re: Algebraic simplification and speed

• To: mathgroup at smc.vnet.net
• Subject: [mg52677] Re: Algebraic simplification and speed
• From: David Bailey <dave at Remove_Thisdbailey.co.uk>
• Date: Tue, 7 Dec 2004 05:48:39 -0500 (EST)
• References: <cp3t4s\$9ar\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Steve Gray wrote:
> 1. I'm not sure how to do manually aided simplification. Suppose Mathematica solves a set of equations and
> comes up with something like
> x=(x1^2 + 2 y1^2+3 Z1^2)/Sqrt[x1^2+y1^2+z1^2] and similarly for y and z. I want to manually tell it
> to use r=Sqrt[x1^2+y1^2+z1^2] for the denominator from here on in all expressions and further work.
> How do I do that? (The actual expressions are much more complex.) I could manually substitute "r"
> for the Sqrt by copying/pasting etc. in the further expressions, but doing this in the actual
> equations is very error-prone, so I need Mathematica's help in subsequent stages.
> 	Unless Mathematica does a great job of removing redundant calculations, my manually defined
> temporary variables will also speed up the calculations.
>
> 2. I had a Solve inside a loop. It was taking too long for the millions of numerical executions I
> need. Then I changed the code, executing a Solve once outside the loop, using symbols instead of
> numbers. Then I copied the resulting symbolic expressions into the loop, leaving Mathematica to do only
> numeric multiplies etc. in the loop, there now being no Solve in the loop. This made it about 30
> times faster. This seems like an obvious thing to do, but is there a more standard way? I almost
> didn't think of that trick, obvious as is may be to experienced users.
>
>
> 	Thanks for any help.
>
> Steve Gray
>
Hi,

For the first part of your question - don't cut and paste - as you say,
it is error prone and you don't leave an 'audit trail' of what you have
done. Your substitution can be done thus:

expression /. (x1^2 + y1^2 + z1^2)^p_. -> r^(2p)

Notice the pattern p_. - it is a pattern which in this context
(exponentiation) has a default value of 1.

As regards your second question - if you code an inefficient algorithm,
there is no way that Mathematica will tidy it up for you. However, when
you say that you 'copied the resulting symbolic expressions into the
loop' I wonder if you mean that you did that manually. If you look at
the output from Solve, you will see that it is in the form of a list of
lists of transformation rules - just ready to be used with the /.
operator. So you should have something like

mysolution=(Solve[.............])[]

..
..
..
..
..
xxx=(myexpression /.mysolution)

Note that Solve actually returns a list of solutions. I am assuming
there is only one in your case, so the [] is used to extract that
single solution as a list of transformation rules.

If you find yourself performing steps like that manually, it is time to
read the manual or get a bit of help!

David Bailey

```

• Prev by Date: Re: Re: Redefining the minus operator
• Next by Date: Re: Re: A problem of numerical precision
• Previous by thread: Algebraic simplification and speed
• Next by thread: Re: Algebraic simplification and speed