{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "646732e9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:34.829302Z",
     "iopub.status.busy": "2026-06-02T12:07:34.829141Z",
     "iopub.status.idle": "2026-06-02T12:07:37.432823Z",
     "shell.execute_reply": "2026-06-02T12:07:37.432029Z"
    }
   },
   "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": "412d4c47",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.435669Z",
     "iopub.status.busy": "2026-06-02T12:07:37.435148Z",
     "iopub.status.idle": "2026-06-02T12:07:37.444955Z",
     "shell.execute_reply": "2026-06-02T12:07:37.443712Z"
    }
   },
   "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": "1deb3971",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.447756Z",
     "iopub.status.busy": "2026-06-02T12:07:37.446867Z",
     "iopub.status.idle": "2026-06-02T12:07:37.458688Z",
     "shell.execute_reply": "2026-06-02T12:07:37.457455Z"
    }
   },
   "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": "feb1d506",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.461560Z",
     "iopub.status.busy": "2026-06-02T12:07:37.460658Z",
     "iopub.status.idle": "2026-06-02T12:07:37.466786Z",
     "shell.execute_reply": "2026-06-02T12:07:37.465586Z"
    }
   },
   "outputs": [],
   "source": [
    "mat.set_rhs(In1.from_idx, 1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "672f22a3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.469607Z",
     "iopub.status.busy": "2026-06-02T12:07:37.468715Z",
     "iopub.status.idle": "2026-06-02T12:07:37.482701Z",
     "shell.execute_reply": "2026-06-02T12:07:37.481448Z"
    }
   },
   "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": "a3e1b370",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.485602Z",
     "iopub.status.busy": "2026-06-02T12:07:37.484686Z",
     "iopub.status.idle": "2026-06-02T12:07:37.608226Z",
     "shell.execute_reply": "2026-06-02T12:07:37.607428Z"
    }
   },
   "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": "ddc48f13",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.610750Z",
     "iopub.status.busy": "2026-06-02T12:07:37.610232Z",
     "iopub.status.idle": "2026-06-02T12:07:37.623823Z",
     "shell.execute_reply": "2026-06-02T12:07:37.623064Z"
    }
   },
   "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": "d47e1236",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.626167Z",
     "iopub.status.busy": "2026-06-02T12:07:37.625919Z",
     "iopub.status.idle": "2026-06-02T12:07:37.959014Z",
     "shell.execute_reply": "2026-06-02T12:07:37.957453Z"
    }
   },
   "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": "50402240",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-06-02T12:07:37.961948Z",
     "iopub.status.busy": "2026-06-02T12:07:37.961111Z",
     "iopub.status.idle": "2026-06-02T12:07:38.000727Z",
     "shell.execute_reply": "2026-06-02T12:07:37.999450Z"
    }
   },
   "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}