Module: Mathmas

Defined in:
lib/mathmas/monkey.rb,
lib/mathmas/version.rb,
lib/mathmas/plot/plot.rb,
lib/mathmas/core/basic.rb,
lib/mathmas/core/number.rb,
lib/mathmas/core/symbol.rb,
lib/mathmas/core/function.rb,
lib/mathmas/plot/function.rb,
lib/mathmas/core/expression.rb

Defined Under Namespace

Modules: Basic Classes: Expression, Function, Multiply, Number, Plus, Power, Variable

Constant Summary collapse

VERSION =
"0.0.1"
@@functions =
{}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mathmas/monkey.rb', line 2

def method_missing(name, *args)
  if /[a-zA-Z]/ =~ name && name.to_s.length == 1
    if args.length == 0
      if Mathmas.find_function(name).nil?
        return Mathmas::Variable.new(name)
      else
        return Mathmas.find_function(name)
      end
    else
      if args.all? {|arg| arg.is_a?(Numeric)}
        func = Mathmas.find_function(name)
        return func.exec(*args)
      else
        return Mathmas::Function.new(name, args)
      end
    end
  end
  super
end

Class Method Details

.add_function(func) ⇒ Object


22
23
24
25
# File 'lib/mathmas/monkey.rb', line 22

def add_function(func)
  raise "The first argument should be an instance of Mathmas#Function" unless func.is_a?(Function)
  @@functions[func.name] = func
end

.find_function(name) ⇒ Object


27
28
29
# File 'lib/mathmas/monkey.rb', line 27

def find_function(name)
  @@functions[name]
end

.plot(obj, *args) ⇒ Object


5
6
7
8
9
# File 'lib/mathmas/plot/plot.rb', line 5

def plot(obj, *args)
  if obj.is_a?(Function)
    Mathmas.plot_function(obj, args[0])
  end
end

.plot_function(func, args = {}) ⇒ Object

Mathmas#plot_function plot Mathmas::Function.

Examples:

f(x) = 1/x
f.plot(func, x: 1..2)

g(x, y, a, b) = a*x**2 + b*y**2
g.plot(a: 3, b: 3, x: -1..1, y: -1..1)

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mathmas/plot/function.rb', line 10

def plot_function(func, args={})
  args={div_num: 100}.merge(args)

  div_num = args[:div_num]
  args.delete :div_num

  ranges = args.select{|key, val| val.is_a?(Range)}
  numerics = args.select{|key, val| val.is_a?(Numeric)}

  raise "the number of arguments is wrong" unless ranges.length + numerics.length == func.vals.length

  case ranges.length
  when 2
    plot = Nyaplot::Plot3D.new
    return plot
  when 1
    plot = Nyaplot::Plot.new
    x_label = ranges.keys[0]
    range = ranges[x_label]
    step = (range.last.to_f - range.begin.to_f)/(div_num-1)

    x_arr = []; div_num.times {|i| x_arr.push(range.begin + step*i)}
    y_arr = x_arr.map{|x| func.exec({x_label => x})}

    plot.add(:line, x_arr, y_arr)
    plot.x_label(x_label)
    plot.y_label(func.to_s)

    return plot
  else
    raise "Nyaplot cannot plot function whose dimention > 3"
  end
end