Sha256: 3356b036b19809a5ade2c4a8d50b0ed559a288e8a6ef1cf26e9452ce81e7a955

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Mathmas
  class Function
    include Basic
    attr_reader :name, :vals

    def initialize(name, vals)
      raise "This is not function" unless vals.all?{|val| val.is_a?(Mathmas::Variable)}
      @expr = nil
      @name = name
      @vals = vals
      Mathmas.add_function self
    end

    def <=(expr)
      raise "The right hand of Mathmas::Function#<- should be an instance of Mathmas::Basic" unless expr.is_a?(Mathmas::Basic)
      @expr = expr
    end

    def to_s
      strs = @vals.map{|val| val.to_s}
      @name.to_s + "(#{ strs.join(",") }) = " + @expr.to_s
    end

    def to_tex
      strs = @vals.map{|val| val.to_s}
      @name.to_s + "(#{ strs.join(",") }) = " + @expr.to_tex
    end

    # @example
    #   f(x) = 1/x
    #   f.exec(x: 3) #-> 1/3
    #   f.exec(3) #-> 1/3
    #
    def exec(*args)
      if args.length == 1 && args[0].is_a?(Hash)
        @expr.exec(args[0])
      else
        symbols = @vals.map{|val| val.symbol}
        hash = args.zip(symbols).reduce({}){|memo, pair| memo[pair[1]] = pair[0]; memo}
        @expr.exec(hash)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mathmas-0.0.1 lib/mathmas/core/function.rb