Guest User

Untitled

a guest
Nov 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. transformation = Transformation(MultiplyBy(5), Add(10))
  2. transformed_4 = transformation(4)
  3.  
  4. restored_4 = transformation.restore(transformed_4)
  5. assert restored_4 == 4 # не будем учитывать пока ошибки округления
  6.  
  7. from sympy import symbols, solve, simplify, factor
  8. from sympy.parsing.sympy_parser import (
  9. parse_expr,
  10. standard_transformations,
  11. implicit_multiplication,
  12. implicit_multiplication_application)
  13.  
  14. trf = (
  15. standard_transformations +
  16. (implicit_multiplication_application,))
  17.  
  18. x, y, z, t = symbols('x y z t')
  19.  
  20. formula_str = "5*x + 10"
  21. formula = parse_expr(formula_str, transformations=trf)
  22. print(formula)
  23. #5*x + 10
  24.  
  25. res = formula.subs(x, 4)
  26. print(res)
  27. #30
  28.  
  29. # решение уравнения: `5*x + 10 = 0`
  30. print(solve(formula))
  31. #[-2]
  32.  
  33. # решение уравнения: `5*x + 10 = 30` или `5*x + 10 - 30 = 0`
  34. print(solve(formula_str + f" - {res}"))
  35. #[4]
  36.  
  37. In [94]: [r for r in solve('x**4-(x-2)**2') if r.is_real]
  38. Out[94]: [-2, 1]
  39.  
  40. In [95]: simplify('sin(x)**2 + cos(x)**2')
  41. Out[95]: 1
  42.  
  43. In [96]: factor('x**4-(x-2)**2')
  44. Out[96]: (x - 1)*(x + 2)*(x**2 - x + 2)
  45.  
  46. In [100]: simplify('sin(x)**2 / (1 - sin(x)**2)')
  47. Out[100]: tan(x)**2
  48.  
  49. class Transform():
  50. def __init__(self,x=0,mul=1,add=0):
  51. self.x = x
  52. self.mul = mul
  53. self.add = add
  54. def calculate (self):
  55. self.res = self.x*self.mul + self.add
  56. return self.res
  57. def reverse (self):
  58. self.reverse = (self.res-self.add)/self.mul
  59. return self.reverse
  60. def expose (self):
  61. return "{}x{}+{}".format(self.x, self.mul, self.add)
  62.  
  63. eq1 = Transform(5, 2, 20)
  64. print(eq1.calculate())
  65. print(eq1.expose())
  66. print(eq1.reverse())
  67.  
  68. 30
  69. 5x2+20
  70. 5.0
Add Comment
Please, Sign In to add comment