Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f897e1f

Browse files
committed
major reorg of scipy_base -- initial checkin
1 parent 5dcb208 commit f897e1f

10 files changed

+1394
-18
lines changed

scipy_base/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
import fastumath
55
import limits
66

7-
from utility import *
8-
from convenience import *
7+
#from utility import *
8+
#from convenience import *
9+
10+
from type_check import *
11+
from index_tricks import *
12+
from function_base import *
13+
from shape_base import *
14+
from matrix_base import *
15+
916
from polynomial import *
1017
from scimath import *
11-
from helpmod import help, source
12-
from Matrix import Matrix as mat
13-
Mat = mat # deprecated
1418

1519
# needs fastumath
1620
Inf = inf = Numeric.array(1e308)**10
1721
NaN = nan = Numeric.array(0.0) / Numeric.array(0.0)
1822

23+
1924
#---- testing ----#
2025

2126
def test(level=10):
@@ -28,6 +33,10 @@ def test_suite(level=1):
2833
import scipy_base.testing
2934
import scipy_base
3035
this_mod = scipy_base
31-
return scipy_base.testing.harvest_test_suites(this_mod,level=level)
36+
# ieee_754 gets tested in the type_check module.
37+
# testing is the module that actually does all the testing...
38+
ignore = ['ieee_754','testing']
39+
return scipy_base.testing.harvest_test_suites(this_mod,ignore = ignore,
40+
level=level)
3241

3342

