rb_maxima 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7f8a7781dde39239a581fe9e88b9d127b93bdd7225bb027b361950c84f7ad6a
4
- data.tar.gz: 5393dfa5952b36216eb717a7b47c00ac8cc144f96815c60675f29ef68fa224be
3
+ metadata.gz: a94090141028eea6bf1d0ffdcf9262333a7fd052de88503e6893def622e51344
4
+ data.tar.gz: 444426905a43c8ab5379c0573383d9621ef5d2aef34121ab6970ce70a2120934
5
5
  SHA512:
6
- metadata.gz: 9d195381e9231e60d11f5dc9b2f814e69a4478e79e2fd45185e459b3866c6e626c22c1b4c0741e63984f8ee68939589acd1ec1077dc91ae93032a9a46392d83d
7
- data.tar.gz: 287a8b6257429192fb76706750eecbe1a1eade21a0d55da9dce3f8f586bd31a9d8da6ba45e845382d93f172ca20aaf54904db26a38b0afdbe0b518059fe86932
6
+ metadata.gz: 0b28b6aceac03844a0d3cfbbff19ab982086a44c3e0d167dc4dd659783d75f41ca65cdde989bb671cb09d5543344a84f51152416b76c0326eb5b9d374cd8a924
7
+ data.tar.gz: 90da4e7f1b39ec540c385ce8acf631e67b72ae1bec4b3ce3e6cf65281557b1f1a87eddd1c8dac6c191de87a28b2c522ceb37364df0629900123a1cf8312fc6f7
@@ -10,7 +10,9 @@ module Maxima
10
10
  end
11
11
 
12
12
  # This strategy fails for functions (cos etc.). However, that does not impact it's actual usage.
13
- VARIABLE_REGEX = /[%|a-z|A-Z]+/.freeze
13
+ VARIABLE_REGEX = /[%|a-z|A-Z]+[0-9|a-z|A-Z]*/.freeze
14
+ VARIABLE_REGEX_LOOK_PATTERN = /[%|0-9|a-z|A-Z]/
15
+ VARIABLE_REPLACEMENT_REGEX = ->(variable) { /(?<!#{VARIABLE_REGEX_LOOK_PATTERN})#{variable}(?!#{VARIABLE_REGEX_LOOK_PATTERN})/ }
14
16
  IGNORE_VARIABLES = %w(%e %i).freeze
15
17
  def self.variables_in_string(string)
16
18
  (string.scan(VARIABLE_REGEX) - IGNORE_VARIABLES).to_set
@@ -74,12 +76,12 @@ module Maxima
74
76
  v.each do |k,t|
75
77
  k = k.to_s
76
78
  if @variables.include?(k)
77
- s.gsub!(k, "(#{t})")
79
+ s.gsub!(VARIABLE_REPLACEMENT_REGEX.call(k), "(#{t})")
78
80
  end
79
81
  end
80
82
  else
81
83
  throw :must_specify_variables_in_hash if @variables.length != 1
82
- s.gsub!(@variables.first, "(#{v})")
84
+ s.gsub!(VARIABLE_REPLACEMENT_REGEX.call(@variables.first), "(#{v})")
83
85
  end
84
86
  Function.parse(s).simplified
85
87
  end
@@ -3,37 +3,32 @@ module Maxima
3
3
 
4
4
  def self.fit(histogram, degrees)
5
5
  throw :degrees_must_be_zero_or_positive if degrees < 0
6
- degrees += 1
7
6
 
8
7
  equation_string, variables = polynomial_equation(degrees)
9
8
  results = Maxima.lsquares_estimation(histogram.to_a, [:x, :y], "y = #{equation_string}", variables)
10
- mse = results.delete(:mse)
11
9
 
12
- results.each do |variable, value|
13
- equation_string.gsub!("(#{variable})", value.to_s)
14
- end
10
+ function = Maxima::Function.new(equation_string)
15
11
 
16
12
  {
17
- function: Maxima::Function.new(equation_string).simplified,
18
- mse: mse
13
+ mse: results.delete(:mse),
14
+ function: function.at(results),
19
15
  }
20
16
  end
21
17
 
22
18
  def self.polynomial_equation(degrees, f_of: "x")
23
- polynomials = []
24
- constant_variables = []
19
+ polynomials, constant_variables = [], []
25
20
 
26
- degrees.times.each do |degree|
21
+ (degrees + 1).times.each do |degree|
27
22
  constant_variable = "c#{degree}"
28
23
  constant_variables << constant_variable
29
24
 
30
25
  case degree
31
26
  when 0
32
- polynomials << "(#{constant_variable})"
27
+ polynomials << constant_variable
33
28
  when 1
34
- polynomials << "(#{constant_variable}) * #{f_of}"
29
+ polynomials << "#{constant_variable} * #{f_of}"
35
30
  else
36
- polynomials << "(#{constant_variable}) * #{f_of} ^ #{degree}"
31
+ polynomials << "#{constant_variable} * #{f_of} ^ #{degree}"
37
32
  end
38
33
  end
39
34
 
@@ -1,3 +1,3 @@
1
1
  module Maxima
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb_maxima
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Ackerman