Set of real numbers: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: add methods (new_Rset, valid?, ^, ==)) |
(→{{header|Ruby}}: add length method and Optional work) |
||
Line 1,223: | Line 1,223: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.9.3}} |
|||
<lang ruby>class Rset |
<lang ruby>class Rset |
||
Set = Struct.new(:lo, :hi, :inc_lo, :inc_hi) do |
Set = Struct.new(:lo, :hi, :inc_lo, :inc_hi) do |
||
def include?(x) |
def include?(x) |
||
(inc_lo ? lo<=x : lo<x) and (inc_hi ? x<=hi : x<hi) |
(inc_lo ? lo<=x : lo<x) and (inc_hi ? x<=hi : x<hi) |
||
end |
|||
def length |
|||
hi - lo |
|||
end |
end |
||
def to_s |
def to_s |
||
Line 1,239: | Line 1,243: | ||
raise TypeError unless lo.is_a?(Numeric) and hi.is_a?(Numeric) |
raise TypeError unless lo.is_a?(Numeric) and hi.is_a?(Numeric) |
||
raise ArgumentError unless valid?(lo, hi, inc_lo, inc_hi) |
raise ArgumentError unless valid?(lo, hi, inc_lo, inc_hi) |
||
@sets = [Set[lo, hi, inc_lo, inc_hi]] |
@sets = [Set[lo, hi, !!inc_lo, !!inc_hi]] # !! -> Boolean values |
||
end |
end |
||
end |
end |
||
Line 1,303: | Line 1,307: | ||
else |
else |
||
lo = [set.lo, oset.lo].max |
lo = [set.lo, oset.lo].max |
||
if set.lo |
if set.lo == oset.lo |
||
inc_lo = oset.inc_lo |
inc_lo = set.inc_lo && oset.inc_lo |
||
elsif set.lo > oset.lo |
|||
inc_lo = set.inc_lo |
|||
else |
else |
||
inc_lo = set. |
inc_lo = (set.lo < oset.lo) ? oset.inc_lo : set.inc_lo |
||
end |
end |
||
hi = [set.hi, oset.hi].min |
hi = [set.hi, oset.hi].min |
||
if set.hi |
if set.hi == oset.hi |
||
inc_hi = set.inc_hi |
inc_hi = set.inc_hi && oset.inc_hi |
||
elsif set.hi > oset.hi |
|||
inc_hi = oset.inc_hi |
|||
else |
else |
||
inc_hi = set.inc_hi |
inc_hi = (set.hi < oset.hi) ? set.inc_hi : oset.inc_hi |
||
end |
end |
||
work << Set[lo, hi, inc_lo, inc_hi] if valid?(lo, hi, inc_lo, inc_hi) |
work << Set[lo, hi, inc_lo, inc_hi] if valid?(lo, hi, inc_lo, inc_hi) |
||
Line 1,364: | Line 1,364: | ||
return false if self.class != other.class |
return false if self.class != other.class |
||
@sets == other.sets |
@sets == other.sets |
||
end |
|||
def length |
|||
@sets.inject(0){|len, set| len + set.length} |
|||
end |
end |
||
Line 1,420: | Line 1,424: | ||
test_set.each do |sa,ope,sb| |
test_set.each do |sa,ope,sb| |
||
str = "#{sa} #{ope} #{sb}" |
str = "#{sa} #{ope} #{sb}" |
||
e = eval("Rset.from_s(sa) #{ope} Rset.from_s(sb)") |
|||
⚫ | |||
⚫ | |||
⚫ | |||
c = eval("a #{ope} b") |
|||
⚫ | |||
⚫ | |||
end |
end |
||
Line 1,430: | Line 1,432: | ||
test_set = ["x = Rset[0,2] | Rset(3,7) | Rset[8,10]", |
test_set = ["x = Rset[0,2] | Rset(3,7) | Rset[8,10]", |
||
"y = Rset(7,9) | Rset(5,6) | Rset[1,4]", |
"y = Rset(7,9) | Rset(5,6) | Rset[1,4]", |
||
"x | y", "x & y", "x - y", "y - x", "x ^ y" |
"x | y", "x & y", "x - y", "y - x", "x ^ y", |
||
"y ^ x == (x | y) - (x & y)"] |
|||
x = y = nil |
x = y = nil |
||
test_set.each {|str| puts "#{str} -> #{eval(str)}"} |
test_set.each {|str| puts "#{str} -> #{eval(str)}"} |
||
Line 1,438: | Line 1,441: | ||
puts "a = #{a = Rset(-inf,inf)}" |
puts "a = #{a = Rset(-inf,inf)}" |
||
puts "b = #{b = Rset.from_s('[1/3,11/7)')}" |
puts "b = #{b = Rset.from_s('[1/3,11/7)')}" |
||
puts "a - b -> #{a - b}" |
puts "a - b -> #{a - b}"</lang> |
||
⚫ | |||
{{out}} |
{{out}} |
||
Line 1,484: | Line 1,486: | ||
y - x -> Rset(2,3],(7,8) |
y - x -> Rset(2,3],(7,8) |
||
x ^ y -> Rset[0,1),(2,3],(4,5],[6,7),(7,8),[9,10] |
x ^ y -> Rset[0,1),(2,3],(4,5],[6,7),(7,8),[9,10] |
||
y ^ x == (x | y) - (x & y) -> true |
|||
a = Rset(-Infinity,Infinity) |
a = Rset(-Infinity,Infinity) |
||
b = Rset[1/3,11/7) |
b = Rset[1/3,11/7) |
||
a - b -> Rset(-Infinity,1/3),[11/7,Infinity) |
a - b -> Rset(-Infinity,1/3),[11/7,Infinity) |
||
</pre> |
|||
create empty set : Rset |
|||
'''Optional work:''' |
|||
{{works with|Ruby|2.1+}} |
|||
<lang ruby>str, e = "e = Rset.new", nil |
|||
puts "#{str} -> #{eval(str)}\t\t# create empty set" |
|||
str = "e.empty?" |
|||
puts "#{str} -> #{eval(str)}" |
|||
puts |
|||
include Math |
|||
lohi = Enumerator.new do |y| |
|||
t = 1 / sqrt(6) |
|||
0.step do |n| |
|||
y << [sqrt(12*n+1) * t, sqrt(12*n+5) * t] |
|||
y << [sqrt(12*n+7) * t, sqrt(12*n+11) * t] |
|||
end |
|||
end |
|||
a = Rset.new |
|||
loop do |
|||
lo, hi = lohi.next |
|||
break if 10 <= lo |
|||
⚫ | |||
end |
|||
a &= Rset(0,10) |
|||
b = (0...10).inject(Rset.new){|res,i| res |= Rset(i+1/6r,i+5/6r)} |
|||
puts "a : #{a}" |
|||
puts "a.length : #{a.length}" |
|||
puts "b : #{b}" |
|||
puts "b.length : #{b.length}" |
|||
puts "a - b : #{a - b}" |
|||
⚫ | |||
{{out}} |
|||
<pre> |
|||
e = Rset.new -> Rset # create empty set |
|||
e.empty? -> true |
|||
a : Rset(0.4082482904638631,0.912870929175277),(1.0801234497346435,1.3540064007726602),(1.4719601443879746,1.6832508230603467), ... (9.907909298467901,9.941495528004495),(9.958246164193106,9.991663191547909) |
|||
a.length : 6.50103079235655 |
|||
b : Rset(1/6,5/6),(7/6,11/6),(13/6,17/6),(19/6,23/6),(25/6,29/6),(31/6,35/6),(37/6,41/6),(43/6,47/6),(49/6,53/6),(55/6,59/6) |
|||
b.length : 20/3 |
|||
a - b : Rset[5/6,0.912870929175277),(1.0801234497346435,7/6],[11/6,1.9578900207451218),(2.041241452319315,13/6], ... (9.907909298467901,9.941495528004495),(9.958246164193106,9.991663191547909) |
|||
(a-b).length : 2.0758648411846745 |
|||
</pre> |
</pre> |
||