Skip to content

Instantly share code, notes, and snippets.

@insertinterestingnamehere
Last active October 23, 2015 16:42
Show Gist options
  • Save insertinterestingnamehere/92da1c7571a70fee4a75 to your computer and use it in GitHub Desktop.
Save insertinterestingnamehere/92da1c7571a70fee4a75 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from dynd import nd, ndt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"@nd.functional.elwise\n",
"def add(a, b, c):\n",
" return a + b + c"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"nd.array([[17, 19, 21], [21, 23, 25]],\n",
" type=\"2 * 3 * int64\")"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = nd.array([[1, 2, 3],\n",
" [4, 5, 6]])\n",
"b = nd.array([[7],\n",
" [8]])\n",
"c = nd.array([[9, 10, 11]])\n",
"add(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"nd.array([ [18, 19], [21], [23, 24, 25]],\n",
" type=\"3 * var * int64\")"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = nd.array([[1, 2], [3], [4, 5, 6]])\n",
"b = nd.array([[7], [8], [9]])\n",
"c = nd.array(10)\n",
"add(a, b, c)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"nd.array(<callable at 0x10a3fa230>,\n",
" type=\"(Dims... * Scalar) -> Dims... * Scalar\")"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
"@nd.functional.apply\n",
"def f_factorial(n: ndt.int32) -> ndt.int32:\n",
" return math.factorial(n - 1)\n",
"\n",
"@nd.functional.apply\n",
"def f_gamma(x: ndt.float64) -> ndt.float64:\n",
" return math.gamma(x)\n",
"\n",
"f = nd.functional.elwise(nd.functional.multidispatch(\n",
" ndt.type('(Scalar) -> Scalar'), [f_factorial, f_gamma]))\n",
"\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"nd.array([49892.6, 50140.1, 49871.9, 50090.8, 50192.2, 50050.8, 49918.9, 50164.1,\n",
" 50107.8, 49888.9],\n",
" type=\"10 * float64\")"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@nd.functional.reduction\n",
"def mysum(a: ndt.float64, b: ndt.float64) -> ndt.float64:\n",
" return a + b\n",
"a = np.random.rand(10, 100000)\n",
"mysum(a, axes=(1,))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting myadd_cpp.cpp\n"
]
}
],
"source": [
"%%file myadd_cpp.cpp\n",
"#include \"dynd/func/callable.hpp\"\n",
"#include \"dynd/func/apply.hpp\"\n",
"#include \"dynd/func/elwise.hpp\"\n",
"\n",
"// Write a function to turn into a DyND callable.\n",
"double add_basic(double a, double b) {\n",
" return a + b;\n",
"}\n",
"\n",
"// Make the callable object.\n",
"dynd::nd::callable myadd =\n",
" dynd::nd::functional::elwise(\n",
" dynd::nd::functional::apply(&f));"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = nd.array([1, 2, 3], \"Fixed * uint64\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1.53125]\n"
]
}
],
"source": [
"def decasteljau(a, t):\n",
" for i in range(a.shape[0]-1):\n",
" a = (1-t) * a[:-1] + t * a[1:]\n",
" return a\n",
"\n",
"a = np.array([1, 2, 2, -1], 'd')\n",
"print(decasteljau(a, .25))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting decasteljau.cpp\n"
]
}
],
"source": [
"%%file decasteljau.cpp\n",
"#include <iostream>\n",
"#include <dynd/array.hpp>\n",
"\n",
"using namespace dynd;\n",
"\n",
"nd::array decasteljau(nd::array a, double t){\n",
" size_t e = a.get_dim_size();\n",
" for(size_t i=0; i < e-1; i++){\n",
" a = (1.-t) * a(irange()<(e-i-1)) + t * a(0<irange());}\n",
" return a(0);}\n",
"\n",
"int main(){\n",
" std::cout << decasteljau({1., 2., 2., -1.}, .25).as<double>();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['1.53125']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%system clang++ -std=c++14 -ldynd decasteljau.cpp -o decasteljau\n",
"%system ./decasteljau"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting myadd_2.cpp\n"
]
}
],
"source": [
"%%file myadd_2.cpp\n",
"#include <iostream>\n",
"#include <dynd/array.hpp>\n",
"#include <dynd/func/callable.hpp>\n",
"#include <dynd/func/apply.hpp>\n",
"#include <dynd/func/elwise.hpp>\n",
"\n",
"using namespace dynd;\n",
"\n",
"double myadd_cpp(double a, double b) {\n",
" return a + b;\n",
"}\n",
"\n",
"nd::callable myadd =\n",
" nd::functional::elwise(\n",
" nd::functional::apply(&myadd_cpp));\n",
"\n",
"int main() {\n",
" nd::array a = {1., 2., 3.};\n",
" nd::array b = {{1.}, {2.}};\n",
" std::cout << myadd(a, b);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['array([[2, 3, 4], [3, 4, 5]],', ' type=\"2 * 3 * float64\")']"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%system clang++ -std=c++14 -ldynd myadd_2.cpp -o myadd_2.o\n",
"%system ./myadd_2"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 3, 4],\n",
" [3, 4, 5]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([1, 2, 3])\n",
"b = np.array([[1], [2]])\n",
"a + b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment