diff --git a/test/Data/dyadic_tree_3.dot b/examples/Data/dyadic_tree_3.dot similarity index 74% rename from test/Data/dyadic_tree_3.dot rename to examples/Data/dyadic_tree_3.dot index e59d77e..8b9192e 100644 --- a/test/Data/dyadic_tree_3.dot +++ b/examples/Data/dyadic_tree_3.dot @@ -10,14 +10,14 @@ node_day_1 [shape=none, pos="7.875,1!", label="Day 1", fontsize=18] node_1 [shape=none,margin=0,label= < - +
-1
ϕ  
0
ϕ  
0
>,pos="-2.25,1!" ]; node_1:R -> node_0; node_2 [shape=none,margin=0,label= < - +
1
 
0
ϕ
 
0
ϕ
>,pos="2.25,1!" ]; node_2:L -> node_0; @@ -25,21 +25,21 @@ node_day_2 [shape=none, pos="7.875,2!", label="Day 2", fontsize=18] node_3 [shape=none,margin=0,label= < - +
-2
ϕ  
-1
ϕ  
-1
>,pos="-4.5,2!" ]; node_3:R -> node_1; node_4 [shape=none,margin=0,label= < - +
2
 
1
ϕ
 
1
ϕ
>,pos="4.5,2!" ]; node_4:L -> node_2; node_5 [shape=none,margin=0,label= < - +
1/2
 
0
 
1
 
0
 
1
>,pos="1.125,2!" ]; node_5:L -> node_0; @@ -47,7 +47,7 @@ node_day_2 [shape=none, pos="7.875,2!", label="Day 2", fontsize=18] node_6 [shape=none,margin=0,label= < - +
-1/2
 
-1
 
0
 
-1
 
0
>,pos="-1.125,2!" ]; node_6:L -> node_1; @@ -56,21 +56,21 @@ node_day_3 [shape=none, pos="7.875,3!", label="Day 3", fontsize=18] node_7 [shape=none,margin=0,label= < - +
-3
ϕ  
-2
ϕ  
-2
>,pos="-6.75,3!" ]; node_7:R -> node_3; node_8 [shape=none,margin=0,label= < - +
3
 
2
ϕ
 
2
ϕ
>,pos="6.75,3!" ]; node_8:L -> node_4; node_9 [shape=none,margin=0,label= < - +
1/4
 
0
 
1/2
 
0
 
1/2
>,pos="0.5625,3!" ]; node_9:L -> node_0; @@ -78,7 +78,7 @@ node_day_3 [shape=none, pos="7.875,3!", label="Day 3", fontsize=18] node_10 [shape=none,margin=0,label= < - +
-1/4
 
-1/2
 
0
 
-1/2
 
0
>,pos="-0.5625,3!" ]; node_10:L -> node_6; @@ -86,7 +86,7 @@ node_day_3 [shape=none, pos="7.875,3!", label="Day 3", fontsize=18] node_11 [shape=none,margin=0,label= < - +
3/4
 
1/2
 
1
 
1/2
 
1
>,pos="1.6875,3!" ]; node_11:L -> node_5; @@ -94,7 +94,7 @@ node_day_3 [shape=none, pos="7.875,3!", label="Day 3", fontsize=18] node_12 [shape=none,margin=0,label= < - +
-3/4
 
-1
 
-1/2
 
-1
 
-1/2
>,pos="-1.6875,3!" ]; node_12:L -> node_1; @@ -102,7 +102,7 @@ node_day_3 [shape=none, pos="7.875,3!", label="Day 3", fontsize=18] node_13 [shape=none,margin=0,label= < - +
3/2
 
1
 
2
 
1
 
2
>,pos="3.375,3!" ]; node_13:L -> node_2; @@ -110,7 +110,7 @@ node_day_3 [shape=none, pos="7.875,3!", label="Day 3", fontsize=18] node_14 [shape=none,margin=0,label= < - +
-3/2
 
-2
 
-1
 
-2
 
-1
>,pos="-3.375,3!" ]; node_14:L -> node_3; diff --git a/examples/Data/dyadic_tree_3.dot.pdf b/examples/Data/dyadic_tree_3.dot.pdf new file mode 100644 index 0000000..596ee54 Binary files /dev/null and b/examples/Data/dyadic_tree_3.dot.pdf differ diff --git a/test/Data/dyadic_tree_3.dot.svg b/examples/Data/dyadic_tree_3.dot.svg similarity index 100% rename from test/Data/dyadic_tree_3.dot.svg rename to examples/Data/dyadic_tree_3.dot.svg diff --git a/test/Data/dyadic_tree_4.dot b/examples/Data/dyadic_tree_4.dot similarity index 100% rename from test/Data/dyadic_tree_4.dot rename to examples/Data/dyadic_tree_4.dot diff --git a/test/Data/dyadic_tree_4.dot.pdf b/examples/Data/dyadic_tree_4.dot.pdf similarity index 53% rename from test/Data/dyadic_tree_4.dot.pdf rename to examples/Data/dyadic_tree_4.dot.pdf index 62ff774..8632bdd 100644 Binary files a/test/Data/dyadic_tree_4.dot.pdf and b/examples/Data/dyadic_tree_4.dot.pdf differ diff --git a/test/Data/dyadic_tree_4.dot.svg b/examples/Data/dyadic_tree_4.dot.svg similarity index 100% rename from test/Data/dyadic_tree_4.dot.svg rename to examples/Data/dyadic_tree_4.dot.svg diff --git a/test/dyadic_tree.jl b/examples/dyadic_tree.jl similarity index 99% rename from test/dyadic_tree.jl rename to examples/dyadic_tree.jl index 5c8a84d..6a64ed4 100644 --- a/test/dyadic_tree.jl +++ b/examples/dyadic_tree.jl @@ -3,7 +3,7 @@ using SurrealNumbers # var_args = "color=red" var_args = "" n=3; x_scale = 2.25 -n=4; x_scale = 5.00 +# n=4; x_scale = 5.00 out_dir = "Data/" name_map = Dict{SurrealFinite, Integer}() diff --git a/test/examples.jl b/examples/examples.jl similarity index 99% rename from test/examples.jl rename to examples/examples.jl index a821030..eb6e6c9 100644 --- a/test/examples.jl +++ b/examples/examples.jl @@ -27,7 +27,7 @@ generation.(A) A .== canonicalise.(A) -n = 3 +n = 1 x = convert.(SurrealFinite, 0:n) A = zeros(SurrealFinite, n+1, n+1) for i=1:n+1 diff --git a/src/SurrealFinite.jl b/src/SurrealFinite.jl index 610f608..1129035 100644 --- a/src/SurrealFinite.jl +++ b/src/SurrealFinite.jl @@ -311,7 +311,29 @@ end pf(io::IO, x::SurrealFinite) = println(io, "{ ", x.L, " | ", x.R, " }") pf(x::SurrealFinite) = pf(STDOUT, x) -# expand at some level, 0 steps (if possible), 1 step, or completely +""" + expand(x::SurrealFinite; level=0) + + Writes a surreal as a string with varying levels of expansion. + +## Arguments +* `x::SurrealFinite`: the number of elements to expand +* `level=0`: the amount of expansion + + + 0 : write shorthand if it exists, or ``\\{ X_L \\| X_R \\}`` if not + + 1 : ``\\{ X_L \\| X_R \\}`` + + 2 : expand out ``X_L`` and ``X_R`` recursively + +## Examples +```jldoctest +julia> expand( convert(SurrealFinite, 2)) +"2" +julia> expand( convert(SurrealFinite, 2); level=1) +"{ 1 | ϕ }" +julia> expand( convert(SurrealFinite, 2); level=2) +"{ { { ϕ | ϕ } | ϕ } | ϕ }" +``` +""" function expand(x::SurrealFinite; level=0) if level==0 s = x.shorthand != "" ? x.shorthand : expand(x; level=1) @@ -396,6 +418,31 @@ end # special "canonicalised" output spf(x::SurrealFinite) = print("{ ", canonicalise.(x.L), " | ", canonicalise.(x.R), " }") +""" + surreal2dag(x::SurrealFinite) + surreal2dag(io::IO, x::SurrealFinite) + + Writes a surreal representation as a DAG out in DOT format for drawing using GraphVis, + and returns the number of nodes in the graph. + +## Arguments +* `io::IO`: output stream, default is STDOUT +* `x::SurrealFinite`: the number to write out + +## Examples +```jldoctest +julia> surreal2dag(convert(SurrealFinite, 0)) +digraph "0.0" { + node_1 [shape=none,margin=0,label= + < + + +
0
ϕ ϕ
>, + ]; +} +1 +``` +""" function surreal2dag(io::IO, x::SurrealFinite) println(io, "digraph \"", float(x), "\" {") k = 1 @@ -442,6 +489,33 @@ end surreal2dag(x::SurrealFinite) = surreal2dag(STDOUT, x) +""" + surreal2dot(x::SurrealFinite) + surreal2dot(io::IO, x::SurrealFinite) + + Writes a surreal representation as a tree out in DOT format for drawing using GraphVis, + and returns the number of nodes in the graph. + +## Arguments +* `io::IO`: output stream, default is STDOUT +* `x::SurrealFinite`: the number to write out + +## Examples +```jldoctest +julia> surreal2dot(convert(SurrealFinite, 1)) +digraph "1.0" { + node_1 [shape=none,margin=0,label= + < + + +
1
 
0
ϕ
>, + ]; + node_1:"0,1" -> node_2; + node_2 [shape=none,margin=0,label=<0>] +} +2 +``` +""" function surreal2dot(io::IO, x::SurrealFinite) println(io, "digraph \"", float(x), "\" {") k = 1 @@ -525,7 +599,20 @@ surreal2dot(x::SurrealFinite) = surreal2dot(STDOUT, x) ####################################################### -# generation or birth day calculation +""" + generation(x::SurrealFinite) + + Finds the birthday of a surreal number, which is 1 + the max of any of its components. + +## Arguments +* `x::SurrealFinite`: the number to operate on + +## Examples +```jldoctest +julia> generation( convert(SurrealFinite, 1) ) +1 +``` +""" function generation(x::SurrealFinite) if x==zero(x) return 0 @@ -536,6 +623,22 @@ function generation(x::SurrealFinite) end # this is a bit of a cheat, but I'm not smart enough to work out how to do it otherwise +""" + canonicalise(s::SurrealFinite) + + Convert a surreal number form into its equivalent canonical form. + +## Arguments +* `x::SurrealFinite`: the number to operate on + +## Examples +```jldoctest +julia> convert(SurrealFinite, 1) - convert(SurrealFinite, 1) +{ { ϕ | { ϕ | ϕ } } | { { ϕ | ϕ } | ϕ } } +julia> pf( canonicalise( convert(SurrealFinite, 1) - convert(SurrealFinite, 1) ) ) +{ ϕ | ϕ } +``` +""" canonicalise(s::SurrealFinite) = convert(SurrealFinite, convert(Rational, s)) iscanonical(s::SurrealFinite) = canonicalise(s) == s @@ -553,7 +656,6 @@ end ###### standard math routines ############################## - sign(x::SurrealFinite) = xzero(x) ? one(x) : zero(x) # abs(x::SurrealFinite) = x a = [1 2; 3 4] -2×2 Array{Int64,2}: - 1 2 - 3 4 -``` -""" -function special_case(input) - - - -end - diff --git a/test/timing_2.jl b/test/timing_2.jl new file mode 100644 index 0000000..e60ff81 --- /dev/null +++ b/test/timing_2.jl @@ -0,0 +1,69 @@ +# compare the time taken to do simple subtractions compared to < +# with a view to making some routiens that use subtraction a lot faster +using IndexedTables +using SurrealNumbers +convert(SurrealFinite, 2) * convert(SurrealFinite, 1) + +#ns = 6:-1:0 +ns = 0:6 +n = length(ns) +m = 100 * ones(size(ns)) +x1 = Array{SurrealFinite,1}(n) +x2 = Array{Bool,1}(n) +t1 = Array{Float64,1}(n) +t2 = Array{Float64,1}(n) +bytes2 = Array{Float64,1}(n) +bytes1 = Array{Float64,1}(n) +gctime1 = Array{Float64,1}(n) +gctime2 = Array{Float64,1}(n) +s = convert(SurrealFinite, 1) +s > 0 +s - 1 +for i=1:n + println(" working on ", ns[i]) + t1[i] = 0 + t2[i] = 0 + b1 = 0 + g1 = 0 + mem1 = 0 + val1 = 0 + b2 = 0 + g2 = 0 + mem2 = 0 + val2 = false + for j=1:m[i] + s = convert(SurrealFinite, ns[i]) + # val, time, b, g, mem = @timed convert(Rational, s) + val1, time1, b1, g1, mem1 = @timed s - 1 + val2, time2, b2, g2, mem2 = @timed s > 1 + t1[i] += time1 + t2[i] += time2 + end + x1[i] = val1 + bytes1[i] = b1 + gctime1[i] = g1 + x2[i] = val2 + bytes2[i] = b2 + gctime2[i] = g2 +end +# s = size.(x) +# d = depth_av.(x) +# f = n_zeros.(x) +t1 = t1./m[1:n] +bytes1 = bytes1./m[1:n] +t2 = t2./m[1:n] +bytes2 = bytes2./m[1:n] + +t1_p_ms = 1.0e6 * t1 +t2_p_ms = 1.0e6 * t2 + +tab1 = table(@NT( i=0:n-1, + time1=t1_p_ms, + time2=t2_p_ms, + bytes1=bytes1, + bytes2=bytes2); + pkey = [:i]) + + + + diff --git a/test/timing_3.jl b/test/timing_3.jl new file mode 100644 index 0000000..a6d3fd2 --- /dev/null +++ b/test/timing_3.jl @@ -0,0 +1,115 @@ +# compare the time taken to do simple subtractions compared to = one(s) + return floor(s - one(s)) + one(s) + end +end + +function isinteger2(s::SurrealFinite) + if s ≅ zero(s) + return true + elseif s <= -one(s) + return isinteger(s + one(s)) + elseif s >= one(s) + return isinteger(s - one(s)) + else + return false + end +end + + +# not the more general form of rounding defined in Julia -- should fix +function round2(s::SurrealFinite) + if s ≅ zero(s) + return s + elseif s <= -one(s) + return round(s + one(s)) - one(s) + elseif s >= one(s) + return round(s - one(s)) + one(s) + elseif s >= convert(SurrealFinite, 1//2) + return one(s) + elseif s >= convert(SurrealFinite, -1//2) + return zero(s) + else + return -one(s) + end +end + + + +convert(SurrealFinite, 2) * convert(SurrealFinite, 1) + +#ns = 6:-1:0 +ns = 0:4 +n = length(ns) +m = 100 * ones(size(ns)) +x1 = Array{SurrealFinite,1}(n) +x2 = Array{SurrealFinite,1}(n) +t1 = Array{Float64,1}(n) +t2 = Array{Float64,1}(n) +bytes2 = Array{Float64,1}(n) +bytes1 = Array{Float64,1}(n) +gctime1 = Array{Float64,1}(n) +gctime2 = Array{Float64,1}(n) +s = convert(SurrealFinite, 1) +floor(s) +floor2(s) +for i=1:n + println(" working on ", ns[i]) + t1[i] = 0 + t2[i] = 0 + b1 = 0 + g1 = 0 + mem1 = 0 + val1 = 0 + b2 = 0 + g2 = 0 + mem2 = 0 + val2 = 0 + for j=1:m[i] + s = convert(SurrealFinite, ns[i] + 0.5) + # val, time, b, g, mem = @timed convert(Rational, s) + val1, time1, b1, g1, mem1 = @timed floor( s ) + val2, time2, b2, g2, mem2 = @timed floor2( s ) + t1[i] += time1 + t2[i] += time2 + end + x1[i] = val1 + bytes1[i] = b1 + gctime1[i] = g1 + x2[i] = val2 + bytes2[i] = b2 + gctime2[i] = g2 +end +# s = size.(x) +# d = depth_av.(x) +# f = n_zeros.(x) +t1 = t1./m[1:n] +bytes1 = bytes1./m[1:n] +t2 = t2./m[1:n] +bytes2 = bytes2./m[1:n] + +t1_p_ms = 1.0e6 * t1 +t2_p_ms = 1.0e6 * t2 + +tab1 = table(@NT( i=0:n-1, + time1=t1_p_ms, + time2=t2_p_ms, + bytes1=bytes1, + bytes2=bytes2); + pkey = [:i]) + + + +