Skip to content

Commit c0df55e

Browse files
committed
Merge pull request #30 from gifnksm/master
Add numeric functions and traits that provide numeric traits for generic mathematics.
2 parents aec7518 + 29011d1 commit c0df55e

File tree

8 files changed

+1047
-35
lines changed

8 files changed

+1047
-35
lines changed

benches/shootout-pidigits.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@
4141
extern crate num;
4242
extern crate test;
4343

44-
use std::from_str::FromStr;
45-
use std::num::One;
46-
use std::num::Zero;
44+
use std::str::FromStr;
4745
use std::num::FromPrimitive;
4846

4947
use test::Bencher;
5048

51-
use num::Integer;
52-
use num::bigint::BigInt;
49+
use num::{BigInt, Integer, One, Zero};
5350

5451
struct Context {
5552
numer: BigInt,

src/bigint.rs

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
//! ## Example
2020
//!
2121
//! ```rust
22-
//! use num::bigint::BigUint;
23-
//! use std::num::{Zero, One};
22+
//! use num::{BigUint, Zero, One};
2423
//! use std::mem::replace;
2524
//!
2625
//! // Calculate large fibonacci numbers.
@@ -61,14 +60,14 @@ use rand::Rng;
6160

6261
use std::{cmp, fmt, hash};
6362
use std::default::Default;
64-
use std::from_str::FromStr;
65-
use std::num::CheckedDiv;
66-
use std::num::{ToPrimitive, FromPrimitive};
67-
use std::num::{Zero, One, FromStrRadix};
68-
use std::str;
69-
use std::string::String;
63+
use std::iter::{AdditiveIterator, MultiplicativeIterator};
64+
use std::num::{Int, ToPrimitive, FromPrimitive};
65+
use std::num::FromStrRadix;
66+
use std::str::{mod, FromStr};
7067
use std::{i64, u64};
7168

69+
use {Num, Unsigned, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, Signed, Zero, One};
70+
7271
/// A `BigDigit` is a `BigUint`'s composing element.
7372
pub type BigDigit = u32;
7473

@@ -739,6 +738,20 @@ impl FromStrRadix for BigUint {
739738
}
740739
}
741740

741+
impl<T: Iterator<BigUint>> AdditiveIterator<BigUint> for T {
742+
fn sum(&mut self) -> BigUint {
743+
let init: BigUint = Zero::zero();
744+
self.fold(init, |acc, x| acc + x)
745+
}
746+
}
747+
748+
impl<T: Iterator<BigUint>> MultiplicativeIterator<BigUint> for T {
749+
fn product(&mut self) -> BigUint {
750+
let init: BigUint = One::one();
751+
self.fold(init, |acc, x| acc * x)
752+
}
753+
}
754+
742755
impl BigUint {
743756
/// Creates and initializes a `BigUint`.
744757
///
@@ -1084,7 +1097,6 @@ impl CheckedDiv for BigInt {
10841097
}
10851098
}
10861099

1087-
10881100
impl Integer for BigInt {
10891101
#[inline]
10901102
fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
@@ -1374,6 +1386,20 @@ impl<R: Rng> RandBigInt for R {
13741386
}
13751387
}
13761388

1389+
impl<T: Iterator<BigInt>> AdditiveIterator<BigInt> for T {
1390+
fn sum(&mut self) -> BigInt {
1391+
let init: BigInt = Zero::zero();
1392+
self.fold(init, |acc, x| acc + x)
1393+
}
1394+
}
1395+
1396+
impl<T: Iterator<BigInt>> MultiplicativeIterator<BigInt> for T {
1397+
fn product(&mut self) -> BigInt {
1398+
let init: BigInt = One::one();
1399+
self.fold(init, |acc, x| acc * x)
1400+
}
1401+
}
1402+
13771403
impl BigInt {
13781404
/// Creates and initializes a BigInt.
13791405
///
@@ -1416,6 +1442,29 @@ impl BigInt {
14161442
Minus => None
14171443
}
14181444
}
1445+
1446+
#[inline]
1447+
pub fn checked_add(&self, v: &BigInt) -> Option<BigInt> {
1448+
return Some(self.add(v));
1449+
}
1450+
1451+
#[inline]
1452+
pub fn checked_sub(&self, v: &BigInt) -> Option<BigInt> {
1453+
return Some(self.sub(v));
1454+
}
1455+
1456+
#[inline]
1457+
pub fn checked_mul(&self, v: &BigInt) -> Option<BigInt> {
1458+
return Some(self.mul(v));
1459+
}
1460+
1461+
#[inline]
1462+
pub fn checked_div(&self, v: &BigInt) -> Option<BigInt> {
1463+
if v.is_zero() {
1464+
return None;
1465+
}
1466+
return Some(self.div(v));
1467+
}
14191468
}
14201469