scipy_base/function_base.py

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
import types
2+
import Numeric
3+
from Numeric import *
4+
5+
__all__ = ['round','any','all','logspace','linspace','fix','mod',
6+
'select','trim_zeros','amax','amin','ptp','cumsum',
7+
'prod','cumprod','diff','angle','unwrap','sort_complex']
8+
9+
round = Numeric.around
10+
any = Numeric.sometrue
11+
all = Numeric.alltrue
12+
13+
def logspace(start,stop,num=50,endpoint=1):
14+
""" Evenly spaced samples on a logarithmic scale.
15+
16+
Return num evenly spaced samples from 10**start to 10**stop. If
17+
endpoint=1 then last sample is 10**stop.
18+
"""
19+
if endpoint:
20+
step = (stop-start)/float((num-1))
21+
y = Numeric.arange(0,num) * step + start
22+
else:
23+
step = (stop-start)/float(num)
24+
y = Numeric.arange(0,num) * step + start
25+
return Numeric.power(10.0,y)
26+
27+
def linspace(start,stop,num=50,endpoint=1,retstep=0):
28+
""" Evenly spaced samples.
29+
30+
Return num evenly spaced samples from start to stop. If endpoint=1 then
31+
last sample is stop. If retstep is 1 then return the step value used.
32+
"""
33+
if endpoint:
34+
step = (stop-start)/float((num-1))
35+
y = Numeric.arange(0,num) * step + start
36+
else:
37+
step = (stop-start)/float(num)
38+
y = Numeric.arange(0,num) * step + start
39+
if retstep:
40+
return y, step
41+
else:
42+
return y
43+
44+
def fix(x):
45+
""" Round x to nearest integer towards zero.
46+
"""
47+
x = Numeric.asarray(x)
48+
y = Numeric.floor(x)
49+
return Numeric.where(x<0,y+1,y)
50+
51+
def mod(x,y):
52+
""" x - y*floor(x/y)
53+
54+
For numeric arrays, x % y has the same sign as x while
55+
mod(x,y) has the same sign as y.
56+
"""
57+
return x - y*Numeric.floor(x*1.0/y)
58+
59+
def select(condlist, choicelist, default=0):
60+
""" Returns an array comprised from different elements of choicelist
61+
depending on the list of conditions.
62+
63+
condlist is a list of condition arrays containing ones or zeros
64+
65+
choicelist is a list of choice matrices (of the "same" size as the
66+
arrays in condlist). The result array has the "same" size as the
67+
arrays in choicelist. If condlist is [c0,...,cN-1] then choicelist
68+
must be of length N. The elements of the choicelist can then be
69+
represented as [v0,...,vN-1]. The default choice if none of the
70+
conditions are met is given as the default argument.
71+
72+
The conditions are tested in order and the first one statisfied is
73+
used to select the choice. In other words, the elements of the
74+
output array are found from the following tree (notice the order of
75+
the conditions matters):
76+
77+
if c0: v0
78+
elif c1: v1
79+
elif c2: v2
80+
...
81+
elif cN-1: vN-1
82+
else: default
83+
84+
Note, that one of the condition arrays must be large enough to handle
85+
the largest array in the choice list.
86+
"""
87+
n = len(condlist)
88+
n2 = len(choicelist)
89+
if n2 != n:
90+
raise ValueError, "List of cases, must be same length as the list of conditions."
91+
choicelist.insert(0,default)
92+
S = 0
93+
pfac = 1
94+
for k in range(1,n+1):
95+
S += k * pfac * asarray(condlist[k-1])
96+
if k < n:
97+
pfac *= (1-asarray(condlist[k-1]))
98+
# handle special case of a 1-element condition but
99+
# a multi-element choice
100+
if type(S) in ScalarType or max(asarray(S).shape)==1:
101+
pfac = asarray(1)
102+
for k in range(n2+1):
103+
pfac = pfac + asarray(choicelist[k])
104+
S = S*ones(asarray(pfac).shape)
105+
return choose(S, tuple(choicelist))
106+
107+
# Basic operations
108+
def amax(m,axis=-1):
109+
"""Returns the maximum of m along dimension axis.
110+
"""
111+
if axis is None:
112+
m = ravel(m)
113+
axis = 0
114+
else:
115+
m = asarray(m)
116+
return maximum.reduce(m,axis)
117+
118+
def amin(m,axis=-1):
119+
"""Returns the minimum of m along dimension axis.
120+
"""
121+
if axis is None:
122+
m = ravel(m)
123+
axis = 0
124+
else:
125+
m = asarray(m)
126+
return minimum.reduce(m,axis)
127+
128+
# Actually from Basis, but it fits in so naturally here...
129+
130+
def ptp(m,axis=-1):
131+
"""Returns the maximum - minimum along the the given dimension
132+
"""
133+
if axis is None:
134+
m = ravel(m)
135+
axis = 0
136+
else:
137+
m = asarray(m)
138+
return amax(m,axis)-amin(m,axis)
139+
140+
def cumsum(m,axis=-1):
141+
"""Returns the cumulative sum of the elements along the given axis
142+
"""
143+
if axis is None:
144+
m = ravel(m)
145+
axis = 0
146+
else:
147+
m = asarray(m)
148+
return add.accumulate(m,axis)
149+
150+
def prod(m,axis=-1):
151+
"""Returns the product of the elements along the given axis
152+
"""
153+
if axis is None:
154+
m = ravel(m)
155+
axis = 0
156+
else:
157+
m = asarray(m)
158+
return multiply.reduce(m,axis)
159+
160+
def cumprod(m,axis=-1):
161+
"""Returns the cumulative product of the elments along the given axis
162+
"""
163+
if axis is None:
164+
m = ravel(m)
165+
axis = 0
166+
else:
167+
m = asarray(m)
168+
return multiply.accumulate(m,axis)
169+
170+
def diff(x, n=1,axis=-1):
171+
"""Calculates the nth order, discrete difference along given axis.
172+
"""
173+
x = asarray(x)
174+
nd = len(x.shape)
175+
slice1 = [slice(None)]*nd
176+
slice2 = [slice(None)]*nd
177+
slice1[axis] = slice(1,None)
178+
slice2[axis] = slice(None,-1)
179+
if n > 1:
180+
return diff(x[slice1]-x[slice2], n-1, axis=axis)
181+
else:
182+
return x[slice1]-x[slice2]
183+
184+
def angle(z,deg=0):
185+
"""Return the angle of complex argument z."""
186+
if deg:
187+
fact = 180/pi
188+
else:
189+
fact = 1.0
190+
z = asarray(z)
191+
if z.typecode() in ['D','F']:
192+
zimag = z.imag
193+
zreal = z.real
194+
else:
195+
zimag = 0
196+
zreal = z
197+
return arctan2(zimag,zreal) * fact
198+
199+
def unwrap(p,discont=pi,axis=-1):
200+
"""unwrap(p,discont=pi,axis=-1)
201+
202+
unwraps radian phase p by changing absolute jumps greater than discont to
203+
their 2*pi complement along the given axis.
204+
"""
205+
p = asarray(p)
206+
nd = len(p.shape)
207+
dd = diff(p,axis=axis)
208+
slice1 = [slice(None,None)]*nd # full slices
209+
slice1[axis] = slice(1,None)
210+
ddmod = mod(dd+pi,2*pi)-pi
211+
putmask(ddmod,(ddmod==-pi) & (dd > 0),pi)
212+
ph_correct = ddmod - dd;
213+
putmask(ph_correct,abs(dd)<discont,0)
214+
up = array(p,copy=1,typecode='d')
215+
up[slice1] = p[slice1] + cumsum(ph_correct,axis)
216+
return up
217+
218+
def sort_complex(a):
219+
""" Doesn't currently work for integer arrays -- only float or complex.
220+
"""
221+
a = asarray(a,typecode=a.typecode().upper())
222+
def complex_cmp(x,y):
223+
res = cmp(x.real,y.real)
224+
if res == 0:
225+
res = cmp(x.imag,y.imag)
226+
return res
227+
l = a.tolist()
228+
l.sort(complex_cmp)
229+
return array(l)
230+
231+
def trim_zeros(filt,trim='fb'):
232+
""" Trim the leading and trailing zeros from a 1D array.
233+
234+
Example:
235+
>>> import scipy
236+
>>> a = array((0,0,0,1,2,3,2,1,0))
237+
>>> scipy.trim_zeros(a)
238+
array([1, 2, 3, 2, 1])
239+
"""
240+
first = 0
241+
if 'f' in trim or 'F' in trim:
242+
for i in filt:
243+
if i != 0.: break
244+
else: first = first + 1
245+
last = len(filt)
246+
if 'b' in trim or 'B' in trim:
247+
for i in filt[::-1]:
248+
if i != 0.: break
249+
else: last = last - 1
250+
return filt[first:last]
251+
252+
#-----------------------------------------------------------------------------
253+
# Test Routines
254+
#-----------------------------------------------------------------------------
255+
256+
def test(level=10):
257+
from scipy_base.testing import module_test
258+
module_test(__name__,__file__,level=level)
259+
260+
def test_suite(level=1):
261+
from scipy_base.testing import module_test_suite
262+
return module_test_suite(__name__,__file__,level=level)
263+
264+
if __name__ == '__main__':
265+
test()

