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 +4 -4
- data/lib/maxima/function.rb +5 -3
- data/lib/maxima/polynomial.rb +8 -13
- data/lib/maxima/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a94090141028eea6bf1d0ffdcf9262333a7fd052de88503e6893def622e51344
|
4
|
+
data.tar.gz: 444426905a43c8ab5379c0573383d9621ef5d2aef34121ab6970ce70a2120934
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b28b6aceac03844a0d3cfbbff19ab982086a44c3e0d167dc4dd659783d75f41ca65cdde989bb671cb09d5543344a84f51152416b76c0326eb5b9d374cd8a924
|
7
|
+
data.tar.gz: 90da4e7f1b39ec540c385ce8acf631e67b72ae1bec4b3ce3e6cf65281557b1f1a87eddd1c8dac6c191de87a28b2c522ceb37364df0629900123a1cf8312fc6f7
|
data/lib/maxima/function.rb
CHANGED
@@ -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]
|
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
|
data/lib/maxima/polynomial.rb
CHANGED
@@ -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
|
-
|
13
|
-
equation_string.gsub!("(#{variable})", value.to_s)
|
14
|
-
end
|
10
|
+
function = Maxima::Function.new(equation_string)
|
15
11
|
|
16
12
|
{
|
17
|
-
|
18
|
-
|
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 <<
|
27
|
+
polynomials << constant_variable
|
33
28
|
when 1
|
34
|
-
polynomials << "
|
29
|
+
polynomials << "#{constant_variable} * #{f_of}"
|
35
30
|
else
|
36
|
-
polynomials << "
|
31
|
+
polynomials << "#{constant_variable} * #{f_of} ^ #{degree}"
|
37
32
|
end
|
38
33
|
end
|
39
34
|
|
data/lib/maxima/version.rb
CHANGED