一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 821|回复: 6
收起左侧

[cs61a]hw04

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-5-23 00:09:43 | 显示全部楼层 |阅读模式

[其他]CS61A #2 - 04@UCBerkely

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
hw04: http://gaotx.com/cs61a/hw/hw04/
my solution: http://gaotx.com/blogs/2015/05/14/cs61a-hw04/

评分

1

查看全部评分

karte_polo 发表于 2015-5-25 18:28:48 | 显示全部楼层
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a,b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert upper_bound(y) !=0 and lower_bound(y)!=0
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    p1 = lower_bound(x) - lower_bound(y)
    p2 = lower_bound(x) - upper_bound(y)
    p3 = upper_bound(x) - lower_bound(y)
    p4 = upper_bound(x) - upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))
   

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"
r1= interval(-100,99)
r2= interval(-150,-148)
print ("Using Par1",par1(r1,r2))
print ("Using Par2",par2(r1,r2))

def multiple_references_explanation():
    return """The mulitple reference problem..."""

def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    p1 = lower_bound(x)
    p2 = upper_bound(x)
    p3 = -b/(2*a)
    def f(t):
        return a*t*t+b*t+c
    v1,v2,v3 = map(f,(p1,p2,p3))
    if p3<=p2 and p3>=p1:
        return interval(min(v1,v2,v3),max(v1,v2,v3))
    else:
        return interval(min(v1,v2),max(v1,v2))

def polynomial(x, c):
    """Return the interval that is the range of the polynomial defined by
    coefficients c, for domain interval x.

    >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
    '-3 to 0.125'
    >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
    '0 to 10'
    >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
    '18.0 to 23.0'
    """
    "*** YOUR CODE HERE ***"
    def f(t):
        return sum([x*pow(t,y) for x,y in zip(c,range(len(c)))])
    "def f1(t):return sum([x*y(pow(t,y-1) for x,y in zip("


评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-28 08:34:55 | 显示全部楼层
只做了一部分。。。
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a, b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert lower_bound(y) > 0 or upper_bound(y) < 0, 'Divided by zero'
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    lower = lower_bound(x) - upper_bound(y)
    upper = upper_bound(x) -  lower_bound(y)
    return interval(lower, upper)

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"
a = interval(1, 1)
b = interval(2, 1)
print(str_interval(par1(a, b)), '!=', str_interval(par1(a, b)))

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

FFFelix 发表于 2015-5-28 09:03:52 | 显示全部楼层
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a,b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    negative_y = interval(-upper_bound(y), -lower_bound(y))
    return add_interval(x, negative_y)

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"

def multiple_references_explanation():
    return """The mulitple reference problem..."""

def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    sol = - b / (2 * a)
    f = lambda x: a * x * x + b * x + c
    o, p, q = map(f,(lower_bound(x), upper_bound(x), sol)
    if lower_bound(x) <=  sol and upper_bound(x) >= sol:
        return interval(min(o,p,q), max(o,p,q))
    else:
        return interval(min(o,p), max(o,p))


def polynomial(x, c):
    """Return the interval that is the range of the polynomial defined by
    coefficients c, for domain interval x.

    >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
    '-3 to 0.125'
    >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
    '0 to 10'
    >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
    '18.0 to 23.0'
    """
    "*** YOUR CODE HERE ***"


也是只做了必做的代码部分

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-20 23:07:01 | 显示全部楼层
Q5没搞懂是什么意思。。。其余的都做啦~代码如下:
#hw4
#Q1
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a,b]   

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]
   
def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

#Q2        
def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert upper_bound(y) != 0
    assert lower_bound(y) != 0
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

#Q3
def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    lower = lower_bound(x) - upper_bound(y)
    upper = upper_bound(x) - lower_bound(y)
   
    return interval(lower, upper)
               
#Q4
def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)


def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
ex1 = interval(-300, 299)
ex2 = interval(-550,-548)
print('This is par1',par1(ex1, ex2))
print('This is par2',par2(ex1, ex2))

#Q6
def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    f = lambda x: a * x ** 2 + b * x + c
    extreme = -b / 2 * a
    lower = f(lower_bound(x))
    upper = f(upper_bound(x))
    mid = f(extreme)
    if extreme <= upper_bound(x) and extreme >= lower_bound(x):
        return interval(min(lower, upper, mid), max(lower, upper, mid))
    else:
        return interval(min(lower, upper), max(lower, upper))

#back up
def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

liyimeng 发表于 2016-2-1 12:15:02 | 显示全部楼层
最后一题数**不准备做了。。。反正是Challenge。。。
前面的都做了
hw04.png

代码如下:
  1. def interval(a, b):
  2.     """Construct an interval from a to b."""
  3.     "*** YOUR CODE HERE ***"
  4.     return [a, b]


  5. def lower_bound(x):
  6.     """Return the lower bound of interval x."""
  7.     "*** YOUR CODE HERE ***"
  8.     return x[0]


  9. def upper_bound(x):
  10.     """Return the upper bound of interval x."""
  11.     "*** YOUR CODE HERE ***"
  12.     return x[1]


  13. def str_interval(x):
  14.     """Return a string representation of interval x.

  15.     >>> str_interval(interval(-1, 2))
  16.     '-1 to 2'
  17.     """
  18.     return '{0} to {1}'.format(lower_bound(x), upper_bound(x))


  19. def add_interval(x, y):
  20.     """Return an interval that contains the sum of any value in interval x and
  21.     any value in interval y.

  22.     >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
  23.     '3 to 10'
  24.     """
  25.     lower = lower_bound(x) + lower_bound(y)
  26.     upper = upper_bound(x) + upper_bound(y)
  27.     return interval(lower, upper)


  28. def mul_interval(x, y):
  29.     """Return the interval that contains the product of any value in x and any
  30.     value in y.

  31.     >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
  32.     '-8 to 16'
  33.     """
  34.     p1 = lower_bound(x) * lower_bound(y)
  35.     p2 = lower_bound(x) * upper_bound(y)
  36.     p3 = upper_bound(x) * lower_bound(y)
  37.     p4 = upper_bound(x) * upper_bound(y)
  38.     return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))


  39. def div_interval(x, y):
  40.     """Return the interval that contains the quotient of any value in x divided
  41.     by any value in y.

  42.     Division is implemented as the multiplication of x by the reciprocal of y.

  43.     >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
  44.     '-0.25 to 0.5'
  45.     """
  46.     "*** YOUR CODE HERE ***"
  47.     assert (upper_bound(y) != 0 and lower_bound(y) != 0), "Zero can't be divided"
  48.     reciprocal_y = interval(1 / upper_bound(y), 1 / lower_bound(y))
  49.     return mul_interval(x, reciprocal_y)


  50. def sub_interval(x, y):
  51.     """Return the interval that contains the difference between any value in x
  52.     and any value in y.

  53.     >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
  54.     '-9 to -2'
  55.     """
  56.     "*** YOUR CODE HERE ***"
  57.     return interval(lower_bound(x) - upper_bound(y), upper_bound(x) - lower_bound(y))


  58. def par1(r1, r2):
  59.     return div_interval(mul_interval(r1, r2), add_interval(r1, r2))


  60. def par2(r1, r2):
  61.     one = interval(1, 1)
  62.     rep_r1 = div_interval(one, r1)
  63.     rep_r2 = div_interval(one, r2)
  64.     return div_interval(one, add_interval(rep_r1, rep_r2))


  65. # These two intervals give different results for parallel resistors:
  66. "*** YOUR CODE HERE ***"
  67. interval_1 = (1, 2)
  68. interval_2 = (4, 5)
  69. print("Using par1", par1(interval_1, interval_2))
  70. print("Using par2", par2(interval_1, interval_2))


  71. def multiple_references_explanation():
  72.     return """Because it calculate the inexact value, the more times it reference, the less precision it has"""


  73. def quadratic(x, a, b, c):
  74.     """Return the interval that is the range of the quadratic defined by
  75.     coefficients a, b, and c, for domain interval x.

  76.     >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
  77.     '-3 to 0.125'
  78.     >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
  79.     '0 to 10'
  80.     """
  81.     "*** YOUR CODE HERE ***"
  82.     extreme = -b / (2 * a)
  83.     f = lambda x: a * x * x + b * x + c
  84.     l = lower_bound(x)
  85.     r = upper_bound(x)
  86.     if extreme >= l and extreme <= r:
  87.         return interval(min(f(extreme), f(l), f(r)), max(f(extreme), f(l), f(r)))
  88.     else:
  89.         return interval(min(f(l), f(r)), max(f(l), f(r)))


  90. def polynomial(x, c):
  91.     """Return the interval that is the range of the polynomial defined by
  92.     coefficients c, for domain interval x.

  93.     >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
  94.     '-3 to 0.125'
  95.     >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
  96.     '0 to 10'
  97.     >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
  98.     '18.0 to 23.0'
  99.     """
  100.     "*** YOUR CODE HERE ***"
复制代码
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-2-21 17:37:52 | 显示全部楼层
最后一题各种想还是做不出……

def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a, b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert upper_bound(y) != 0 and lower_bound(y) != 0
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    return [lower_bound(x) - upper_bound(y), upper_bound(x) - lower_bound(y)]

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"
r1 = interval(1133, 1134)
r2 = interval(3216, 3217)
print(par1(r1, r2))
print(par2(r1, r2))


def multiple_references_explanation():
    return """The mulitple reference problem..."""

def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    x_ext = - b / ( 2 * a )
    extreme = a * x_ext * x_ext + b * x_ext + c
    p = a * lower_bound(x) * lower_bound(x) + b * lower_bound(x) + c
    q = a * upper_bound(x) * upper_bound(x) + b * upper_bound(x) + c
    if lower_bound(x) < x_ext < upper_bound(x):
        return interval(min(extreme, p, q), max(extreme, p, q))
    else:
        return interval(min(p, q), max(p, q))

def polynomial(x, c):
    """Return the interval that is the range of the polynomial defined by
    coefficients c, for domain interval x.

    >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
    '-3 to 0.125'
    >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
    '0 to 10'
    >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
    '18.0 to 23.0'
    """
    "*** YOUR CODE HERE ***"

   
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-8 17:55

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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