{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MatrixVectorProductComp\n",
    "\n",
    "`MatrixVectorProductComp` performs a matrix-vector product.  It may be vectorized to provide the result at one or more points simultaneously.\n",
    "\n",
    "$$\n",
    "    \\bar{b}_i = \\left[ A_i \\right] \\bar{x}_i\n",
    "$$\n",
    "\n",
    "## MatrixVectorProductComp Options\n",
    "\n",
    "The default `vec_size` is 1, providing the matrix vector product of $a$ and $x$ at a single\n",
    "point.\n",
    "\n",
    "Other options for MatrixVectorProductComp allow the user to rename the input variables $a$ and $x$ and the output $b$, as well as specifying their units."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "om.show_options_table(\"openmdao.components.matrix_vector_product_comp.MatrixVectorProductComp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MatrixVectorProductComp Constructor\n",
    "\n",
    "The call signature for the `MatrixVectorProductComp` constructor is:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.matrix_vector_product_comp.MatrixVectorProductComp.__init__\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "## MatrixVectorProductComp Usage\n",
    "\n",
    "There are often situations when numerous products need to be computed, essentially in parallel.\n",
    "You can reduce the number of components required by having one `MatrixVectorProductComp` perform multiple operations.\n",
    "This is also convenient when the different operations have common inputs.\n",
    "\n",
    "The ``add_product`` method is used to create additional products after instantiation.\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.matrix_vector_product_comp.MatrixVectorProductComp.add_product\n",
    "       :noindex:\n",
    "```\n",
    "\n",
    "## MatrixVectorProductComp Example\n",
    "\n",
    "The following code demonstrates the use of the MatrixVectorProductComp, finding the product\n",
    "of a random 3x3 matrix `Mat` and a 3-vector `x` at 100 points simultaneously."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import openmdao.api as om\n",
    "\n",
    "nn = 2\n",
    "\n",
    "p = om.Problem()\n",
    "\n",
    "p.model.add_subsystem(name='mat_vec_product_comp',\n",
    "                      subsys=om.MatrixVectorProductComp(A_name='Mat', vec_size=nn,\n",
    "                                                        b_name='y', b_units='m',\n",
    "                                                        x_units='m'),\n",
    "                      promotes_inputs=['Mat', 'x'])\n",
    "\n",
    "p.setup()\n",
    "\n",
    "p.set_val('Mat', np.random.rand(nn, 3, 3))\n",
    "p.set_val('x', np.random.rand(nn, 3))\n",
    "\n",
    "p.run_model()\n",
    "\n",
    "print(p.get_val('mat_vec_product_comp.y', units='ft')[0, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[0, :],\n",
    "                  np.dot(p['Mat'][0, :, :], p['x'][0, :]) * 3.2808399,\n",
    "                  tolerance=1.0E-8)\n",
    "\n",
    "assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[1, :],\n",
    "                  np.dot(p['Mat'][1, :, :], p['x'][1, :]) * 3.2808399,\n",
    "                  tolerance=1.0E-8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MatrixVectorProductComp Example with Multiple Products\n",
    "\n",
    "When defining multiple products:\n",
    "\n",
    "- An input name in one call to `add_product` may not be an output name in another call, and vice-versa.\n",
    "- The units and shape of variables used across multiple products must be the same in each one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "nn = 2\n",
    "\n",
    "p = om.Problem()\n",
    "\n",
    "mvp = om.MatrixVectorProductComp(A_name='Mat', vec_size=nn,\n",
    "                                 b_name='y', b_units='m',\n",
    "                                 x_units='m')\n",
    "\n",
    "mvp.add_product(A_name='Mat', vec_size=nn,\n",
    "                b_name='z', b_units='m',\n",
    "                x_name='w', x_units='m')\n",
    "\n",
    "p.model.add_subsystem(name='mat_vec_product_comp',\n",
    "                      subsys=mvp,\n",
    "                      promotes_inputs=['Mat', 'x', 'w'])\n",
    "\n",
    "p.setup()\n",
    "\n",
    "p.set_val('Mat', np.random.rand(nn, 3, 3))\n",
    "p.set_val('x', np.random.rand(nn, 3))\n",
    "p.set_val('w', np.random.rand(nn, 3))\n",
    "\n",
    "p.run_model()\n",
    "\n",
    "print(p.get_val('mat_vec_product_comp.y', units='ft')[0, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(p.get_val('mat_vec_product_comp.y', units='ft')[1, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(p.get_val('mat_vec_product_comp.z', units='ft')[0, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(p.get_val('mat_vec_product_comp.z', units='ft')[1, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[0, :],\n",
    "                  np.dot(p['Mat'][0, :, :], p['x'][0, :]) * 3.2808399,\n",
    "                  tolerance=1.0E-8)\n",
    "\n",
    "assert_near_equal(p.get_val('mat_vec_product_comp.y', units='ft')[1, :],\n",
    "                  np.dot(p['Mat'][1, :, :], p['x'][1, :]) * 3.2808399,\n",
    "                  tolerance=1.0E-8)\n",
    "\n",
    "assert_near_equal(p.get_val('mat_vec_product_comp.z', units='ft')[0, :],\n",
    "                  np.dot(p['Mat'][0, :, :], p['w'][0, :]) * 3.2808399,\n",
    "                  tolerance=1.0E-8)\n",
    "\n",
    "assert_near_equal(p.get_val('mat_vec_product_comp.z', units='ft')[1, :],\n",
    "                  np.dot(p['Mat'][1, :, :], p['w'][1, :]) * 3.2808399,\n",
    "                  tolerance=1.0E-8)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "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.8.1"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
