質問編集履歴

2

実行可能なコードにした

2021/03/01 09:24

投稿

Yhaya
Yhaya

スコア439

test CHANGED
File without changes
test CHANGED
@@ -8,10 +8,28 @@
8
8
 
9
9
  ```rust
10
10
 
11
+ use pyo3::prelude::*;
12
+
13
+ use pyo3::wrap_pyfunction;
14
+
15
+
16
+
11
17
  use num_traits::{NumAssign, NumCast};
12
18
 
13
19
 
14
20
 
21
+ #[pymodule]
22
+
23
+ fn pykk(_py: Python, m: &PyModule) -> PyResult<()> {
24
+
25
+ m.add_wrapped(wrap_pyfunction!(imag2real))?;
26
+
27
+ Ok(())
28
+
29
+ }
30
+
31
+
32
+
15
33
  #[pyfunction]
16
34
 
17
35
  fn imag2real<T>(x: Vec<T>, y: Vec<T>) -> PyResult<Vec<f64>>

1

追記

2021/03/01 09:24

投稿

Yhaya
Yhaya

スコア439

test CHANGED
File without changes
test CHANGED
@@ -127,3 +127,95 @@
127
127
 
128
128
 
129
129
  初歩的な質問かと思いますが、どうぞよろしくお願いいたします。
130
+
131
+
132
+
133
+ ## 追記
134
+
135
+
136
+
137
+ Cargo.toml
138
+
139
+
140
+
141
+ ```toml
142
+
143
+ [package]
144
+
145
+ authors = ["xxx xxx"]
146
+
147
+ edition = "2018"
148
+
149
+ name = "pykk"
150
+
151
+ version = "0.1.0"
152
+
153
+
154
+
155
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
156
+
157
+
158
+
159
+ [lib]
160
+
161
+ crate-type = ["cdylib"]
162
+
163
+ name = "pykk"
164
+
165
+
166
+
167
+ [dependencies.pyo3]
168
+
169
+ features = ["extension-module"]
170
+
171
+ version = "0.13.2"
172
+
173
+
174
+
175
+ [dependencies]
176
+
177
+ num-traits = "0.2.14"
178
+
179
+ ```
180
+
181
+
182
+
183
+ 関数 `integrate` の実装
184
+
185
+
186
+
187
+ ```rust
188
+
189
+ fn integrate<T>(x: &Vec<T>, y: &Vec<T>, num: usize) -> f64
190
+
191
+ where
192
+
193
+ T: NumAssign + NumCast + Copy,
194
+
195
+ {
196
+
197
+ let mut result = T::from(0.0).unwrap();
198
+
199
+ let diff = x[1] - x[0];
200
+
201
+
202
+
203
+ for i in 0..x.len() {
204
+
205
+ if i == num {
206
+
207
+ continue;
208
+
209
+ }
210
+
211
+ result += x[num] * y[i] / (x[i] * x[i] - x[num] * x[num]) * diff;
212
+
213
+ }
214
+
215
+
216
+
217
+ result.to_f64().unwrap()
218
+
219
+ }
220
+
221
+ ```