Advertisement
kokokozhina

Untitled

Dec 13th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.70 KB | None | 0 0
  1. # подгружаются основные определения
  2. require_relative './task16-math'
  3.  
  4. class Pi < Number
  5.   PI = Math::PI
  6. end
  7.  
  8. class Sin < MathExpression
  9.   def initialize(e = Number.new(1))
  10.     @e = e
  11.   end
  12.  
  13.   def eval env
  14.     e = @e.eval env
  15.     e.evalExp
  16.   end
  17.  
  18.   def derivative var
  19.     e = @e.derivative var
  20.     cosinus = Cos.new(@e)
  21.     e.multiply cosinus
  22.   end
  23.  
  24.   def to_s
  25.     "sin(#{@e.to_s})"
  26.   end
  27.  
  28. end
  29.  
  30. class Cos < MathExpression
  31.   def initialize(e = Number.new(1))
  32.     @e = e
  33.   end
  34.  
  35.   def eval env
  36.     e = @e.eval env
  37.     e.evalExp
  38.   end
  39.  
  40.   def derivative var
  41.     e = @e.derivative var
  42.     minus_sinus = Negate(Sin.new(@e)).new
  43.     e.multiply minus_sinus
  44.   end
  45.  
  46.   def to_s
  47.     "cos(#{@e.to_s})"
  48.   end
  49.  
  50. end
  51.  
  52.  
  53.  
  54. # ПРИМЕРЫ
  55.  
  56. puts(Derivative.new(Cos.new(Variable.new("y")), "y"))
  57.  
  58.  
  59. puts(Derivative.new(
  60.         Multiply.new(
  61.           Add.new(
  62.             Add.new(
  63.               Multiply.new(Number.new(2),      
  64.                            Variable.new("x")),
  65.               Multiply.new(Number.new(3),
  66.                            Variable.new("y"))),
  67.             Negate.new(Variable.new("z"))),
  68.           Add.new(Number.new(5),
  69.                   Variable.new("x"))),
  70.         "x"))  
  71.  
  72. # puts(Let.new("a", Multiply.new(Number.new(2),
  73. #                                Variable.new("x")),        # a = 2x
  74. #        Let.new("b", Multiply.new(Number.new(3),
  75. #                                  Variable.new("y")),      # b = 3y
  76. #          Let.new("e", Add.new(Variable.new("a"),        
  77. #                               Variable.new("b")),         # e = a + b
  78. #             Multiply.new(Multiply.new(Variable.new("e"),
  79. #                                       Variable.new("e")),
  80. #                          Variable.new("e"))))))            
  81.  
  82. # puts(Let.new("a", Multiply.new(Number.new(2),
  83. #                                Variable.new("x")),   # a = 2x
  84. #        Let.new("b", Multiply.new(Number.new(3),
  85. #                                  Variable.new("y")), # b = 3y
  86. #          Let.new("cube",                             # cube(e) = e * e * e
  87. #                  MyFunc.new(nil, "e",
  88. #                    Multiply.new(Multiply.new(Variable.new("e"),
  89. #                                              Variable.new("e")),
  90. #                                 Variable.new("e"))),
  91. #            # вычисление cube(a + b) + cube(b)
  92. #            Add.new(Call.new(Variable.new("cube"),        # вызов cube(a + b)
  93. #                             Add.new(Variable.new("a"),
  94. #                                     Variable.new("b"))),
  95. #                    Call.new(Variable.new("cube"),        # вызов cube(b)
  96. #                             Variable.new("b")))))))
  97.  
  98. # # вычисление выражений
  99. # puts(Derivative.new(
  100. #         Multiply.new(
  101. #           Add.new(
  102. #             Add.new(
  103. #               Multiply.new(Number.new(2),      
  104. #                            Variable.new("x")),
  105. #               Multiply.new(Number.new(3),
  106. #                            Variable.new("y"))),
  107. #             Negate.new(Variable.new("z"))),
  108. #           Add.new(Number.new(5),
  109. #                   Variable.new("x"))),
  110. #         "x").eval_exp)  
  111.  
  112. # puts(Let.new("a", Multiply.new(Number.new(2),
  113. #                                Variable.new("x")),        # a = 2x
  114. #        Let.new("b", Multiply.new(Number.new(3),
  115. #                                  Variable.new("y")),      # b = 3y
  116. #          Let.new("e", Add.new(Variable.new("a"),        
  117. #                               Variable.new("b")),         # e = a + b
  118. #             Multiply.new(Multiply.new(Variable.new("e"),
  119. #                                       Variable.new("e")),
  120. #                          Variable.new("e"))))).eval_exp)            
  121.  
  122. # puts(Let.new("a", Multiply.new(Number.new(2),
  123. #                                Variable.new("x")),   # a = 2x
  124. #        Let.new("b", Multiply.new(Number.new(3),
  125. #                                  Variable.new("y")), # b = 3y
  126. #          Let.new("cube",                             # cube(e) = e * e * e
  127. #                  MyFunc.new(nil, "e",
  128. #                    Multiply.new(Multiply.new(Variable.new("e"),
  129. #                                              Variable.new("e")),
  130. #                                 Variable.new("e"))),
  131. #            # вычисление cube(a + b) + cube(b)
  132. #            Add.new(Call.new(Variable.new("cube"),        # вызов cube(a + b)
  133. #                             Add.new(Variable.new("a"),
  134. #                                     Variable.new("b"))),
  135. #                    Call.new(Variable.new("cube"),        # вызов cube(b)
  136. #                             Variable.new("b")))))).eval_exp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement