Imaginary base numbers: Difference between revisions

Content deleted Content added
Wherrera (talk | contribs)
Robbie (talk | contribs)
Line 2,294: Line 2,294:
</pre>
</pre>
ie the unnecessary trailing ".0" are trimmed. (see talk page)
ie the unnecessary trailing ".0" are trimmed. (see talk page)

=={{header|Python}}==
{{trans|C++}}
<lang python>import math
import re

def inv(c):
denom = c.real * c.real + c.imag * c.imag
return complex(c.real / denom, -c.imag / denom)

class QuaterImaginary:
twoI = complex(0, 2)
invTwoI = inv(twoI)

def __init__(self, str):
if not re.match("^[0123.]+$", str) or str.count('.') > 1:
raise Exception('Invalid base 2i number')
self.b2i = str

def toComplex(self):
pointPos = self.b2i.find('.')
posLen = len(self.b2i) if (pointPos < 0) else pointPos
sum = complex(0, 0)
prod = complex(1, 0)
for j in xrange(0, posLen):
k = (ord(self.b2i[posLen - 1 - j]) - ord('0'))
if k > 0:
sum = sum + prod * k
prod = prod * QuaterImaginary.twoI
if pointPos != -1:
prod = QuaterImaginary.invTwoI
for j in xrange(posLen + 1, len(self.b2i)):
k = (ord(self.b2i[j]) - ord('0'))
if k > 0:
sum = sum + prod * k
prod = prod * QuaterImaginary.invTwoI
return sum

def __str__(self):
return str(self.b2i)

def toQuaterImaginary(c):
if c.real == 0.0 and c.imag == 0.0:
return QuaterImaginary("0")

re = int(c.real)
im = int(c.imag)
fi = -1
ss = ""
while re != 0:
rem = re % -4
re = re / -4
if rem < 0:
rem = 4 + rem
re = re + 1
ss = ss + str(rem) + '0'
if im != 0:
f = (complex(0, c.imag) / complex(0, 2)).real
im = int(math.ceil(f))
f = -4 * (f - im)
index = 1
while im != 0:
rem = im % -4
im = im / -4
if rem < 0:
rem = 4 + rem
im = im + 1
if index < len(ss):
ss[index] = chr(rem + 48)
else:
ss = ss + '0' + str(rem)
index = index + 2
fi = int(f)
ss = ss[::-1]
if fi != -1:
ss = ss + '.' + str(fi)
ss = ss.lstrip('0')
if ss[0] == '.':
ss = '0' + ss
return QuaterImaginary(ss)

for i in xrange(1,17):
c1 = complex(i, 0)
qi = toQuaterImaginary(c1)
c2 = qi.toComplex()
print "{0:8} -> {1:>8} -> {2:8} ".format(c1, qi, c2),

c1 = -c1
qi = toQuaterImaginary(c1)
c2 = qi.toComplex()
print "{0:8} -> {1:>8} -> {2:8}".format(c1, qi, c2)
print

for i in xrange(1,17):
c1 = complex(0, i)
qi = toQuaterImaginary(c1)
c2 = qi.toComplex()
print "{0:8} -> {1:>8} -> {2:8} ".format(c1, qi, c2),

c1 = -c1
qi = toQuaterImaginary(c1)
c2 = qi.toComplex()
print "{0:8} -> {1:>8} -> {2:8}".format(c1, qi, c2)

print "done"
</lang>
{{out}}
<pre> (1+0j) -> 1 -> (1+0j) (-1-0j) -> 103 -> (-1+0j)
(2+0j) -> 2 -> (2+0j) (-2-0j) -> 102 -> (-2+0j)
(3+0j) -> 3 -> (3+0j) (-3-0j) -> 101 -> (-3+0j)
(4+0j) -> 10300 -> (4+0j) (-4-0j) -> 100 -> (-4+0j)
(5+0j) -> 10301 -> (5+0j) (-5-0j) -> 203 -> (-5+0j)
(6+0j) -> 10302 -> (6+0j) (-6-0j) -> 202 -> (-6+0j)
(7+0j) -> 10303 -> (7+0j) (-7-0j) -> 201 -> (-7+0j)
(8+0j) -> 10200 -> (8+0j) (-8-0j) -> 200 -> (-8+0j)
(9+0j) -> 10201 -> (9+0j) (-9-0j) -> 303 -> (-9+0j)
(10+0j) -> 10202 -> (10+0j) (-10-0j) -> 302 -> (-10+0j)
(11+0j) -> 10203 -> (11+0j) (-11-0j) -> 301 -> (-11+0j)
(12+0j) -> 10100 -> (12+0j) (-12-0j) -> 300 -> (-12+0j)
(13+0j) -> 10101 -> (13+0j) (-13-0j) -> 1030003 -> (-13+0j)
(14+0j) -> 10102 -> (14+0j) (-14-0j) -> 1030002 -> (-14+0j)
(15+0j) -> 10103 -> (15+0j) (-15-0j) -> 1030001 -> (-15+0j)
(16+0j) -> 10000 -> (16+0j) (-16-0j) -> 1030000 -> (-16+0j)

1j -> 10.2 -> 1j (-0-1j) -> 0.2 -> -1j
2j -> 10.0 -> 2j (-0-2j) -> 1030.0 -> -2j
3j -> 20.2 -> 3j (-0-3j) -> 1030.2 -> -3j
4j -> 20.0 -> 4j (-0-4j) -> 1020.0 -> -4j
5j -> 30.2 -> 5j (-0-5j) -> 1020.2 -> -5j
6j -> 30.0 -> 6j (-0-6j) -> 1010.0 -> -6j
7j -> 103000.2 -> 7j (-0-7j) -> 1010.2 -> -7j
8j -> 103000.0 -> 8j (-0-8j) -> 1000.0 -> -8j
9j -> 103010.2 -> 9j (-0-9j) -> 1000.2 -> -9j
10j -> 103010.0 -> 10j (-0-10j) -> 2030.0 -> -10j
11j -> 103020.2 -> 11j (-0-11j) -> 2030.2 -> -11j
12j -> 103020.0 -> 12j (-0-12j) -> 2020.0 -> -12j
13j -> 103030.2 -> 13j (-0-13j) -> 2020.2 -> -13j
14j -> 103030.0 -> 14j (-0-14j) -> 2010.0 -> -14j
15j -> 102000.2 -> 15j (-0-15j) -> 2010.2 -> -15j
16j -> 102000.0 -> 16j (-0-16j) -> 2000.0 -> -16j
done</pre>


=={{header|Sidef}}==
=={{header|Sidef}}==