Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- At=Struct.new(:al, :bl, :a, :b) do
- def initialize(*args)
- super
- self.b ||= self.a
- end
- def -@
- At.new(al, bl, -a, -b)
- end
- def positive?
- if self.a >= 0 && self.b >= 0
- return true
- end
- end
- def negative?
- if self.a < 0 && self.b<0
- return true
- end
- end
- def +(other)
- At.new(
- self.al + other.al,
- self.bl + other.bl,
- self.a + other.a,
- self.b + other.b
- )
- end
- def -(other)
- At.new(
- self.al + other.al,
- self.bl + other.bl,
- self.a - other.a,
- self.b - other.b
- )
- end
- def *(other)
- if [self, other].all?(&:positive?)
- At.new(
- self.a * other.al + other.a * self.al,
- self.b * other.bl + other.b * self.bl,
- self.a * other.a,
- self.b * other.b
- )
- elsif [self, other].all?(&:negative?)
- -self * -other
- elsif self.negative? && other.positive?
- -(-self * other)
- elsif self.positive? && other.negative?
- -(self * -other)
- end
- end
- def /(other)
- if [self, other].all?(&:positive?)
- At.new(
- ((self.a * other.bl + other.b * self.al) / (other.b**2)).round(2),
- ((self.b * other.al + other.a * self.bl) / (other.a**2)).round(2),
- (self.a / other.b).round(2),
- (self.b / other.a).round(2)
- )
- elsif [self, other].all?(&:negative?)
- -self / -other
- elsif self.negative? && other.positive?
- -(-self / other)
- elsif self.positive? && other.negative?
- -(self / -other)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement