sabetsuka 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +17 -0
- data/Gemfile +5 -0
- data/LICENSE +8 -0
- data/README.md +30 -0
- data/Rakefile +8 -0
- data/bin/sabetsuka +5 -0
- data/lib/sabetsuka/exceptions.rb +21 -0
- data/lib/sabetsuka/polynomial.rb +62 -0
- data/lib/sabetsuka/version.rb +6 -0
- data/lib/sabetsuka.rb +7 -0
- data/sabetsuka.gemspec +15 -0
- data/test/test_correct.rb +41 -0
- data/test/test_incorrect.rb +60 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 11cbea208b2c026463cc403f9279511eb5d450cc56739d4c415b4de8b3f62c65
|
4
|
+
data.tar.gz: 3d31e797bd9f87769fc07de0db696595d203a0d38f6450af71854f1597d03c10
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd60c04e8e8f4a59383cd8968cd819f92320588acfd9f96cec9b9391d37a2ab18d712eac38a83a83a54ca27a7f42d1121b694f7838d645e40843fc6d4c50da2b
|
7
|
+
data.tar.gz: '049faa8306b43739d68269a7eeee7d53ff7b5666cd6344f97cc2d5d441add352f9649a7f1f5e7b870a76d6405eb8b08b897e6b49016b3d89f1a729622ccd2931'
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
### v1.3.0 (03.05.2023)
|
2
|
+
- update incorrect tests
|
3
|
+
- update readme
|
4
|
+
|
5
|
+
### v1.2.0 (02.05.2023)
|
6
|
+
- custom exceptions added
|
7
|
+
- version.rb added
|
8
|
+
- minor bug fixes
|
9
|
+
|
10
|
+
### v1.1.0 (31.05.2023)
|
11
|
+
- added test files
|
12
|
+
- added support for negative term
|
13
|
+
- added support for different variables
|
14
|
+
- fixed bugs with one term
|
15
|
+
|
16
|
+
### v1.0.0 (18.05.2023)
|
17
|
+
- initial release
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright © 2023 <copyright holders>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
5
|
+
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
7
|
+
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# sabetsuka
|
2
|
+
|
3
|
+
Sabetsuka (差別化) makes it easy and painless to differentiate polynomials.
|
4
|
+
|
5
|
+
The repository was inspired by the [Nokogiri](https://github.com/sparklemotion/nokogiri) gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
9
|
+
|
10
|
+
$ gem build sabetsuka.gemspec
|
11
|
+
$ gem install sabetsuka
|
12
|
+
or
|
13
|
+
|
14
|
+
$ bundle add sabetsuka
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
The term format: `3x^5`, `5y`, `7`
|
18
|
+
|
19
|
+
The polynomial format: `3x + 5y + 6`, `5x - 2`, `-3y^2 - 5`
|
20
|
+
```ruby
|
21
|
+
polynomial = Sabetsuka::Polynomial.new("3x^3 + 4x^2 + 5x + 4", "x")
|
22
|
+
derivative = polynomial.differentiate
|
23
|
+
|
24
|
+
puts "Polynomial: #{polynomial.to_s}"
|
25
|
+
puts "Derivative: #{derivative.to_s}"
|
26
|
+
|
27
|
+
# Output:
|
28
|
+
# Polynomial: 3x^3 + 4x^2 + 5x + 4
|
29
|
+
# Derivative: 9x^2 + 8x + 5
|
30
|
+
```
|
data/Rakefile
ADDED
data/bin/sabetsuka
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sabetsuka
|
4
|
+
class EmptyArgumentError < StandardError
|
5
|
+
def initialize(message = "Empty polynomial expression or variable")
|
6
|
+
super(message)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class WrongTypeArgumentError < StandardError
|
11
|
+
def initialize(message = "Expression and variable must be strings")
|
12
|
+
super(message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class InvalidSyntaxError < StandardError
|
17
|
+
def initialize(message = "Invalid expression syntax")
|
18
|
+
super(message)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative 'exceptions'
|
2
|
+
|
3
|
+
module Sabetsuka
|
4
|
+
class Polynomial
|
5
|
+
def initialize(expression, variable)
|
6
|
+
raise WrongTypeArgumentError unless expression.is_a?(String) && variable.is_a?(String)
|
7
|
+
raise EmptyArgumentError if expression.empty? || variable.empty?
|
8
|
+
raise InvalidSyntaxError unless expression.match?(/\A(\s*[+-]?\s*\d+[a-zA-Z]*\s*(\^\d+)?)+\s*\z/)
|
9
|
+
|
10
|
+
@expression = expression
|
11
|
+
@variable_to_diff = variable
|
12
|
+
end
|
13
|
+
|
14
|
+
def differentiate
|
15
|
+
@expression.sub! '- ', '+ -'
|
16
|
+
terms = @expression.split('+')
|
17
|
+
derivative_terms = []
|
18
|
+
|
19
|
+
terms.each do |term|
|
20
|
+
sign, coefficient, variable, exponent = parse_term(term)
|
21
|
+
|
22
|
+
variable = 'none' if variable != @variable_to_diff
|
23
|
+
|
24
|
+
next unless exponent > 0 && variable != 'none'
|
25
|
+
|
26
|
+
derivative_coefficient = sign * coefficient * exponent
|
27
|
+
derivative_exponent = exponent - 1
|
28
|
+
derivative_terms << format_term(derivative_coefficient, variable, derivative_exponent)
|
29
|
+
end
|
30
|
+
|
31
|
+
derivative_expression = derivative_terms.empty? ? "0" : derivative_terms.join(' + ')
|
32
|
+
derivative_expression.sub! '+ -', '- '
|
33
|
+
Polynomial.new(derivative_expression, @variable_to_diff)
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
@expression
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def parse_term(term)
|
43
|
+
term.strip!
|
44
|
+
matches = term.match(/(-)?([\d\-]*)([a-zA-Z]*)(\^(\d+))?/)
|
45
|
+
|
46
|
+
sign = matches[1].nil? ? 1 : -1
|
47
|
+
coefficient = matches[2].nil? ? 1 : matches[2].to_i
|
48
|
+
variable = matches[3].empty? ? 'none' : matches[3].to_s
|
49
|
+
exponent = matches[5].nil? ? 1 : matches[5].to_i
|
50
|
+
|
51
|
+
[sign, coefficient, variable, exponent]
|
52
|
+
end
|
53
|
+
|
54
|
+
def format_term(coefficient, variable, exponent)
|
55
|
+
term = coefficient.to_s
|
56
|
+
term += variable if exponent > 0
|
57
|
+
term += "^#{exponent}" if exponent > 1
|
58
|
+
|
59
|
+
term
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/sabetsuka.rb
ADDED
data/sabetsuka.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#require File.expand_path('lib/sabetsuka/version', __dir__)
|
2
|
+
require_relative 'lib/sabetsuka/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'sabetsuka'
|
6
|
+
spec.version = Sabetsuka::VERSION
|
7
|
+
spec.authors = ['johtai', 'Tempire', 'ompus']
|
8
|
+
spec.summary = 'differentiate polynomials'
|
9
|
+
spec.description = 'Sabetsuka (差別化) makes it easy and painless to differentiate polynomials'
|
10
|
+
spec.homepage = 'https://github.com/johtai/sabetsuka'
|
11
|
+
spec.license = 'MIT'
|
12
|
+
spec.files = Dir['lib/sabetsuka.rb', 'lib/sabetsuka/*.rb', 'README.md',
|
13
|
+
'LICENSE', 'Gemfile', 'Rakefile', 'bin/*', 'sabetsuka.gemspec',
|
14
|
+
'CHANGELOG.md', 'test/*.rb']
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/sabetsuka'
|
3
|
+
|
4
|
+
class MyMethodTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_zero
|
7
|
+
assert_equal("0", Sabetsuka::Polynomial.new("0", "x").differentiate.to_s, )
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_constant
|
11
|
+
assert_equal("0", Sabetsuka::Polynomial.new("3", "x").differentiate.to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_neggativenumber
|
15
|
+
assert_equal("-15", Sabetsuka::Polynomial.new("-15x", "x").differentiate.to_s)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def test_p1
|
20
|
+
assert_equal("9x^2 + 8x + 5", Sabetsuka::Polynomial.new("3x^3 + 4x^2 + 5x + 4", "x").differentiate.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_p2
|
24
|
+
assert_equal("-6x^2 + 10x - 3", Sabetsuka::Polynomial.new("-2x^3 + 5x^2 - 3x + 1", "x").differentiate.to_s)
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def test_p3
|
29
|
+
assert_equal("20x^4 + 8x^3 - 14x + 3", Sabetsuka::Polynomial.new("4x^5 + 2x^4 - 7x^2 + 3x - 9" , "x").differentiate.to_s)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_p4
|
33
|
+
assert_equal("-3", Sabetsuka::Polynomial.new("5x^3 + 4x^2 - 3y" , "y").differentiate.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_p5
|
37
|
+
assert_equal("-6y^2 - 13", Sabetsuka::Polynomial.new("-2y^3 + 4x^2 - 13y + 7" , "y").differentiate.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/sabetsuka'
|
3
|
+
|
4
|
+
class MyMethodTest < Test::Unit::TestCase
|
5
|
+
def test_empty_exp
|
6
|
+
assert_raises Sabetsuka::EmptyArgumentError do
|
7
|
+
Sabetsuka::Polynomial.new("", "x").differentiate.to_s
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_empty_var
|
12
|
+
assert_raises Sabetsuka::EmptyArgumentError do
|
13
|
+
Sabetsuka::Polynomial.new("4x^2 - 5y", "").differentiate.to_s
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_string_exp
|
18
|
+
assert_raises Sabetsuka::WrongTypeArgumentError do
|
19
|
+
Sabetsuka::Polynomial.new(5, "y").differentiate.to_s
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_two_signs
|
24
|
+
assert_raises Sabetsuka::InvalidSyntaxError do
|
25
|
+
Sabetsuka::Polynomial.new("2x^3 ++ 9x", "x").differentiate.to_s
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_sign_without_term
|
30
|
+
assert_raises Sabetsuka::InvalidSyntaxError do
|
31
|
+
Sabetsuka::Polynomial.new("2x^3 + + 5", "y").differentiate.to_s
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_sign_without_second_term
|
36
|
+
assert_raises Sabetsuka::InvalidSyntaxError do
|
37
|
+
Sabetsuka::Polynomial.new("2x^3 + 9x^2 +" , "x").differentiate.to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_letter_deg
|
42
|
+
assert_raises Sabetsuka::InvalidSyntaxError do
|
43
|
+
Sabetsuka::Polynomial.new("2x^a + 5", "y").differentiate.to_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_deg_without_var
|
48
|
+
assert_raises Sabetsuka::InvalidSyntaxError do
|
49
|
+
Sabetsuka::Polynomial.new("^4 + 56", "x").differentiate.to_s
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_deg_without_num
|
54
|
+
assert_raises Sabetsuka::InvalidSyntaxError do
|
55
|
+
Sabetsuka::Polynomial.new("x^ + 56", "x").differentiate.to_s
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sabetsuka
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- johtai
|
8
|
+
- Tempire
|
9
|
+
- ompus
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2023-06-03 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Sabetsuka (差別化) makes it easy and painless to differentiate polynomials
|
16
|
+
email:
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- CHANGELOG.md
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/sabetsuka
|
27
|
+
- lib/sabetsuka.rb
|
28
|
+
- lib/sabetsuka/exceptions.rb
|
29
|
+
- lib/sabetsuka/polynomial.rb
|
30
|
+
- lib/sabetsuka/version.rb
|
31
|
+
- sabetsuka.gemspec
|
32
|
+
- test/test_correct.rb
|
33
|
+
- test/test_incorrect.rb
|
34
|
+
homepage: https://github.com/johtai/sabetsuka
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.4.10
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: differentiate polynomials
|
57
|
+
test_files: []
|