{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  Save and load data in polymake\n",
    "\n",
    "In polymake there are different ways to save and load data depending on the type and the format of the data. We distinguish between `big' objects (Polytope, Matroid,...), complex data types (Set, Matrix, Array<Vector<Rational>>,...), and data from files in arbitrary formats.\n",
    "\n",
    "## Handling polymake objects\n",
    "\n",
    "Let us take this nice example object:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "$p = cube(3);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To store big objects use the command\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "save($p,\"myPolyObject.poly\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This will create a file in JSON format.\n",
    "An existing files will be silently overwritten.\n",
    "\n",
    "To load a big object from such a file use the command\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "$p=load(\"myPolyObject.poly\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you did not start `polymake` in the directory containing your object, it is necessary to add the relative or absolute path, e.g.\n",
    "```perl\n",
    "$p=load(\"MyFolder/myPolyObject.poly\");\n",
    "```\n",
    "TAB completion like in a usual UNIX shell supports you in navigating through the file system.\n",
    "\n",
    "\n",
    "**Note:** If you load a polymake object and compute new properties, these properties will automatically be added to the original file at the end of the session. You can suppress this with the command \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "$p->dont_save;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "called prior to leaving the session (but after the last computation with $p).\n",
    "\n",
    "\n",
    "If you want to store a collection of objects into a single file, there is an [extra tutorial](tarballs.ipynb) for you.\n",
    "## Handling complex data types\n",
    "\n",
    "Apart from big objects, you can also persistently store arbitrary data structures like matrices or graphs format via `save_data`, e.g.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "$s=new Set<Int>(1,2,3,4);\n",
    "save_data($s, \"mySet.poly\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To load data objects from such a file use the command\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "$s=load_data(\"mySet.poly\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In fact, the command pairs `load`/`save` and `load_data`/`save_data` are almost equivalent.\n",
    "The only difference is in filename handling: `load` can find a data file by trying to append file suffixes configured for the current application,\n",
    "while `save` by default overwrites the file the object was initially loaded from.\n",
    "`load_data` and `save_data` always require full file name and do not try to guess anything.\n",
    "\n",
    "\n",
    "## Pretty formatting of JSON files\n",
    "\n",
    "Files produced by `save` and `save_data` are very dense, they don't contain line breaks and other redundant whitespaces.\n",
    "The order of properties in a big object is random, they can be arbitrarily reshuffled with every new `save` operation.\n",
    "If you are going to keep your data files under version control like git, or just want to have a look into such file, create it with an option `canonical`:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "save($p,\"myPrettyObject.poly\",canonical=>true);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving visualized objects\n",
    "\n",
    "Furthermore, most visualization methods provide an option to save the visualized object in a suitable format. Consult the [F1 help](https://polymake.org/doku.php/user_guide/intro_tutorial#getting_help) for information on the file format and further options.\n",
    "\n",
    "To save the cube visualized via JReality in a new file called `mycube.bsh`, do this:\n",
    "\n",
    "```perl\n",
    "jreality(cube(3)->VISUAL,File=>\"mycube\");\n",
    "```\n",
    "\n",
    "To save the cube as a TiKz file named `mycube.tikz` that you can e.g. import in a LaTeX document, do this instead:\n",
    "\n",
    "```perl\n",
    "tikz(cube(3)->VISUAL,File=>\"mycube\");\n",
    "```\n",
    "\n",
    "## Handling arbitrary files\n",
    "\n",
    "Of course, it is also possible to load data from files in other formats. For this purpose use the standard Perl functions for reading and writing. Here is an example:\n",
    "\n",
    " Assume you want to load some points stored in the file points.txt which looks like this:\n",
    "    1 0 0 0\n",
    "    1 1 0 0\n",
    "    1 0 1 0\n",
    "    1 1 1 0\n",
    "    1 0 0 1\n",
    "    1 1 0 1\n",
    "    1 0 1 1\n",
    "    1 1 1 1\n",
    "For the sake of the example, let's create this file:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "open(my $f, \">\", \"points.txt\"); print $f \"1 0 0 0\\n1 1 0 0\\n1 0 1 0\\n1 1 1 0\\n1 0 0 1\\n1 1 0 1\\n1 0 1 1\\n1 1 1 1\\n\"; close $f;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To read this file try the following:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 0 0\n",
       "1 1 0 0\n",
       "1 0 1 0\n",
       "1 1 1 0\n",
       "1 0 0 1\n",
       "1 1 0 1\n",
       "1 0 1 1\n",
       "1 1 1 1\n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "open(INPUT, \"<\", \"points.txt\");\n",
    "while(<INPUT>){\n",
    "  print $_;\n",
    "}\n",
    "close(INPUT);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "`<INPUT>` is a perl input iterator reading the file line by line.  Variable `$_` refers to the current line within this loop; it has a plain string value.\n",
    "\n",
    " A reasonable task could be to store the points from the file as a matrix.  This can be done immediately, because the matrix constructor called with a list of values interprets each value as a matrix line:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1 0 0 0\n",
       "1 1 0 0\n",
       "1 0 1 0\n",
       "1 1 1 0\n",
       "1 0 0 1\n",
       "1 1 0 1\n",
       "1 0 1 1\n",
       "1 1 1 1\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "open(INPUT, \"<\", \"points.txt\");\n",
    "$matrix=new Matrix<Rational>(<INPUT>);\n",
    "close(INPUT);\n",
    "print $matrix;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pl",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
