Message Boards Message Boards

0
|
5903 Views
|
11 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

How do you adapt the pattern/replace method when the expression deepens?

Posted 9 years ago

The title will be clearer with the examples below:

  expr1 = \!\(
   \*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(2\)]\(
   \*SubsuperscriptBox[\(\[Sum]\), \(j = 1\), \(3\)]
   \*SubscriptBox[\(x\), \(i\)] 
   \*SubscriptBox[\(y\), \(j\)]\)\) + xy 
  expr2= \!\(
    \*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(3\)]\ \(
    \*SubsuperscriptBox[\(\[Product]\), \(j = 1\), \(2\)]\((
    \*SubscriptBox[\(x\), \(i\)] - \ 
    \*SubscriptBox[\(y\), \(j\)])\)\(\ \ \)\)\) + (x-y)

I want to keep in expr1 all the terms whose subscripts are not identical. I can obtain this with

Cases[expr1 /. Plus -> List, 
  Times[Subscript[_, a_], Subscript[_, b_]] /; a != b] /. List -> Plus

With expr2 same objective but I have now more than one Plus in the FullForm of this expression and I do not know the method to extend the Cases I coded before. I know the existence of TreeForm but not how it can help here.(if it does) I would like to get;

 (Subscript[x, 1] - Subscript[y, 2]) + (Subscript[x, 2] - Subscript[y,
  1]) + (Subscript[x, 3] - Subscript[y, 1]) (Subscript[x, 3] - 
  Subscript[y, 2])

Can the the pattern/replace here be used without coding Cases?

POSTED BY: wojtek potocki
11 Replies
Posted 9 years ago

Hi Wojtek,

It's not a direct answer, but one way to accomplish this is to avoid generating the terms in the first place by using the Kronecker delta:

In[1]:=  expr1 = \!\(
\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(2\)]\(
\*SubsuperscriptBox[\(\[Sum]\), \(j = 
      1\), \(3\)]\((1 - KroneckerDelta[i, j])\) 
\*SubscriptBox[\(x\), \(i\)] 
\*SubscriptBox[\(y\), \(j\)]\)\) + xy 

Out[1]= xy + Subscript[x, 2] Subscript[y, 1] + 
 Subscript[x, 1] Subscript[y, 2] + Subscript[x, 1] Subscript[y, 3] + 
 Subscript[x, 2] Subscript[y, 3]

In[2]:=   expr2 = \!\(
\*SubsuperscriptBox[\(\[Sum]\), \(i = 1\), \(3\)]\ \(
\*SubsuperscriptBox[\(\[Product]\), \(j = 
      1\), \(2\)]\((1 - KroneckerDelta[i, j])\) \((
\*SubscriptBox[\(x\), \(i\)] - \ 
\*SubscriptBox[\(y\), \(j\)])\)\)\)   + (x - y)

Out[2]= x - y + (Subscript[x, 3] - Subscript[y, 1]) (Subscript[x, 3] -
     Subscript[y, 2])

enter image description here

Kind regards, David

POSTED BY: David Keith
Posted 9 years ago

Yes I can accomplish it with your answer..However I would like to have answer based on pattern/replace which is much more adaptive to a change of the original expression.

POSTED BY: wojtek potocki

You should also think about "-". In case of polynomials this should work:

Cases[
  expr2 // Expand,
  (sign_.) (l : Subscript[_, a_]) ( k : Subscript[_, b_]) /; a != b :>  sign l k
  ] // Total

> -Subscript[x, 2] Subscript[y, 1]-Subscript[x, 3] Subscript[y, 1]-Subscript[x, 1] Subscript[y, 2]-Subscript[x, 3] Subscript[y, 2]+3 Subscript[y, 1] Subscript[y, 2]

POSTED BY: Kuba Podkalicki
Posted 9 years ago

Hi Wojtek,

I think it is possible to do what you want by crafting a set of rules for the purpose. For example, the rules below handle the cases from both expr1 and expr2, although in a more general case. It may be needed to add more rules to the set, depending on how diverse your expressions. And note that the rules can be applied at all levels of the expression if needed.

The application can be seen in the attached notebook. Although please note that it obtains a result for expr2 which is different from your desired result above. (I think that the desired result you show was perhaps a typo, since it contains components not present in the expression before replacement.)

rule = {
   (* terms like x1y1 or x1+y1 *)
   _[Subscript[_, x_], Subscript[_, x_]] -> 0,
   (* terms like (x1-y1) *)
   _[Subscript[_, x_], _[_, Subscript[_, x_]]] -> 0
   };

Kind regards, David

Attachments:
POSTED BY: David Keith

I think the following might be a simpler method:

