math_symbols 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: edc8e9a4edbcb1fc0d04a97a63ed2d5f533d09b62b3c7c36f76359d06bea4908
4
+ data.tar.gz: 75bb6cffe3695d599df6700c9666b82d41e37dab6fefe7176e29753e2c7e305b
5
+ SHA512:
6
+ metadata.gz: 94026b5b073870d8298f932b69e60fc6f21e10e461aceeabc0586aaedbe7352dad6dc343ccafd9c85d1ab10e74fe0d25f610a61dee9b0232b4ce1d6b87b43795
7
+ data.tar.gz: 31f4e73151d8a392919aaa0a4022c6571243f2089afedab3c87741894025869fbc5430ffd6d84969f09e9cf8abb8c60a4b26c8abbafd30449f96ef62920571a9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,49 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/FrozenStringLiteralComment:
5
+ Enabled: false
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: single_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ Layout/SpaceAroundOperators:
19
+ Enabled: false
20
+
21
+ Naming/BinaryOperatorParameterName:
22
+ Enabled: false
23
+
24
+ Naming/MethodName:
25
+ Enabled: false
26
+
27
+ Naming/AsciiIdentifiers:
28
+ Enabled: false
29
+
30
+ Style/Alias:
31
+ Enabled: false
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Layout/SpaceInsideParens:
37
+ Enabled: false
38
+
39
+ Style/NestedParenthesizedCalls:
40
+ Enabled: false
41
+
42
+ Style/RedundantParentheses:
43
+ Enabled: false
44
+
45
+ Style/EmptyLiteral:
46
+ Enabled: false
47
+
48
+ Lint/AmbiguousOperator:
49
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-01-26
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Dalton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # MathSymbols
2
+
3
+ Support for mathematical syntactic sugars!
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ gem install math_symbols
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ This is how we should write function that calculates a fourier for a step
14
+ function without this gem:
15
+
16
+ ```ruby
17
+ def traditional_way(x, n_max = 10)
18
+ (4/Math::PI) * (1..n_max).step(2).inject(0) do |acc, n|
19
+ acc += Math.sin(n*Math::PI*x)/n
20
+ end
21
+ end
22
+
23
+ traditional_way(0.5) # => 1.0630539690963425
24
+ ```
25
+
26
+ This is how we can do with it:
27
+
28
+ ```ruby
29
+ require 'math_symbols'
30
+ using MathSymbols
31
+
32
+ sweet_way = ƒ do |x, n_max = 10|
33
+ (4/π) * (1..n_max).step(2).∑{ |n| sin(n*π*x)/n }
34
+ end
35
+
36
+ sweet_way[0.5] # => 1.0630539690963425
37
+ ```
38
+
39
+ or
40
+
41
+ ```ruby
42
+ require 'math_symbols'
43
+ using MathSymbols
44
+
45
+ def sweet_way(x, n_max = 10)
46
+ λ do
47
+ (4/π) * (1..n_max).step(2).∑{ |n| sin(n*π*x)/n }
48
+ end
49
+ end
50
+
51
+ sweet_way(0.5) # => 1.0630539690963425
52
+ ```
53
+
54
+ Which one is easier to read and therefor maintain?
55
+
56
+ ## Usage Without Refinements
57
+
58
+ If you prefer not to use refinements, just load its core extensions in the same
59
+ way that you would have done on ActiveSupport.
60
+
61
+ ```ruby
62
+ require 'math_symbols/core_ext'
63
+
64
+ π # => 3.141592
65
+ ```
66
+
67
+ ## Development
68
+
69
+ Run `rake` to rubocop, RBS and RSpec tests.
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and PRs are welcome on GitHub at
74
+ https://github.com/dalthon/math_symbols.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the
79
+ [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new :spec
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: %i[rubocop steep spec]
9
+
10
+ task :steep do
11
+ require 'steep'
12
+ require 'steep/cli'
13
+
14
+ Steep::CLI.new(argv: ['check'], stdout: $stdout, stderr: $stderr, stdin: $stdin).run
15
+ end
16
+
17
+ namespace :steep do
18
+ task :stats do
19
+ exec %q(bundle exec steep stats --log-level=fatal | awk -F',' '{ printf "%-28s %-9s %-12s %-14s %-10s\n", $2, $3, $4, $5, $7 }') # rubocop:disable Layout/LineLength
20
+ end
21
+ end
data/Steepfile ADDED
@@ -0,0 +1,11 @@
1
+ target :lib do
2
+ signature 'sig'
3
+
4
+ check 'lib/math_symbols'
5
+ end
6
+
7
+ # target :spec do
8
+ # signature 'sig'
9
+ #
10
+ # check 'spec'
11
+ # end
@@ -0,0 +1,6 @@
1
+ class << Array
2
+ def ø
3
+ []
4
+ end
5
+ alias :Ø :ø
6
+ end
@@ -0,0 +1,29 @@
1
+ class TrueClass
2
+ def ⊕(other)
3
+ !other
4
+ end
5
+
6
+ def ⇔(other)
7
+ other
8
+ end
9
+ alias :↔ :⇔
10
+
11
+ def ⇒(other)
12
+ other
13
+ end
14
+ end
15
+
16
+ class FalseClass
17
+ def ⊕(other)
18
+ other
19
+ end
20
+
21
+ def ⇔(other)
22
+ !other
23
+ end
24
+ alias :↔ :⇔
25
+
26
+ def ⇒(_other)
27
+ true
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ class Complex
2
+ def √(value)
3
+ value ** (1.0 / self)
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module Enumerable
2
+ def ∑
3
+ inject 0 do |acc, x|
4
+ yield(x) + acc
5
+ end
6
+ end
7
+
8
+ def ∏
9
+ inject 1 do |acc, x|
10
+ yield(x) * acc
11
+ end
12
+ end
13
+
14
+ alias :∀ :all?
15
+ alias :∃ :any?
16
+ alias :∄ :none?
17
+ end
@@ -0,0 +1,5 @@
1
+ class Float
2
+ def √(value)
3
+ value ** (1.0 / self)
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class << Hash
2
+ def ø
3
+ {}
4
+ end
5
+ alias :Ø :ø
6
+ end
@@ -0,0 +1,23 @@
1
+ class Integer
2
+ def √(value)
3
+ value ** (1.0 / self)
4
+ end
5
+
6
+ def ⊕(value)
7
+ self ^ value
8
+ end
9
+
10
+ def ⇔(value)
11
+ max_bit_length = value.bit_length
12
+ max_bit_length = bit_length if bit_length > max_bit_length
13
+ mask = (1 << max_bit_length) - 1
14
+
15
+ (self & value) | ((mask ^ self) & (mask ^ value))
16
+ end
17
+ alias :↔ :⇔
18
+
19
+ def ⇒(value)
20
+ mask = (1 << bit_length) - 1
21
+ (mask ^ self) | value
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Math
2
+ extend MathSymbols::MathExtensions
3
+ end
@@ -0,0 +1,21 @@
1
+ class Object
2
+ include MathSymbols::MathExtensions
3
+
4
+ def ∈(set)
5
+ set.include? self
6
+ end
7
+
8
+ def ∉(set)
9
+ !∈(set)
10
+ end
11
+
12
+ def ƒ(&block)
13
+ lambda do |*x|
14
+ Math.instance_exec(*x, &block)
15
+ end
16
+ end
17
+
18
+ def λ(&block)
19
+ Math.instance_eval(&block)
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ class Rational
2
+ def √(value)
3
+ value ** (1.0 / self)
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'math_extensions'
2
+
3
+ require_relative 'core_ext/array'
4
+ require_relative 'core_ext/boolean'
5
+ require_relative 'core_ext/complex'
6
+ require_relative 'core_ext/enumerable'
7
+ require_relative 'core_ext/float'
8
+ require_relative 'core_ext/hash'
9
+ require_relative 'core_ext/integer'
10
+ require_relative 'core_ext/math'
11
+ require_relative 'core_ext/object'
12
+ require_relative 'core_ext/rational'
@@ -0,0 +1,35 @@
1
+ module MathSymbols
2
+ module MathExtensions
3
+ def ∑(enumerable)
4
+ enumerable.inject 0 do |acc, x|
5
+ acc + yield(x)
6
+ end
7
+ end
8
+
9
+ def ∏(enumerable)
10
+ enumerable.inject 1 do |acc, x|
11
+ acc * yield(x)
12
+ end
13
+ end
14
+
15
+ def √(value)
16
+ Math.sqrt value
17
+ end
18
+
19
+ def Γ(value)
20
+ Math.gamma value
21
+ end
22
+
23
+ def π
24
+ Math::PI
25
+ end
26
+
27
+ def ∞
28
+ Float::INFINITY
29
+ end
30
+
31
+ def δ(value)
32
+ value.zero? ? ∞ : 0
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module MathSymbols
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,133 @@
1
+ require_relative 'math_symbols/version'
2
+ require_relative 'math_symbols/math_extensions'
3
+
4
+ module MathSymbols # rubocop:disable Metrics/ModuleLength
5
+ refine Math.singleton_class do
6
+ import_methods MathSymbols::MathExtensions
7
+ end
8
+
9
+ refine Array.singleton_class do
10
+ def ø
11
+ []
12
+ end
13
+ alias :Ø :ø
14
+ end
15
+
16
+ refine Hash.singleton_class do
17
+ def ø
18
+ {}
19
+ end
20
+ alias :Ø :ø
21
+ end
22
+
23
+ refine Enumerable do
24
+ def ∑
25
+ inject 0 do |acc, x|
26
+ yield(x) + acc
27
+ end
28
+ end
29
+
30
+ def ∏
31
+ inject 1 do |acc, x|
32
+ yield(x) * acc
33
+ end
34
+ end
35
+
36
+ alias :∀ :all?
37
+ alias :∃ :any?
38
+ alias :∄ :none?
39
+ end
40
+
41
+ refine Object do
42
+ import_methods MathSymbols::MathExtensions
43
+
44
+ def δ(value)
45
+ value.zero? ? ∞ : 0
46
+ end
47
+
48
+ def ∈(set)
49
+ set.include? self
50
+ end
51
+
52
+ def ∉(set)
53
+ !∈(set)
54
+ end
55
+
56
+ def ƒ(&block)
57
+ lambda do |*x|
58
+ Math.instance_exec(*x, &block)
59
+ end
60
+ end
61
+
62
+ def λ(&block)
63
+ Math.instance_eval(&block)
64
+ end
65
+ end
66
+
67
+ refine TrueClass do
68
+ def ⊕(other)
69
+ !other
70
+ end
71
+
72
+ def ⇔(other)
73
+ other
74
+ end
75
+ alias :↔ :⇔
76
+
77
+ def ⇒(other)
78
+ other
79
+ end
80
+ end
81
+
82
+ refine FalseClass do
83
+ def ⊕(other)
84
+ other
85
+ end
86
+
87
+ def ⇔(other)
88
+ !other
89
+ end
90
+ alias :↔ :⇔
91
+
92
+ def ⇒(_other)
93
+ true
94
+ end
95
+ end
96
+
97
+ refine Integer do
98
+ def √(value)
99
+ value ** (1.0 / self)
100
+ end
101
+
102
+ def ⊕(value)
103
+ self ^ value
104
+ end
105
+
106
+ def ⇔(value)
107
+ (self && value) || (!self && !value)
108
+ end
109
+ alias :↔ :⇔
110
+
111
+ def ⇒(value)
112
+ !self || value
113
+ end
114
+ end
115
+
116
+ refine Float do
117
+ def √(value)
118
+ value ** (1.0 / self)
119
+ end
120
+ end
121
+
122
+ refine Complex do
123
+ def √(value)
124
+ value ** (1.0 / self)
125
+ end
126
+ end
127
+
128
+ refine Rational do
129
+ def √(value)
130
+ value ** (1.0 / self)
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,4 @@
1
+ class Array[unchecked out T]
2
+ def self.`ø`: [T] () -> Array[T]
3
+ def self.`Ø`: [T] () -> Array[T]
4
+ end
@@ -0,0 +1,13 @@
1
+ class TrueClass
2
+ def `⊕`: (bool) -> bool
3
+ def `⇔`: (bool) -> bool
4
+ def `↔`: (bool) -> bool
5
+ def `⇒`: (bool) -> bool
6
+ end
7
+
8
+ class FalseClass
9
+ def `⊕`: (bool) -> bool
10
+ def `⇔`: (bool) -> bool
11
+ def `↔`: (bool) -> bool
12
+ def `⇒`: (bool) -> bool
13
+ end
@@ -0,0 +1,3 @@
1
+ class Complex
2
+ def `√`: (Integer | Float | Complex | Rational value) -> Numeric
3
+ end
@@ -0,0 +1,8 @@
1
+ module Enumerable[unchecked out Elem] : _Each[Elem]
2
+ def `∑`: [Elem] () { (Elem) -> (Numeric & _Summable) } -> (Numeric & _Summable)
3
+ def `∏`: [Elem] () { (Elem) -> (Numeric & _Multipliable) } -> (Numeric & _Multipliable)
4
+
5
+ def `∀`: [Elem] () { (Elem) -> boolish } -> bool
6
+ def `∃`: [Elem] () { (Elem) -> boolish } -> bool
7
+ def `∄`: [Elem] () { (Elem) -> boolish } -> bool
8
+ end
@@ -0,0 +1,3 @@
1
+ class Float
2
+ def `√`: (Integer | Float | Complex | Rational value) -> Numeric
3
+ end
@@ -0,0 +1,4 @@
1
+ class Hash[unchecked out K, unchecked out V]
2
+ def self.`ø`: [K, V] () -> Hash[K, V]
3
+ def self.`Ø`: [K, V] () -> Hash[K, V]
4
+ end
@@ -0,0 +1,8 @@
1
+ class Integer
2
+ def `√`: (Integer | Float | Complex | Rational value) -> Numeric
3
+
4
+ def `⊕`: (Integer value) -> Integer
5
+ def `⇔`: (Integer value) -> Integer
6
+ def `↔`: (Integer value) -> Integer
7
+ def `⇒`: (Integer value) -> Integer
8
+ end
@@ -0,0 +1,7 @@
1
+ class Object
2
+ def `∈`: (untyped set) -> bool
3
+ def `∉`: (untyped set) -> bool
4
+
5
+ def `ƒ`: [V, U] () { (*V) [self: singleton(Math)] -> U } -> (^(*V) [self: singleton(Math)] -> U)
6
+ def `λ`: [U] () { (singleton(Math)) [self: singleton(Math)] -> U } -> U
7
+ end
@@ -0,0 +1,3 @@
1
+ class Rational
2
+ def `√`: (Integer | Float | Complex | Rational value) -> Numeric
3
+ end
@@ -0,0 +1,22 @@
1
+ interface _Summable
2
+ def +: (Integer | Float | Complex | Rational) -> (Integer | Float | Complex | Rational)
3
+ end
4
+
5
+ interface _Multipliable
6
+ def *: (Integer | Float | Complex | Rational) -> (Integer | Float | Complex | Rational)
7
+ end
8
+
9
+ module MathSymbols
10
+ module MathExtensions
11
+ def `∑`: [Elem] (Enumerable[Elem] enumerable) { (Elem) -> (Numeric & _Summable) } -> (Numeric & _Summable)
12
+ def `∏`: [Elem] (Enumerable[Elem] enumerable) { (Elem) -> (Numeric & _Multipliable) } -> (Numeric & _Multipliable)
13
+
14
+ def `√`: (Numeric & _ToF value) -> (Numeric & _ToF)
15
+ def `Γ`: (Numeric & _ToF value) -> (Numeric & _ToF)
16
+
17
+ def `π`: () -> Float
18
+ def `∞`: () -> Numeric
19
+
20
+ def `δ`: (untyped value) -> Numeric
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module MathSymbols
2
+ VERSION: String
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: math_symbols
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dalton Pinto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Support for mathematical syntactic sugars
14
+ email:
15
+ - dalton.pinto@local
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - Steepfile
27
+ - lib/math_symbols.rb
28
+ - lib/math_symbols/core_ext.rb
29
+ - lib/math_symbols/core_ext/array.rb
30
+ - lib/math_symbols/core_ext/boolean.rb
31
+ - lib/math_symbols/core_ext/complex.rb
32
+ - lib/math_symbols/core_ext/enumerable.rb
33
+ - lib/math_symbols/core_ext/float.rb
34
+ - lib/math_symbols/core_ext/hash.rb
35
+ - lib/math_symbols/core_ext/integer.rb
36
+ - lib/math_symbols/core_ext/math.rb
37
+ - lib/math_symbols/core_ext/object.rb
38
+ - lib/math_symbols/core_ext/rational.rb
39
+ - lib/math_symbols/math_extensions.rb
40
+ - lib/math_symbols/version.rb
41
+ - sig/math_symbols.rbs
42
+ - sig/math_symbols/core_ext/array.rbs
43
+ - sig/math_symbols/core_ext/boolean.rbs
44
+ - sig/math_symbols/core_ext/complex.rbs
45
+ - sig/math_symbols/core_ext/enumerable.rbs
46
+ - sig/math_symbols/core_ext/float.rbs
47
+ - sig/math_symbols/core_ext/hash.rbs
48
+ - sig/math_symbols/core_ext/integer.rbs
49
+ - sig/math_symbols/core_ext/object.rbs
50
+ - sig/math_symbols/core_ext/rational.rbs
51
+ - sig/math_symbols/math_extensions.rbs
52
+ homepage: https://github.com/dalthon/math_symbols
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/dalthon/math_symbols
57
+ source_code_uri: https://github.com/dalthon/math_symbols
58
+ changelog_uri: https://github.com/dalthon/math_symbols/blob/master/CHANGELOG.md
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.6.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.5.5
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Support for mathematical syntactic sugars
78
+ test_files: []