Memoisation – 伯努利数

huangapple go评论63阅读模式
英文:

Memoisation - Bernoulli numbers

问题

我理解你只需要对代码进行翻译,以下是代码的翻译部分:

from fractions import Fraction
from scipy.special import comb
import numpy as np

# 缓存
bernoulli_cache = {}

def bernoulli(n: float):
    # 检查输入,如果存在则返回值
    if n in bernoulli_cache:
        return bernoulli_cache[n]
    
    # 当输入为0或1时
    if n == 0:
        value = Fraction(1)
    else:  
        value = Fraction(1)
        for k in range(n):
            value -= Fraction(comb(n, k)) * Fraction(bernoulli(k), (n - k + 1))
            
    # 写入缓存
    bernoulli_cache[n] = value
    
    # 分数部分用于输出
    numerator = value.numerator
    denominator = value.denominator
    
    return numerator, denominator

# 测试是否有效 - 缓存中已有部分
bernoulli_cache = {}
bernoulli(0) # 1     
bernoulli(1) # 1/2    
bernoulli(2) # 1/6     
bernoulli(3) # 0
bernoulli(4) # −1/30
bernoulli(5) # 0
bernoulli(6) # 1/42
bernoulli(7) # 0
bernoulli(8) # -1/30

# 这个不会有效 - 缓存中没有部分
bernoulli_cache = {}
bernoulli(8) # -1/30

希望这对你有所帮助!

英文:

I am trying to compute some Bernoulli numbers and am trying to use memoisation. In the example below, I can get the Bernoulli number for 8 when I run 1-7 before it, but not if the cache is clear. What changes would I need to make to run it when the cache is clear?

from fractions import Fraction
from scipy.special import comb
import numpy as np

# memoisation
bernoulli_cache = {}

def bernoulli(n: float):
    # check input, if it exists, return value
    if n in bernoulli_cache:
        return bernoulli_cache[n]
    
    # when input is 0 or 1
    if n == 0:
        value = Fraction(1)
    else:  
        value = Fraction(1)
        for k in range(n):
            value -= Fraction(comb(n, k)) * Fraction(bernoulli(k), (n - k + 1))
            
    # write to cache
    bernoulli_cache[n] = value
    
    # fraction parts for output
    numerator = value.numerator
    denominator = value.denominator
    
    return numerator, denominator

# test this works- bits in cache aleady
bernoulli_cache = {}
bernoulli(0) # 1     
bernoulli(1) # 1/2    
bernoulli(2) # 1/6     
bernoulli(3) # 0
bernoulli(4) # −1/30
bernoulli(5) # 0
bernoulli(6) # 1/42
bernoulli(7) # 0
bernoulli(8) # -1/30

# this doesn't  - bits not in cache
bernoulli_cache = {}
bernoulli(8) # -1/30

答案1

得分: 1

Your cache is storing a Fraction so when you have a cache hit you're returning a Fraction. When you have a cache miss you're returning a tuple. You can fix this by changing return numerator, denominator to return Fraction(numerator, denominator).

英文:

Your cache is storing a Fraction so when you have a cache hit you're returning a Fraction. When you have a cache miss you're returning a tuple. You can fix this by changing return numerator, denominator to return Fraction(numerator, denominator).

huangapple
  • 本文由 发表于 2023年1月6日 10:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定