{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "024cdc8d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:11.667154Z",
     "iopub.status.busy": "2026-06-12T17:04:11.666914Z",
     "iopub.status.idle": "2026-06-12T17:04:14.509788Z",
     "shell.execute_reply": "2026-06-12T17:04:14.506549Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Matrix mirror_example: nnz=4 neqs=4\n",
      "    (col, row) = value\n",
      "    (0, 0) = (1+0j) : I_In1 mode=0 -> I_In1 mode=0\n",
      "    (1, 1) = (1+0j) : I_Out1 mode=0 -> I_Out1 mode=0\n",
      "    (2, 2) = (1+0j) : I_In2 mode=0 -> I_In2 mode=0\n",
      "    (3, 3) = (1+0j) : I_Out2 mode=0 -> I_Out2 mode=0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from finesse.cymath.cmatrix import KLUMatrix\n",
    "\n",
    "mat = KLUMatrix(name=\"mirror_example\")\n",
    "In1 = mat.declare_equations(Neqs=1, index=0, name=\"I_In1\", is_diagonal=True)\n",
    "Out1 = mat.declare_equations(Neqs=1, index=1, name=\"I_Out1\", is_diagonal=True)\n",
    "In2 = mat.declare_equations(Neqs=1, index=2, name=\"I_In2\", is_diagonal=True)\n",
    "Out2 = mat.declare_equations(Neqs=1, index=3, name=\"I_Out2\", is_diagonal=True)\n",
    "\n",
    "mat.construct()\n",
    "mat.print_matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4d963552",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.512727Z",
     "iopub.status.busy": "2026-06-12T17:04:14.512195Z",
     "iopub.status.idle": "2026-06-12T17:04:14.521859Z",
     "shell.execute_reply": "2026-06-12T17:04:14.520529Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Matrix mirror_example: nnz=8 neqs=4\n",
      "    (col, row) = value\n",
      "    (0, 0) = (1+0j) : I_In1 mode=0 -> I_In1 mode=0\n",
      "    (0, 1) = 0j     : I_In1 mode=0 -> I_Out1 mode=0\n",
      "    (0, 3) = 0j     : I_In1 mode=0 -> I_Out2 mode=0\n",
      "    (1, 1) = (1+0j) : I_Out1 mode=0 -> I_Out1 mode=0\n",
      "    (2, 1) = 0j     : I_In2 mode=0 -> I_Out1 mode=0\n",
      "    (2, 2) = (1+0j) : I_In2 mode=0 -> I_In2 mode=0\n",
      "    (2, 3) = 0j     : I_In2 mode=0 -> I_Out2 mode=0\n",
      "    (3, 3) = (1+0j) : I_Out2 mode=0 -> I_Out2 mode=0\n",
      "\n",
      "a11.shape=(1, 1)\n"
     ]
    }
   ],
   "source": [
    "a11 = mat.declare_submatrix_view(In1.from_idx, Out1.from_idx, \"_In1->Out1\", False)\n",
    "a22 = mat.declare_submatrix_view(In2.from_idx, Out2.from_idx, \"_In2->Out2\", False)\n",
    "\n",
    "a12 = mat.declare_submatrix_view(In1.from_idx, Out2.from_idx, \"_In1->Out2\", False)\n",
    "a21 = mat.declare_submatrix_view(In2.from_idx, Out1.from_idx, \"_In2->Out1\", False)\n",
    "\n",
    "mat.construct()\n",
    "mat.print_matrix()\n",
    "\n",
    "print(f\"{a11.shape=}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "83053ae1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.524151Z",
     "iopub.status.busy": "2026-06-12T17:04:14.523901Z",
     "iopub.status.idle": "2026-06-12T17:04:14.531815Z",
     "shell.execute_reply": "2026-06-12T17:04:14.530526Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Matrix mirror_example: nnz=8 neqs=4\n",
      "    (col, row) = value\n",
      "    (0, 0) = (1+0j)                    : I_In1 mode=0 -> I_In1 mode=0\n",
      "    (0, 1) = (-0.9486832980505138+0j)  : I_In1 mode=0 -> I_Out1 mode=0\n",
      "    (0, 3) = (-0-0.31622776601683794j) : I_In1 mode=0 -> I_Out2 mode=0\n",
      "    (1, 1) = (1+0j)                    : I_Out1 mode=0 -> I_Out1 mode=0\n",
      "    (2, 1) = (-0-0.31622776601683794j) : I_In2 mode=0 -> I_Out1 mode=0\n",
      "    (2, 2) = (1+0j)                    : I_In2 mode=0 -> I_In2 mode=0\n",
      "    (2, 3) = (-0.9486832980505138+0j)  : I_In2 mode=0 -> I_Out2 mode=0\n",
      "    (3, 3) = (1+0j)                    : I_Out2 mode=0 -> I_Out2 mode=0\n",
      "\n",
      "[[ 1. +0.j   0. +0.j   0. +0.j   0. +0.j ]\n",
      " [-0.9+0.j   1. +0.j   0. -0.3j  0. +0.j ]\n",
      " [ 0. +0.j   0. +0.j   1. +0.j   0. +0.j ]\n",
      " [ 0. -0.3j  0. +0.j  -0.9+0.j   1. +0.j ]]\n"
     ]
    }
   ],
   "source": [
    "r = np.sqrt(0.9)\n",
    "t = np.sqrt(0.1)\n",
    "\n",
    "a11[:] = r\n",
    "a22[:] = r\n",
    "a12[:] = 1j * t\n",
    "a21[:] = 1j * t\n",
    "\n",
    "mat.print_matrix()\n",
    "\n",
    "# print in dense format\n",
    "np.set_printoptions(precision=1, linewidth=250)\n",
    "print(mat.to_scipy_csc().todense())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "14a87c53",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.534053Z",
     "iopub.status.busy": "2026-06-12T17:04:14.533817Z",
     "iopub.status.idle": "2026-06-12T17:04:14.537601Z",
     "shell.execute_reply": "2026-06-12T17:04:14.536611Z"
    }
   },
   "outputs": [],
   "source": [
    "mat.set_rhs(In1.from_idx, 1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fbc21c8d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.540248Z",
     "iopub.status.busy": "2026-06-12T17:04:14.540019Z",
     "iopub.status.idle": "2026-06-12T17:04:14.544398Z",
     "shell.execute_reply": "2026-06-12T17:04:14.543527Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Vector mirror_example: neqs=4\n",
      "    (row) = value\n",
      "    (0) = (1+0j) : I_In1 mode=0\n",
      "    (1) = 0j     : I_Out1 mode=0\n",
      "    (2) = 0j     : I_In2 mode=0\n",
      "    (3) = 0j     : I_Out2 mode=0\n",
      "\n",
      "\n",
      "Vector mirror_example: neqs=4\n",
      "    (row) = value\n",
      "    (0) = (1+0j)                  : I_In1 mode=0\n",
      "    (1) = (0.9486832980505138+0j) : I_Out1 mode=0\n",
      "    (2) = 0j                      : I_In2 mode=0\n",
      "    (3) = 0.31622776601683794j    : I_Out2 mode=0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "mat.print_rhs()\n",
    "\n",
    "mat.factor()\n",
    "mat.solve()\n",
    "\n",
    "mat.print_rhs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a1134685",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.546453Z",
     "iopub.status.busy": "2026-06-12T17:04:14.546199Z",
     "iopub.status.idle": "2026-06-12T17:04:14.661026Z",
     "shell.execute_reply": "2026-06-12T17:04:14.659503Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Matrix carrier: nnz=12 neqs=6\n",
      "    (col, row) = value\n",
      "    (0, 0) = (1+0j)                   : I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0 -> I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0\n",
      "    (1, 1) = (1+0j)                   : I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0 -> I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0\n",
      "    (1, 2) = (-1-0j)                  : I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0 -> I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0\n",
      "    (2, 2) = (1+0j)                   : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0 -> I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0\n",
      "    (2, 3) = (-0.9486832980505138-0j) : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0 -> I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (2, 5) = -0.31622776601683794j    : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0 -> I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "    (3, 0) = (-1-0j)                  : I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0 -> I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0\n",
      "    (3, 3) = (1+0j)                   : I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0 -> I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (4, 3) = -0.31622776601683794j    : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0 -> I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (4, 4) = (1+0j)                   : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0 -> I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0\n",
      "    (4, 5) = (-0.9486832980505138+0j) : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0 -> I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "    (5, 5) = (1+0j)                   : I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0 -> I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "\n",
      "\n",
      "Matrix in dense format:\n",
      "\n",
      "[[ 1. +0.j   0. +0.j   0. +0.j  -1. +0.j   0. +0.j   0. +0.j ]\n",
      " [ 0. +0.j   1. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j ]\n",
      " [ 0. +0.j  -1. +0.j   1. +0.j   0. +0.j   0. +0.j   0. +0.j ]\n",
      " [ 0. +0.j   0. +0.j  -0.9+0.j   1. +0.j   0. -0.3j  0. +0.j ]\n",
      " [ 0. +0.j   0. +0.j   0. +0.j   0. +0.j   1. +0.j   0. +0.j ]\n",
      " [ 0. +0.j   0. +0.j   0. -0.3j  0. +0.j  -0.9+0.j   1. +0.j ]]\n",
      "\n",
      "Right hand side vector:\n",
      "\n",
      "Vector carrier: neqs=6\n",
      "    (row) = value\n",
      "    (0) = (1.3416407864998738+0j) : I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0\n",
      "    (1) = (1.4142135623730951+0j) : I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0\n",
      "    (2) = (1.4142135623730951+0j) : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0\n",
      "    (3) = (1.3416407864998738+0j) : I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (4) = 0j                      : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0\n",
      "    (5) = 0.447213595499958j      : I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import finesse\n",
    "import numpy as np\n",
    "\n",
    "model = finesse.Model()\n",
    "\n",
    "np.set_printoptions(linewidth=200, precision=1)\n",
    "\n",
    "model.parse(\n",
    "    \"\"\"\n",
    "        l l1\n",
    "        s s1 l1.p1 m1.p1\n",
    "        m m1 R=0.9 T=0.1\n",
    "    \"\"\"\n",
    ")\n",
    "model.run(simulation_options={\"debug_mode\": True});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a084dc91",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.663807Z",
     "iopub.status.busy": "2026-06-12T17:04:14.663224Z",
     "iopub.status.idle": "2026-06-12T17:04:14.679082Z",
     "shell.execute_reply": "2026-06-12T17:04:14.677689Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Matrix carrier: nnz=12 neqs=6\n",
      "    (col, row) = value\n",
      "    (0, 0) = (1+0j)                   : I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0 -> I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0\n",
      "    (1, 1) = (1+0j)                   : I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0 -> I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0\n",
      "    (1, 2) = (-1-0j)                  : I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0 -> I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0\n",
      "    (2, 2) = (1+0j)                   : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0 -> I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0\n",
      "    (2, 3) = (-0.9486832980505138-0j) : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0 -> I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (2, 5) = -0.31622776601683794j    : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0 -> I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "    (3, 0) = (-1-0j)                  : I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0 -> I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0\n",
      "    (3, 3) = (1+0j)                   : I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0 -> I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (4, 3) = -0.31622776601683794j    : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0 -> I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (4, 4) = (1+0j)                   : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0 -> I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0\n",
      "    (4, 5) = (-0.9486832980505138+0j) : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0 -> I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "    (5, 5) = (1+0j)                   : I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0 -> I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "\n",
      "\n",
      "Vector carrier: neqs=6\n",
      "    (row) = value\n",
      "    (0) = (1.3416407864998738+0j) : I,node=l1.p1.i,f=0,fidx=0,Neq=1 mode=0\n",
      "    (1) = (1.4142135623730951+0j) : I,node=l1.p1.o,f=0,fidx=1,Neq=1 mode=0\n",
      "    (2) = (1.4142135623730951+0j) : I,node=m1.p1.i,f=0,fidx=2,Neq=1 mode=0\n",
      "    (3) = (1.3416407864998738+0j) : I,node=m1.p1.o,f=0,fidx=3,Neq=1 mode=0\n",
      "    (4) = 0j                      : I,node=m1.p2.i,f=0,fidx=4,Neq=1 mode=0\n",
      "    (5) = 0.447213595499958j      : I,node=m1.p2.o,f=0,fidx=5,Neq=1 mode=0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with model.built() as sim:\n",
    "    carrier_solver = sim.carrier\n",
    "    klu_mat = carrier_solver.M()\n",
    "    klu_mat.print_matrix()\n",
    "    klu_mat.print_rhs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b962eb42",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.682096Z",
     "iopub.status.busy": "2026-06-12T17:04:14.681146Z",
     "iopub.status.idle": "2026-06-12T17:04:14.861352Z",
     "shell.execute_reply": "2026-06-12T17:04:14.859517Z"
    }
   },
   "outputs": [
    {
     "ename": "Exception",
     "evalue": "An error occurred in KLU during factoring: STATUS=1 (KLU_SINGULAR)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mException\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 14\u001b[39m\n\u001b[32m     10\u001b[39m \n\u001b[32m     11\u001b[39m a12[:] = \u001b[32m1\u001b[39m\n\u001b[32m     12\u001b[39m a21[:] = \u001b[32m1\u001b[39m\n\u001b[32m     13\u001b[39m \n\u001b[32m---> \u001b[39m\u001b[32m14\u001b[39m mat.factor()\n",
      "\u001b[36mFile \u001b[39m\u001b[32msrc/finesse/cymath/cmatrix.pyx:1741\u001b[39m, in \u001b[36mfinesse.cymath.cmatrix.KLUMatrix.factor\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m1741\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
      "\u001b[36mFile \u001b[39m\u001b[32msrc/finesse/cymath/cmatrix.pyx:1776\u001b[39m, in \u001b[36mfinesse.cymath.cmatrix.KLUMatrix.factor\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m1776\u001b[39m \u001b[33m'Could not get source, probably due dynamically evaluated source code.'\u001b[39m\n",
      "\u001b[31mException\u001b[39m: An error occurred in KLU during factoring: STATUS=1 (KLU_SINGULAR)\n"
     ]
    }
   ],
   "source": [
    "from finesse.cymath.cmatrix import KLUMatrix\n",
    "\n",
    "mat = KLUMatrix(name=\"singular_example\")\n",
    "In1 = mat.declare_equations(Neqs=1, index=0, name=\"I_In1\", is_diagonal=True)\n",
    "Out1 = mat.declare_equations(Neqs=1, index=1, name=\"I_Out1\", is_diagonal=True)\n",
    "a12 = mat.declare_submatrix_view(In1.from_idx, Out1.from_idx, \"_In1->Out1\", False)\n",
    "a21 = mat.declare_submatrix_view(Out1.from_idx, In1.from_idx, \"_Out1->In1\", False)\n",
    "\n",
    "mat.construct()\n",
    "\n",
    "a12[:] = 1\n",
    "a21[:] = 1\n",
    "\n",
    "mat.factor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "674638fc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-12T17:04:14.865105Z",
     "iopub.status.busy": "2026-06-12T17:04:14.864154Z",
     "iopub.status.idle": "2026-06-12T17:04:14.899978Z",
     "shell.execute_reply": "2026-06-12T17:04:14.898466Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l1.p1.i (0.447213595499958+0j)\n",
      "l1.p1.o (1.4142135623730951+0j)\n",
      "m1.p1.i (1.4142135623730951+0j)\n",
      "m1.p1.o (0.447213595499958+0j)\n",
      "m1.p2.i 0j\n",
      "m1.p2.o 1.3416407864998738j\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import finesse\n",
    "\n",
    "\n",
    "model = finesse.Model()\n",
    "\n",
    "model.parse(\n",
    "    \"\"\"\n",
    "    l l1 P=1\n",
    "    s s1 l1.p1 m1.p1\n",
    "    m m1 R=0.1 T=0.9\n",
    "    \"\"\"\n",
    ")\n",
    "with model.built() as sim:\n",
    "    sim.run_carrier()\n",
    "    for node, val in zip(\n",
    "        sim.carrier.nodes.keys(), np.array(sim.carrier.M().rhs_view[0, :]),\n",
    "        strict=True\n",
    "    ):\n",
    "        print(node, val)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}