From 04548db1e0f49c66d22d55a82268288ff9da645b Mon Sep 17 00:00:00 2001 From: yodalee Date: Sun, 25 Nov 2018 20:09:42 +0800 Subject: [PATCH 1/2] add implementation of special math function function include: erf, erfc, gamma, lgamma use package statrs' implementation --- vm/Cargo.toml | 1 + vm/src/lib.rs | 1 + vm/src/stdlib/math.rs | 14 ++++++-------- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 84a95c5ffa..7e408c7788 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -15,3 +15,4 @@ serde_derive = "1.0.66" serde_json = "1.0.26" byteorder = "1.2.6" regex = "1" +statrs = "0.10.0" diff --git a/vm/src/lib.rs b/vm/src/lib.rs index f2f61fac73..cfcaf94339 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -15,6 +15,7 @@ extern crate num_complex; extern crate num_traits; extern crate serde; extern crate serde_json; +extern crate statrs; extern crate rustpython_parser; diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index 0ee716b9bd..496453b5ad 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -9,6 +9,8 @@ use super::super::pyobject::{ }; use super::super::VirtualMachine; use std; +use statrs::function::erf::{erf, erfc}; +use statrs::function::gamma::{gamma, ln_gamma}; // Helper macro: macro_rules! make_math_func { @@ -156,8 +158,7 @@ fn math_erf(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { if x.is_nan() { Ok(vm.ctx.new_float(x)) } else { - // TODO: implement algorithm - unimplemented!("TODO"); + Ok(vm.ctx.new_float(erf(x))) } } @@ -168,8 +169,7 @@ fn math_erfc(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { if x.is_nan() { Ok(vm.ctx.new_float(x)) } else { - // TODO: implement algorithm - unimplemented!("TODO"); + Ok(vm.ctx.new_float(erfc(x))) } } @@ -178,8 +178,7 @@ fn math_gamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let x = objfloat::get_value(value); if x.is_finite() { - // TODO: implement algorithm - unimplemented!("TODO"); + Ok(vm.ctx.new_float(gamma(x))) } else { if x.is_nan() || x.is_sign_positive() { Ok(vm.ctx.new_float(x)) @@ -194,8 +193,7 @@ fn math_lgamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let x = objfloat::get_value(value); if x.is_finite() { - // TODO: implement algorithm - unimplemented!("TODO"); + Ok(vm.ctx.new_float(ln_gamma(x))) } else { if x.is_nan() { Ok(vm.ctx.new_float(x)) From 68892fb4e202524cce6efdf53f9f6d59cbbf5a45 Mon Sep 17 00:00:00 2001 From: yodalee Date: Sun, 25 Nov 2018 22:13:26 +0800 Subject: [PATCH 2/2] fix rustfmt --- vm/src/stdlib/math.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index 496453b5ad..0cc800b129 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -8,9 +8,9 @@ use super::super::pyobject::{ DictProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol, }; use super::super::VirtualMachine; -use std; use statrs::function::erf::{erf, erfc}; use statrs::function::gamma::{gamma, ln_gamma}; +use std; // Helper macro: macro_rules! make_math_func {