class Maxima::Function

Constants

IGNORE_VARIABLES
VARIABLE_REGEX

This strategy fails for functions (cos etc.). However, that does not impact it's actual usage.

VARIABLE_REGEX_LOOK_PATTERN
VARIABLE_REPLACEMENT_REGEX

Attributes

string[RW]
variables[RW]

Public Class Methods

new(string, variables = nil, **options) click to toggle source
Calls superclass method
# File lib/maxima/function.rb, line 5
def initialize(string, variables = nil, **options)
  string = string.to_s
  options[:maxima_output] ||= string
  super(**options)
  @variables = variables || Function.variables_in_string(string)
end
parse(string) click to toggle source

Assume what we get is what we need

# File lib/maxima/function.rb, line 47
def self.parse(string)
  variables = variables_in_string(string)

  if variables.any?
    Function.new(string, variables)
  else
    Unit.parse_float(string)
  end
rescue
  nil
end
variables_in_string(string) click to toggle source
# File lib/maxima/function.rb, line 17
def self.variables_in_string(string)
  (string.scan(VARIABLE_REGEX) - IGNORE_VARIABLES).to_set
end

Public Instance Methods

==(other) click to toggle source
# File lib/maxima/function.rb, line 89
def ==(other)
  to_s == other.to_s
end
at(v) click to toggle source
# File lib/maxima/function.rb, line 71
def at(v)
  s = self.to_s.dup

  case v
  when Hash
    v.each do |k,t|
      k = k.to_s
      if @variables.include?(k)
        s.gsub!(VARIABLE_REPLACEMENT_REGEX.call(k), "(#{t})")
      end
    end
  else
    throw :must_specify_variables_in_hash if @variables.length != 1
    s.gsub!(VARIABLE_REPLACEMENT_REGEX.call(@variables.first), "(#{v})")
  end
  Function.parse(s).simplified
end
between(min, max, steps) click to toggle source
# File lib/maxima/function.rb, line 38
def between(min, max, steps)
  step = (max - min).fdiv(steps)

  Command.output(r: Histogram) do |c|
    c.let :r, "makelist([x,float(#{self})],x, #{min}, #{max}, #{step})"
  end[:r]
end
definite_integral(t0, t1, v: "x") click to toggle source
# File lib/maxima/function.rb, line 29
def definite_integral(t0, t1, v: "x")
  i_v = self.integral(v: v)
  i_v.at(v => t1) - i_v.at(v => t0)
end
derivative(variable = nil, v: "x") click to toggle source
# File lib/maxima/function.rb, line 34
def derivative(variable = nil, v: "x")
  Maxima.diff(to_maxima_input, v: (variable || v))[:diff]
end
gnu_plot_text() click to toggle source
Calls superclass method
# File lib/maxima/function.rb, line 59
def gnu_plot_text
  super.gsub("^", "**")
end
gnu_plot_w() click to toggle source
# File lib/maxima/function.rb, line 63
def gnu_plot_w
  "lines"
end
integral(t0 = nil, t1 = nil, v: "x") click to toggle source
# File lib/maxima/function.rb, line 21
def integral(t0 = nil, t1 = nil, v: "x")
  if t0 && t1
    Maxima.integrate(to_maxima_input, t0, t1, v: v)[:integral]
  else
    Maxima.integrate(to_maxima_input, v: v)[:integral]
  end
end
to_maxima_input() click to toggle source
# File lib/maxima/function.rb, line 67
def to_maxima_input
  self.to_s
end