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
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
.plot_function(func, args = {}) ⇒ Object
Mathmas#plot_function plot Mathmas::Function.
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
|