Note that there are some explanatory texts on larger screens.

plurals
  1. POCombining patterns to collect terms
    primarykey
    data
    text
    <p>I have a Mathematica expressions (called expr), which is a sum of many terms. Also I have a list (called var) with some of the variables and functions that may appear in some of these terms.</p> <p>The first thing I would like to do is to extract terms that contain a number of variables and functions a certain number of times. For example if the <code>var = {a, f[_]}</code>, then I may want to extract all terms that contain the variable <code>a</code> one time and the function <code>f</code> 2 times. <code>f[f[a + b]]</code> is an example of a term that satisfies these criteria. </p> <p>The second thing I would like to do, is to create a list (called output) that contains all terms of the original expression one time. The list should be such that it groups terms according to the number of times they contain the variables and functions specified in var. For <code>var = {a, f[_]}</code> the output would be <code>output = {{sum of those terms containing 0 * a and 0 * f[_], "sum of..." 1 * a and 0 f, "sum of..." 2a 0f, ... }, {"sum of..." 0a 1f, "sum of..." 1a, 1f, ... }}</code></p> <p>Given a solution to problem 2, it is easy to solve problem 1: To extract a certain term of the expression, you just have to pick the right element from the list output. For that reason I tried to solve problem 2. To keep things clear I started with a simple expression, containing just one term. First I generate a list of patterns</p> <pre><code>expr = f[a + f[y]] var = {{a, 1}, {f[_], 3}} basicpattern[symbol_, n_, term_] = Hold[Table[Count[{term}, symbol, 10] == i, {i, 0, n}]] basicpattern[#1, #2, expr] &amp; @@@ var // ReleaseHold </code></pre> <p>The output is</p> <pre><code>{{False, True}, {False, False, True, False}} </code></pre> <p>The interpretation is that: variable a occurs one time, function f appears 2 times. Now I would like to take the outer product of the lists inside basicpattern to make combinations of patterns. Then the new list of patterns can be used together with Cases to select terms from expr and put them in a list. </p> <p>Here I am stuck: How to take the outer product of the lists inside a list? I guessed</p> <pre><code>Outer[And, {{True, False}, {True, False, False, False}}, 1] </code></pre> <p>But this does not give the eight terms.</p> <p><strong>Update</strong></p> <p>With Sjoerd's help I came a bit further.</p> <pre><code>expr = f[a + f[y]]; var = {{a, 1}, {f[_], 3}}; basicpattern[symbol_, n_, term_] := Table[Hold[Count[{term}, symbol, 10]] == i, {i, 0, n}]; basicpattern[#1, #2, expr] &amp; @@@ var; Outer[And, ##] &amp; @@ %; test = %[[2, 3]] %// ReleaseHold </code></pre> <p>Gives as output</p> <pre><code>Hold[Count[{f[a + f[y]]}, a, 10]] == 1 &amp;&amp; Hold[Count[{f[a + f[y]]}, f[_], 10]] == 2 True </code></pre> <p>The interpretation is that f[a + f[y]] contains one time a and two times f[_]. The outer product is a list of tests like these. </p> <p>Suppose I change expr to</p> <p>expr = f[a + f[y]] + g[z] + y^2 - 13 x + 12a + a f[x]</p> <p>How can I use the content of test to collect all terms containing one a and two times f[_]?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload