Shunyaya Symbolic Mathematical Chemistry – Minimal Pseudocode (2.12)

This page provides a compact, dependency-free reference implementation of the core building blocks used throughout this series. Everything is bounded, collapse-safe, and plain ASCII. Use these helpers as the stable substrate for pooling, bond-combines, and downstream preference (RSI) and gating.

What’s included (and when to use it)

  • sign, clip — tiny utilities to keep behavior deterministic and bounded.
  • clamp_a, atanh_safe — enforce the strict open interval for alignment and provide a numerically safe rapidity inverse.
  • phi — collapse map to recover classical magnitudes exactly.
  • pool — mixes species by averaging in rapidity with stoichiometric weights and guards.
  • combine_M2 / combine_M1 — covalent (rapidity-additive) and ionic (multiplicative) alignment combines with clamps.

Implementation notes

  • Use double precision; apply clamp_a before every atanh_safe and after each combine.
  • Keep eps_a, eps_w consistent with your study manifest.
  • If species_list is empty, pool returns (0.0, 0.0) before clamping (outer tanh guards the division).
  • Determinism: fix seeds and threads at the application level if you rely on solver backends.

Plain ASCII pseudocode (copy-ready)

# sign helper (no +0 / -0 ambiguity)
sign(x):
  return 1 if x >= 0 else -1

# clip helper (bounded outputs)
clip(x, lo, hi):
  return min(max(x, lo), hi)

# alignment clamp (keeps |a| <= 1 - eps_a)
clamp_a(a, eps_a):
  if abs(a) >= 1 - eps_a:
    return sign(a) * (1 - eps_a)
  return a

# safe atanh in rapidity space (uses the same clamp policy)
atanh_safe(a, eps_a):
  ac = clamp_a(a, eps_a)
  return 0.5 * log((1 + ac) / (1 - ac))

# collapse map (recover classical magnitude)
phi(m, a):
  return m

# pooling (mix/average in rapidity space), returns a single pooled species
# species_list = [ (m, a), ... ], w = |m|^gamma with gamma >= 0
pool(species_list, gamma=1, eps_a=1e-6, eps_w=1e-12):
  U, W, m_sum = 0.0, 0.0, 0.0
  for (m, a) in species_list:
    u   = atanh_safe(a, eps_a)
    w   = (abs(m)) ** gamma
    U  += w * u
    W  += w
    m_sum += m
  a_pool = tanh( U / max(W, eps_w) )
  return (m_sum, clamp_a(a_pool, eps_a))

# covalent combine (M2): additive in rapidity
combine_M2(a1, a2, eps_a=1e-6):
  u1 = atanh_safe(a1, eps_a)
  u2 = atanh_safe(a2, eps_a)
  return clamp_a( tanh(u1 + u2), eps_a )

# ionic combine (M1): product in alignment, then clamp
combine_M1(a1, a2, eps_a=1e-6):
  a1c = clamp_a(a1, eps_a)
  a2c = clamp_a(a2, eps_a)
  return clamp_a( a1c * a2c, eps_a )

Result
These primitives are bounded and collapse-safe. They provide the base for pooling, bond-combines (M1/M2), and downstream use in preference (RSI) and condition gating (g_t).


Navigation
Previous – Normative Defaults (2.11)
Next – Reaction Stability Index — Definition (3, 3.1)