Class: Maxima::Function

Inherits:
Unit
  • Object
show all
Defined in:
lib/maxima/function.rb

Constant Summary collapse

VARIABLE_REGEX =

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

/[%|a-z|A-Z]+[0-9|a-z|A-Z]*/.freeze
VARIABLE_REGEX_LOOK_PATTERN =
/[%|0-9|a-z|A-Z]/
VARIABLE_REPLACEMENT_REGEX =
->(variable) { /(?<!#{VARIABLE_REGEX_LOOK_PATTERN})#{variable}(?!#{VARIABLE_REGEX_LOOK_PATTERN})/ }
IGNORE_VARIABLES =
%w(%e %i).freeze

Instance Attribute Summary collapse

Attributes inherited from Unit

#maxima_output, #plot_title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#===, #gnu_plot_options, #imaginary?, #inspect, #negative?, parse_float, #positive?, #real?, #simplified, #through_maxima, #to_f, #to_gnu_plot, #to_pdf, #to_s, #with_plot_title, #zero?

Constructor Details

#initialize(string, variables = nil, **options) ⇒ Function

Returns a new instance of Function.



5
6
7
8
9
10
# 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

Instance Attribute Details

#stringObject

Returns the value of attribute string.



3
4
5
# File 'lib/maxima/function.rb', line 3

def string
  @string
end

#variablesObject

Returns the value of attribute variables.



3
4
5
# File 'lib/maxima/function.rb', line 3

def variables
  @variables
end

Class Method Details

.parse(string) ⇒ Object

Assume what we get is what we need



47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



17
18
19
# File 'lib/maxima/function.rb', line 17

def self.variables_in_string(string)
  (string.scan(VARIABLE_REGEX) - IGNORE_VARIABLES).to_set
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
# File 'lib/maxima/function.rb', line 89

def ==(other)
  to_s == other.to_s
end

#at(v) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



38
39
40
41
42
43
44
# 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") ⇒ Object



29
30
31
32
# 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") ⇒ Object



34
35
36
# 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_textObject



59
60
61
# File 'lib/maxima/function.rb', line 59

def gnu_plot_text
  super.gsub("^", "**")
end

#gnu_plot_wObject



63
64
65
# File 'lib/maxima/function.rb', line 63

def gnu_plot_w
  "lines"
end

#integral(t0 = nil, t1 = nil, v: "x") ⇒ Object



21
22
23
24
25
26
27
# 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_inputObject



67
68
69
# File 'lib/maxima/function.rb', line 67

def to_maxima_input
  self.to_s
end