Top 8

Outer WildsストーリーMODを色々やってみた
June 18, 2023, 10:04 a.m.
表面符号と戯れる【量子コンピューター Advent Calendar 2023 23 日目】
Dec. 23, 2023, 3:28 a.m.
位数発見アルゴリズム ~Quantum Zooやっていく【特別編】~
Jan. 27, 2023, 2:50 p.m.
ストーリー追加 Mod: The Outsider やっていく日記【Outer Wilds】
Feb. 19, 2023, 6:33 a.m.意識が量子効果で生じることを示す実験結果についてちょっと調べただけのメモ
April 21, 2022, 3:09 p.m.
ストーリー追加 MOD: Astral Codec やっていく日記【Outer Wilds】
Feb. 25, 2024, 8:47 a.m.Outer Wilds の量子は計算能力が(ある程度)すごいのではという話
Jan. 15, 2022, 8:35 a.m.
MacでAge of Empires 2 DE (AoE2DE)をCrossOverで動かす
May 31, 2021, 11:52 a.m.PyO3とrust-numpyを使ってPythonからNumPyをRustに渡して操作する
Feb. 22, 2019, 2:31 a.m. edited Dec. 21, 2019, 5:22 a.m.#NumPy #PyO3 #Rust #シミュレーション #Python
前回の記事でPyO3を使ってPythonで使えるライブラリを作りました.今回はPythonを使って科学計算をする上で非常によく使われるNumPyをRustに渡して操作します.そのためにPyO3に加えて使うライブラリがrust-numpy1です.
前回のプログラムを流用します.見ていない方はここから.
まず,Cargo.toml
に
[package]
name = "python_rust_example"
version = "0.1.0"
[lib]
name = "python_rust_example"
crate-type = ["cdylib"]
[dependencies]
numpy = "0.4.0"
ndarray = "0.12"
[dependencies.pyo3]
version = "0.5.2"
features = ["extension-module"]
とnumpy
とndarray
を導入します.前者はRustでNumPyを扱えるようにするライブラリ(rust-numpyのこと),後者はRustにおけるNumPyのような線形代数ライブラリです.
次に,src/lib.rs
に
#![feature(proc_macro)]
extern crate ndarray;
extern crate numpy;
extern crate pyo3;
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
use numpy::{IntoPyArray, PyArrayDyn};
use pyo3::prelude::*;
#[pymodinit]
fn python_rust_example(_py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "add_double")]
fn add_double_py(_py: Python, a: f64, b: f64) -> PyResult<f64> {
Ok(a + b)
}
#[pyfn(m, "add_numpy")]
fn add_numpy_py(py: Python, x: &PyArrayDyn<f64>, y: &PyArrayDyn<f64>) -> PyResult<Py<PyArrayDyn<f64>>> {
let x = x.as_array();
let y = y.as_array();
Ok((&x + &y).into_pyarray(py).to_owned())
}
#[pyfn(m, "mult_numpy")]
fn mult_numpy_py(_py: Python, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
let mut x = x.as_array_mut();
x *= a;
Ok(())
}
Ok(())
}
とadd_numpy()
とmult_numpy()
の2つをPythonから呼べるように新たに作ります.ここで,add_numpy()
は2つのNumPy配列を受け取ってその和を新しいNumPy配列で返す関数です.一方,mult_numpy()
は小数値とNumPy配列を受け取って,NumPy配列にその小数値を掛ける,つまり,配列自体の中身を書き換える関数です.
そして前回同様にビルドして,ライブラリをPython側で使えるようにtarget/release/
から配置します.それからPythonで,
>>> from python_rust_example import add_numpy, mult_numpy
>>> import numpy as np
>>> a = np.arange(10, dtype=np.float)
>>> b = np.arange(10, dtype=np.float) * 2
>>> a
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> b
array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18.])
>>>
>>> add_numpy(a, b)
array([ 0., 3., 6., 9., 12., 15., 18., 21., 24., 27.])
>>>
>>> mult_numpy(5, b)
>>> b
array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.])
と動作を確認します.なお,ちゃんとdtype=np.float
でないと型が違うというエラーが出るので気をつけましょう.
2019-10-28 追記
現在(rustc 1.40.0-nightly (95f437b3c 2019-10-27)
)はこちらが動きました.(上述したものでも動くとは思いますが,一応最新のレポジトリのサンプルと合わせた)
[dependencies]
numpy = "0.7.0"
ndarray = "0.12"
[dependencies.pyo3]
version = "0.8"
features = ["extension-module"]
extern crate ndarray;
extern crate numpy;
extern crate pyo3;
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
use numpy::{IntoPyArray, PyArrayDyn};
use pyo3::prelude::*;
#[pymodule]
fn edge_adjacency(_py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "add_double")]
fn add_double_py(_py: Python, a: f64, b: f64) -> PyResult<f64> {
Ok(a + b)
}
#[pyfn(m, "add_numpy")]
fn add_numpy_py(py: Python, x: &PyArrayDyn<f64>, y: &PyArrayDyn<f64>) -> PyResult<Py<PyArrayDyn<f64>>> {
let x = x.as_array();
let y = y.as_array();
Ok((&x + &y).into_pyarray(py).to_owned())
}
#[pyfn(m, "mult_numpy")]
fn mult_numpy_py(_py: Python, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
let mut x = x.as_array_mut();
x *= a;
Ok(())
}
Ok(())
}
-
rust-numpyは,以前 (v0.2.1) はrust-cpythonに沿っていたようですが,今はPyO3の方をメインでサポートしているようです ↩
Top 8

