鱼C论坛

 找回密码
 立即注册
查看: 3811|回复: 1

[作品展示] 利用Python进行简单的求导

[复制链接]
发表于 2022-9-4 11:17:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码:
  1. class Monomial:
  2.     def __init__(self, term=1, times=1, have_space = True):
  3.         self.term = term
  4.         self.times = times
  5.         self.have_space = have_space
  6.     def derivate(self):
  7.         return Monomial(self.term*self.times, self.times-1)
  8.     def __repr__(self):
  9.         if self.times == 0:
  10.             return str(self.term)
  11.         else:
  12.             return str(self.term) + \
  13.                    (' ' if self.have_space else '') + \
  14.                    '*' + \
  15.                    (' ' if self.have_space else '') + \
  16.                    'x' + \
  17.                    (' ' if self.have_space else '') + \
  18.                    '^' + \
  19.                    (' ' if self.have_space else '') + \
  20.                    str(self.times)
  21.     def __str__(self):
  22.         if self.times == 0:
  23.             return str(self.term)
  24.         else:
  25.             return str(self.term) + \
  26.                    (' ' if self.have_space else '') + \
  27.                    '*' + \
  28.                    (' ' if self.have_space else '') + \
  29.                    'x' + \
  30.                    (' ' if self.have_space else '') + \
  31.                    '^' + \
  32.                    (' ' if self.have_space else '') + \
  33.                    str(self.times)

  34. class Polynomial:
  35.     def __init__(self, formula, have_space = True):
  36.         self.have_space = have_space
  37.         self.formula = formula.split('+')
  38.         self.formula = [i.replace(' ', '') for i in self.formula]
  39.         for i in range(len(self.formula)):
  40.             temp = self.formula[i]
  41.             if not(len(temp) == 5 and temp[2] == 'x' and temp[3] == '^' and temp[1] == '*'):
  42.                 raise TypeError
  43.             self.formula[i] = Monomial(int(temp[0]), int(temp[4]), self.have_space)
  44.     def derivate(self):
  45.         import copy
  46.         temp = copy.copy(self)
  47.         #print(temp)
  48.         del copy
  49.         for i in range(len(temp.formula)):
  50.             #print(temp.formula[i], type(temp.formula[i]))
  51.             temp.formula[i] = temp.formula[i].derivate()
  52.         return temp
  53.     def __str__(self):
  54.         temp = ''
  55.         for i in range(len(self.formula)):
  56.             temp += str(self.formula[i])
  57.         return temp
  58.     def __repr__(self):
  59.         temp = ''
  60.         for i in range(len(self.formula)):
  61.             temp += repr(self.formula[i])
  62.         return temp
复制代码

演示:
  1. >>> x = Polynomial('1*x^5+1*x^4')
  2. >>> x
  3. 1 * x ^ 5 + 1 * x ^ 4
  4. >>> x.derivate()
  5. 5 * x ^ 4 + 4 * x ^ 3
  6. >>> y = Polynomial('3*x^5', False)
  7. >>> y
  8. 3*x^5
  9. >>> y.derivate()
  10. 15 * x ^ 4
  11. >>>
复制代码

缺点:
1. 输入的多项式只能是包含x的式子,并且必须是1 * x ^ 5这种形式(不能写成x^5)
2. 不能输入ln sin cos tan 等函数
3. 不能输入 (x+1)^2 这种式子
总的来说,就是特别简单,限制很多
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-1-6 09:34:24 | 显示全部楼层
zhichi
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 06:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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