1
$\begingroup$

For a given $n$, I would like to calculate a function $f_n$ recursively. Here is what I have:

(*for n=2 I have my functions already:*)

px2 = 1/2 (x3 (1 - x1) + x1 x2)
py2 = 1/2 (y2 (1 - y1) + y1 y3)

(*for n=3 I have:*)

px3 = px22[x3->2*px2]
py3 = py22[y3->2*py2]

(*for n=4 I have:*)

px4 = px222[x5->2*px3] (*increase the indexes of all variables in px2 function by 4 and insert 2*px3 wherever you see x5 in this new function*)
py4 = py222[y5->2*py3]

(*for n=5 I have:*)

px5 = px2222[x7->2*px4]
py5 = py2222[y7->2*py4]

etc.

so what is px22, px222 etc..? they are just px2 whose indexes are increased by the sum of the following 2s. For example:

px22=1/2 (x5 (1 - x3) + x3 x4)
px222=1/2 (x7 (1 - x5) + x5 x6)

etc.

Similarly,

py22 = 1/2 (y4 (1 - y3) + y3 y5)
py222 = 1/2 (y6 (1 - y5) + y5 y7)
py2222 = 1/2 (y8 (1 - y7) + y7 y9)

etc.

Example: for n=3

px3 = px22[x3->2*px2]

Since I know that

px22=1/2 (x5 (1 - x3) + x3 x4)

and

px2 = 1/2 (x3 (1 - x1) + x1 x2)

I just insert 2*px2 in px22, wherever I see x3 and I am done:

px22=1/2 (x5 (1 - ( (x3 (1 - x1) + x1 x2))) + ( (x3 (1 - x1) + x1 x2)) x4)

EDIT: I am trying to find the functions px3, px4..., py3, py4,... etc. using the given iterations in the question.

px2 and py2 functions are explicitly defined so they are already known. All the rest are unknown and can only be found iteratively. The main idea is to increase the indexes of all variables in px2 by two and then in this new function to put 2*px2 whereever I see the variable x3. So finally I get px3 but this function is now explicit. Namely it is (see above example):

px3[x1_,x2_,x3_,x4_,x5_]=1/2 (x5 (1 - ( (x3 (1 - x1) + x1 x2))) + ( (x3 (1 - x1) + x1 x2)) x4)    

But before it was

px3 = px22[x3->2*px2]

I want mathematica to give me these explicit functions of px3,py3,px4,py4,.... which I can evaluate whenever x1,x2,x3,...are given.

How can Mathematica do this thing which I am doingy by hand automatically when I just give $n$?

$\endgroup$
6
  • 4
    $\begingroup$ It is not clear what you are trying to do. Can you explain what the $x$ and $y$'s are. Use LaTeX rather than Mathematica to explain. $\endgroup$
    – SPPearce
    Commented Sep 28, 2018 at 14:56
  • $\begingroup$ @KraZug please see the edit and let me know if something is unclear. Thanks! $\endgroup$ Commented Sep 28, 2018 at 15:12
  • $\begingroup$ So, something like Nest[ReplaceAll[x[k_] :> x[k + 2]], 1/2 (x[3] (1 - x[1]) + x[1] x[2]), 3] for your px222? $\endgroup$ Commented Sep 28, 2018 at 15:19
  • $\begingroup$ @J.M. no. px222 is already given in the question. It is just px22 in which all variables indexes are increased by 2. Similarly px22 is px2 where all variables indexes are increased by two. So px222=1/2 (x7 (1 - x5) + x5 x6), and px2222=1/2 (x9 (1 - x7) + x7 x8) and px22222=1/2 (x11 (1 - x9) + x9 x10)... $\endgroup$ Commented Sep 28, 2018 at 15:30
  • $\begingroup$ Yes, I only replaced e.g. x3 with x[3] in what I showed, because those are easier to manipulate. That can be converted into x3, but it needs additional work. $\endgroup$ Commented Sep 28, 2018 at 15:33

1 Answer 1

3
$\begingroup$

As J.M. points out this problem can be dealt with more easily in Mathematica using indexed variables, so I will use them to write a set of recursive functions that satisfy your conditions as I understand them.

px[2] = 1/2 (x[3] (1 - x[1]) + x[1] x[2]);
py[2] = 1/2 (y[2] (1 - y[1]) + y[1] y[3]);
ppx[2] = px[2];
ppy[2] = py[2];
ppx[k_] := ppx[k - 1] /. x[i_] -> x[i + 2]
ppy[k_] := ppy[k - 1] /. y[i_] -> y[i + 2]
px[k_] := ppx[k] /. x[2 k - 3] -> 2 px[k - 1]
py[k_] := ppy[k] /. y[2 k - 3] -> 2 py[k - 1]

Tests

ppx[3]
1/2 (x[3] x[4] + (1 - x[3]) x[5])
ppy[3]
1/2 ((1 - y[3]) y[4] + y[3] y[5])
ppy[5]
1/2 ((1 - y[7]) y[8] + y[7] y[9])
px[3] // Simplify
1/2 (x[1] (x[2] - x[3]) (x[4] - x[5]) + x[3] (x[4] - x[5]) + x[5])

All of these tests give results compatible with the ones you show modulo the notational differences.

Note

The following shows my px[3] is structurally the same as your p3.

px3 = (x5 (1 - ((x3 (1 - x1) + x1 x2))) + ((x3 (1 - x1) + x1 x2)) x4)/2;
px3 // Simplify
1/2 (x1 (x2 - x3) (x4 - x5) + x3 (x4 - x5) + x5)
$\endgroup$
3
  • $\begingroup$ yes. thats working correctly. thank you very much. $\endgroup$ Commented Sep 29, 2018 at 19:25
  • $\begingroup$ just a quick question. Is there any way to get this result in some reasonable timie: FullSimplify[(px[20] + py[20])]? $\endgroup$ Commented Sep 29, 2018 at 23:52
  • $\begingroup$ @SeyhmusGüngören. The functions I defined can speeded up by introducing memoization into their definitions. See this documentation articled $\endgroup$
    – m_goldberg
    Commented Sep 29, 2018 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.