Outer WildsストーリーMODを色々やってみた
June 18, 2023, 10:04 a.m.
表面符号と戯れる【量子コンピューター Advent Calendar 2023 23 日目】
Dec. 23, 2023, 3:28 a.m.
位数発見アルゴリズム ~Quantum Zooやっていく【特別編】~
Jan. 27, 2023, 2:50 p.m.
ストーリー追加 Mod: The Outsider やっていく日記【Outer Wilds】
Feb. 19, 2023, 6:33 a.m.意識が量子効果で生じることを示す実験結果についてちょっと調べただけのメモ
April 21, 2022, 3:09 p.m.
ストーリー追加 MOD: Astral Codec やっていく日記【Outer Wilds】
Feb. 25, 2024, 8:47 a.m.Outer Wilds の量子は計算能力が(ある程度)すごいのではという話
Jan. 15, 2022, 8:35 a.m.
MacでAge of Empires 2 DE (AoE2DE)をCrossOverで動かす
May 31, 2021, 11:52 a.m.Tags
- #Python (26)
- #量子力学 (25)
- #量子情報 (23)
- #Outer Wilds (13)
- #Unity (11)
- #数学 (9)
- #Mac (9)
- #AoE2 (8)
- #Linux (7)
- #Bash (6)
- #Quantum Zoo (6)
- #意識 (5)
- #シミュレーション (5)
- #NumPy (5)
- #相対論 (4)
- #Docker (4)
- #Android (4)
- #Qiskit (4)
- #Rust (3)
- #PyO3 (3)
- #GitHub (3)
- #Django (2)
- #情報理論 (2)
- #LaTeX (2)
- #AR (2)
- #Git (2)
- #iOS (2)
- #C++ (2)
- #正規表現 (2)
- #論文 (2)
- #電磁気学 (1)
- #Google Drive (1)
- #Overleaf (1)
- #Let's Encrypt (1)
- #ポケモン (1)
- #AdMob (1)
- #Autoya (1)
- #docopt (1)
- #SymPy (1)
- #AWS (1)
- #Twitter (1)
- #URP (1)
- #iMovie (1)
- #PyTorch (1)
- #C# (1)
- #Vim (1)