在给定整数约束的 Mathematica 中简化积分结果的正确方法

发布于 2024-12-09 15:01:11 字数 443 浏览 3 评论 0原文

计算以下积分应该是非零,并且 mathematica 正确地给出非零结果

Integrate[ Cos[ (Pi * x)/2 ]^2 * Cos[ (3*Pi*x)/2 ]^2, {x, -1, 1}]

但是,尝试更一般的积分:

FullSimplify[
    Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2], 
              {x, -1, 1}], 
    Element[{m, n}, Integers]]

产生零,这对于 m = n = 1 来说绝对不正确

我期望有一个条件表达式。在计算积分之前,是否可以“告诉”mathematica 我对 m 和 n 的约束,以便它正确处理特殊情况?

Evaluating the following integral should be non-zero, and mathematica correctly gives a non-zero result

Integrate[ Cos[ (Pi * x)/2 ]^2 * Cos[ (3*Pi*x)/2 ]^2, {x, -1, 1}]

However, attempting a more general integral:

FullSimplify[
    Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2], 
              {x, -1, 1}], 
    Element[{m, n}, Integers]]

yields zero, which is definitely not true for m = n = 1

I'd expect a conditional expression. Is it possible to "tell" mathematica about my constraints on m and n before the integral is evaluated so that it handles the special cases properly?

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

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

发布评论

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

评论(4

沦落红尘 2024-12-16 15:01:11

虽然我迟到了,但到目前为止,还没有人给出完整的解决方案。

有时,在积分之前更好地理解被积函数是值得的。考虑一下,

ef = TrigReduce[
    Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]]/.
  Cos[a_] :> Cos[ Simplify[a, Element[{m,n}, Integers] ] ]

它返回

(2 Cos[(m - n) Pi x] + Cos[(1 + m - n) Pi x] + Cos[(1 - m + n) Pi x] + 
 Cos[(m + n) Pi x] + 2 Cos[(1 + m + n) Pi x] + Cos[(2 + m + n) Pi x] )/8

每个项的形式为 Cos[q Pi x] 和整数 q。现在,在 -1 到 1(其中 q 是整数)上积分 Cos[q Pi x] 时需要考虑两种情况:q == 0q != 0

情况q = 0:这是 Mathematica 在一般结果中遗漏的特殊情况,因为它意味着一个常数被积函数。 (当我手动执行此操作时,我也经常会错过它,因此 Mathematica 不完全是罪魁祸首。)因此,在本例中,积分为 2。

严格来说,这不是真的。当被告知在 -1 -1 上积分 Cos[ q Pi x ] 时x < 1,Mathematica 返回

2 Sin[ Pi q ]/( Pi q )

0,除非 q == 0。此时,该函数在严格意义上是未定义的,但 Limit[Sin[x]/x, q -> 0]==1。由于 q == 0 处的奇点是可移除,因此积分当 q -> 时,为 2 0 。所以,Mathematica 并没有错过它,它只是以一种无法立即识别的形式。

情况q != 0:由于Cos[Pi x]是周期性的,周期为2,因此Cos[q Pi x]的积分]x == -1x == 1将始终超过q个周期。换句话说,

Integrate[ Cos[q Pi x], {x, -1, 1}, 
  Assumptions -> (Element[ q, Integers ] && q != 0) ] == 0

综合起来,这意味着

Integrate[ Cos[q Pi x], {x, -1, 1}, Assumptions -> Element[ q, Integers ] ] == 
Piecewise[{{ q == 0, 2 }, { 0, q!=0 }}]

积分被积函数的扩展形式

intef = ef /. Cos[q_ Pi x] :> Piecewise[{{2, q == 0}, {0, q != 0}}] // 
 PiecewiseExpand 

使用它,我们可以通过它承认非积分解来 。为了解决这个问题,我们需要将条件减少到只有那些具有整体解决方案的条件,并且我们不妨简化一下:

