Skip to content

Instantly share code, notes, and snippets.

@Juvar1
Created May 24, 2023 03:14
Show Gist options
  • Save Juvar1/238ed57850607350db5e61c8e2b7bfc2 to your computer and use it in GitHub Desktop.
Save Juvar1/238ed57850607350db5e61c8e2b7bfc2 to your computer and use it in GitHub Desktop.
Simulation program for ternary gates
# ternary-math-test.py
# Simulation program for ternary gates
#
# Copyright (C) 2023, Juha-Pekka Varjonen
#
#
# Internet is full of wrong and badly incorrect information about ternary operations.
# This simple program simulates NOT, NAND and NOR gates in all their states.
#
# Based on ternary Bible, U.S. Patent 4,107,549
#
# Usage example:
# input1 = 2
# input2 = 0
# variation = 1 # <--- gate output point is negative=0, standard=1 or positive=2
# output = tnand(input1, input2).get(variation)
#
#
# ternary-math-test.py is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# ternary-math-test.py is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ternary-math-test.py. If not, see <https://www.gnu.org/licenses/>.
def tnot(x):
cond = {
2: 0 if x == 2 else 2,
1: 2 - x,
0: 2 if x == 0 else 0
}
return cond
def tnand(x, y):
cond = {
2: 0 if min(x, y) == 2 else 2,
1: 2 - min(x, y),
0: 2 if min(x, y) == 0 else 0
}
return cond
def tnor(x, y):
cond = {
2: 0 if max(x, y) == 2 else 2,
1: 2 - max(x, y),
0: 2 if max(x, y) == 0 else 0 # <--- currently untested
}
return cond
# This example shows how to simulate trinary-to-decade converter with two input trits
# Program prints out it's truth table
for b in range(3):
for a in range(3):
in0 = tnot(a).get(1)
j00 = tnot(a).get(0)
j02 = tnot(in0).get(0)
j01 = tnor(j02, in0).get(2)
in1 = tnot(b).get(1)
j10 = tnot(b).get(0)
j12 = tnot(in1).get(0)
j11 = tnor(j12, in1).get(2)
out1 = tnot(tnand(j00, j10).get(1)).get(1)
out2 = tnot(tnand(j01, j10).get(1)).get(1)
out3 = tnot(tnand(j02, j10).get(1)).get(1)
out4 = tnot(tnand(j00, j11).get(1)).get(1)
out5 = tnot(tnand(j01, j11).get(1)).get(1)
out6 = tnot(tnand(j02, j11).get(1)).get(1)
out7 = tnot(tnand(j00, j12).get(1)).get(1)
out8 = tnot(tnand(j01, j12).get(1)).get(1)
out9 = tnot(tnand(j02, j12).get(1)).get(1)
print(f'b={b}, a={a}, {out1}, {out2}, {out3}, {out4}, {out5}, {out6}, {out7}, {out8}, {out9}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment