Mathematica : Easy? Function that returns and increases counter.

Click For Summary

Discussion Overview

The discussion revolves around creating a function in Mathematica that replaces instances of a noncommutative variable with a specific format while incrementing an index for each replacement. The focus is on finding a method to achieve this through replacement rules and counters.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes a need to replace instances of vD with a format v[a].D[a] while ensuring that the index "a" does not repeat.
  • Another participant suggests using a Product function to achieve a similar result, but the original poster clarifies that a replacement rule is necessary.
  • The original poster provides an example chain and expresses the need for a hidden counter that increments with each replacement.
  • A solution is proposed where a counter variable is incremented within the replacement rule, which the original poster confirms works.
  • A later reply reflects on the simplicity of the solution compared to more complex alternatives previously considered.

Areas of Agreement / Disagreement

Participants generally agree on the need for a replacement rule with an incrementing counter, and the original poster successfully finds a solution. There is no significant disagreement noted in the discussion.

Contextual Notes

The discussion includes considerations about the noncommutative nature of the variables and the specific formatting required for the replacements, which may affect the implementation of the solution.

Who May Find This Useful

Individuals working with Mathematica who need to implement dynamic variable replacements with incrementing indices, particularly in the context of noncommutative algebra.

Hepth
Science Advisor
Gold Member
Messages
458
Reaction score
40
I have a statement that looks like :

vD.vD.vD.vD

Where vD is noncommutative (but that doesn't really matter)

I want to replace it so that each vD becomes v[a].D[a]

But I would like it so the "a" is not repeating. So I need somehow a function that each time it is run returns it with an increased index : v[a[1]].D[a[1]]
So that
vD.vD.vD /.{vD :> FXN}
results in
v[a[1]].D[a[1]].v[a[2]].D[a[2]].v[a[3]].D[a[3]]


Does anyone know how to do this?
 
Physics news on Phys.org
Would something like Product[v[a[j]].d[a[j]], {j, 1, 3}] work for you?
 
No, i need a replacement rule.

Lets say my chain is like:

chain = A.vD.B.C.vD.vD.E.F

i want something so I can say:

chain/.{vD:> XXXXX}

where the result would be

A.v[a[1]].D[a[1]].B.C.v[a[2]].D[a[2]].v[a[3]].D[a[3]].E.F

and then if I did it again, it would be

A.v[a[4]].D[a[4]].B.C.v[a[5]].D[a[5]].v[a[6]].D[a[6]].E.FSo there is some hidden counter I can set "acount = 0"

Then when I make a replacement rule, it not only replaces it, but increments the counter:

chain/.{vD:> v[a[acount]].D[a[acount]];acount++;}

but I can't force a command like a++ to run in a replacement rule, and if I make it a Do statement, I don't know how to return what I want...Ah, I just tried this and it worked!
cnt=0;
chain /. {vD :> v[a[cnt]].DD[a[cnt++]]}

So obviously simple I am an idiot :)
 
Aha. Yeah, I wondered if the product solution was too simple to suggest but then I remembered that I once spent an hour perfecting a recursive algorithm in java to format a text string before someone reminded me that I could just have used a string replace function.

Nice solution, by the way.
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K