组合模式来收集术语

发布于 2024-12-20 11:46:34 字数 1837 浏览 2 评论 0原文

我有一个 Mathematica 表达式(称为 expr),它是许多项的总和。我还有一个列表(称为 var),其中包含可能出现在其中一些术语中的一些变量和函数。

我想做的第一件事是提取包含一定次数的多个变量和函数的术语。例如,如果 var = {a, f[_]},那么我可能想要一次性提取包含变量 a 和函数 的所有术语f 2 次。 f[f[a + b]] 是满足这些条件的术语示例。

我想做的第二件事是创建一个列表(称为输出),其中一次包含原始表达式的所有项。该列表应该根据术语包含 var 中指定的变量和函数的次数对术语进行分组。 对于 var = {a, f[_]} ,输出将为 output = {{包含 0 * a 和 0 * f[_] 的项的总和,“.. ." 1 * a 和 0 f, "...的总和" 2a 0f, ... }, {"...的总和" 0a 1f, "...的总和" 1a, 1f, ... } }

给定问题 2 的解决方案,解决问题1很容易:要提取表达式的某个项,您只需从列表输出中选择正确的元素即可。出于这个原因,我尝试解决问题 2。为了让事情变得清晰,我从一个简单的表达式开始,只包含一个术语。首先我生成一个模式列表

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] & @@@ var // ReleaseHold

输出为

{{False, True}, {False, False, True, False}}

解释是:变量 a 出现一次,函数 f 出现 2 次。 现在我想采用 basicpattern 内列表的外积来进行模式组合。然后,新的模式列表可以与 Cases 一起使用,从 expr 中选择术语并将它们放入列表中。

我被困在这里:如何获取列表内列表的外积?我猜

Outer[And, {{True, False}, {True, False, False, False}}, 1]

但这并没有给出八项。

更新

在 Sjoerd 的帮助下,我走得更远了。

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] & @@@ var;
Outer[And, ##] & @@ %;
test = %[[2, 3]]
%// ReleaseHold

给出输出

Hold[Count[{f[a + f[y]]}, a, 10]] == 1 &&
Hold[Count[{f[a + f[y]]}, f[_], 10]] == 2
True

解释是 f[a + f[y]] 包含一次 a 和两次 f[_]。 外部产品是这样的测试列表。

假设我将 expr 更改为

expr = f[a + f[y]] + g[z] + y^2 - 13 x + 12a + af[x]

如何使用 test 的内容收集包含一个 a 的所有项两次 f[_]?

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.

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 var = {a, f[_]}, then I may want to extract all terms that contain the variable a one time and the function f 2 times. f[f[a + b]] is an example of a term that satisfies these criteria.

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 var = {a, f[_]} the output would be 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, ... }}

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

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] & @@@ var // ReleaseHold

The output is

{{False, True}, {False, False, True, False}}

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.

Here I am stuck: How to take the outer product of the lists inside a list? I guessed

Outer[And, {{True, False}, {True, False, False, False}}, 1]

But this does not give the eight terms.

Update

With Sjoerd's help I came a bit further.

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] & @@@ var;
Outer[And, ##] & @@ %;
test = %[[2, 3]]
%// ReleaseHold

Gives as output

Hold[Count[{f[a + f[y]]}, a, 10]] == 1 &&
Hold[Count[{f[a + f[y]]}, f[_], 10]] == 2
True

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.

Suppose I change expr to

expr = f[a + f[y]] + g[z] + y^2 - 13 x + 12a + a f[x]

How can I use the content of test to collect all terms containing one a and two times f[_]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2024-12-27 11:46:34

你的故事很长,但我想你的问题可以归结为:

如何获取列表内列表的外积?

如果这是您唯一想知道的事情,那么您就很接近了。可以简单地这样做:

booleanLists = {{True, False}, {True, False, False, False}};

Outer[And, ##] & @@ booleanLists

(* ==>  {{True, False, False, False}, {False, False, False, False}} *)

Yours is a long story, but I guess your question boils down to:

How to take the outer product of the lists inside a list?

If this is the only thing you wanted to know you were close. It can be done simply like this:

booleanLists = {{True, False}, {True, False, False, False}};

Outer[And, ##] & @@ booleanLists

(* ==>  {{True, False, False, False}, {False, False, False, False}} *)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文