Sha256: 0da0b6e42a947be8ee01a5a92c92bbcbe6b96fe4e97959c03e12f70d515594a0

Contents?: true

Size: 909 Bytes

Versions: 2

Compression:

Stored size: 909 Bytes

Contents

require 'symath/definition/function'

module SyMath
  class Definition::Abs < Definition::Function
    def initialize()
      super(:abs)
    end

    def description()
      return 'abs(x) - absolute value'
    end

    def reduce_call(c)
      arg = c.args[0]
      if arg.is_nan?
        return :nan.to_m
      # Corner case, -oo is positive with complex arithmetic, so we need a
      # specific check for that.
      elsif arg.is_negative? or arg == -:oo
        return - arg
      elsif arg.is_positive? or arg.is_zero?
        return arg
      else
        return c
      end
    end

    def to_s(args = nil)
      if args
        arg = args[0].to_s
      else
        arg = '...'
      end

      return "|#{arg}|"
    end

    def to_latex(args = nil)
      if args
        arg = args[0].to_latex
      else
        arg = '...'
      end
      
      return "\\lvert#{arg}\\rvert"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
symath-0.1.1 lib/symath/definition/abs.rb
symath-0.1.0 lib/symath/definition/abs.rb