{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dealing with matrices and vectors\n",
    "\n",
    "The following list gives an overview over the operators and basic constructions for working with matrices in `polymake`. Some of them also apply to vectors. Operators and functions are described in `C++`-syntax, but most of them can also be used in the interactive shell on `Perl` side, compare [release_docs/common](http://polymake.org/release_docs/2.12/common.html). \n",
    "\n",
    "\n",
    "### Operations\n",
    "\n",
    "#### C++\n",
    "\n",
    "    \n",
    "    const GenericMatrix operator- (const GenericMatrix&); \n",
    "    const GenericMatrix operator+ (const GenericMatrix&, const GenericMatrix&); \n",
    "    const GenericMatrix operator- (const GenericMatrix&, const GenericMatrix&); \n",
    "    const GenericMatrix operator* (const Scalar&, const GenericMatrix&); \n",
    "    const GenericMatrix operator* (const GenericMatrix&, const Scalar&); \n",
    "    const GenericMatrix operator/ (const GenericMatrix&, const Scalar&); \n",
    "    const GenericMatrix operator* (const GenericMatrix&, const GenericMatrix&); \n",
    "    const GenericMatrix operator* (const GenericMatrix&, const GenericVector&); \n",
    "    const GenericMatrix operator* (const GenericVector&, const GenericMatrix&);\n",
    "\n",
    "\n",
    "Do exactly what the mnemonics suggest. Every type combination is allowed, including mixing of sparse and dense vectors and matrices. ElementType of matrix and vector operands, and Scalar type can be different, provided the arithmetic operator with corresponding arguments exists.\n",
    "\n",
    " The dimensions of the operands must match.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    _Matrix& GenericMatrix::negate(); \n",
    "    _Matrix& GenericMatrix::operator+= (const GenericMatrix&); \n",
    "    _Matrix& GenericMatrix::operator-= (const GenericMatrix&); \n",
    "    _Matrix& GenericMatrix::operator*= (const Scalar&); \n",
    "    _Matrix& GenericMatrix::operator/= (const Scalar&);\n",
    "\n",
    "\n",
    "Corresponding assignment versions of the arithmetic operators.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    bool operator== (const GenericMatrix&, const GenericMatrix&); \n",
    "    bool operator!= (const GenericMatrix&, const GenericMatrix&); \n",
    "    bool operator< (const GenericMatrix&, const GenericMatrix&); \n",
    "    bool operator> (const GenericMatrix&, const GenericMatrix&); \n",
    "    bool operator<= (const GenericMatrix&, const GenericMatrix&); \n",
    "    bool operator>= (const GenericMatrix&, const GenericMatrix&);\n",
    "\n",
    "\n",
    "Compare two matrices lexicographically (iterating over elements in row order). The dimensions of the operands must match.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    GenericMatrix::operator bool() const; bool GenericMatrix::operator! () const;\n",
    "\n",
    "\n",
    "Look for a non-zero element. \n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#### Perl\n",
    "\n",
    "The operators given above in `C++`-syntax can be translated to `Perl` in the following way. Consider e.g. the operator\n",
    "\n",
    "    \n",
    "    const GenericMatrix operator+ (const GenericMatrix&, const GenericMatrix&);\n",
    "\n",
    "In `Perl` you can apply this operator via\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$m1=new Matrix(3,3); $m2=new Matrix(3,3);\n",
    "$m1+=$m2; # changing the variable $m1"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "or\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$m1=unit_matrix(4); $m2=3*unit_matrix(4);\n",
    "$m=$m1+$m2; print $m; print dense($m);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "All other operators are translated in a similar way.\n",
    "\n",
    "### Other constructions\n",
    "\n",
    "#### C++\n",
    "\n",
    "    \n",
    "    GenericMatrix GenericMatrix::minor(const RowIndexSet& row_indices, const ColIndexSet& column_indices);\n",
    "\n",
    "\n",
    "Select a matrix minor lying on the intersection of the given row and column subsets. The const variant of this method creates an immutable minor object.\n",
    "\n",
    " The indices must lie in the valid ranges.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    GenericVector GenericMatrix::diagonal (int i=0); \n",
    "    GenericVector GenericMatrix::anti_diagonal (int i=0);\n",
    "\n",
    "\n",
    "Select the diagonal or anti-diagonal of a matrix:\n",
    "\n",
    "*  i=0     the main diagonal (anti-diagonal)\n",
    "\n",
    "*  i>0     the i-th diagonal below the main\n",
    "\n",
    "*  i<0     the (-i)-th diagonal above the main\n",
    "The const variants of these methods create immutable slice objects.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    GenericMatrix T (GenericMatrix&);\n",
    "\n",
    "\n",
    "Transpose the matrix. The roles of Rows and Cols get swapped. (**Perl:** `transpose()`)\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    GenericMatrix operator/ (const GenericMatrix&, const GenericMatrix&); \n",
    "    GenericMatrix operator/ (const GenericVector&, const GenericMatrix&); \n",
    "    GenericMatrix operator/ (const GenericMatrix&, const GenericVector&); \n",
    "    GenericMatrix operator/ (const GenericVector&, const GenericVector&);\n",
    "\n",
    "\n",
    "Create a block matrix, virtually appending the rows of the second matrix after the last row of the first matrix. A vector argument is treated as a matrix with one row. Column dimensions of the operands must be equal.\n",
    "ElementType of the operands must be identical, otherwise you will get a rather cryptic message from the compiler.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    GenericMatrix operator| (const GenericMatrix&, const GenericMatrix&); \n",
    "    GenericMatrix operator| (const GenericVector&, const GenericMatrix&); \n",
    "    GenericMatrix operator| (const GenericMatrix&, const GenericVector&);\n",
    "\n",
    "\n",
    "Create a block matrix, virtually appending the columns of the second matrix after the last column of the first matrix. A vector argument is treated as a matrix with one column. Row dimensions of the operands must be equal.\n",
    "\n",
    " ElementType of the operands must be identical, otherwise you will get a rather cryptic message from the compiler.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    Matrix& Matrix::operator/= (const GenericMatrix&); \n",
    "    Matrix& Matrix::operator/= (const GenericVector&); \n",
    "    Matrix& Matrix::operator|= (const GenericMatrix&); \n",
    "    Matrix& Matrix::operator|= (const GenericVector&);\n",
    "\n",
    "\n",
    "Append rows or columns to the matrix. All constraints for the non-assigning operators apply here too.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    const GenericMatrix diag(const GenericMatrix&, const GenericMatrix&); \n",
    "    const GenericMatrix diag(const GenericMatrix&, const GenericVector&); \n",
    "    const GenericMatrix diag(const GenericVector&, const GenericMatrix&);\n",
    "\n",
    "\n",
    "Create a block-diagonal matrix. Vector arguments are treated as square diagonal matrices.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    GenericMatrix vector2row(const GenericVector& v); \n",
    "    GenericMatrix vector2col(const GenericVector& v);\n",
    "\n",
    "\n",
    "Disguise v as a matrix with 1 row (column).\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    const GenericMatrix diag(const GenericVector& v);\n",
    "\n",
    "\n",
    "Create a square diagonal matrix. The elements of v appear on the main diagonal.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    const GenericMatrix repeat_row(const GenericVector& v, int n); \n",
    "    const GenericMatrix repeat_col(const GenericVector& v, int n);\n",
    "\n",
    "\n",
    "Create a matrix with n rows (columns), each equal to v.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    const GenericMatrix same_element_matrix(const ElementType& x, int m, int n); \n",
    "    const GenericMatrix ones_matrix<ElementType>(int m, int n); \n",
    "    const GenericMatrix zero_matrix<ElementType>(int m, int n);\n",
    "\n",
    "\n",
    "Create a dense matrix with m rows and n columns, with all elements equal to x, 1, or 0, respectively. Note the obligatory explicit specification of the template parameter ElementType, where it cannot be deduced from the function arguments.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    const GenericMatrix same_element_sparse_matrix<ElementType>(const GenericIncidenceMatrix& M); \n",
    "    const GenericMatrix same_element_sparse_matrix(const GenericIncidenceMatrix& M, const ElementType& x);\n",
    "\n",
    "\n",
    "Create a sparse matrix with the same structure as M, with elements equal to x in true cells and 0 in false cells.\n",
    "If omitted, x is taken equal to 1; note the obligatory explicit template parameter specification in this case.\n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "    const GenericMatrix unit_matrix<ElementType>(int n);\n",
    "\n",
    "\n",
    "Create a unit matrix n×n. \n",
    "\n",
    "\n",
    " \n",
    "\n",
    "\n",
    "\n",
    "#### Perl\n",
    "\n",
    "Many of the `C++`-functions also exist on `Perl` side (under the same name), compare [release_docs/common](http://polymake.org/release_docs/2.12/common.html). To append rows or columns in `Perl`, you can also use the operators `/` and `|`:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$m1 = new Matrix(3,4); $v1 = new Vector(4);\n",
    "$m1_ext = $m1/$v1;\n",
    "$m2 = new Matrix(2,4);\n",
    "$m = $m1_ext / $m2; print $m;\n",
    "$zero_vec6 = ones_vector<Rational>(6);\n",
    "print $zero_vec6|$m;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pm",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}