# How do I get Mathematica to recognize this simplification? (ReplaceAll pattern rule)

I want Mathematica to understand this $$(\sum_{k=0}^n a_k)(\sum_{j=0}^n b_j) = (\sum_{k=0}^n \sum_{j=0}^n a_k b_j)$$ I have tried this

exprr1 = Sum[Subscript[a, k], {k, 0, n}] *
Sum[Subscript[b, j], {j, 0, n}]

rules = {Sum[Subscript[a, k], {k, 0, n}]*
Sum[Subscript[b, j], {j, 0, n}] :>
Sum[Subscript[a, k]*Subscript[b, j], {k, 0, n}, {j, 0, n}]}

exprr1 /. rules


Now this works. The problem is that when I try

rules = {Sum[Subscript[d, k], {k, 0, n}]*
Sum[Subscript[e, j], {j, 0, n}] :>
Sum[Subscript[d, k]*Subscript[e, j], {k, 0, n}, {j, 0, n}]}


This does not work.

I want Mathematica to understand that no matter what the variable in the summation, and no matter what the index variable, the simplification/rules should work.

How do I do this? Thanks in advance!

• Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basic rules of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. Thanks for accepting my answer, but in the future may be a good idea to wait 24hours for other answers before accepting the best one for you. Read "What to do when someone answers", – rhermans Sep 27 '17 at 10:59
• I don't understand why you would consider the right-hand side of the equation a simplification. The left-side takes $2(n+1)+1$ operations and the right-hand side would take $(n+1)^3$ operations. What am I missing? – JimB Sep 30 '17 at 3:59
• @JimB , I wanted to know how I would make Mathematica recognize things or put things in the way I want it too. You are right, it is not necessarily a simplification in this case – S. Khan Sep 30 '17 at 10:30

You should use Blank, a pattern object that can stand for any Wolfram Language expression, usually written as _. Learn about Patterns here.

rules2 = {
Times[
Sum[
Subscript[d_, k_]
, {k_, m_, n_}
],
Sum[
Subscript[e_, j_]
, {j_, m_, n_}
]
]
:>
Sum[
Times[
Subscript[d, k],
Subscript[e, j]
]
, {k, m, n}
, {j, m, n}
]
}

exprr1 /. rules2


Try to think about the patterns in FullForm. Indentation is only for readability.

## Side note about using Subscript

By the way, not the source of your problem, but you should avoid using Subscript while defining symbols (variables) for anything other than nice display. Subscript[x, 1] is not a symbol, but a composite expression where Subscript is an operator without built-in meaning. You expect to do $x_1=2$ but you are actually doing Set[Subscript[x, 1], 2] which is to assign a Downvalue to the oprator Subscript and not an Ownvalue to an indexed x as you may intend. Read how to properly define indexed variables here.