# Changeset 6818

Ignore:
Timestamp:
Apr 16, 2009, 9:15:06 AM (15 years ago)
Message:

Numeric to numpy conversion.

Location:
branches/numpy_misc/tools/pytools
Files:
4 edited

Unmodified
Added
Removed
• ## branches/numpy_misc/tools/pytools/combinatorics.py

 r18 """ import Numeric import numpy as num def factorial(n): v = Numeric.arange(n)+1 return Numeric.multiply.reduce(v) v = num.arange(n)+1 return num.multiply.reduce(v) def binom (n,k): return(1) else: v = Numeric.arange(k) + n-k+1 #[n-k+1, ..., n-1, n] nom = Numeric.multiply.reduce(v) v = num.arange(k) + n-k+1 #[n-k+1, ..., n-1, n] nom = num.multiply.reduce(v) denom = factorial(k)
• ## branches/numpy_misc/tools/pytools/numtools.py

 r18 """ import types, Numeric import types import numpy as num if type(x) == Numeric.ArrayType: return Numeric.sum(x) elif type(x) in [types.ListType, types.TupleType]: if isinstance(x, num.ndarray): return num.sum(x) elif isinstance(x, (list, tuple)): s = x[0] for e in x[1:]: """ from Numeric import dot, reshape import numpy as num x = reshape(x, (A.shape[1], 1)) #Make x a column vector x = num.reshape(x, (A.shape[1], 1)) #Make x a column vector return dot(A, x) return num.dot(A, x) """ from Numeric import ones, arange, Float, multiply import numpy as num d = len(N) X = [] for s in range(d): local_shape = ones(d) local_shape = num.ones(d) local_shape[s] = N[s] a = arange(N[s]) a = num.arange(N[s]) if N[s] > 1: a = a.astype(Float)/(N[s]-1) a = a.astype(num.float)/(N[s]-1) else: a = a.astype(Float) a = a.astype(num.float) # Put ones in all other dimensions e.append(a) else: e.append(ones(N[t])) e.append(num.ones(N[t])) # Take kronecker product of all dimensions x = 1 for t in range(d): x=multiply.outer(x,e[t]) x=num.multiply.outer(x,e[t]) #print x, x.shape Number of ones in mask must equal len(x).""" from Numeric import sum, ArrayType, zeros import numpy as num assert type(x) == ArrayType assert type(mask) == ArrayType assert isinstance(x, num.ndarray) assert isinstance(mask, num.ndarray) #FIXME: Assert that mask contains only ones and zeros assert len(x) == sum(mask), 'Number of ones in mask must equal length of x' assert len(x) == num.sum(mask), 'Number of ones in mask must equal length of x' d = len(mask) y = zeros(d, x.typecode()) y = num.zeros(d, x.dtype) i = 0
• ## branches/numpy_misc/tools/pytools/stats.py

 r2017 def mean(x): import Numeric import numpy as num sum = Numeric.sum(x) sum = num.sum(x) avg = float(sum)/len(x) def cov(x,y=None): import Numeric import numpy as num if y is None: y = x cy = y - mean(y) p = Numeric.innerproduct(cx,cy) / N p = num.inner(cx,cy) / N """ import Numeric y = Numeric.ravel(x) p = Numeric.sqrt(Numeric.innerproduct(y,y)) import numpy as num y = num.ravel(x) p = num.sqrt(num.inner(y,y)) return p
• ## branches/numpy_misc/tools/pytools/test_pytools.py

 r18 import unittest import Numeric import numpy as num from combinatorics import bitvector from numtools import expand d = 4 assert Numeric.allclose(bitvector(0, d), Numeric.array([0,0,0,0])) assert Numeric.allclose(bitvector(1, d), Numeric.array([1,0,0,0])) assert Numeric.allclose(bitvector(2, d), Numeric.array([0,1,0,0])) assert Numeric.allclose(bitvector(3, d), Numeric.array([1,1,0,0])) assert Numeric.allclose(bitvector(4, d), Numeric.array([0,0,1,0])) assert Numeric.allclose(bitvector(5, d), Numeric.array([1,0,1,0])) assert Numeric.allclose(bitvector(6, d), Numeric.array([0,1,1,0])) assert Numeric.allclose(bitvector(7, d), Numeric.array([1,1,1,0])) assert Numeric.allclose(bitvector(8, d), Numeric.array([0,0,0,1])) assert Numeric.allclose(bitvector(9, d), Numeric.array([1,0,0,1])) assert Numeric.allclose(bitvector(10, d), Numeric.array([0,1,0,1])) assert Numeric.allclose(bitvector(11, d), Numeric.array([1,1,0,1])) assert Numeric.allclose(bitvector(12, d), Numeric.array([0,0,1,1])) assert Numeric.allclose(bitvector(13, d), Numeric.array([1,0,1,1])) assert Numeric.allclose(bitvector(14, d), Numeric.array([0,1,1,1])) assert Numeric.allclose(bitvector(15, d), Numeric.array([1,1,1,1])) assert num.allclose(bitvector(0, d), num.array([0,0,0,0])) assert num.allclose(bitvector(1, d), num.array([1,0,0,0])) assert num.allclose(bitvector(2, d), num.array([0,1,0,0])) assert num.allclose(bitvector(3, d), num.array([1,1,0,0])) assert num.allclose(bitvector(4, d), num.array([0,0,1,0])) assert num.allclose(bitvector(5, d), num.array([1,0,1,0])) assert num.allclose(bitvector(6, d), num.array([0,1,1,0])) assert num.allclose(bitvector(7, d), num.array([1,1,1,0])) assert num.allclose(bitvector(8, d), num.array([0,0,0,1])) assert num.allclose(bitvector(9, d), num.array([1,0,0,1])) assert num.allclose(bitvector(10, d), num.array([0,1,0,1])) assert num.allclose(bitvector(11, d), num.array([1,1,0,1])) assert num.allclose(bitvector(12, d), num.array([0,0,1,1])) assert num.allclose(bitvector(13, d), num.array([1,0,1,1])) assert num.allclose(bitvector(14, d), num.array([0,1,1,1])) assert num.allclose(bitvector(15, d), num.array([1,1,1,1])) d = 4 mask = Numeric.array([0,1,1,0,1]) v = Numeric.array([2,3,4]) mask = num.array([0,1,1,0,1]) v = num.array([2,3,4]) assert Numeric.allclose(expand(v, mask), Numeric.array([0,2,3,0,4])) assert num.allclose(expand(v, mask), num.array([0,2,3,0,4]))
Note: See TracChangeset for help on using the changeset viewer.