{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import networkx as nx\n",
    "import time\n",
    "import math\n",
    "import pandas as pd\n",
    "import scipy as sp\n",
    "import plotly.express as px\n",
    "import plotly.graph_objs as go\n",
    "from scipy.sparse import *\n",
    "from scipy import linalg\n",
    "from scipy.sparse.linalg import norm\n",
    "from scipy.optimize import least_squares"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Algorithm 2 testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Arnoldi(A, v, m): #  defined ad algorithm 2 in the paper\n",
    "    beta = norm(v)\n",
    "    print(\"A\")\n",
    "    v = v/beta\n",
    "    print(\"B\")\n",
    "    h = sp.sparse.lil_matrix((m,m))\n",
    "    print(\"C\")\n",
    "\n",
    "    for j in range(m):\n",
    "        w = A.dot(v)\n",
    "        print(\"D\")\n",
    "        for i in range(j):\n",
    "            h[i,j] = v.T.dot(w)\n",
    "            print(\"E\")\n",
    "            w = w - h[i,j]*v[i]\n",
    "            print(\"F\")\n",
    "\n",
    "        h[j+1,j] = norm(w)\n",
    "        print(\"G\")\n",
    "\n",
    "        if h[j+1,j] == 0:\n",
    "            print(\"The algorithm didn't converge\")\n",
    "            m = j\n",
    "            v[m+1] = 0\n",
    "            break\n",
    "        else:\n",
    "            print(\"H\")\n",
    "            v[j+1] = w**h[j+1,j] # THIS IS WRONG, I DON'T KNOW HOW TO FIX IT. ERROR \" matrix is not square\"\n",
    "            print(\"I\")\n",
    "\n",
    "    return v, h, m, beta, j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Creating a small test case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = sp.sparse.rand(100,100, density=0.5, format='lil')\n",
    "v = sp.sparse.rand(100,1, density=0.5, format='lil')\n",
    "m = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v, h, m, beta, j = Arnoldi(A, v, m)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 64-bit",
   "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.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
