DEV Community

Cover image for B13 Fractal Phase Library (BASE = 3120)
MORC
MORC

Posted on

B13 Fractal Phase Library (BASE = 3120)

        carry = 0
    out[i] = s

return pack_u64_digits(out), carry
Enter fullscreen mode Exit fullscreen mode

"""

evaluator_proto.py

Prototype evaluator for multi-digit phase -> (cos, sin) integer vector.

Important:

  • The phase representation (digits) is exact.
  • This evaluator is experimental and intentionally labeled "proto".

What it does:

  • Uses Level0 table from digit-0 as a base vector.
  • Applies lower digits as small rotation-like corrections.

This is useful for demonstrations and pipeline shaping, but not guaranteed to be
a mathematically exact refinement of angle.

If/when you want a "production evaluator", replace this module.
"""

from future import annotations
from typing import List, Tuple
from .constants import BASE
from .level0_table import COS_TABLE, SIN_TABLE

def fractal_cos_sin_proto(digits: List[int]) -> Tuple[int, int]:
"""
digits: MSD->LSD radix-BASE digits (length >= 1)
returns: (cx, cy) scaled roughly by BASE
"""
if len(digits) < 1:
raise ValueError("digits must have length >= 1")
for v in digits:
if v < 0 or v >= BASE:
raise ValueError("digit out of range")

coarse = digits[0]
cx = COS_TABLE[coarse]
cy = SIN_TABLE[coarse]

# Rotation-like corrections (prototype)
for l in range(1, len(digits)):
    fine = digits[l]
    scale = BASE ** l
    new_cx = (cx * scale - cy * fine) // scale
    new_cy = (cy * scale + cx * fine) // scale
    cx, cy = new_cx, new_cy

return cx, cy
Enter fullscreen mode Exit fullscreen mode

"""

evaluator_proto.py

Prototype evaluator for multi-digit phase -> (cos, sin) integer vector.

Important:

  • The phase representation (digits) is exact.
  • This evaluator is experimental and intentionally labeled "proto".

What it does:

  • Uses Level0 table from digit-0 as a base vector.
  • Applies lower digits as small rotation-like corrections.

This is useful for demonstrations and pipeline shaping, but not guaranteed to be
a mathematically exact refinement of angle.

If/when you want a "production evaluator", replace this module.
"""

from future import annotations
from typing import List, Tuple
from .constants import BASE
from .level0_table import COS_TABLE, SIN_TABLE

def fractal_cos_sin_proto(digits: List[int]) -> Tuple[int, int]:
"""
digits: MSD->LSD radix-BASE digits (length >= 1)
returns: (cx, cy) scaled roughly by BASE
"""
if len(digits) < 1:
raise ValueError("digits must have length >= 1")
for v in digits:
if v < 0 or v >= BASE:
raise ValueError("digit out of range")

coarse = digits[0]
cx = COS_TABLE[coarse]
cy = SIN_TABLE[coarse]

# Rotation-like corrections (prototype)
for l in range(1, len(digits)):
    fine = digits[l]
    scale = BASE ** l
    new_cx = (cx * scale - cy * fine) // scale
    new_cy = (cy * scale + cx * fine) // scale
    cx, cy = new_cx, new_cy

return cx, cy
Enter fullscreen mode Exit fullscreen mode

from b13phase.phase_digits import digits_from_int, digits_to_int, digits_add, digits_inc
from b13phase.constants import BASE

def test_roundtrip():
n = 6
for x in [0, 1, 123, BASE-1, BASE, BASE+1, 123456789]:
d = digits_from_int(x, n)
assert digits_to_int(d) == x

def test_add_simple():
n = 4
a = digits_from_int(1000, n)
b = digits_from_int(2500, n)
s, carry = digits_add(a, b)
assert digits_to_int(s) == 3500
assert carry == 0

def test_add_with_carry():
n = 2
a = [0, BASE-1]
b = [0, 1]
s, carry = digits_add(a, b)
assert s == [1, 0]
assert carry == 0

def test_inc():
d = [0, BASE-1]
out, carry = digits_inc(d, 1)
assert out == [1, 0]
assert carry == 0

from b13phase.phase_packed_u64 import pack_u64_digits, unpack_u64_digits, add_u64_packed, U64_DIGITS
from b13phase.constants import BASE

def test_pack_unpack():
d = [1, 2, 3, 4, 5]
x = pack_u64_digits(d)
assert unpack_u64_digits(x) == d

def test_add_no_overflow():
a = pack_u64_digits([0, 0, 0, 0, 3000])
b = pack_u64_digits([0, 0, 0, 0, 500])
s, carry = add_u64_packed(a, b)
assert unpack_u64_digits(s) == [0, 0, 0, 1, 380] # 3500 = 1*3120 + 380
assert carry == 0

def test_add_overflow_out_of_5_digits():
# Maximum digits all BASE-1 plus 1 -> overflow carry out
a = pack_u64_digits([BASE-1]*U64_DIGITS)
b = pack_u64_digits([0, 0, 0, 0, 1])
s, carry = add_u64_packed(a, b)
assert unpack_u64_digits(s) == [0]*U64_DIGITS
assert carry == 1

Top comments (0)