3

I want to do the following:

f[x_] := If[OddQ[x[[1]]], Nothing, x/2 // Simplify]

A = Table[2 (n + k), {k, 0, 4}]/2 n = 2 n1; f /@ A n = 2 n1 + 1; f /@ A

This outputs the following:

{n, 1 + n, 2 + n, 3 + n, 4 + n}
{n1, 1 + n1, 2 + n1}
{1 + n1, 2 + n1}

That is: It takes the list A, makes a substitution, sieves out whatever has an odd constant. Now I want to do apply the same process to the second and third list, and then to the second and third list of the previous ones, etc. This will yield a binary tree structure but I have no idea how to do that in Mathematica. Can you help me? I'm not sure if the tagging is correct, tho.


EDIT: I'll write a simpler example that If solved, I could use it to my original problem. Suppose I want to program the following:

enter image description here

That is: It starts with $a$, if it moves to the left, it concatenates $a$ on the left and we get $aa$, if it moves to the left, it concatenates $b$ to the left so we get $ab$ and then, we proceed this way and build the tree. Now, I want to store every level of the tree so that I can show it in a graph such as the one in here. I've been thinking and I guess I could make some elementary program to do this but it is possible that Mathematica already has some neat stuff for this?

Red Banana
  • 5,329
  • 2
  • 29
  • 47
  • What should it do when the constant is zero? If you have the element n1 then does this get removed, or does it get changed to n1/2? – Daniel Lichtblau Jan 31 '21 at 16:25
  • @DanielLichtblau Nothing. I want "sieve out" odd integers that would appear there. – Red Banana Jan 31 '21 at 17:09
  • 1
    Then how does the set become empty? – Daniel Lichtblau Jan 31 '21 at 17:17
  • @DanielLichtblau Yeah, that was poor hindsight on my part. I tought the set would get empty but when I wrote it on paper, I saw it doesn't. I edited it. – Red Banana Jan 31 '21 at 17:24
  • 1
    Okay. I don't fully understand the question, but here are a few tips. (1) You can use Select to "sieve out by retention" (that is, keeping only the ones you want). (2) You can use FixedPoint to stop the sieving once the set stabilizes. (3) It is not really safe to assume the constant (which might be zero) is found as x[[1]]. I recommend instead finding the variable(s) in the list, call them vars, and determining whether constant term is odd using e.g. OddQ[x/.Thread[vars->0]]. – Daniel Lichtblau Jan 31 '21 at 17:33
  • @DanielLichtblau See the edit: I've made a simpler problem that if solved, would help me to do that. – Red Banana Feb 01 '21 at 00:09

0 Answers0