mathpack 0.4.3 → 0.4.4

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
  SHA1:
3
- metadata.gz: 9b544fc6133006b26c674fd82657520f9569d95d
4
- data.tar.gz: b7a523bf46386d4046a978d3591abc267ce54516
3
+ metadata.gz: 218d151cc62bb32f4f7080413b396d4d11f585ee
4
+ data.tar.gz: 8929b3f9775a341f3f491f1eb26bad71e490be51
5
5
  SHA512:
6
- metadata.gz: 8e2d7cf42e3d4528c1e156d636ff393fe4c52d2da6a2197c14f6fe6d16ea9b615514f3aa3f544c6248884bd59d59d66306e28f8aa3ba26a7b448a79fa519d4f8
7
- data.tar.gz: dab1058342ca55b4ce594a9cc8398d154a4f1774d9c7e73dccb6d6aeb5f725f7f67f48698690e0b3679e854598fb5aef0eb40d9750adbb6e475927261410acf4
6
+ metadata.gz: c45dcf42f3b70354c4a536aa5a053b5c1390664cd857d463d1ba1175277a01647772465861c7d3ec0d805229e622eac0b0288be24899968e9c717cd38f80fc36
7
+ data.tar.gz: 40d736560eb81d34c25c1dc985cdda85d838b8bc24c810469bed7ae6b6f562e5c7ff54f28de72c95a9021c13d8a6aeff2444f18963f2acde0026f8c216d598b8
data/lib/mathpack/io.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  module Mathpack
2
2
  module IO
3
+ @@next_stop = /[+*-\/() ]/
4
+
5
+ @@operators = %w{+ - / ** *}
6
+ @@delimiters = %w{, .}
7
+ @@numbers = %w{0 1 2 3 4 5 6 7 8 9}
8
+ @@math_constants = { 'pi' => 'Math::PI' }
9
+ @@math_functions = { 'ln(' => 'Math.log(', 'e**(' => 'Math.exp(', 'arctg(' => 'Math.atan(', 'arcsin(' => 'Math.asin(', 'arccos(' => 'Math.acos(', 'sin(' => 'Math.sin(', 'cos(' => 'Math.cos(', 'tg(' => 'Math.tan(', 'lg(' => 'Math.log10(' }
3
10
  def self.print_table_function(params = {})
4
11
  fail 'Arrays length dismatch' if params[:x].length != params[:y].length
5
12
  File.open(params[:filename] + '.csv'|| 'table_function.csv', 'w+') do |file|
@@ -9,5 +16,84 @@ module Mathpack
9
16
  end
10
17
  end
11
18
  end
19
+
20
+ def self.read_table_function(filename)
21
+ x = []
22
+ y = []
23
+ data = File.read(filename + '.csv')
24
+ rows = data.split("\n")
25
+ rows.delete!(0) unless rows[0].split(';')[0].to_f
26
+ rows.each do |row|
27
+ parts = row.split(';')
28
+ x << parts[0].to_f
29
+ y << parts[1].to_f
30
+ end
31
+ { x: x, y: y}
32
+ end
33
+
34
+ def self.parse_function(string_function)
35
+ transformed_function = transform_string_function string_function
36
+ produce_block(transformed_function) if is_valid_function?(transformed_function)
37
+ end
38
+
39
+ def self.is_valid_function?(function_string)
40
+ str = function_string.dup
41
+ @@math_functions.values.each do |math_func|
42
+ str.gsub!(math_func, '(')
43
+ end
44
+ str.delete!(' ')
45
+ str.gsub!('x', '')
46
+ (@@operators + @@delimiters + @@numbers).each do |sym|
47
+ str.gsub!(sym, '')
48
+ end
49
+ brackets_stack = []
50
+ str.each_char do |bracket|
51
+ if brackets_stack.empty?
52
+ brackets_stack << bracket
53
+ next
54
+ end
55
+ if bracket.eql? '('
56
+ brackets_stack << bracket
57
+ else
58
+ if brackets_stack.last.eql? '('
59
+ brackets_stack.pop
60
+ else
61
+ brackets_stack << bracket
62
+ end
63
+ end
64
+ end
65
+ str.gsub!(/[\(\)]/, '')
66
+ brackets_stack.empty? && str.empty?
67
+ end
68
+
69
+ def self.transform_string_function(function_string)
70
+ function = function_string.dup
71
+ i_next = 0
72
+ while i_next != function.length do
73
+ i = function.index('^', i_next)
74
+ break if i.nil?
75
+ if function[i + 1].eql? '('
76
+ function[i..i + 1] = '**('
77
+ else
78
+ i_next = function.index(@@next_stop, i)
79
+ i_next = function.length unless i_next
80
+ function[i...i_next] = '**('+ function[i + 1...i_next] + ')'
81
+ end
82
+ end
83
+ @@math_functions.each do |key, value|
84
+ function.gsub!(key, value)
85
+ end
86
+ @@math_constants.each do |key, value|
87
+ function.gsub!(key, value)
88
+ end
89
+ function
90
+ end
91
+
92
+ def self.produce_block(valid_function)
93
+ valid_function.gsub!('x', 'var')
94
+ valid_function.gsub!('evarp', 'exp')
95
+ calculate = -> str { ->x { eval str.gsub('var', x.to_s) } }
96
+ calculate.(valid_function)
97
+ end
12
98
  end
13
99
  end
@@ -1,3 +1,3 @@
1
1
  module Mathpack
2
- VERSION = '0.4.3'
2
+ VERSION = '0.4.4'
3
3
  end
data/spec/io_spec.rb ADDED
@@ -0,0 +1,17 @@
1
+ describe 'IO' do
2
+ require 'mathpack/io'
3
+
4
+ describe '#parse_function' do
5
+ it 'returns nil if function is not valid' do
6
+ expect(Mathpack::IO.parse_function('puts "rm ."')).to be_nil
7
+ end
8
+
9
+ it 'returns nil if function have incorrect braces' do
10
+ expect(Mathpack::IO.parse_function('e^(sin(x)')).to be_nil
11
+ end
12
+
13
+ it 'returns block if function is correct' do
14
+ expect(Mathpack::IO.parse_function('e^(sin(x))').call(Math::PI / 2.0)).to eq(Math::E)
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mathpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxmilan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-14 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,6 +65,7 @@ files:
65
65
  - spec/equation_spec.rb
66
66
  - spec/functions_spec.rb
67
67
  - spec/integration_spec.rb
68
+ - spec/io_spec.rb
68
69
  - spec/sle_spec.rb
69
70
  - spec/statistics_spec.rb
70
71
  homepage: ''