{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7b08c48e",
   "metadata": {},
   "source": [
    "# Lab - NumPy Arrays and Matplotlib\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7eb8daa5",
   "metadata": {},
   "source": [
    "## Exercise 1.1\n",
    "\n",
    "Create an array of integers from $-3$ to $3$ included.\n",
    "Convert it to float32, then reshape it to a $2 \\times 3$ matrix\n",
    "(discard or adapt one element if needed).\n",
    "Verify dtype, shape and size.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a3b51c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def printTest(x, message):\n",
    "    print(message)\n",
    "    print(\"Number of dimensions:\", x.ndim)\n",
    "    print(x, \"\\n\")\n",
    "\n",
    "\n",
    "# Initial Array\n",
    "x = np.array([-3, -2, -1, 1, 2, 3], np.int32)\n",
    "printTest(x, \"Initial array\")\n",
    "\n",
    "# to float64\n",
    "x = np.array(x.astype(np.float64))\n",
    "printTest(x, \"After type change\")\n",
    "\n",
    "x = np.array(x.reshape(2, 3))\n",
    "printTest(x, \"After dimension change\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d00eae5e",
   "metadata": {},
   "source": [
    "## Exercise 1.3\n",
    "\n",
    "Create a $4 \\times 4$ array with `np.arange`. Slice the central\n",
    "$2 \\times 2$ block into C, set all entries of C to 999, and inspect the original array. Then force a\n",
    "copy with `C = C.copy()` and repeat. What changes?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4f5f0a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.arange(16).reshape(4, 4)\n",
    "print(\"Original array:\\n\", x)\n",
    "\n",
    "C = x[1:3, 1:3]\n",
    "C[:] = 999\n",
    "print(\"\\nAfter modifying C (view):\\n\", x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdf5600f",
   "metadata": {},
   "source": [
    "## Exercise 2.1\n",
    "\n",
    "Generate a random $5 \\times 5$ matrix of integers in the range $[-50, 50]$.\n",
    "\n",
    "- Compute the mean of each row\n",
    "- Compute the standard deviation of each column\n",
    "- Calculate the global mean and standard deviation\n",
    "- Normalize the matrix using the formula: $$\\text{normalized} = \\frac{x - \\text{mean}}{\\text{std}}$$\n",
    "- Verify that the normalized matrix has mean close to 0 and standard deviation close to 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5c61e87",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.random.randint(-50, 50, size=(5, 5))\n",
    "\n",
    "print(\"Original Matrix:\\n\", x)\n",
    "\n",
    "for i in range(0, 5):\n",
    "    print(f\"Row {i} mean: {x.mean(axis=1)}\")\n",
    "\n",
    "for i in range(0, 5):\n",
    "    print(f\"Column {i} standard deviation: {x.std(axis=0)}\")\n",
    "\n",
    "globalMean = x.mean()\n",
    "print(f\"\\nGlobal Mean: {globalMean}\")\n",
    "\n",
    "globalStd = x.std()\n",
    "print(f\"\\nGlobal Standard Deviation: {globalStd}\")\n",
    "\n",
    "normalized = (x - globalMean) / globalStd\n",
    "print(f\"\\nNormalized normal form: \", normalized)\n",
    "\n",
    "testMean = normalized.mean()\n",
    "print(f\"\\nNormalized mean: {testMean}\")\n",
    "\n",
    "testStandardDeviation = normalized.std()\n",
    "print(f\"\\nNormalized standard deviation: {testStandardDeviation}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac754aae",
   "metadata": {},
   "source": [
    "## Exercise 3.1\n",
    "\n",
    "Plot both $\\sin(x)$ and $\\cos(x)$ on the same axes from $0$ to $2\\pi$. Use different line\n",
    "styles or colors, add a legend, and a grid.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6c54327",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.linspace(0, 2 * np.pi, 100)\n",
    "sin_x = np.sin(x)\n",
    "cos_x = np.cos(x)\n",
    "\n",
    "plt.plot(x, sin_x, label=\"sin(x)\", color=\"blue\", linestyle=\"-\")\n",
    "plt.plot(x, cos_x, label=\"cos(x)\", color=\"orange\", linestyle=\"--\")\n",
    "plt.title(\"Plot of sin(x) and cos(x)\")\n",
    "plt.xlabel(\"x (radians)\")\n",
    "plt.ylabel(\"Function value\")\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66decdfe",
   "metadata": {},
   "source": [
    "## Exercise 3.3\n",
    "\n",
    "Generate 300 random points with coordinates $(x, y)$ uniformly sampled in $[-1, 1]^2$.\n",
    "Plot them with a scatter plot, then highlight in a different color the points inside the unit\n",
    "circle $x^2 + y^2 \\leq 1$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5ad45ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Generate 300 random points in the range [-1, 1] for both x and y\n",
    "num_points = 300\n",
    "x = np.random.uniform(-1, 1, num_points)\n",
    "y = np.random.uniform(-1, 1, num_points)\n",
    "\n",
    "\n",
    "def f(x, y):\n",
    "    return x**2 + y**2\n",
    "\n",
    "\n",
    "# Determine which points are inside the unit circle\n",
    "inside_circle = x**2 + y**2 <= 1\n",
    "\n",
    "# Create a scatter plot\n",
    "plt.figure(figsize=(8, 8))\n",
    "plt.scatter(x[inside_circle], y[inside_circle], color=\"blue\", label=\"Inside Circle\")\n",
    "plt.scatter(x[~inside_circle], y[~inside_circle], color=\"red\", label=\"Outside Circle\")\n",
    "\n",
    "# Draw the unit circle for reference\n",
    "circle = plt.Circle(\n",
    "    (0, 0), 1, color=\"green\", fill=False, linestyle=\"--\", label=\"Unit Circle\"\n",
    ")\n",
    "plt.gca().add_artist(circle)\n",
    "\n",
    "plt.title(\"Scatter Plot of Random Points with Unit Circle Highlighted\")\n",
    "plt.xlabel(\"x\")\n",
    "plt.ylabel(\"y\")\n",
    "plt.axhline(0, color=\"black\", linewidth=0.5, ls=\"--\")\n",
    "plt.axvline(0, color=\"black\", linewidth=0.5, ls=\"--\")\n",
    "plt.gca().set_aspect(\"equal\", adjustable=\"box\")\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  }
 ],
 "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
