Skip to content

Commit 7ccfd5d

Browse files
authored
Merge pull request #66 from abinashmeher999/rubocop
Introducing Rubocop
2 parents 0caa11f + 1d7c3ac commit 7ccfd5d

25 files changed

+298
-239
lines changed

.rubocop.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
inherit_from: .rubocop_todo.yml
2+
3+
Style/PercentLiteralDelimiters:
4+
PreferredDelimiters:
5+
'%w': '[]'
6+
'%W': '[]'
7+
8+
Style/BlockDelimiters:
9+
EnforcedStyle: braces_for_chaining
10+
# The `braces_for_chaining` style enforces braces around single line blocks
11+
# and do..end around multi-line blocks, except for multi-line blocks whose
12+
# return value is being chained with another method (in which case braces
13+
# are enforced).
14+
Exclude:
15+
- 'spec/**/*'
16+
17+
Metrics/MethodLength:
18+
Max: 16
19+

.rubocop_todo.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2016-10-06 20:09:56 +0530 using RuboCop version 0.42.0.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 1
10+
Lint/Eval:
11+
Exclude:
12+
- 'lib/symengine.rb'
13+
14+
# Offense count: 28
15+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
16+
# URISchemes: http, https
17+
Metrics/LineLength:
18+
Max: 178
19+
20+
# Offense count: 8
21+
Style/Documentation:
22+
Exclude:
23+
- 'spec/**/*'
24+
- 'test/**/*'
25+
- 'lib/symengine.rb'
26+
- 'lib/symengine/basic.rb'
27+
- 'lib/symengine/complex.rb'
28+
- 'lib/symengine/complex_double.rb'
29+
- 'lib/symengine/integer.rb'
30+
- 'lib/symengine/iruby.rb'
31+
- 'lib/symengine/undef_function.rb'
32+

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ matrix:
2626
include:
2727
- os: linux
2828
rvm: 2.0
29-
env: BUILD_TYPE="Debug" WITH_BFD="yes" WITH_MPC="yes"
29+
env: BUILD_TYPE="Debug" WITH_BFD="yes" WITH_MPC="yes" RUBOCOP="yes"
3030
- os: linux
3131
rvm: 2.3.0
3232
env: BUILD_TYPE="Debug" WITH_BFD="yes" WITH_MPC="yes"
@@ -66,6 +66,8 @@ script:
6666
- export CFLAGS="-Werror"
6767
# Install ruby gem
6868
- gem install symengine-0.1.0.gem --install-dir $HOME --verbose -- -DSymEngine_DIR=$our_install_dir
69+
# Check for code style using rubocop
70+
- if [[ "${RUBOCOP}" == "yes" ]]; then rubocop; fi
6971

7072
# Test ruby gem
7173
- gem install gem-path --no-ri --no-rdoc

ext/symengine/extconf.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
ruby_executable = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['RUBY_INSTALL_NAME'] + RbConfig::CONFIG['EXEEXT'])
44

5-
generator = ""
6-
7-
if (RbConfig::CONFIG['host_os'] =~ /mingw/)
8-
generator = '-G "MSYS Makefiles"'
9-
elsif (RbConfig::CONFIG['host_os'] =~ /cygwin/)
10-
generator = '-G "Unix Makefiles"'
11-
elsif (RbConfig::CONFIG['host_os'] =~ /mswin/)
12-
generator = '-G "NMake Makefiles"'
5+
generator = ''
6+
7+
if RbConfig::CONFIG['host_os'] =~ /mingw/
8+
generator = '-G "MSYS Makefiles"'
9+
elsif RbConfig::CONFIG['host_os'] =~ /cygwin/
10+
generator = '-G "Unix Makefiles"'
11+
elsif RbConfig::CONFIG['host_os'] =~ /mswin/
12+
generator = '-G "NMake Makefiles"'
1313
end
1414

