-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualization.jl
86 lines (66 loc) · 1.82 KB
/
Visualization.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module Visualization
export summation, formula
using LogicalFunctions: evaluate, natural_code, positive, dontcare, TruthTable
using Base.Iterators: flatten
to_string(bits::BitVector)::String = join(Int.(bits))
to_dec(bits::String)::Int = parse(Int, bits, base=2)
to_comma(nums::Vector{Int})::String = join(nums, ", ")
represent(table::TruthTable)::String =
table |>
keys .|>
to_string .|>
to_dec |>
sort |>
to_comma
function summation(values::TruthTable; name::String = "f")::String
ones =
values |>
positive |>
represent
dontcares =
values |>
dontcare |>
represent
"$name = Σ m($(ones)) + d($(dontcares))"
end
function summation(fun::Function, arity::Int; name::String = "f")::String
summation(
evaluate(fun, natural_code(arity)),
name = name
)
end
function formula(dnf::Vector{Vector{Int}}, name::String = "f", val_names::Union{Nothing, Dict{Int, String}} = nothing)::String
if val_names === nothing
val_names =
dnf |>
flatten |>
unique .|>
abs |>
sort .|>
(i -> i => "x$i") |>
Dict
end
signs(minterm::Vector{Int})::Vector{String} =
minterm .|>
(num -> num < 0 ? "-" : "")
to_literals(minterm::Vector{Int})::Vector{String} =
join.(
zip(
signs(minterm),
get.([val_names], abs.(minterm), "?")
)
)
to_conjunction(minterm::Vector{Int})::String = "($(
join(
to_literals(minterm),
" ∧ "
)
))"
to_disjunction(minterms::Vector{String})::String = join(minterms, " ∨ ")
expr =
dnf .|>
to_conjunction |>
to_disjunction
"$name = $expr"
end
end# module