<commit_before>{
 "metadata": {
  "name": "",
  "signature": "sha256:d5ea9241cf2b3c55ad26bba0f82ad5a5b269e83b332041691444a9827887f9c9"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Computational Physics \u2013\u00a0\u00dcbungsblatt 8\n",
      "## <NAME> \u2013\u00a0<NAME> \u2013\u00a0<NAME>\n",
      "### Hausaufgabe 14"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N = 10\n",
      "size = 2**N"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "H = np.zeros((size, size))\n",
      "for s in range(0, size):\n",
      "    s_bin = np.binary_repr(s, width=N)\n",
      "    for i in range(0, N):\n",
      "        b1 = s_bin[i]\n",
      "        b2 = s_bin[(i + 1) % N]\n",
      "        if b1 == b2:\n",
      "            H[s, s] += 1\n",
      "        else:\n",
      "            H[s, s] += -1\n",
      "            x = s ^ ((1 << i) | (1 << ((i + 1) % N)))  # swap bits at position i and i+1\n",
      "            H[s, x] += 2\n",
      "H *= -1. / 4"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "evalues, evectors = np.linalg.eig(H)\n",
      "\n",
      "# We expect to have only real eigenvalues, otherwise there's a problem\n",
      "Es = np.real_if_close(evalues)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def partition_function(E, T):\n",
      "    beta = 1. / T\n",
      "    return np.sum(np.exp(-beta * E))\n",
      "\n",
      "Ts = np.linspace(1, 10, 100)\n",
      "Zs = [partition_function(Es, T) for T in Ts]\n",
      "plt.plot(Ts, Zs)\n",
      "plt.xlabel('T')\n",
      "_ = plt.ylabel('Z')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def average_energy(E, T):\n",
      "    Z = partition_function(E, T)\n",
      "    beta = 1. / T\n",
      "    return 1. / Z * np.sum(E * np.exp(-beta * E))\n",
      "\n",
      "Us = [average_energy(Es, T) for T in Ts]\n",
      "plt.plot(Ts, Us)\n",
      "plt.xlabel('T')\n",
      "_ = plt.ylabel('U')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def heat_capacity(E, T, N):\n",
      "    return 1. / T ** 2 / N ** 2 * np.var(E)\n",
      "\n",
      "Cs = [heat_capacity(Es, T, N) for T in Ts]\n",
      "plt.plot(Ts, Cs)\n",
      "plt.xlabel('T')\n",
      "_ = plt.ylabel('C')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}<commit_msg>More bit-magic, also consistency.<commit_after>{
 "metadata": {
  "name": "",
  "signature": "sha256:247e5b25c2ec0213bdaaec19e8fa7b9108637cd23bff1471b3243473483ca5ee"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Computational Physics \u2013\u00a0\u00dcbungsblatt 8\n",
      "## <NAME> \u2013\u00a0<NAME> \u2013\u00a0<NAME>\n",
      "### Hausaufgabe 14"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "import matplotlib.pyplot as plt"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N = 2\n",
      "size = 2**N"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "H = np.zeros((size, size))\n",
      "for s in range(0, size):\n",
      "    for i in range(0, N):\n",
      "        j = (i + 1) % N\n",
      "        mask_i = 1 << i\n",
      "        mask_j = 1 << j\n",
      "        bit_i = bool(s & mask_i)\n",
      "        bit_j = bool(s & mask_j)\n",
      "      \n",
      "        if bit_i == bit_j:\n",
      "            H[s, s] += 1\n",
      "        else:\n",
      "            H[s, s] += -1\n",
      "            x = s ^ (mask_i | mask_j)  # swap bits at position i and i+1\n",
      "            H[s, x] += 2\n",
      "H *= -1. / 4"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "evalues, evectors = np.linalg.eig(H)\n",
      "\n",
      "# We expect to have only real eigenvalues, otherwise there's a problem\n",
      "Es = np.real_if_close(evalues)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def partition_function(E, T):\n",
      "    beta = 1. / T\n",
      "    return np.sum(np.exp(-beta * E))\n",
      "\n",
      "Ts = np.linspace(1, 10, 100)\n",
      "Zs = [partition_function(Es, T) for T in Ts]\n",
      "plt.plot(Ts, Zs)\n",
      "plt.xlabel('T')\n",
      "_ = plt.ylabel('Z')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def average_energy(E, T):\n",
      "    Z = partition_function(E, T)\n",
      "    beta = 1. / T\n",
      "    return 1. / Z * np.sum(E * np.exp(-beta * E))\n",
      "\n",
      "Us = [average_energy(Es, T) for T in Ts]\n",
      "plt.plot(Ts, Us)\n",
      "plt.xlabel('T')\n",
      "_ = plt.ylabel('U')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def heat_capacity(E, T, N):\n",
      "    return 1. / T ** 2 / N ** 2 * np.var(E)\n",
      "\n",
      "Cs = [heat_capacity(Es, T, N) for T in Ts]\n",
      "plt.plot(Ts, Cs)\n",
      "plt.xlabel('T')\n",
      "_ = plt.ylabel('C')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
{'fraction_non_alphanumeric': 0.22346838637319502, 'fraction_numerical': 0.027898499929903266, 'mean_word_length': 1.7022727272727274, 'pattern_counts': {'":': 124, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6247340', 'n_tokens_mistral': 2934, 'n_tokens_neox': 2774, 'n_words': 530}