15-
exec 'cmake %s -DCMAKE_INSTALL_PREFIX=../../ -DRUBY_EXECUTABLE=%s %s ../../ ' % [generator, ruby_executable, ARGV.join(" ")]
15+
exec format('cmake %s -DCMAKE_INSTALL_PREFIX=../../ -DRUBY_EXECUTABLE=%s %s ../../ ', generator, ruby_executable, ARGV.join(' '))

lib/symengine.rb

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class << self
1313
# x, y = SymEngine.symbols(['x', 'y'])
1414
# x, y = SymEngine.symbols('x', 'y')
1515
# x, y = SymEngine.symbols('x y')
16-
#
17-
def symbols ary_or_string, *params
16+
#
17+
def symbols(ary_or_string, *params)
1818
# Want to make sure we can accept an array or a bunch of splatted arguments
19-
if params.size > 0
19+
if !params.empty?
2020
ary_or_string = (ary_or_string.is_a?(String) ? [ary_or_string] : ary_or_string).concat(params)
2121
elsif ary_or_string.is_a?(String)
2222
# or accept a string
@@ -25,45 +25,56 @@ def symbols ary_or_string, *params
2525

2626
# Make an Array of SymEngine::Symbols from the parameters we received,
2727
# now that they're normalized.
28-
ary_or_string.map do |symbol_or_string|
29-
SymEngine::Symbol.new(symbol_or_string)
30-
end
31-
end
32-
def Function(n)
28+
ary_or_string.map(&SymEngine::Symbol.method(:new))
29+
end
30+
31+
def Function(n) # rubocop:disable Style/MethodName
3332
SymEngine::UndefFunction.new(n)
3433
end
35-
def evalf(operand, prec=53, real=false)
34+
35+
def evalf(operand, prec = 53, real = false)
3636
_evalf(operand, prec, real)
3737
end
38+
3839
def lambdify(exp, *syms)
3940
syms.flatten!
4041
if exp.free_symbols.count > syms.length
4142
raise ArgumentError, "Formula contains #{exp.free_symbols.count} free "\
42-
"symbols. You should provide at least this number "\
43+
'symbols. You should provide at least this number '\
4344
"of arguments (only #{syms.length} given)."
4445
end
45-
eval(SymEngine::Utils::lambdify_code(exp, syms))
46+
# TODO: Implement one of the two options below.
47+
# 1. check that all the symbol names and function names are valid.
48+
# 2. wrap symengine's LambdaRealDouble into C and then to ruby
49+
eval(SymEngine::Utils.lambdify_code(exp, syms))
4650
end
4751
end
4852
module Utils
4953
class << self
50-
REPLACEMENTS = { sin: 'Math.sin', cos: 'Math.cos', tan: 'Math.tan',
51-
asin: 'Math.asin', acos: 'Math.acos', atan: 'Math.atan',
52-
sinh: 'Math.sinh', cosh: 'Math.cosh', tanh: 'Math.tanh',
53-
asinh: 'Math.asinh', acosh: 'Math.acosh', atanh: 'Math.atanh',
54-
pi: 'Math::PI', E: 'Math::E', I: '::Complex::I',
55-
dirichlet_eta: 'SymEngine::Utils::evalf_dirichlet_eta',
56-
zeta: 'SymEngine::Utils::evalf_zeta', gamma: 'Math.gamma' }.map { |from, to| [/(\b#{from}\b)/, to] }.to_h.freeze
54+
REPLACEMENTS = {
55+
sin: 'Math.sin', cos: 'Math.cos', tan: 'Math.tan',
56+
asin: 'Math.asin', acos: 'Math.acos', atan: 'Math.atan',
57+
sinh: 'Math.sinh', cosh: 'Math.cosh', tanh: 'Math.tanh',
58+
asinh: 'Math.asinh', acosh: 'Math.acosh', atanh: 'Math.atanh',
59+
pi: 'Math::PI', E: 'Math::E', I: '::Complex::I',
60+
dirichlet_eta: 'SymEngine::Utils::evalf_dirichlet_eta',
61+
zeta: 'SymEngine::Utils::evalf_zeta', gamma: 'Math.gamma'
62+
}.map { |from, to|
63+
[/(\b#{from}\b)/, to]
64+
}.to_h.freeze
65+
5766
def evalf_dirichlet_eta(exp)
58-
SymEngine::evalf(SymEngine::dirichlet_eta(exp))
67+
SymEngine.evalf(SymEngine.dirichlet_eta(exp))
5968
end
69+
6070
def evalf_zeta(exp)
61-
SymEngine::evalf(SymEngine::zeta(exp))
71+
SymEngine.evalf(SymEngine.zeta(exp))
6272
end
73+
6374
def lambdify_code(exp, syms)
6475
body = exp.to_s.gsub(/[\d\.]+/, 'Rational(\0,1)')
65-
sym_map = syms.join(",")
66-
rubified_body = REPLACEMENTS.inject(body) { |res, (from, to)| res.gsub(from, to)}
76+
sym_map = syms.join(',')
77+
rubified_body = REPLACEMENTS.inject(body) { |res, (from, to)| res.gsub(from, to) }
6778
"proc { | #{sym_map} | #{rubified_body} }"
6879
end
6980
end

lib/symengine/basic.rb

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,39 @@
33
module SymEngine
44
class Basic
55
def inspect
6-
"#<#{self.class}(#{to_s})>"
6+
"#<#{self.class}(#{self})>"
77
end
88

99
def free_symbols
1010
pr_free_symbols.to_set
1111
end
12-
12+
1313
def abs
14-
SymEngine::abs(self)
14+
SymEngine.abs(self)
1515
end
16+
1617
def to_proc(*args)
17-
if args.empty?
18-
if free_symbols.count > 1
19-
raise ArgumentError, "You should provide symbols order for #to_proc"\
20-
". Only formulae with 1 free symbol can deduce"\
21-
" its name automatically (#{free_symbols.count}"\
22-
" found in #{self})."
23-
end
24-
SymEngine::lambdify(self, free_symbols.map {|s| s})
25-
else
26-
if free_symbols.count > args.length
27-
raise ArgumentError, "Formula contains #{free_symbols.count} free "\
28-
"symbols. You should provide at least this number "\
29-
"of arguments (only #{args.length} given)."
30-
end
31-
SymEngine::lambdify(self, args)
18+
SymEngine.lambdify(self, args.empty? ? default_args : check_args(*args))
19+
end
20+
21+
def default_args
22+
if free_symbols.count > 1
23+
raise ArgumentError, 'You should provide symbols order for #to_proc'\
24+
'. Only formulae with 1 free symbol can deduce'\
25+
" its name automatically (#{free_symbols.count}"\
26+
" found in #{self})."
3227
end
28+
free_symbols.to_a
29+
end
30+
31+
def check_args(*args)
32+
if free_symbols.count > args.length
33+
raise ArgumentError, "Formula contains #{free_symbols.count} free "\
34+
'symbols. You should provide at least this number '\
35+
"of arguments (only #{args.length} given)."
36+
end
37+
38+
args
3339
end
3440
end
3541
end

lib/symengine/complex.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SymEngine
22
class Complex
33
def to_c
4-
to_s.tr('I', 'i').delete(' *').to_c
4+
to_s.tr('I', 'i').delete(' *').to_c
55
end
66
end
7-
end
7+
end

lib/symengine/complex_double.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SymEngine
22
class ComplexDouble
33
def to_c
4-
to_s.tr('I', 'i').delete(" *").to_c
4+
to_s.tr('I', 'i').delete(' *').to_c
55
end
66
end
7-
end
7+
end

lib/symengine/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SymEngine
22
class Integer
33
def to_int
4-
self.to_s.to_i
4+
to_s.to_i
55
end
66
end
77
end

lib/symengine/iruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module SymEngine
33
class Basic
44
def to_iruby
5-
['text/html', self.to_s] unless self.instance_of? Basic
5+
['text/html', to_s] unless instance_of? Basic
66
end
77
end
88
end

0 commit comments

Comments
 (0)