3
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ 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? $\endgroup$ Commented Jan 31, 2021 at 16:25
  • $\begingroup$ @DanielLichtblau Nothing. I want "sieve out" odd integers that would appear there. $\endgroup$
    – Red Banana
    Commented Jan 31, 2021 at 17:09
  • 1
    $\begingroup$ Then how does the set become empty? $\endgroup$ Commented Jan 31, 2021 at 17:17
  • $\begingroup$ @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. $\endgroup$
    – Red Banana
    Commented Jan 31, 2021 at 17:24
  • 1
    $\begingroup$ 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]]. $\endgroup$ Commented Jan 31, 2021 at 17:33

0

Your Answer

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

Browse other questions tagged or ask your own question.