两个区间的乘法

时间:2015-04-21 05:53:41

标签: math python-3.x intervals

根据以下问题从自我培训中挑选exercise

  

顺便说一句,Ben也在密码评论,"通过测试间隔端点的符号,可以将mul_interval分解为九种情况,其中只有一种情况需要两次以上的乘法。"使用Ben的建议写一个快速乘法函数:

def mul_interval_fast(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y, using as few multiplications as possible.

    >>> str_interval(mul_interval_fast(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    >>> str_interval(mul_interval_fast(interval(-2, -1), interval(4, 8)))
    '-16 to -4'
    >>> str_interval(mul_interval_fast(interval(-1, 3), interval(-4, 8)))
    '-12 to 24'
    >>> str_interval(mul_interval_fast(interval(-1, 2), interval(-8, 4)))
    '-16 to 8'
    """
    "*** YOUR CODE HERE ***"

我可以分析一下,下面是结果模式:

(1, 3)(5, 7)  ---> [min(5, 7, 15, 21), max(5, 7, 15, 21)]              --->   (5, 21)
(-1, -3)(-5, -7)  --->  [min(5, 7, 15, 21), max(5, 7, 15, 21)]         --->   (5, 21)
+++++++++++++++++++++++++++++++
(1, 3)(5, -7)  --->  [min(5, -7, 15, -21), max(5, -7, 15, -21)]        --->   (-21, 15)
(-1, 3)(5, -7)  --->  [min(-5, 7, 15, -21), max(-5, 7, 15, -21)]       --->   (-21, 15)
(1, -3)(-5, 7)  --->  [min(-5, 7, 15, -21), max(-5, 7, 15, -21)]       --->   (-21, 15)
(-1, -3)(-5, 7)  --->  [min(5, -7, 15, -21), max(5, -7, 15, -21)]      --->   (-21, 15)
+++++++++++++++++++++++++++++++++++
(1, 3)(-5, 7)  --->  [min(-5, 7, -15, 21), max(-5, 7, -15, 21)]        --->   (-15, 21)
(-1, 3)(-5, 7)  --->  [min(5, -7, -15, 21), max(5, -7, -15, 21)]       --->   (-15, 21)
(1, -3)(5, -7)  --->  [min(5, -7, -15, 21), max(5, -7, -15, 21)]       --->   (-15, 21)
(-1, -3)(5, -7)  --->  [min(-5, 7, -15, 21), max(-5, 7, -15, 21)]      --->   (-15, 21)
++++++++++++++++++++++++++++++++
(1, 3)(-5, -7)   ---> [min(-5, -7, -15, -21), max(-5, -7, -15, -21)]   --->   (-21, -5)
(-1, -3)(5, 7)  --->  [min(-5, -7, -15, -21), max(-5, -7, -15, -21)]   --->   (-21, -5)
++++++++++++++++++++++++++++++
(-1, 3)(5, 7)   ---> [min(-5, -7, 15, 21), max(-5, -7, 15, 21)]        --->   (-7, 21)
(1, -3)(-5, -7)  --->  [min(-5, -7, 15, 21), max(-5, -7, 15, 21)]      --->   (-7, 21)
+++++++++++++++++++++++++++++++
(-1, 3)(-5, -7) --->   [min(5, 7, -15, -21), max(5, 7, -15, -21)]      --->   (-21, 7)
(1, -3)(5, 7)  --->  [min(5, 7, -15, -21), max(5, 7, -15, -21)]        --->   (-21, 7)

但是上面的模式没有显示那些nine cases

所以,具体而言,我想理解这一点:By testing the signs of the endpoints of the intervals, it is possible to break mul_interval into nine cases,

请帮我解决这个间隔算术!!!

2 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解,但有一个时间间隔(a1,b1)有3种情况:a<b<=0a<0<b0<=a<b。因此,对于两个区间(a1,b1)(a2,b2),有3x3 = 9个案例。就乘法而言,最成问题的是a1<0<b1a2<0<b2。然后,您需要知道minmaxa1*a2b1*b2,甚至a1*b2的{​​{1}}和a2*b1,而其他案例只是需要知道两个端点的缩放。

答案 1 :(得分:0)

您可以检查以下情况: 研究间隔是否重叠零,或者始终是正数还是负数,可以将问题分解为最多需要两个乘数的情况。

请注意,原始问题假设x [0]

例如:

if (x[1] <= 0) {
   if (y[1] <= 0) {} // both negative
   if (y[0] >= 0) {} // one negative ane positive
   else {} // y is both negative and positive, x is only negative
}

if (x[0] >= 0) {
   if (y[1] <= 0) {} // one negative ane positive
   if (y[0] >= 0) {} // both positive
   else {} // y is both negative and positive, x is only positive
}
else {} //both overlap zero