{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.1 \u4f7f\u7528ctypes\u8bbf\u95eeC\u4ee3\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e9bC\u51fd\u6570\u5df2\u7ecf\u88ab\u7f16\u8bd1\u5230\u5171\u4eab\u5e93\u6216DLL\u4e2d\u3002\u4f60\u5e0c\u671b\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u4ee3\u7801\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\n\u800c\u4e0d\u7528\u7f16\u5199\u989d\u5916\u7684C\u4ee3\u7801\u6216\u4f7f\u7528\u7b2c\u4e09\u65b9\u6269\u5c55\u5de5\u5177\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u9700\u8981\u8c03\u7528C\u4ee3\u7801\u7684\u4e00\u4e9b\u5c0f\u7684\u95ee\u9898\uff0c\u901a\u5e38\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684 ctypes \u6a21\u5757\u5c31\u8db3\u591f\u4e86\u3002\n\u8981\u4f7f\u7528 ctypes \uff0c\u4f60\u9996\u5148\u8981\u786e\u4fdd\u4f60\u8981\u8bbf\u95ee\u7684C\u4ee3\u7801\u5df2\u7ecf\u88ab\u7f16\u8bd1\u5230\u548cPython\u89e3\u91ca\u5668\u517c\u5bb9\n\uff08\u540c\u6837\u7684\u67b6\u6784\u3001\u5b57\u5927\u5c0f\u3001\u7f16\u8bd1\u5668\u7b49\uff09\u7684\u67d0\u4e2a\u5171\u4eab\u5e93\u4e2d\u4e86\u3002\n\u4e3a\u4e86\u8fdb\u884c\u672c\u8282\u7684\u6f14\u793a\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5171\u4eab\u5e93\u540d\u5b57\u53eb libsample.so \uff0c\u91cc\u9762\u7684\u5185\u5bb9\u5c31\u662f15\u7ae0\u4ecb\u7ecd\u90e8\u5206\u90a3\u6837\u3002\n\u53e6\u5916\u8fd8\u5047\u8bbe\u8fd9\u4e2a libsample.so \u6587\u4ef6\u88ab\u653e\u7f6e\u5230\u4f4d\u4e8e sample.py \u6587\u4ef6\u76f8\u540c\u7684\u76ee\u5f55\u4e2d\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8bbf\u95ee\u8fd9\u4e2a\u51fd\u6570\u5e93\uff0c\u4f60\u8981\u5148\u6784\u5efa\u4e00\u4e2a\u5305\u88c5\u5b83\u7684Python\u6a21\u5757\uff0c\u5982\u4e0b\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# sample.py\nimport ctypes\nimport os\n\n# Try to locate the .so file in the same directory as this file\n_file = 'libsample.so'\n_path = os.path.join(*(os.path.split(__file__)[:-1] + (_file,)))\n_mod = ctypes.cdll.LoadLibrary(_path)\n\n# int gcd(int, int)\ngcd = _mod.gcd\ngcd.argtypes = (ctypes.c_int, ctypes.c_int)\ngcd.restype = ctypes.c_int\n\n# int in_mandel(double, double, int)\nin_mandel = _mod.in_mandel\nin_mandel.argtypes = (ctypes.c_double, ctypes.c_double, ctypes.c_int)\nin_mandel.restype = ctypes.c_int\n\n# int divide(int, int, int *)\n_divide = _mod.divide\n_divide.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int))\n_divide.restype = ctypes.c_int\n\ndef divide(x, y):\n    rem = ctypes.c_int()\n    quot = _divide(x, y, rem)\n\n    return quot,rem.value\n\n# void avg(double *, int n)\n# Define a special type for the 'double *' argument\nclass DoubleArrayType:\n    def from_param(self, param):\n        typename = type(param).__name__\n        if hasattr(self, 'from_' + typename):\n            return getattr(self, 'from_' + typename)(param)\n        elif isinstance(param, ctypes.Array):\n            return param\n        else:\n            raise TypeError(\"Can't convert %s\" % typename)\n\n    # Cast from array.array objects\n    def from_array(self, param):\n        if param.typecode != 'd':\n            raise TypeError('must be an array of doubles')\n        ptr, _ = param.buffer_info()\n        return ctypes.cast(ptr, ctypes.POINTER(ctypes.c_double))\n\n    # Cast from lists/tuples\n    def from_list(self, param):\n        val = ((ctypes.c_double)*len(param))(*param)\n        return val\n\n    from_tuple = from_list\n\n    # Cast from a numpy array\n    def from_ndarray(self, param):\n        return param.ctypes.data_as(ctypes.POINTER(ctypes.c_double))\n\nDoubleArray = DoubleArrayType()\n_avg = _mod.avg\n_avg.argtypes = (DoubleArray, ctypes.c_int)\n_avg.restype = ctypes.c_double\n\ndef avg(values):\n    return _avg(values, len(values))\n\n# struct Point { }\nclass Point(ctypes.Structure):\n    _fields_ = [('x', ctypes.c_double),\n                ('y', ctypes.c_double)]\n\n# double distance(Point *, Point *)\ndistance = _mod.distance\ndistance.argtypes = (ctypes.POINTER(Point), ctypes.POINTER(Point))\ndistance.restype = ctypes.c_double"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u5c31\u53ef\u4ee5\u52a0\u8f7d\u5e76\u4f7f\u7528\u91cc\u9762\u5b9a\u4e49\u7684C\u51fd\u6570\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sample\nsample.gcd(35,42)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.in_mandel(0,0,500)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.in_mandel(2.0,1.0,500)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.divide(42,8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.avg([1,2,3])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1 = sample.Point(1,2)\np2 = sample.Point(4,5)\nsample.distance(p1,p2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u5c0f\u8282\u6709\u5f88\u591a\u503c\u5f97\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u7684\u5730\u65b9\u3002\n\u9996\u5148\u662f\u5bf9\u4e8eC\u548cPython\u4ee3\u7801\u4e00\u8d77\u6253\u5305\u7684\u95ee\u9898\uff0c\u5982\u679c\u4f60\u5728\u4f7f\u7528 ctypes \u6765\u8bbf\u95ee\u7f16\u8bd1\u540e\u7684C\u4ee3\u7801\uff0c\n\u90a3\u4e48\u9700\u8981\u786e\u4fdd\u8fd9\u4e2a\u5171\u4eab\u5e93\u653e\u5728 sample.py \u6a21\u5757\u540c\u4e00\u4e2a\u5730\u65b9\u3002\n\u4e00\u79cd\u53ef\u80fd\u662f\u5c06\u751f\u6210\u7684 .so \u6587\u4ef6\u653e\u7f6e\u5728\u8981\u4f7f\u7528\u5b83\u7684Python\u4ee3\u7801\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\u3002\n\u6211\u4eec\u5728 recipe\u2014sample.py \u4e2d\u4f7f\u7528 __file__ \u53d8\u91cf\u6765\u67e5\u770b\u5b83\u88ab\u5b89\u88c5\u7684\u4f4d\u7f6e\uff0c\n\u7136\u540e\u6784\u9020\u4e00\u4e2a\u6307\u5411\u540c\u4e00\u4e2a\u76ee\u5f55\u4e2d\u7684 libsample.so \u6587\u4ef6\u7684\u8def\u5f84\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679cC\u51fd\u6570\u5e93\u88ab\u5b89\u88c5\u5230\u5176\u4ed6\u5730\u65b9\uff0c\u90a3\u4e48\u4f60\u5c31\u8981\u4fee\u6539\u76f8\u5e94\u7684\u8def\u5f84\u3002\n\u5982\u679cC\u51fd\u6570\u5e93\u5728\u4f60\u673a\u5668\u4e0a\u88ab\u5b89\u88c5\u4e3a\u4e00\u4e2a\u6807\u51c6\u5e93\u4e86\uff0c\n\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 ctypes.util.find_library() \u51fd\u6570\u6765\u67e5\u627e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from ctypes.util import find_library\nfind_library('m')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "find_library('pthread')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "find_library('sample')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u77e5\u9053\u4e86C\u51fd\u6570\u5e93\u7684\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 ctypes.cdll.LoadLibrary() \u6765\u52a0\u8f7d\u5b83\uff0c\n\u5176\u4e2d _path \u662f\u6807\u51c6\u5e93\u7684\u5168\u8def\u5f84\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "_mod = ctypes.cdll.LoadLibrary(_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u51fd\u6570\u5e93\u88ab\u52a0\u8f7d\u540e\uff0c\u4f60\u9700\u8981\u7f16\u5199\u51e0\u4e2a\u8bed\u53e5\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u7b26\u53f7\u5e76\u6307\u5b9a\u5b83\u4eec\u7684\u7c7b\u578b\u3002\n\u5c31\u50cf\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4e00\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# int in_mandel(double, double, int)\nin_mandel = _mod.in_mandel\nin_mandel.argtypes = (ctypes.c_double, ctypes.c_double, ctypes.c_int)\nin_mandel.restype = ctypes.c_int"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c.argtypes \u5c5e\u6027\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u5305\u542b\u4e86\u67d0\u4e2a\u51fd\u6570\u7684\u8f93\u5165\u6309\u65f6\uff0c\n\u800c .restype \u5c31\u662f\u76f8\u5e94\u7684\u8fd4\u56de\u7c7b\u578b\u3002\nctypes \u5b9a\u4e49\u4e86\u5927\u91cf\u7684\u7c7b\u578b\u5bf9\u8c61\uff08\u6bd4\u5982c_double, c_int, c_short, c_float\u7b49\uff09\uff0c\n\u4ee3\u8868\u4e86\u5bf9\u5e94\u7684C\u6570\u636e\u7c7b\u578b\u3002\u5982\u679c\u4f60\u60f3\u8ba9Python\u80fd\u591f\u4f20\u9012\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u5e76\u4e14\u6b63\u786e\u7684\u8f6c\u6362\u6570\u636e\u7684\u8bdd\uff0c\n\u90a3\u4e48\u8fd9\u4e9b\u7c7b\u578b\u7b7e\u540d\u7684\u7ed1\u5b9a\u662f\u5f88\u91cd\u8981\u7684\u4e00\u6b65\u3002\u5982\u679c\u4f60\u6ca1\u6709\u8fd9\u4e48\u505a\uff0c\u4e0d\u4f46\u4ee3\u7801\u4e0d\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\n\u8fd8\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6574\u4e2a\u89e3\u91ca\u5668\u8fdb\u7a0b\u6302\u6389\u3002\n\u4f7f\u7528ctypes\u6709\u4e00\u4e2a\u9ebb\u70e6\u70b9\u7684\u5730\u65b9\u662f\u539f\u751f\u7684C\u4ee3\u7801\u4f7f\u7528\u7684\u672f\u8bed\u53ef\u80fd\u8ddfPython\u4e0d\u80fd\u660e\u786e\u7684\u5bf9\u5e94\u4e0a\u6765\u3002\ndivide() \u51fd\u6570\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\uff0c\u5b83\u901a\u8fc7\u4e00\u4e2a\u53c2\u6570\u9664\u4ee5\u53e6\u4e00\u4e2a\u53c2\u6570\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u503c\u3002\n\u5c3d\u7ba1\u8fd9\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684C\u6280\u672f\uff0c\u4f46\u662f\u5728Python\u4e2d\u5374\u4e0d\u77e5\u9053\u600e\u6837\u6e05\u6670\u7684\u8868\u8fbe\u51fa\u6765\u3002\n\u4f8b\u5982\uff0c\u4f60\u4e0d\u80fd\u50cf\u4e0b\u9762\u8fd9\u6837\u7b80\u5355\u7684\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "divide = _mod.divide\ndivide.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int))\nx = 0\ndivide(10, 3, x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c31\u7b97\u8fd9\u4e2a\u80fd\u6b63\u786e\u7684\u5de5\u4f5c\uff0c\u5b83\u4f1a\u8fdd\u53cdPython\u5bf9\u4e8e\u6574\u6570\u7684\u4e0d\u53ef\u66f4\u6539\u539f\u5219\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6574\u4e2a\u89e3\u91ca\u5668\u9677\u5165\u4e00\u4e2a\u9ed1\u6d1e\u4e2d\u3002\n\u5bf9\u4e8e\u6d89\u53ca\u5230\u6307\u9488\u7684\u53c2\u6570\uff0c\u4f60\u901a\u5e38\u9700\u8981\u5148\u6784\u5efa\u4e00\u4e2a\u76f8\u5e94\u7684ctypes\u5bf9\u8c61\u5e76\u50cf\u4e0b\u9762\u8fd9\u6837\u4f20\u8fdb\u53bb\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = ctypes.c_int()\ndivide(10, 3, x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x.value"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\uff0c\u4e00\u4e2a ctypes.c_int \u5b9e\u4f8b\u88ab\u521b\u5efa\u5e76\u4f5c\u4e3a\u4e00\u4e2a\u6307\u9488\u88ab\u4f20\u8fdb\u53bb\u3002\n\u8ddf\u666e\u901aPython\u6574\u5f62\u4e0d\u540c\u7684\u662f\uff0c\u4e00\u4e2a c_int \u5bf9\u8c61\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\u3002\n.value \u5c5e\u6027\u53ef\u88ab\u7528\u6765\u83b7\u53d6\u6216\u66f4\u6539\u8fd9\u4e2a\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u50cfPython\u7684C\u8c03\u7528\uff0c\u901a\u5e38\u53ef\u4ee5\u5199\u4e00\u4e2a\u5c0f\u7684\u5305\u88c5\u51fd\u6570\u3002\n\u8fd9\u91cc\uff0c\u6211\u4eec\u8ba9 divide() \u51fd\u6570\u901a\u8fc7\u5143\u7ec4\u6765\u8fd4\u56de\u4e24\u4e2a\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# int divide(int, int, int *)\n_divide = _mod.divide\n_divide.argtypes = (ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int))\n_divide.restype = ctypes.c_int\n\ndef divide(x, y):\n    rem = ctypes.c_int()\n    quot = _divide(x,y,rem)\n    return quot, rem.value"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "avg() \u51fd\u6570\u53c8\u662f\u4e00\u4e2a\u65b0\u7684\u6311\u6218\u3002C\u4ee3\u7801\u671f\u671b\u63a5\u53d7\u5230\u4e00\u4e2a\u6307\u9488\u548c\u4e00\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u503c\u3002\n\u4f46\u662f\uff0c\u5728Python\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u8003\u8651\u8fd9\u4e2a\u95ee\u9898\uff1a\u6570\u7ec4\u662f\u5565\uff1f\u5b83\u662f\u4e00\u4e2a\u5217\u8868\uff1f\u4e00\u4e2a\u5143\u7ec4\uff1f\n\u8fd8\u662f array \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u6570\u7ec4\uff1f\u8fd8\u662f\u4e00\u4e2a numpy \u6570\u7ec4\uff1f\u8fd8\u662f\u8bf4\u6240\u6709\u90fd\u662f\uff1f\n\u5b9e\u9645\u4e0a\uff0c\u4e00\u4e2aPython\u201c\u6570\u7ec4\u201d\u6709\u591a\u79cd\u5f62\u5f0f\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u652f\u6301\u591a\u79cd\u53ef\u80fd\u6027\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "DoubleArrayType \u6f14\u793a\u4e86\u600e\u6837\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002\n\u5728\u8fd9\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u4e2a\u65b9\u6cd5 from_param() \u3002\n\u8fd9\u4e2a\u65b9\u6cd5\u7684\u89d2\u8272\u662f\u63a5\u53d7\u4e00\u4e2a\u5355\u4e2a\u53c2\u6570\u7136\u540e\u5c06\u5176\u5411\u4e0b\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5408\u9002\u7684ctypes\u5bf9\u8c61\n\uff08\u672c\u4f8b\u4e2d\u662f\u4e00\u4e2a ctypes.c_double \u7684\u6307\u9488\uff09\u3002\n\u5728 from_param() \u4e2d\uff0c\u4f60\u53ef\u4ee5\u505a\u4efb\u4f55\u4f60\u60f3\u505a\u7684\u4e8b\u3002\n\u53c2\u6570\u7684\u7c7b\u578b\u540d\u88ab\u63d0\u53d6\u51fa\u6765\u5e76\u88ab\u7528\u4e8e\u5206\u53d1\u5230\u4e00\u4e2a\u66f4\u5177\u4f53\u7684\u65b9\u6cd5\u4e2d\u53bb\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5217\u8868\u88ab\u4f20\u9012\u8fc7\u6765\uff0c\u90a3\u4e48 typename \u5c31\u662f list \uff0c\n\u7136\u540e from_list \u65b9\u6cd5\u88ab\u8c03\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5217\u8868\u548c\u5143\u7ec4\uff0cfrom_list \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a ctypes \u7684\u6570\u7ec4\u5bf9\u8c61\u3002\n\u8fd9\u4e2a\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\uff0c\u4e0b\u9762\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u4f8b\u5b50\u6765\u5c06\u4e00\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a ctypes \u6570\u7ec4\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "nums = [1, 2, 3]\na = (ctypes.c_double * len(nums))(*nums)\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a[2]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6570\u7ec4\u5bf9\u8c61\uff0cfrom_array() \u63d0\u53d6\u5e95\u5c42\u7684\u5185\u5b58\u6307\u9488\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a ctypes \u6307\u9488\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\na = array.array('d',[1,2,3])\na"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ptr_ = a.buffer_info()\nptr"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ctypes.cast(ptr, ctypes.POINTER(ctypes.c_double))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "from_ndarray() \u6f14\u793a\u4e86\u5bf9\u4e8e numpy \u6570\u7ec4\u7684\u8f6c\u6362\u64cd\u4f5c\u3002\n\u901a\u8fc7\u5b9a\u4e49 DoubleArrayType \u7c7b\u5e76\u5728 avg() \u7c7b\u578b\u7b7e\u540d\u4e2d\u4f7f\u7528\u5b83\uff0c\n\u90a3\u4e48\u8fd9\u4e2a\u51fd\u6570\u5c31\u80fd\u63a5\u53d7\u591a\u4e2a\u4e0d\u540c\u7684\u7c7b\u6570\u7ec4\u8f93\u5165\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sample\nsample.avg([1,2,3])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.avg((1,2,3))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\nsample.avg(array.array('d',[1,2,3]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy\nsample.avg(numpy.array([1.0,2.0,3.0]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6700\u540e\u4e00\u90e8\u5206\u5411\u4f60\u6f14\u793a\u4e86\u600e\u6837\u5904\u7406\u4e00\u4e2a\u7b80\u5355\u7684C\u7ed3\u6784\u3002\n\u5bf9\u4e8e\u7ed3\u6784\u4f53\uff0c\u4f60\u53ea\u9700\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u7b80\u5355\u7684\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5305\u542b\u76f8\u5e94\u7684\u5b57\u6bb5\u548c\u7c7b\u578b\u5373\u53ef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point(ctypes.Structure):\n    _fields_ = [('x', ctypes.c_double),\n                ('y', ctypes.c_double)]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u7c7b\u88ab\u5b9a\u4e49\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u7c7b\u578b\u7b7e\u540d\u4e2d\u6216\u8005\u662f\u9700\u8981\u5b9e\u4f8b\u5316\u7ed3\u6784\u4f53\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1 = sample.Point(1,2)\np2 = sample.Point(4,5)\np1.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1.y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.distance(p1,p2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e9b\u5c0f\u7684\u63d0\u793a\uff1a\u5982\u679c\u4f60\u60f3\u5728Python\u4e2d\u8bbf\u95ee\u4e00\u4e9b\u5c0f\u7684C\u51fd\u6570\uff0c\u90a3\u4e48 ctypes \u662f\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u51fd\u6570\u5e93\u3002\n\u5c3d\u7ba1\u5982\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8981\u53bb\u8bbf\u95ee\u4e00\u4e2a\u5f88\u5927\u7684\u5e93\uff0c\u90a3\u4e48\u53ef\u80fd\u5c31\u9700\u8981\u5176\u4ed6\u7684\u65b9\u6cd5\u4e86\uff0c\u6bd4\u5982 Swig (15.9\u8282\u4f1a\u8bb2\u5230) \u6216\nCython\uff0815.10\u8282\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5927\u578b\u5e93\u7684\u8bbf\u95ee\u6709\u4e2a\u4e3b\u8981\u95ee\u9898\uff0c\u7531\u4e8ectypes\u5e76\u4e0d\u662f\u5b8c\u5168\u81ea\u52a8\u5316\uff0c\n\u90a3\u4e48\u4f60\u5c31\u5fc5\u987b\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\u6765\u7f16\u5199\u6240\u6709\u7684\u7c7b\u578b\u7b7e\u540d\uff0c\u5c31\u50cf\u4f8b\u5b50\u4e2d\u90a3\u6837\u3002\n\u5982\u679c\u51fd\u6570\u5e93\u591f\u590d\u6742\uff0c\u4f60\u8fd8\u5f97\u53bb\u7f16\u5199\u5f88\u591a\u5c0f\u7684\u5305\u88c5\u51fd\u6570\u548c\u652f\u6301\u7c7b\u3002\n\u53e6\u5916\uff0c\u9664\u975e\u4f60\u5df2\u7ecf\u5b8c\u5168\u7cbe\u901a\u4e86\u6240\u6709\u5e95\u5c42\u7684C\u63a5\u53e3\u7ec6\u8282\uff0c\u5305\u62ec\u5185\u5b58\u5206\u914d\u548c\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\n\u901a\u5e38\u4e00\u4e2a\u5f88\u5c0f\u7684\u4ee3\u7801\u7f3a\u9677\u3001\u8bbf\u95ee\u8d8a\u754c\u6216\u5176\u4ed6\u7c7b\u4f3c\u9519\u8bef\u5c31\u80fd\u8ba9Python\u7a0b\u5e8f\u5954\u6e83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a ctypes \u7684\u4e00\u4e2a\u66ff\u4ee3\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8003\u8651\u4e0bCFFI\u3002CFFI\u63d0\u4f9b\u4e86\u5f88\u591a\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\n\u4f46\u662f\u4f7f\u7528C\u8bed\u6cd5\u5e76\u652f\u6301\u66f4\u591a\u9ad8\u7ea7\u7684C\u4ee3\u7801\u7c7b\u578b\u3002\n\u5230\u5199\u8fd9\u672c\u4e66\u4e3a\u6b62\uff0cCFFI\u8fd8\u662f\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u7684\u5de5\u7a0b\uff0c\n\u4f46\u662f\u5b83\u7684\u6d41\u884c\u5ea6\u6b63\u5728\u5feb\u901f\u4e0a\u5347\u3002\n\u751a\u81f3\u8fd8\u6709\u5728\u8ba8\u8bba\u5728Python\u5c06\u6765\u7684\u7248\u672c\u4e2d\u5c06\u5b83\u5305\u542b\u8fdb\u53bb\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u771f\u7684\u503c\u5f97\u4e00\u770b\u3002"
      ]
    }
  ],
  "metadata": {
    "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.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}