Class: Mathmas::Function

Inherits:
Object
  • Object
show all
Includes:
Basic
Defined in:
lib/mathmas/core/function.rb,
lib/mathmas/plot/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Basic

#*, #**, #+, #-, #/, #coerce, #to_iruby

Constructor Details

#initialize(name, vals) ⇒ Function

Returns a new instance of Function.



6
7
8
9
10
11
12
# File 'lib/mathmas/core/function.rb', line 6

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mathmas/core/function.rb', line 4

def name
  @name
end

#valsObject (readonly)

Returns the value of attribute vals.



4
5
6
# File 'lib/mathmas/core/function.rb', line 4

def vals
  @vals
end

Instance Method Details

#<=(expr) ⇒ Object



14
15
16
17
# File 'lib/mathmas/core/function.rb', line 14

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

#exec(*args) ⇒ Object

Examples:

f(x) = 1/x
f.exec(x: 3) #-> 1/3
f.exec(3) #-> 1/3


34
35
36
37
38
39
40
41
42
# File 'lib/mathmas/core/function.rb', line 34

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

#plot(args) ⇒ Object



47
48
49
# File 'lib/mathmas/plot/function.rb', line 47

def plot(args)
  Mathmas.plot_function(self, args)
end

#to_sObject



19
20
21
22
# File 'lib/mathmas/core/function.rb', line 19

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

#to_texObject



24
25
26
27
# File 'lib/mathmas/core/function.rb', line 24

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