- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
C
%%file fib.h double fib(int n);
Writing fib.h
%%file fib.c double fib(int n) { double a = 0, b = 1; for (int i=0; i<n; i++) { double tmp = b; b = a; a += tmp; } return a; }
Writing fib.c
Using bitey and clang
This is perhaps the simplest method, but it only works with the clang
compiler and does not geenrate highly optimized code.
import bitey
!clang -O3 -emit-llvm -c fib.c -o fib1.o
import fib1 fib1.fib(100)
354224848179261997056.0000
%timeit fib1.fib(100)
1000000 loops, best of 3: 941 ns per loop
Using Cython
I recomment using Cython for all your C/C++ interface needs as it is highly optimized and can do boht C \(\rightarrow\) Python and Python \(\rightarrow\) C. It is a littel more involved, but the steps always follow the same template.
Define functions to be imported from C
%%file fib.pxd cdef extern from "fib.h": double fib(int n)
Writing fib.pxd
Define wrapper for calling function from Python
%%file fib2.pyx cimport fib def fib(n): return fib.fib(n)
Writing fib2.pyx
Use distutils to compile shared library for Python
This is the standard way all Python modules are compiled for distribution, and results in a build that is portable over different platforms.
%%file setup.py from distutils.core import setup, Extension from Cython.Build import cythonize ext = Extension("fib2", sources=["fib2.pyx", "fib.c"]) setup(name = "cython_fib", ext_modules = cythonize(ext))
Overwriting setup.py
! python setup.py build_ext -i &> /dev/null
import fib2 fib2.fib(100)
354224848179261997056.0000
%timeit fib2.fib(100)
1000000 loops, best of 3: 224 ns per loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论