Changeset 6818


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

Numeric to numpy conversion.

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

Legend:

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

    r18 r6818  
    44""" 
    55       
    6 import Numeric
     6import numpy as num
    77
    88def factorial(n):
    9   v = Numeric.arange(n)+1
    10   return Numeric.multiply.reduce(v)
     9  v = num.arange(n)+1
     10  return num.multiply.reduce(v)
    1111
    1212def binom (n,k):
     
    1919    return(1)
    2020  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)
    2323    denom = factorial(k)
    2424   
  • branches/numpy_misc/tools/pytools/numtools.py

    r18 r6818  
    1313  """
    1414
    15   import types, Numeric
     15  import types
     16  import numpy as num
    1617 
    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)):
    2021    s = x[0]
    2122    for e in x[1:]:
     
    2728  """
    2829
    29   from Numeric import dot, reshape
     30  import numpy as num
    3031
    31   x = reshape(x, (A.shape[1], 1)) #Make x a column vector             
     32  x = num.reshape(x, (A.shape[1], 1)) #Make x a column vector             
    3233
    33   return dot(A, x)
     34  return num.dot(A, x)
    3435
    3536
     
    4950  """
    5051 
    51   from Numeric import ones, arange, Float, multiply
     52  import numpy as num
    5253
    5354  d = len(N)
     
    5556  X = []
    5657  for s in range(d):
    57     local_shape = ones(d)
     58    local_shape = num.ones(d)
    5859    local_shape[s] = N[s]
    59     a = arange(N[s])
     60    a = num.arange(N[s])
    6061    if N[s] > 1:
    61       a = a.astype(Float)/(N[s]-1)
     62      a = a.astype(num.float)/(N[s]-1)
    6263    else:
    63       a = a.astype(Float)
     64      a = a.astype(num.float)
    6465   
    6566    # Put ones in all other dimensions
     
    7071        e.append(a)
    7172      else:
    72         e.append(ones(N[t]))
     73        e.append(num.ones(N[t]))
    7374   
    7475    # Take kronecker product of all dimensions
     
    7677    x = 1 
    7778    for t in range(d):
    78       x=multiply.outer(x,e[t])
     79      x=num.multiply.outer(x,e[t])
    7980
    8081    #print x, x.shape
     
    8990     Number of ones in mask must equal len(x)."""
    9091
    91   from Numeric import sum, ArrayType, zeros
     92  import numpy as num
    9293
    93   assert type(x) == ArrayType
    94   assert type(mask) == ArrayType
     94  assert isinstance(x, num.ndarray)
     95  assert isinstance(mask, num.ndarray)
    9596  #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'
    9798
    9899  d = len(mask)
    99   y = zeros(d, x.typecode())
     100  y = num.zeros(d, x.dtype)
    100101
    101102  i = 0
  • branches/numpy_misc/tools/pytools/stats.py

    r2017 r6818  
    1010
    1111def mean(x):
    12   import Numeric
     12  import numpy as num
    1313
    14   sum = Numeric.sum(x) 
     14  sum = num.sum(x) 
    1515  avg = float(sum)/len(x)
    1616
     
    1919
    2020def cov(x,y=None):
    21   import Numeric
     21  import numpy as num
    2222  if y is None:
    2323    y = x
     
    2929  cy = y - mean(y) 
    3030
    31   p = Numeric.innerproduct(cx,cy) / N
     31  p = num.inner(cx,cy) / N
    3232
    3333
     
    6565  """
    6666 
    67   import Numeric 
    68   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))
    7070  return p
    7171   
  • branches/numpy_misc/tools/pytools/test_pytools.py

    r18 r6818  
    11import unittest
    2 import Numeric
     2import numpy as num
    33from combinatorics import bitvector
    44from numtools import expand
     
    1414
    1515        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]))
    3232       
    3333
     
    3535
    3636        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])
    3939
    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]))
    4141
    4242
Note: See TracChangeset for help on using the changeset viewer.