Changeset 6818 for branches/numpy_misc/tools/pytools
- Timestamp:
- Apr 16, 2009, 9:15:06 AM (16 years ago)
- Location:
- branches/numpy_misc/tools/pytools
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/numpy_misc/tools/pytools/combinatorics.py
r18 r6818 4 4 """ 5 5 6 import Numeric6 import numpy as num 7 7 8 8 def factorial(n): 9 v = Numeric.arange(n)+110 return Numeric.multiply.reduce(v)9 v = num.arange(n)+1 10 return num.multiply.reduce(v) 11 11 12 12 def binom (n,k): … … 19 19 return(1) 20 20 else: 21 v = Numeric.arange(k) + n-k+1 #[n-k+1, ..., n-1, n]22 nom = Numeric.multiply.reduce(v)21 v = num.arange(k) + n-k+1 #[n-k+1, ..., n-1, n] 22 nom = num.multiply.reduce(v) 23 23 denom = factorial(k) 24 24 -
branches/numpy_misc/tools/pytools/numtools.py
r18 r6818 13 13 """ 14 14 15 import types, Numeric 15 import types 16 import numpy as num 16 17 17 if type(x) == Numeric.ArrayType:18 return Numeric.sum(x)19 elif type(x) in [types.ListType, types.TupleType]:18 if isinstance(x, num.ndarray): 19 return num.sum(x) 20 elif isinstance(x, (list, tuple)): 20 21 s = x[0] 21 22 for e in x[1:]: … … 27 28 """ 28 29 29 from Numeric import dot, reshape30 import numpy as num 30 31 31 x = reshape(x, (A.shape[1], 1)) #Make x a column vector32 x = num.reshape(x, (A.shape[1], 1)) #Make x a column vector 32 33 33 return dot(A, x)34 return num.dot(A, x) 34 35 35 36 … … 49 50 """ 50 51 51 from Numeric import ones, arange, Float, multiply52 import numpy as num 52 53 53 54 d = len(N) … … 55 56 X = [] 56 57 for s in range(d): 57 local_shape = ones(d)58 local_shape = num.ones(d) 58 59 local_shape[s] = N[s] 59 a = arange(N[s])60 a = num.arange(N[s]) 60 61 if N[s] > 1: 61 a = a.astype( Float)/(N[s]-1)62 a = a.astype(num.float)/(N[s]-1) 62 63 else: 63 a = a.astype( Float)64 a = a.astype(num.float) 64 65 65 66 # Put ones in all other dimensions … … 70 71 e.append(a) 71 72 else: 72 e.append( ones(N[t]))73 e.append(num.ones(N[t])) 73 74 74 75 # Take kronecker product of all dimensions … … 76 77 x = 1 77 78 for t in range(d): 78 x= multiply.outer(x,e[t])79 x=num.multiply.outer(x,e[t]) 79 80 80 81 #print x, x.shape … … 89 90 Number of ones in mask must equal len(x).""" 90 91 91 from Numeric import sum, ArrayType, zeros92 import numpy as num 92 93 93 assert type(x) == ArrayType94 assert type(mask) == ArrayType94 assert isinstance(x, num.ndarray) 95 assert isinstance(mask, num.ndarray) 95 96 #FIXME: Assert that mask contains only ones and zeros 96 assert len(x) == sum(mask), 'Number of ones in mask must equal length of x'97 assert len(x) == num.sum(mask), 'Number of ones in mask must equal length of x' 97 98 98 99 d = len(mask) 99 y = zeros(d, x.typecode())100 y = num.zeros(d, x.dtype) 100 101 101 102 i = 0 -
branches/numpy_misc/tools/pytools/stats.py
r2017 r6818 10 10 11 11 def mean(x): 12 import Numeric12 import numpy as num 13 13 14 sum = Numeric.sum(x)14 sum = num.sum(x) 15 15 avg = float(sum)/len(x) 16 16 … … 19 19 20 20 def cov(x,y=None): 21 import Numeric21 import numpy as num 22 22 if y is None: 23 23 y = x … … 29 29 cy = y - mean(y) 30 30 31 p = Numeric.innerproduct(cx,cy) / N31 p = num.inner(cx,cy) / N 32 32 33 33 … … 65 65 """ 66 66 67 import Numeric68 y = Numeric.ravel(x)69 p = Numeric.sqrt(Numeric.innerproduct(y,y))67 import numpy as num 68 y = num.ravel(x) 69 p = num.sqrt(num.inner(y,y)) 70 70 return p 71 71 -
branches/numpy_misc/tools/pytools/test_pytools.py
r18 r6818 1 1 import unittest 2 import Numeric2 import numpy as num 3 3 from combinatorics import bitvector 4 4 from numtools import expand … … 14 14 15 15 d = 4 16 assert Numeric.allclose(bitvector(0, d), Numeric.array([0,0,0,0]))17 assert Numeric.allclose(bitvector(1, d), Numeric.array([1,0,0,0]))18 assert Numeric.allclose(bitvector(2, d), Numeric.array([0,1,0,0]))19 assert Numeric.allclose(bitvector(3, d), Numeric.array([1,1,0,0]))20 assert Numeric.allclose(bitvector(4, d), Numeric.array([0,0,1,0]))21 assert Numeric.allclose(bitvector(5, d), Numeric.array([1,0,1,0]))22 assert Numeric.allclose(bitvector(6, d), Numeric.array([0,1,1,0]))23 assert Numeric.allclose(bitvector(7, d), Numeric.array([1,1,1,0]))24 assert Numeric.allclose(bitvector(8, d), Numeric.array([0,0,0,1]))25 assert Numeric.allclose(bitvector(9, d), Numeric.array([1,0,0,1]))26 assert Numeric.allclose(bitvector(10, d), Numeric.array([0,1,0,1]))27 assert Numeric.allclose(bitvector(11, d), Numeric.array([1,1,0,1]))28 assert Numeric.allclose(bitvector(12, d), Numeric.array([0,0,1,1]))29 assert Numeric.allclose(bitvector(13, d), Numeric.array([1,0,1,1]))30 assert Numeric.allclose(bitvector(14, d), Numeric.array([0,1,1,1]))31 assert Numeric.allclose(bitvector(15, d), Numeric.array([1,1,1,1]))16 assert num.allclose(bitvector(0, d), num.array([0,0,0,0])) 17 assert num.allclose(bitvector(1, d), num.array([1,0,0,0])) 18 assert num.allclose(bitvector(2, d), num.array([0,1,0,0])) 19 assert num.allclose(bitvector(3, d), num.array([1,1,0,0])) 20 assert num.allclose(bitvector(4, d), num.array([0,0,1,0])) 21 assert num.allclose(bitvector(5, d), num.array([1,0,1,0])) 22 assert num.allclose(bitvector(6, d), num.array([0,1,1,0])) 23 assert num.allclose(bitvector(7, d), num.array([1,1,1,0])) 24 assert num.allclose(bitvector(8, d), num.array([0,0,0,1])) 25 assert num.allclose(bitvector(9, d), num.array([1,0,0,1])) 26 assert num.allclose(bitvector(10, d), num.array([0,1,0,1])) 27 assert num.allclose(bitvector(11, d), num.array([1,1,0,1])) 28 assert num.allclose(bitvector(12, d), num.array([0,0,1,1])) 29 assert num.allclose(bitvector(13, d), num.array([1,0,1,1])) 30 assert num.allclose(bitvector(14, d), num.array([0,1,1,1])) 31 assert num.allclose(bitvector(15, d), num.array([1,1,1,1])) 32 32 33 33 … … 35 35 36 36 d = 4 37 mask = Numeric.array([0,1,1,0,1])38 v = Numeric.array([2,3,4])37 mask = num.array([0,1,1,0,1]) 38 v = num.array([2,3,4]) 39 39 40 assert Numeric.allclose(expand(v, mask), Numeric.array([0,2,3,0,4]))40 assert num.allclose(expand(v, mask), num.array([0,2,3,0,4])) 41 41 42 42
Note: See TracChangeset
for help on using the changeset viewer.