Skip to content

Commit b8a3be7

Browse files
committed
Add primitive arg that recurses all the way down
1 parent fbcd1b3 commit b8a3be7

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Hyperoperators
22

33
[![Build Status](https://travis-ci.org/thoppe/python-hyperoperators.svg?branch=master)](https://travis-ci.org/thoppe/python-hyperoperators)
4-
4+
[![Coverage Status](https://coveralls.io/repos/github/thoppe/python-hyperoperators/badge.svg?branch=master)](https://coveralls.io/github/thoppe/python-hyperoperators?branch=master)
5+
56
`hyperop` is a small library for representing really, really, ridiculously large numbers in pure python. It does so using [hyperoperations](https://en.wikipedia.org/wiki/Hyperoperation).
67

78
+ Hyperoperation 0, `H0` is the [successor function](https://en.wikipedia.org/wiki/Successor_function), `H0(None, 4) = 5`

hyperop/hyperop.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,27 @@ def __call__(self,a,b):
2222

2323
class hyperop(object):
2424

25-
def __new__(cls,n):
25+
def __new__(cls,n,primitive=False):
2626
if n<0 or int(n) != n:
2727
raise ValueError(_errmsg_invalid_hyperop_n.format(n))
2828

2929
if n==0: return base_hyperop0()
30-
if n==1: return base_hyperop1()
31-
if n==2: return base_hyperop2()
32-
if n==3: return base_hyperop3()
30+
31+
if not primitive:
32+
if n==1: return base_hyperop1()
33+
if n==2: return base_hyperop2()
34+
if n==3: return base_hyperop3()
35+
3336
return object.__new__(cls)
3437

35-
def __init__(self, n):
38+
def __init__(self, n, primitive=False):
3639
'''
3740
Create a hyperoperator of order n.
41+
3842
n must be a non-negative integer.
43+
If primitive is True, evaluate everything from the successor
44+
function, otherwise hardcode in base cases all
45+
the way to exponentiation.
3946
4047
Hyperoperation 0, H0 is the successor function, H0(None, 4) = 5
4148
H1 is addition, H1(2,4) = 2 + (1+1+1+1) = 6
@@ -61,7 +68,6 @@ def _repeat(self,a,b):
6168

6269
# For successor to work properly the base case
6370
# needs to be incremented by one
64-
6571
if self.n == 1:
6672
yield a
6773

tests/hyperop_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
import unittest, itertools, math
22
from hyperop import hyperop
33

4-
testing_values = range(1,20)
4+
testing_values = range(1,15)
5+
6+
#def generic_evaulation(fA, fB):
7+
# for x,y in itertools.product(testing_values,repeat=2):
8+
# assert( fA(x,y) == fB(x,y) )
9+
10+
class PrimitiveSmallValues(unittest.TestCase):
11+
12+
def test_primitive_H1(self):
13+
H = hyperop(1,primitive=True)
14+
15+
for a,b in itertools.product(testing_values,repeat=2):
16+
assert( H(a,b) == a+b )
17+
18+
def test_primitive_H2(self):
19+
H = hyperop(2,primitive=True)
20+
for a,b in itertools.product(testing_values,repeat=2):
21+
assert( H(a,b) == a*b )
22+
23+
def test_primitive_H3(self):
24+
H = hyperop(3,primitive=True)
25+
for a,b in itertools.product(testing_values,repeat=2):
26+
assert( H(a,b) == a**b )
527

628
class SmallValues(unittest.TestCase):
729

0 commit comments

Comments
 (0)