{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7b6d6733-2932-4126-8a7e-84dca21ad7a0",
   "metadata": {},
   "source": [
    "# Broadcasting operations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ff4579-0699-42f2-93cf-62eaefbefc9e",
   "metadata": {},
   "source": [
    "## General principle\n",
    "\n",
    "If you have an $(m, n)$ matrix $A$ and apply operations like sub(-), add(+), mul(*) or div(/) by a vector $v$ the shape of $v$ will be grow vertically or horizontally like:\n",
    "\n",
    "$(1,n)$ -> $(m,n)$\n",
    "\n",
    "$(m,1)$ -> $(m,n)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36bec1be-7bfe-45ed-ab0c-c168ccce945d",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "\n",
    "1) First\n",
    "\n",
    "$v = [1,2,3,4], x = 100$\n",
    "\n",
    "$v + x = $ vector + escalar\n",
    "\n",
    "Python turns the 100 into a vector with the same shape of $v$, like:\n",
    "\n",
    "$v = [1,2,3,4] + [100, 100, 100, 100]$\n",
    "\n",
    "$v = [101, 102, 103, 104]$\n",
    "\n",
    "\n",
    "2) Second\n",
    "\n",
    "$v_1 = [[1,2,3],[4,5,6]]$ + $v_2 = [100, 200, 300]$\n",
    "\n",
    "$v_2$ will become like: $[[100, 200, 300], [100, 200, 300]]$\n",
    "\n",
    "3) Third\n",
    "\n",
    "$v_1 = [[1,2,3],[4,5,6]]$ + $v2 = [[100],[200]]$\n",
    "\n",
    "$v_2$ will become like: $[[100, 100, 100], [200, 200, 200]]$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d7a0fd7-4f80-4317-b073-22d9a1d36e68",
   "metadata": {},
   "source": [
    "## In practice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a3a78e87-8c23-45ee-8f3a-efae06f9d79b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "be8ae231-dd05-4712-a780-6c0611109de1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 56.    0.    4.4  68. ]\n",
      " [  1.2 104.   52.    8. ]\n",
      " [  1.8 135.   99.    0.9]]\n"
     ]
    }
   ],
   "source": [
    "A = np.array(\n",
    "    [[56.0, 0.0, 4.4, 68.0],\n",
    "    [1.2, 104.0, 52.0, 8.0],\n",
    "    [1.8, 135.0, 99.0, 0.9]]\n",
    ")\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8bc8b58f-0621-4e4a-a78c-9063690bc37e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 59. , 239. , 155.4,  76.9])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum = A.sum(axis = 0)\n",
    "sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "de094d9e-7a3d-4401-9773-ba565738cbb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.94915254, 0.        , 0.02831403, 0.88426528],\n",
       "       [0.02033898, 0.43514644, 0.33462033, 0.10403121],\n",
       "       [0.03050847, 0.56485356, 0.63706564, 0.01170351]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A/sum"
   ]
  }
 ],
 "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}