module Mathmas

Constants

VERSION

Public Class Methods

add_function(func) click to toggle source
# 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) click to toggle source
# File lib/mathmas/monkey.rb, line 27
def find_function(name)
  @@functions[name]
end
plot(obj, *args) click to toggle source
# 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={}) click to toggle source

Mathmas#plot_function plot Mathmas::Function. @example

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)
# 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

Public Instance Methods

method_missing(name, *args) click to toggle source
Calls superclass method
# 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

Private Instance Methods

add_function(func) click to toggle source
# 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) click to toggle source
# File lib/mathmas/monkey.rb, line 27
def find_function(name)
  @@functions[name]
end
plot(obj, *args) click to toggle source
# 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={}) click to toggle source

Mathmas#plot_function plot Mathmas::Function. @example

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)
# 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