scipy_base/ieee_754.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""Module ieee: exports a few useful IEEE-754 constants and functions.
2+
3+
PINF positive infinity
4+
MINF minus infinity
5+
NAN a generic quiet NaN
6+
PZERO positive zero
7+
MZERO minus zero
8+
9+
isnan(x)
10+
Return true iff x is a NaN.
11+
"""
12+
13+
def _make_inf():
14+
x = 2.0
15+
x2 = x * x
16+
i = 0
17+
while i < 100 and x != x2:
18+
x = x2
19+
x2 = x * x
20+
i = i + 1
21+
if x != x2:
22+
raise ValueError("This machine's floats go on forever!")
23+
return x
24+
25+
# NaN-testing.
26+
#
27+
# The usual method (x != x) doesn't work.
28+
# Python forces all comparisons thru a 3-outcome cmp protocol; unordered
29+
# isn't a possible outcome. The float cmp outcome is essentially defined
30+
# by this C expression (combining some cross-module implementation
31+
# details, and where px and py are pointers to C double):
32+
# px == py ? 0 : *px < *py ? -1 : *px > *py ? 1 : 0
33+
# Comparing x to itself thus always yields 0 by the first clause, and so x
34+
# != x is never true. If px and py point to distinct NaN objects, a
35+
# strange thing happens: 1. On scrupulous 754 implementations, *px < *py
36+
# returns false, and so
37+
# does *px > *py. Python therefore returns 0, i.e. "equal"!
38+
# 2. On Pentium HW, an unordered outcome sets an otherwise-impossible
39+
# combination of condition codes, including both the "less than" and
40+
# "equal to" flags. Microsoft C generates naive code that accepts the
41+
# "less than" flag at face value, and so the *px < *py clause returns
42+
# true, and Python returns -1, i.e. "not equal".
43+
# So with a proper C 754 implementation Python returns the wrong result,
44+
# and under MS's improper 754 implementation Python yields the right
45+
# result -- both by accident. It's unclear who should be shot <wink>.
46+
#
47+
# Anyway, the point of all that was to convince you it's tricky getting
48+
# the right answer in a portable way!
49+
50+
def isnan(x):
51+
"""x -> true iff x is a NaN."""
52+
# multiply by 1.0 to create a distinct object (x < x *always*
53+
# false in Python, due to object identity forcing equality)
54+
if x * 1.0 < x:
55+
# it's a NaN and this is MS C on a Pentium
56+
return 1
57+
# Else it's non-NaN, or NaN on a non-MS+Pentium combo.
58+
# If it's non-NaN, then x == 1.0 and x == 2.0 can't both be true,
59+
# so we return false. If it is NaN, then assuming a good 754 C
60+
# implementation Python maps both unordered outcomes to true.
61+
return 1.0 == x == 2.0
62+
63+
PINF = _make_inf()
64+
MINF = -PINF
65+
66+
NAN = PINF - PINF
67+
if not isnan(NAN):
68+
raise ValueError("This machine doesn't have NaNs, "
69+
"'overflows' to a finite number, "
70+
"suffers a novel way of implementing C comparisons, "
71+
"or is 754-conformant but is using "
72+
"a goofy rounding mode.")
73+
PZERO = 0.0
74+
MZERO = -PZERO

0 commit comments

Comments
 (0)