Go backward to Operating on Selections. Go up to Selecting Subformulas.

Rearranging Formulas using Selections
-------------------------------------

The `j R' (`calc-commute-right') command moves the selected
sub-formula to the right in its surrounding formula.  Generally the
selection is one term of a sum or product; the sum or product is
rearranged according to the commutative laws of algebra.

As with `j '' and `j DEL', the term under the cursor is used if there
is no selection in the current formula.  All commands described in
this section share this property.  In this example, we place the
cursor on the `a' and type `j R', then repeat.

     1:  a + b - c          1:  b + a - c          1:  b - c + a

Note that in the final step above, the `a' is switched with the `c'
but the signs are adjusted accordingly.  When moving terms of sums and
products, `j R' will never change the mathematical meaning of the
formula.

The selected term may also be an element of a vector or an argument of
a function.  The term is exchanged with the one to its right.  In this
case, the "meaning" of the vector or function may of course be
drastically changed.

     1:  [a, b, c]          1:  [b, a, c]          1:  [b, c, a]

     1:  f(a, b, c)         1:  f(b, a, c)         1:  f(b, c, a)

The `j L' (`calc-commute-left') command is like `j R' except that it
swaps the selected term with the one to its left.

With numeric prefix arguments, these commands move the selected term
several steps at a time.  It is an error to try to move a term left or
right past the end of its enclosing formula.  With numeric prefix
arguments of zero, these commands move the selected term as far as
possible in the given direction.

The `j D' (`calc-sel-distribute') command mixes the selected sum or
product into the surrounding formula using the distributive law.  For
example, in `a * (b - c)' with the `b - c' selected, the result is `a
b - a c'.  This also distributes products or quotients into
surrounding powers, and can also do transformations like `exp(a + b)'
to `exp(a) exp(b)', where `a + b' is the selected term, and `ln(a ^
b)' to `ln(a) b', where `a ^ b' is the selected term.

For multiple-term sums or products, `j D' takes off one term at a
time: `a * (b + c - d)' goes to `a * (c - d) + a b' with the `c - d'
selected so that you can type `j D' repeatedly to expand completely.
The `j D' command allows a numeric prefix argument which specifies the
maximum number of times to expand at once; the default is one time
only.

The `j D' command is implemented using rewrite rules.
See Selections with Rewrite Rules.  The rules are stored in
the Calc variable `DistribRules'.  A convenient way to view these
rules is to use `s e' (`calc-edit-variable') which displays and edits
the stored value of a variable.  Press M-# M-# to return from editing
mode; be careful not to make any actual changes or else you will
affect the behavior of future `j D' commands!

To extend `j D' to handle new cases, just edit `DistribRules'
as described above.  You can then use the `s p' command to save
this variable's value permanently for future Calc sessions.
See Operations on Variables.

The `j M' (`calc-sel-merge') command is the complement of `j D'; given
`a b - a c' with either `a b' or `a c' selected, the result is `a * (b
- c)'.  Once again, `j M' can also merge calls to functions like `exp'
and `ln'; examine the variable `MergeRules' to see all the relevant
rules.

The `j C' (`calc-sel-commute') command swaps the arguments of the
selected sum, product, or equation.  It always behaves as if `j b'
mode were in effect, i.e., the sum `a + b + c' is treated as the
nested sums `(a + b) + c' by this command.  If you put the cursor on
the first `+', the result is `(b + a) + c'; if you put the cursor on
the second `+', the result is `c + (a + b)' (which the default
simplifications will rearrange to `(c + a) + b').  The relevant rules
are stored in the variable `CommuteRules'.

You may need to turn default simplifications off (with the `m O'
command) in order to get the full benefit of `j C'.  For example,
commuting `a - b' produces `-b + a', but the default simplifications
will "simplify" this right back to `a - b' if you don't turn them off.
The same is true of some of the other manipulations described in this
section.

The `j N' (`calc-sel-negate') command replaces the selected term with
the negative of that term, then adjusts the surrounding formula in
order to preserve the meaning.  For example, given `exp(a - b)' where
`a - b' is selected, the result is `1 / exp(b - a)'.  By contrast,
selecting a term and using the regular `n' (`calc-change-sign')
command negates the term without adjusting the surroundings, thus
changing the meaning of the formula as a whole.  The rules variable is
`NegateRules'.

The `j &' (`calc-sel-invert') command is similar to `j N' except it
takes the reciprocal of the selected term.  For example, given `a -
ln(b)' with `b' selected, the result is `a + ln(1/b)'.  The rules
variable is `InvertRules'.

