给定整数约束在 Mathematica 中简化积分结果的正确方法 [英] Proper way to simplify integral result in Mathematica given integer constraints

查看:41
本文介绍了给定整数约束在 Mathematica 中简化积分结果的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]]

产生零,对于 m = n = 1 这绝对不是真的

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

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

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?

推荐答案

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

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] ] ]

哪个返回

(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.现在,在将 Cos[q Pi x] 积分超过 -1 到 1(其中 q 是整数)时需要考虑两种情况:q == 0q != 0.

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:这是 Mathematica 在一般结果中遗漏的一个特殊情况,因为它意味着一个常数被积函数.(我也经常会在手动执行此操作时错过它,因此 Mathematica 不完全是罪魁祸首.)因此,在这种情况下,积分为 2.

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.

严格来说,这不是真的.当被告知将 Cos[ q Pi x ] 积分超过 -1 <×<1,Mathematica 返回

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 )

0 除非 q == 0.此时,严格意义上的函数是未定义的,但是 Limit[Sin[x]/x, q ->0] == 1.由于 q == 0 处的奇点是 removable,所以积分当 q -> 时为 20.所以,Mathematica 不会错过它,它只是一种不能立即识别的形式.

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:由于 Cos[Pi x] 是周期为 2 的周期,所以 Cos[q Pi x]x == -1x == 1 将始终超过 q 周期.换句话说,

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

综合起来,这意味着

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{编辑}

为了减少混淆,内部Piecewise具有结构

To limit confusion, internally Piecewise has the structure

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

在使用Apply(@@)时,条件列表是第一个参数,默认是第二个.为了处理这个,我需要简化每个值的条件,然后我使用 Apply 的第二种简短形式(@@@) 放在条件列表上,以便我得到每个值条件对

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 }

简化过程使用 Reduce 将条件限制为整数,LogicalExpand 帮助消除冗余,以及 Simplify 限制条件的数量.Reduce 内部使用了任意常数C[1],它设置为 C[1] == m,所以我们将 C[1] 设置回 m 完成简化

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{编辑}

给出

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 情况都包括 mn 的值3/4 的情况.似乎 3/4 的情况可能是其他两个的交集,因此是它们的总和.(我没有计算出来,但我强烈怀疑它是真的.)Piecewise 按顺序评估条件(我认为),所以没有机会得到这个错误.

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.

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

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)

然后产生更干净的结果

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}
}]

这篇关于给定整数约束在 Mathematica 中简化积分结果的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