def test_asin(self): start = 0 diff = 0.00000001 for i in range(2): for e in env: set_posit_env(e[0], e[1]) b = Math.asin(Posit(start)) Posit(asin(float(Posit(start).get_value()))) start += diff
def test_log(self): start = 10 diff = 0.1 for i in range(100): for e in env: set_posit_env(e[0], e[1]) b = Math.log(Posit(start)) self.assertEqual(Posit(log(float(Posit(start).get_value()))), b) start += diff
def test_large_tan(self): start = -100 diff = 10 for i in range(100): for e in env: set_posit_env(e[0], e[1]) b = Math.tan(Posit(start)) self.assertEqual(Posit(tan(float(Posit(start).get_value()))), b) start += diff
def test_pow(self): i = 10.0 while i < 10: j = -10.0 while j < 10.0: for e in env: set_posit_env(e[0], e[1]) b = Posit(i)**Posit(j) self.assertEqual( Posit( float(Posit(i).get_value())**float( Posit(j).get_value())), b) j += 0.1 i += 0.1
def test_small_cast(self): ctx = shortcuts.get_context() ctx.load_libs(['../PySigmoid/posit-javascript/js/decimallookupv2.js']) start = 1.5 diff = 0.0001 for i in range(10000): res = eval(ctx.run_script('convertDToP("{}")'.format(str(start)))) for j in res: nbits = j["ps"] es = j["es"] set_posit_env(nbits, es) a, b = bin(Posit(start).number)[2:], bin(int(j["posit"], 2))[2:] self.assertEqual(a, b) start += diff
t = deepcopy(p) x = Posit(float(p)).get_value() y = p d = decacc(x, y) xx.append(float(p)) yy.append(float(d)) p += D("1") print(p) # Plot plt.plot(xx, yy, alpha=1) plt.xlabel('x') plt.ylabel('Decimal Accuracy of x when converting to Posit') plt.show() set_posit_env(8, 1) p = Posit() p.set_bit_pattern(p.maxpos) p = -p p = p.get_value() q = Posit() q.set_bit_pattern(q.maxpos), q = q.get_value() xx = [] yy = [] while p <= q: t = deepcopy(p) x = Posit(float(p)).get_value()
from PySigmoid import Posit as P, set_posit_env import numpy as np set_posit_env(64, 3) # input matrix reg = [ [0, 2, 3, 4], [3, 4, 5, 6], [1, 3, 2, 11], [3, 5, 3, 4] ] # functiont to cast a regular matrix to a posit matrix posify = lambda x: [list(map(P, e)) for e in x] pos = posify(reg) # convert to posit b = np.matrix(pos) a = np.matrix(pos) # addition print(b+b) print(a+a) # subtraction print(b-b) print(a-a) # multiplication print(b*b) print(a*a) # transpose print(np.transpose(b)) print(np.transpose(a))
import matplotlib.pyplot as plt from math import exp from copy import deepcopy as dc # 500 digits of precision getcontext().prec = 50 def decacc(x, y): if x == y: return D('inf') else: try: return -abs((x / y).log10()).log10() except: return 0 set_posit_env(8, 0) start = -100 end = 100 xx = [] yy = [] yy2 = [] def sigmoid(x): return 1 / (1 + exp(-x)) while start <= end: q = Posit(start) q = q.sigmoid() xx.append(start) yy.append(float(q))