14211470
#[cfg(test)]
@@ -1425,15 +1474,16 @@ mod biguint_tests {
14251474
use super::{Plus, BigInt, RandBigInt, ToBigInt};
14261475

14271476
use std::cmp::{Less, Equal, Greater};
1428-
use std::from_str::FromStr;
1477+
use std::str::FromStr;
14291478
use std::i64;
1430-
use std::num::{Zero, One, FromStrRadix};
1479+
use std::num::FromStrRadix;
14311480
use std::num::{ToPrimitive, FromPrimitive};
1432-
use std::num::CheckedDiv;
14331481
use std::rand::task_rng;
14341482
use std::u64;
14351483
use std::hash::hash;
14361484

1485+
use {Zero, One, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
1486+
14371487
#[test]
14381488
fn test_from_slice() {
14391489
fn check(slice: &[BigDigit], data: &[BigDigit]) {
@@ -2289,13 +2339,14 @@ mod bigint_tests {
22892339

22902340
use std::cmp::{Less, Equal, Greater};
22912341
use std::i64;
2292-
use std::num::CheckedDiv;
2293-
use std::num::{Zero, One, FromStrRadix};
2342+
use std::num::FromStrRadix;
22942343
use std::num::{ToPrimitive, FromPrimitive};
22952344
use std::rand::task_rng;
22962345
use std::u64;
22972346
use std::hash::hash;
22982347

2348+
use {Zero, One, Signed};
2349+
22992350
#[test]
23002351
fn test_from_biguint() {
23012352
fn check(inp_s: Sign, inp_n: uint, ans_s: Sign, ans_n: uint) {
@@ -2882,7 +2933,9 @@ mod bench {
28822933
use super::BigUint;
28832934
use std::iter;
28842935
use std::mem::replace;
2885-
use std::num::{FromPrimitive, Zero, One};
2936+
use std::num::FromPrimitive;
2937+
2938+
use {Zero, One};
28862939

28872940
fn factorial(n: uint) -> BigUint {
28882941
let mut f: BigUint = One::one();

src/complex.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
//! Complex numbers.
1313
1414
use std::fmt;
15-
use std::num::{Zero, One};
15+
use std::num::FloatMath;
16+
use std::iter::{AdditiveIterator, MultiplicativeIterator};
17+
18+
use {Zero, One, Num};
1619

1720
// FIXME #1284: handle complex NaN & infinity etc. This
1821
// probably doesn't map to C's _Complex correctly.
@@ -80,7 +83,7 @@ impl<T: Clone + FloatMath> Complex<T> {
8083
}
8184
}
8285

83-
impl<T: Clone + FloatMath> Complex<T> {
86+
impl<T: Clone + FloatMath + Num> Complex<T> {
8487
/// Calculate the principal Arg of self.
8588
#[inline]
8689
pub fn arg(&self) -> T {
@@ -172,14 +175,30 @@ impl<T: fmt::Show + Num + PartialOrd> fmt::Show for Complex<T> {
172175
}
173176
}
174177

178+
impl<A: Clone + Num, T: Iterator<Complex<A>>> AdditiveIterator<Complex<A>> for T {
179+
fn sum(&mut self) -> Complex<A> {
180+
let init: Complex<A> = Zero::zero();
181+
self.fold(init, |acc, x| acc + x)
182+
}
183+
}
184+
185+
impl<A: Clone + Num, T: Iterator<Complex<A>>> MultiplicativeIterator<Complex<A>> for T {
186+
fn product(&mut self) -> Complex<A> {
187+
let init: Complex<A> = One::one();
188+
self.fold(init, |acc, x| acc * x)
189+
}
190+
}
191+
175192
#[cfg(test)]
176193
mod test {
177194
#![allow(non_upper_case_globals)]
178195

179196
use super::{Complex64, Complex};
180-
use std::num::{Zero, One, Float};
197+
use std::num::Float;
181198
use std::hash::hash;
182199

200+
use {Zero, One};
201+
183202
pub const _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 };
184203
pub const _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 };
185204
pub const _1_1i : Complex64 = Complex { re: 1.0, im: 1.0 };
@@ -280,7 +299,7 @@ mod test {
280299

281300
mod arith {
282301
use super::{_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i, _05_05i, all_consts};
283-
use std::num::Zero;
302+
use Zero;
284303

285304
#[test]
286305
fn test_add() {

src/integer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
//! Integer trait and functions.
1212
13+
use {Num, Signed};
14+
1315
pub trait Integer: Num + PartialOrd
1416
+ Div<Self, Self>
1517
+ Rem<Self, Self> {

0 commit comments

Comments
 (0)