expr1 = xy + Subscript[x, 1] Subscript[y, 1] + 
 Subscript[x, 2] Subscript[y, 1] + Subscript[x, 1] Subscript[y, 2] + 
 Subscript[x, 2] Subscript[y, 2] + Subscript[x, 1] Subscript[y, 3] + 
 Subscript[x, 2] Subscript[y, 3];
expr2 = x - y + (Subscript[x, 1] - Subscript[y, 1]) (Subscript[x, 1] - 
    Subscript[y, 2]) + (Subscript[x, 2] - Subscript[y, 1]) (Subscript[
    x, 2] - Subscript[y, 2]) + (Subscript[x, 3] - Subscript[y, 
    1]) (Subscript[x, 3] - Subscript[y, 2])

rule = k_. Subscript[_, a_] Subscript[_, b_] /; a == b :> Sequence[];

Then for expr1:

expr1 /. rule
xy + Subscript[x, 2] Subscript[y, 1] + 
 Subscript[x, 1] Subscript[y, 2] + Subscript[x, 1] Subscript[y, 3] + 
 Subscript[x, 2] Subscript[y, 3]

And for expr2 (I'm not sure how you want to handle the Powers):

Expand[expr2] /. rule
x - y + 
\!\(\*SubsuperscriptBox[\(x\), \(1\), \(2\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(2\), \(2\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(3\), \(2\)]\) - 
 Subscript[x, 2] Subscript[y, 1] - Subscript[x, 3] Subscript[y, 1] - 
 Subscript[x, 1] Subscript[y, 2] - Subscript[x, 3] Subscript[y, 2] + 
 3 Subscript[y, 1] Subscript[y, 2]
Posted 9 years ago

I am confused as to what we are trying to eliminate. Is it Xi*Yj with i=j and (Xi-Yj) with i=j ? Or just the former after expansion?

POSTED BY: David Keith
Posted 9 years ago

There is some misunderstanding in the answers I received.

In the expression $x_{j}$ the subscript is $j$ and not $x_{j}$ . Since in MMA you have to write Subscript[x,j] it makes a confusion about what a subscript means.
When I wrote" I want to keep all the terms whose subscripts are not identical. in 'expr1' and 'expr2' " it means:

In expr1 get rid of terms like $x_{1}y_{1}$, keep terms like $x_{1}y_{2}$ and also get rid of $xy$, which is not a subscripted term

In expr2 get rid of terms like $x_{1}-y_{1}$, keep terms like $x_{1}-y_{2}$ and also get rid of $x-y$, which is not a subscripted term

In my initial l answer I found a clumsy way to do it on one expression but no way to expand on it with a related expression with more levels.

Later on I will answer individually to the 3 answers I received.

POSTED BY: wojtek potocki
Posted 9 years ago

Yes your clever answer does the job for both of my expressions but for expr2 had to be expanded which I would rather not have, I'm not an expert and I do not understand all of it.

Could you please comment on your use of commas () around sign. ,k:...l;.. which is most enigmatic to me since there isn't a single comma in my expanded (FullForm or not) expressions, and what is the use of dot after sign ? Thanks

POSTED BY: Updating Name
Posted 9 years ago

Yes your clever answer does the job forexpr1 but not for expr2

It gives me

  Subscript[x, 2] Subscript[y, 1] - Subscript[x, 3] Subscript[y, 1] - 
  Subscript[x, 1] Subscript[y, 2] - Subscript[x, 3] Subscript[y, 2] + 
  3 Subscript[y, 1] Subscript[y, 2]

Instead of (after elimination)

  (Subscript[x, 1] - Subscript  [y, 2]) + (Subscript[x, 2] - Subscript[y, 
   1]) + (Subscript[x, 3] - Subscript[y, 1]) (Subscript[x, 3] - 
   Subscript[y, 2])  // Expand

I'm not an expert and I do not understand all of your answer:.

Could you please comment on your use of commas () around sign. ,k:...l;.. which is most enigmatic to me since there isn't a single comma in my expanded (FullForm or not) expressions, and what is the use of dot after sign ? Thanks

POSTED BY: wojtek potocki
Posted 9 years ago

Hi David

I replied to your latest reply in the attached notebook as it is probably better to look at that. My updates are in green Best regards

Attachments:
POSTED BY: wojtek potocki
Posted 9 years ago

Hello David

Your answer is OK for expr1 but I am at a loss to understand your use of dot after k_. Could you please comment it a little?

For expr2 your answer is not what I expect because you expand the expression before applying the rule . After the rule is applied I am looking for

$ (x_{1}-y_{2}) + (x_{2}-y_{1}) + (x_{3}-y_{1})(x_{3}-y_{2}) $

As MMA in input form will simplify this by removing some brackets a HoldForm may perhaps be needed..

Regards

POSTED BY: wojtek potocki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract