Sha256: 28eb9be41a4c51a11f8e98bc10076b4908c59113c270f3ee69c93fb344d0d866

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'symath/value'
require 'symath/definition/operator'

module SyMath
  class Definition::Int < Definition::Operator
    def initialize()
      super(:int)
    end

    def description()
      return 'int(f, a, b) - integral of f [from a to b]'
    end
    
    def validate_args(e)
      a = e.args[1]
      b = e.args[2]

      if (!a.nil? and b.nil?) or (a.nil? and !b.nil?)
        raise "A cannot be defined without b and vica versa."
      end
    end

    def get_variable(exp)
      if exp.is_a?(SyMath::Operator) and
        exp.definition.is_function? and
        exp.definition.args.length > 0
        v = exp.definition.args[0]
      else
        v = (exp.variables)[0].to_m
      end

      return v.to_d
    end

    def evaluate_call(c)
      exp = c.args[0]
      var = get_variable(exp)
      a = c.args[1]
      b = c.args[2]

      exp = exp.evaluate

      if a.nil?
        ret = exp.normalize.anti_derivative(var)
        return ret.nil? ? nil : ret + :C.to_m
      else
        int = exp.normalize.anti_derivative(var)
        # TODO: Setting for evaluating the bounds expression?
        return op(:bounds, lmd(int, var.undiff), a, b).evaluate
      end
    end

    def to_latex(args)
      if !args
        args = @args
      end
      
      if args[0].is_sum_exp?
        exp = "\\left(#{args[0].to_latex}\\right)"
      else
        exp = args[0].to_latex
      end

      var = get_variable(args[0])
      a = args[1]
      b = args[2]

      if a.nil?
        return "\\int #{exp}\\,#{var.to_latex}"
      else
        return "\\int_{#{a.to_latex}}^{#{b.to_latex}} #{exp}\\,#{var.to_latex}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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