{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "00a7e715",
   "metadata": {},
   "source": [
    "# DCT Laboratory — Volume I, Chapter 13\n",
    "## Unified Enterprise Transformation Architecture\n",
    "**Seed `26113`** · Companion to the chapter and AXIOM Module **AXIOM-13**\n",
    "\n",
    "Two instruments: the **5×5 interaction matrix** over the constituent\n",
    "architectures $(\\mathcal{A}_S, \\mathcal{A}_T, \\mathcal{A}_C, \\mathcal{A}_P,\n",
    "\\mathcal{A}_R)$, and **integrated dynamics** where a unified transformation\n",
    "operator strengthens ONE coupling at quarter 8 — and a 0.03 coupling change\n",
    "quadruples the steady state. Mirrored in `DCT_V1_Ch13_Lab.xlsx`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27d2fa4a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-13T23:04:02.384108Z",
     "iopub.status.busy": "2026-07-13T23:04:02.383911Z",
     "iopub.status.idle": "2026-07-13T23:04:02.893248Z",
     "shell.execute_reply": "2026-07-13T23:04:02.892115Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['figure.dpi']=110\n",
    "\n",
    "import numpy as np\n",
    "SEED = 26113\n",
    "ARCH = [\"A_S\",\"A_T\",\"A_C\",\"A_P\",\"A_R\"]\n",
    "M = np.array([\n",
    " [0.00,0.30,0.20,0.15,0.25],\n",
    " [0.35,0.00,0.25,0.20,0.15],\n",
    " [0.20,0.30,0.00,0.25,0.10],\n",
    " [0.15,0.25,0.35,0.00,0.10],\n",
    " [0.30,0.20,0.15,0.25,0.00]])\n",
    "\n",
    "# integrated (K, p) dynamics: K' = 0.90K + 0.20p + 4 ; p' = gK + 0.80p + 2\n",
    "G_PRE, G_POST, SWITCH, N = 0.06, 0.09, 8, 24\n",
    "K0, P0 = 40.0, 50.0\n",
    "\n",
    "def A_of(g): return np.array([[0.90,0.20],[g,0.80]])\n",
    "def steady(g):\n",
    "    return np.linalg.solve(np.eye(2)-A_of(g), np.array([4.0,2.0]))\n",
    "def path(n=N):\n",
    "    z = np.empty((n+1,2)); z[0]=(K0,P0)\n",
    "    for k in range(n):\n",
    "        g = G_PRE if k < SWITCH else G_POST\n",
    "        z[k+1] = A_of(g)@z[k] + np.array([4.0,2.0])\n",
    "    return z\n",
    "\n",
    "def reference_values():\n",
    "    sp, spo = steady(G_PRE), steady(G_POST)\n",
    "    z = path()\n",
    "    return {\n",
    "        \"max_entry_M\": round(float(M.max()),4),\n",
    "        \"max_rowsum_M\": round(float(M.sum(axis=1).max()),4),\n",
    "        \"rho_pre\": round(float(max(abs(np.linalg.eigvals(A_of(G_PRE))))),4),\n",
    "        \"rho_post\": round(float(max(abs(np.linalg.eigvals(A_of(G_POST))))),4),\n",
    "        \"K_ss_pre\": round(float(sp[0]),4), \"p_ss_pre\": round(float(sp[1]),4),\n",
    "        \"K_ss_post\": round(float(spo[0]),4), \"p_ss_post\": round(float(spo[1]),4),\n",
    "        \"K_24\": round(float(z[24,0]),4), \"p_24\": round(float(z[24,1]),4),\n",
    "    }\n",
    "if __name__ == \"__main__\":\n",
    "    [print(f\"{k:14s} {v}\") for k,v in reference_values().items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b2d1e11",
   "metadata": {},
   "source": [
    "## Panel 1 — The interaction matrix\n",
    "$M_{ij}$: the strength with which architecture $j$'s state enters architecture\n",
    "$i$'s evolution (Def.: Enterprise Interaction Matrix). Strongest single\n",
    "interaction: 0.35 (state → transformation, and capital → performance). Most\n",
    "*affected* layer by row sum: $\\mathcal{A}_T$ at 0.95 — Enterprise\n",
    "Transformation Simultaneously Affects All Architectures (Prop.), and is affected\n",
    "by all of them in return."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "251855b5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-13T23:04:02.895310Z",
     "iopub.status.busy": "2026-07-13T23:04:02.895016Z",
     "iopub.status.idle": "2026-07-13T23:04:03.147306Z",
     "shell.execute_reply": "2026-07-13T23:04:03.146179Z"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(6.4,5.2))\n",
    "im = ax.imshow(M, cmap=\"Greens\", vmin=0, vmax=0.4)\n",
    "ax.set_xticks(range(5), ARCH); ax.set_yticks(range(5), ARCH)\n",
    "for i in range(5):\n",
    "    for j in range(5):\n",
    "        if M[i,j]>0: ax.text(j,i,f\"{M[i,j]:.2f}\",ha=\"center\",va=\"center\",\n",
    "                             color=\"white\" if M[i,j]>=0.25 else \"#0B3D2E\", fontsize=10)\n",
    "ax.set(title=\"Interaction matrix M (row i ← column j), seed 26113\")\n",
    "plt.colorbar(im, shrink=.8); plt.tight_layout(); plt.show()\n",
    "print(\"row sums (how affected):\", dict(zip(ARCH, np.round(M.sum(axis=1),2))))\n",
    "print(\"col sums (how influential):\", dict(zip(ARCH, np.round(M.sum(axis=0),2))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdc4153f",
   "metadata": {},
   "source": [
    "## Panel 2 — Integrated dynamics and the unified operator\n",
    "Capital and performance coupled: $K' = 0.90K + 0.20p + 4$, $p' = gK + 0.80p + 2$.\n",
    "At quarter 8 a unified transformation operator raises $g$ from 0.06 to 0.09 — a\n",
    "capability program making capital more performance-productive. Consequence of a\n",
    "0.03 coupling change: steady state moves from **(150, 55) to (600, 280)** and\n",
    "$\\rho$ from 0.9704 to 0.9932. The Unified Enterprise Evolution Theorem's\n",
    "lesson: in coupled systems, interactions dominate levels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc2b723e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-13T23:04:03.149233Z",
     "iopub.status.busy": "2026-07-13T23:04:03.149023Z",
     "iopub.status.idle": "2026-07-13T23:04:03.460357Z",
     "shell.execute_reply": "2026-07-13T23:04:03.459113Z"
    }
   },
   "outputs": [],
   "source": [
    "z = path()\n",
    "sp, spo = steady(G_PRE), steady(G_POST)\n",
    "t = np.arange(N+1)\n",
    "fig, axes = plt.subplots(1,2, figsize=(10,4.0))\n",
    "for ax,(idx,nm,ss_pre,ss_post) in zip(axes,[(0,\"capital K\",sp[0],spo[0]),(1,\"performance p\",sp[1],spo[1])]):\n",
    "    ax.plot(t, z[:,idx], \"o-\", c=\"#C8A24B\", lw=2, ms=3.5)\n",
    "    ax.axvline(SWITCH, c=\"#8A8F8B\", ls=\":\", lw=1)\n",
    "    ax.axhline(ss_pre, c=\"#0B3D2E\", ls=\"--\", lw=1, label=f\"pre steady {ss_pre:.0f}\")\n",
    "    ax.set(xlabel=\"quarter\", title=nm); ax.legend(frameon=False, fontsize=9); ax.grid(alpha=.25)\n",
    "plt.tight_layout(); plt.show()\n",
    "print(f\"rho pre {max(abs(np.linalg.eigvals(A_of(G_PRE)))):.4f} → post {max(abs(np.linalg.eigvals(A_of(G_POST)))):.4f}\")\n",
    "print(f\"steady state pre ({sp[0]:.0f}, {sp[1]:.0f}) → post ({spo[0]:.0f}, {spo[1]:.0f})\")\n",
    "print(f\"path at q24: K = {z[24,0]:.4f}, p = {z[24,1]:.4f}  (converging toward the new attractor)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a8236e1",
   "metadata": {},
   "source": [
    "## Panel 3 — The consistency check\n",
    "The Unified Consistency Theorem demands declared architectural invariants hold\n",
    "along the integrated path. Here: the capital floor $K \\ge 25$ (a solvency\n",
    "invariant). Verified along all 25 quarters, pre- and post-operator — the\n",
    "transformation strengthened the system *without* breaching the invariant, which\n",
    "is exactly what the theorem licenses one to require of programs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c348ae38",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-13T23:04:03.462376Z",
     "iopub.status.busy": "2026-07-13T23:04:03.462173Z",
     "iopub.status.idle": "2026-07-13T23:04:03.468258Z",
     "shell.execute_reply": "2026-07-13T23:04:03.467296Z"
    }
   },
   "outputs": [],
   "source": [
    "z = path()\n",
    "viol = int((z[:,0] < 25).sum())\n",
    "print(f\"invariant K >= 25:  min K along path = {z[:,0].min():.4f}   violations = {viol}\")\n",
    "assert viol == 0\n",
    "print(\"invariant maintained — the unified operator is consistency-preserving here\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63c9b102",
   "metadata": {},
   "source": [
    "## Validation — agrees with `DCT_V1_Ch13_Lab.xlsx`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f8c9d6e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-13T23:04:03.470246Z",
     "iopub.status.busy": "2026-07-13T23:04:03.470019Z",
     "iopub.status.idle": "2026-07-13T23:04:03.477700Z",
     "shell.execute_reply": "2026-07-13T23:04:03.476793Z"
    }
   },
   "outputs": [],
   "source": [
    "ref = reference_values()\n",
    "expected = {\"max_entry_M\":0.35,\"max_rowsum_M\":0.95,\"rho_pre\":0.9704,\"rho_post\":0.9932,\n",
    " \"K_ss_pre\":150.0,\"p_ss_pre\":55.0,\"K_ss_post\":600.0,\"p_ss_post\":280.0,\n",
    " \"K_24\":133.7305,\"p_24\":62.7496}\n",
    "for k,v in expected.items():\n",
    "    assert abs(ref[k]-v)<5e-4, f\"MISMATCH {k}\"\n",
    "    print(f\"PASS  {k:14s} {ref[k]}\")\n",
    "print(\"\\nAll checkpoints agree — seed 26113.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec7bf07b",
   "metadata": {},
   "source": [
    "**Next**: Exercises 13.9–13.12 (Part C) sweep the coupling toward criticality; AXIOM-13's unified console animates all five layers at once. Solutions: IM Ch. 13."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