(Piecewise[{#1, 
    LogicalExpand[Reduce[#2 , {m, n}, Integers]] // 
     Simplify[#] &} & @@@ #1, #2] & @@ intef) /. C[1] -> m

\begin{Edit}

为了限制混乱,在内部分段 具有以下结构:

{ { { value, condition } .. }, default }

在使用 Apply (@@) 中,条件列表是第一个参数,默认参数是第二个参数。为了处理这个问题,我需要简化每个值的条件,因此我使用第二种简短形式 Apply (@@@) 在条件列表上,以便对于每个值条件对我得到

{ value, simplified condition }

简化过程使用 Reduce 将条件限制为整数、LogicalExpand 帮助消除冗余,以及 Simplify 限制项数。 Reduce 内部使用 任意常量C[1],它设置为 C[1] == m,因此我们将 C[1] 设置回 m< /code> 完成简化

\end{Edit}

给出了

Piecewise[{
 {3/4, (1 + n == 0 || n == 0) && (1 + m == 0 || m == 0)},
 {1/2, Element[m, Integers] && 
       (n == m || (1 + m + n == 0 && (m <= -2 || m >= 1)))},
 {1/4, (n == 1 + m || (1 + n == m && (m <= -1 || m >= 1)) || 
       (m + n == 0 && (m >= 1 || m <= 0)) || 
       (2 + m + n == 0 && (m <= -1 || m >= 0))) && 
       Element[m, Integers]},
 {0, True}
}

完整的解决方案。

另一个编辑:我应该指出,1/2 和 1/4 情况都包含 3/ 中的 mn 值4 例。看起来 3/4 的情况可能是其他两个的交集,因此也是它们的总和。 (我还没有计算出来,但我强烈怀疑这是真的。)Piecewise 按顺序评估条件(我认为),因此不可能出现错误。

再次编辑分段对象的简化并没有达到应有的效率。问题在于替换规则 C[1] -> 的位置。米。 Simplify 恰好在这个过程的后期使用它。但是,如果将其引入 LogicalExpand 并将假设添加到 Simplify

(Piecewise[{#1, 
    LogicalExpand[Reduce[#2 , {m, n}, Integers] /. C[1] -> m] // 
     Simplify[#, {m, n} \[Element] Integers] &} & @@@ #1, #2] & @@ intef)

,则会产生更清晰的结果

Piecewise[{
 {3/4, -2 < m < 1 && -2 < n < 1}, 
 {1/2, (1 + m + n == 0 && (m >= 1 || m <= -2)) || m == n}, 
 {1/4, 2 + m + n == 0 || (m == 1 + n && m != 0) || m + n == 0 || 1 + m == n},
 {0, True}
}]

While I'm late to the party, no one has given a complete solution, thus far.

Sometimes, it pays to understand the integrand better before you integrate. Consider,

ef = TrigReduce[
    Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]]/.
  Cos[a_] :> Cos[ Simplify[a, Element[{m,n}, Integers] ] ]

which returns

(2 Cos[(m - n) Pi x] + Cos[(1 + m - n) Pi x] + Cos[(1 - m + n) Pi x] + 
 Cos[(m + n) Pi x] + 2 Cos[(1 + m + n) Pi x] + Cos[(2 + m + n) Pi x] )/8

where each term has the form Cos[q Pi x] with integral q. Now, there are two cases to consider when integrating Cos[q Pi x] over -1 to 1 (where q is integral): q == 0 and q != 0.

Case q = 0: This is a special case that Mathematica misses in the general result, as it implies a constant integrand. (I'll often miss it, also, when doing this by hand, so Mathematica isn't entirely to blame.) So, the integral is 2, in this case.

Strictly speaking, this isn't true. When told to integrate Cos[ q Pi x ] over -1 < x < 1, Mathematica returns

2 Sin[ Pi q ]/( Pi q )

which is 0 except when q == 0. At that point, the function is undefined in the strict sense, but Limit[Sin[x]/x, q -> 0] == 1. As the singularity at q == 0 is removable, the integral is 2 when q -> 0. So, Mathematica does not miss it, it is just in a form not immediately recognized.

Case q != 0: Since Cos[Pi x] is periodic with period 2, an integral of Cos[q Pi x] from x == -1 to x == 1 will always be over q periods. In other words,

Integrate[ Cos[q Pi x], {x, -1, 1}, 
  Assumptions -> (Element[ q, Integers ] && q != 0) ] == 0

Taken together, this means

Integrate[ Cos[q Pi x], {x, -1, 1}, Assumptions -> Element[ q, Integers ] ] == 
Piecewise[{{ q == 0, 2 }, { 0, q!=0 }}]

Using this, we can integrate the expanded form of the integrand via

intef = ef /. Cos[q_ Pi x] :> Piecewise[{{2, q == 0}, {0, q != 0}}] // 
 PiecewiseExpand 

which admits non-integral solutions. To clean that up, we need to reduce the conditions to only those that have integral solutions, and we might as well simplify as we go:

(Piecewise[{#1, 
    LogicalExpand[Reduce[#2 , {m, n}, Integers]] // 
     Simplify[#] &} & @@@ #1, #2] & @@ intef) /. C[1] -> m

\begin{Edit}

To limit confusion, internally Piecewise has the structure

{ { { value, condition } .. }, default }

In using Apply (@@), the condition list is the first parameter and the default is the second. To process this, I need to simplify the condition for each value, so then I use the second short form of Apply (@@@) on the condition list so that for each value-condition pair I get

{ value, simplified condition }

The simplification process uses Reduce to restrict the conditions to integers, LogicalExpand to help eliminate redundancy, and Simplify to limit the number of terms. Reduce internally uses the arbitrary constant, C[1], which it sets as C[1] == m, so we set C[1] back to m to complete the simplification

\end{Edit}

which gives

Piecewise[{
 {3/4, (1 + n == 0 || n == 0) && (1 + m == 0 || m == 0)},
 {1/2, Element[m, Integers] && 
       (n == m || (1 + m + n == 0 && (m <= -2 || m >= 1)))},
 {1/4, (n == 1 + m || (1 + n == m && (m <= -1 || m >= 1)) || 
       (m + n == 0 && (m >= 1 || m <= 0)) || 
       (2 + m + n == 0 && (m <= -1 || m >= 0))) && 
       Element[m, Integers]},
 {0, True}
}

as the complete solution.

Another Edit: I should point out that both the 1/2 and 1/4 cases include the values for m and n in the 3/4 case. It appears that the 3/4 case may be the intersection of the other two, and, hence, their sum. (I have not done the calc out, but I strongly suspect it is true.) Piecewise evaluates the conditions in order (I think), so there is no chance of getting this incorrect.

Edit, again: The simplification of the Piecewise object is not as efficient as it could be. At issue is the placement of the replacement rule C[1] -> m. It happens to late in the process for Simplify to make use of it. But, if it is brought inside the LogicalExpand and assumptions are added to Simplify

(Piecewise[{#1, 
    LogicalExpand[Reduce[#2 , {m, n}, Integers] /. C[1] -> m] // 
     Simplify[#, {m, n} \[Element] Integers] &} & @@@ #1, #2] & @@ intef)

then a much cleaner result is produce

Piecewise[{
 {3/4, -2 < m < 1 && -2 < n < 1}, 
 {1/2, (1 + m + n == 0 && (m >= 1 || m <= -2)) || m == n}, 
 {1/4, 2 + m + n == 0 || (m == 1 + n && m != 0) || m + n == 0 || 1 + m == n},
 {0, True}
}]
节枝 2024-12-16 15:01:11

并不总是零...

k = Integrate[
         Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2], 
         {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];

(*Let's find the zeroes of the denominator *)

d = Denominator[k];
s = Solve[d == 0, {m, n}]

(*The above integral is indeterminate at those zeroes, so let's compute 
  the integral again there (a Limit[] could also do the work) *)

denZ = Integrate[
          Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] /.s, 
          {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];

(* All possible results are generated with m=1 *)

denZ /. m -> 1

(*
{1/4, 1/2, 1/4, 1/4, 1/2, 1/4}
*)

可视化这些情况:

Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2] 
     /. s /. m -> 1, {x, -1, 1}]

在此处输入图像描述

与零结果积分一进行比较:

Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] 
     /. {m -> 1, n -> 4}, {x, -1, 1}]

在此处输入图像描述

Not always zero ...

k = Integrate[
         Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2], 
         {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];

(*Let's find the zeroes of the denominator *)

d = Denominator[k];
s = Solve[d == 0, {m, n}]

(*The above integral is indeterminate at those zeroes, so let's compute 
  the integral again there (a Limit[] could also do the work) *)

denZ = Integrate[
          Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] /.s, 
          {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];

(* All possible results are generated with m=1 *)

denZ /. m -> 1

(*
{1/4, 1/2, 1/4, 1/4, 1/2, 1/4}
*)

Visualizing those cases:

Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2] 
     /. s /. m -> 1, {x, -1, 1}]

enter image description here

Compare with a zero result integral one:

Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] 
     /. {m -> 1, n -> 4}, {x, -1, 1}]

enter image description here

梦过后 2024-12-16 15:01:11

如果您只需删除整个 FullSimplify 部分,mathematica 就会为您巧妙地进行集成。

Integrate[
 Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
    2], {x, -1, 1}]

在此处输入图像描述

包含 mn 的条件是整数,最好使用Integrate中的Asminations选项。

Integrate[
 Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
    2], {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]]

在此处输入图像描述

If you just drop the whole FullSimplify part, mathematica does the integration neatly for you.

Integrate[
 Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
    2], {x, -1, 1}]

enter image description here

To include the condition that m and n are integers, it's better to use the Assumptions option in Integrate.

Integrate[
 Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
    2], {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]]

enter image description here

倾`听者〃 2024-12-16 15:01:11

让我们对两个整数使用一些结论性条件m=n||m!=n

Assuming[{(n \[Element] Integers && m \[Element] Integers && m == n)},
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}]]

本例的答案是1/2。对于另一种情况

Assuming[{(n \[Element] Integers && m \[Element] Integers && m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
 2], {x, -1, 1}]]

,答案是0

然而,我惊讶地发现,如果我们将这两个条件添加为“非此即彼”,Mathematica 在积分后会返回一零。我的意思是,在以下情况下,我只会得到零,但不会得到“1/2||0”。

Assuming[{(n \[Element] Integers && m \[Element] Integers && 
 m == n) || (n \[Element] Integers && m \[Element] Integers && 
 m != n)}, 
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
 2], {x, -1, 1}]]

顺便说一句,我们可以看到该积分变为不确定的唯一条件。

res = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
  2], {x, -1, 1}] // Simplify

输出在这里。

在此处输入图像描述

现在让我们看看 mn 的所有关系> 可能不得不使 Integral 变坏!

BadPart = (res*4 Pi);
Flatten@(Solve[(Denominator[#] == 0), m] & /@ 
 Table[BadPart[[i]], {i, 1, Length@BadPart}] /. 
Rule -> Equal) // TableForm

在此处输入图像描述

因此,这些是特殊情况,正如 Sjoerd 提到的那样,它们具有无限实例。

BR

Lets use some conclusive conditions about the two integers m=n||m!=n.

Assuming[{(n \[Element] Integers && m \[Element] Integers && m == n)},
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}]]

The answer for this case is 1/2. For the other case it is

Assuming[{(n \[Element] Integers && m \[Element] Integers && m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
 2], {x, -1, 1}]]

and the answer is 0.

However I am amazed to see that if we add this two conditions as an "either or stuff", Mathematica returns one zero after integration. I mean in case of the following I am getting only zero but not ``1/2||0`.

Assuming[{(n \[Element] Integers && m \[Element] Integers && 
 m == n) || (n \[Element] Integers && m \[Element] Integers && 
 m != n)}, 
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
 2], {x, -1, 1}]]

By the way we can see the conditions exclusively where this integral becomes Indeterminate.

res = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
  2], {x, -1, 1}] // Simplify

The output is here.

enter image description here

Now lets see all the relations m and n can have to make the Integral bad!

BadPart = (res*4 Pi);
Flatten@(Solve[(Denominator[#] == 0), m] & /@ 
 Table[BadPart[[i]], {i, 1, Length@BadPart}] /. 
Rule -> Equal) // TableForm

enter image description here

So these are the special cases which as Sjoerd mentioned are having infinite instances.

BR

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