The `j E' (`calc-sel-jump-equals') command moves the selected term
from one side of an equation to the other.  Given `a + b = c + d' with
`c' selected, the result is `a + b - c = d'.  This command also works
if the selected term is part of a `*', `/', or `^' formula.  The
relevant rules variable is `JumpRules'.

The `j I' (`calc-sel-isolate') command isolates the selected term on
its side of an equation.  It uses the `a S' (`calc-solve-for') command
to solve the equation, and the Hyperbolic flag affects it in the same
way.  See Solving Equations.  When it applies, `j I' is often
easier to use than `j E'.  It understands more rules of algebra, and
works for inequalities as well as equations.

The `j *' (`calc-sel-mult-both-sides') command prompts for a formula
using algebraic entry, then multiplies both sides of the selected
quotient or equation by that formula.  It simplifies each side with `a
s' (`calc-simplify') before re-forming the quotient or equation.  You
can suppress this simplification by providing any numeric prefix
argument.  There is also a `j /' (`calc-sel-div-both-sides') which is
similar to `j *' but dividing instead of multiplying by the factor you
enter.

As a special feature, if the numerator of the quotient is 1, then the
denominator is expanded at the top level using the distributive law
(i.e., using the `C-u -1 a x' command).  Suppose the formula on the
stack is `1 / (sqrt(a) + 1)', and you wish to eliminate the square
root in the denominator by multiplying both sides by `sqrt(a) - 1'.
Calc's default simplifications would change the result `(sqrt(a) - 1)
/ (sqrt(a) - 1) (sqrt(a) + 1)' right back to the original form by
cancellation; Calc expands the denominator to `sqrt(a) (sqrt(a) - 1) +
sqrt(a) - 1' to prevent this.  (You would now want to use an `a x'
command to expand the rest of the way, whereupon the denominator would
cancel out to the desired form, `a - 1'.)  When the numerator is not
1, this initial expansion is not necessary because Calc's default
simplifications will not notice the potential cancellation.

If the selection is an inequality, `j *' and `j /' will accept any
factor, but will warn unless they can prove the factor is either
positive or negative.  (In the latter case the direction of the
inequality will be switched appropriately.)  See Declarations, for
ways to inform Calc that a given variable is positive or negative.  If
Calc can't tell for sure what the sign of the factor will be, it will
assume it is positive and display a warning message.

For selections that are not quotients, equations, or inequalities,
these commands pull out a multiplicative factor: They divide (or
multiply) by the entered formula, simplify, then multiply (or divide)
back by the formula.

The `j +' (`calc-sel-add-both-sides') and `j -'
(`calc-sel-sub-both-sides') commands analogously add to or subtract
from both sides of an equation or inequality.  For other types of
selections, they extract an additive factor.  A numeric prefix
argument suppresses simplification of the intermediate results.

The `j U' (`calc-sel-unpack') command replaces the selected function
call with its argument.  For example, given `a + sin(x^2)' with
`sin(x^2)' selected, the result is `a + x^2'.  (The `x^2' will remain
selected; if you wanted to change the `sin' to `cos', just press `C'
now to take the cosine of the selected part.)

The `j v' (`calc-sel-evaluate') command performs the
normal default simplifications on the selected sub-formula.
These are the simplifications that are normally done automatically
on all results, but which may have been partially inhibited by
previous selection-related operations, or turned off altogether
by the `m O' command.  This command is just an auto-selecting
version of the `a v' command (See Algebraic Manipulation).

With a numeric prefix argument of 2, `C-u 2 j v' applies
the `a s' (`calc-simplify') command to the selected
sub-formula.  With a prefix argument of 3 or more, e.g., `C-u j v'
applies the `a e' (`calc-simplify-extended') command.
See Simplifying Formulas.  With a negative prefix argument
it simplifies at the top level only, just as with `a v'.  Here the
"top" level refers to the top level of the selected sub-formula.

The `j "' (`calc-sel-expand-formula') command is to `a "' (*Note
Algebraic Manipulation::) what `j v' is to `a v'.

You can use the `j r' (`calc-rewrite-selection') command to define
other algebraic operations on sub-formulas.  See Rewrite Rules.