找回密码
 快速注册
搜索
查看: 13|回复: 2

[python] 检验不等式$\min,\max$

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2024-10-14 03:03 |阅读模式
基于这道数学题:kuing.cjhb.site/forum.php?mod=redirect&goto=findpost&ptid=12514&pid=60461
$$\max\{\min\{a,b\},\min\{c,d\}\}
=\min\{\max\{a,c\},\max\{b,d\}\}$$
这对于所有 a,b,c,d 都成立吗?

大多数情况下都是成立。但是,也有一些组合不成立,例如:
- \( (1, 3, 4, 2) \)
- \( (1, 4, 3, 2) \)
- \( (2, 3, 4, 1) \)
- \( (2, 4, 3, 1) \)
- \( (3, 1, 2, 4) \)
- \( (3, 2, 1, 4) \)
- \( (4, 1, 2, 3) \)
- \( (4, 2, 1, 3) \)
  1. import itertools
  2. # Function to compute the left-hand side of the equation
  3. def lhs(a, b, c, d):
  4.     return max(min(a, b), min(c, d))
  5. # Function to compute the right-hand side of the equation
  6. def rhs(a, b, c, d):
  7.     return min(max(a, c), max(b, d))
  8. # Verify for all possible combinations of a, b, c, d where a, b, c, d are distinct integers
  9. values = [1, 2, 3, 4]
  10. all_combinations = list(itertools.permutations(values, 4))
  11. # Check all combinations
  12. results = []
  13. for combo in all_combinations:
  14.     a, b, c, d = combo
  15.     if lhs(a, b, c, d) == rhs(a, b, c, d):
  16.         results.append((a, b, c, d, True))
  17.     else:
  18.         results.append((a, b, c, d, False))
  19. results
复制代码
这表明,虽然该方程在许多情况下成立,但它并不适用于 a、b、c、d 的所有可能组合。

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-10-14 03:03
我们现在要处理一个特殊情况,其中 min 和 max 操作中的值仅取决于 \(a\)、\(b\) 和 \(c\):$$\max\{\min\{0,b\},\min\{c,a+b+c\}\} =\min\{\max\{0,c\},\max\{b,a+b+c\}\}$$
这对于所有 a,b,c 都成立吗?
  1. # Function to compute the left-hand side of the equation
  2. def lhs_extended(a, b, c):
  3.     return max(min(0, b), min(c, a + b + c))
  4. # Function to compute the right-hand side of the equation
  5. def rhs_extended(a, b, c):
  6.     return min(max(0, c), max(b, a + b + c))
  7. # Testing different values of a, b, and c
  8. test_values = [
  9.     (0, 1, 2),   # Simple positive values
  10.     (-1, 2, 3),  # Negative a
  11.     (1, -2, 3),  # Negative b
  12.     (1, 2, -3),  # Negative c
  13.     (0, 0, 0),   # All zeroes
  14.     (0, -1, 1),  # Edge case with 0 and negative values
  15.     (-1, -1, -1) # All negatives
  16. ]
  17. # Check all cases
  18. test_results = [(a, b, c, lhs_extended(a, b, c) == rhs_extended(a, b, c)) for a, b, c in test_values]
  19. test_results
复制代码
似乎对这些测试用例都成立。基于此,该方程似乎通常是正确的。

不,您应该尝试 -4,2,3,该方程不适用于这组特定值。
对于 \( a = -4 \)、\( b = 2 \) 和 \( c = 3 \),结果如下:
  1. a, b, c = -4, 2, 3
  2. lhs_result = lhs_extended(a, b, c)
  3. rhs_result = rhs_extended(a, b, c)
  4. lhs_result, rhs_result, lhs_result == rhs_result
复制代码

左侧=1
右侧=2

这表明,该等式对于 \(a\)、\(b\) 和 \(c\) 的所有值不是都普遍成立的。它在很多情况下都成立,但某些条件(例如 \(a = -4\) 这样的负值)会破坏等式。

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-10-14 04:15
对于 \( a = -4 \)、\( b = 2 \) 和 \( c = 3 \),检验kuing.cjhb.site/forum.php?mod=viewthread&tid=12514
$$\tag1\label1\max_{y\in [0,1]}\left(\min_{x\in [0,1]}f(x,y)\right)= \min_{x\in[0,1]}\left(\max_{y\in[0,1]}f(x,y)\right)$$
计算左侧值:
Minimize [{-4 x y + 2 x + 3 y, 0 <=x <= 1}, x]
$$
\min_{0 \leq x \leq 1}(-4 x y+2 x+3 y)= \begin{cases}2-y & 2 y \geq 1 \\ 3 y & \text { (otherwise) }\end{cases}
$$
Plot[Evaluate[Piecewise[{{2 - y, 2 y >= 1}}, 3 y]], {y, 0, 1}]
download.gif
$$
\max_{0 \leq y \leq 1}\left(\min_{0 \leq x \leq 1}(-4 x y+2 x+3 y)\right)=1.5
$$
计算右侧值:
Minimize [{-4 x y + 2 x + 3 y, 0 <=y <= 1}, y]
$$
\max_{0 \leq y \leq 1}\left(-4 x y+2 x+3 y \right)= \begin{cases}3-2 x & 4 x \geq 3 \\ 2 x & \text { (otherwise) }\end{cases}
$$
Plot[Evaluate[Piecewise[{{3 - 2 x, 4 x >= 3}}, 2 x]], {x, 0, 1}]
download (1).gif
$$
\min_{0 \leq x \leq 1}\left(\max_{0 \leq y \leq 1}(-4 x y+2 x+3 y)\right)=1.5
$$
等式\eqref{1}成立!

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 11:55

Powered by Discuz!

× 快速回复 返回顶部 返回列表