/usr/share/pyshared/numpy/numarray/functions.py is in python-numpy 1:1.6.1-6ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | # missing Numarray defined names (in from numarray import *)
##__all__ = ['ClassicUnpickler', 'Complex32_fromtype',
## 'Complex64_fromtype', 'ComplexArray', 'Error',
## 'MAX_ALIGN', 'MAX_INT_SIZE', 'MAX_LINE_WIDTH',
## 'NDArray', 'NewArray', 'NumArray',
## 'NumError', 'PRECISION', 'Py2NumType',
## 'PyINT_TYPES', 'PyLevel2Type', 'PyNUMERIC_TYPES', 'PyREAL_TYPES',
## 'SUPPRESS_SMALL',
## 'SuitableBuffer', 'USING_BLAS',
## 'UsesOpPriority',
## 'codegenerator', 'generic', 'libnumarray', 'libnumeric',
## 'make_ufuncs', 'memory',
## 'numarrayall', 'numarraycore', 'numinclude', 'safethread',
## 'typecode', 'typecodes', 'typeconv', 'ufunc', 'ufuncFactory',
## 'ieeemask']
__all__ = ['asarray', 'ones', 'zeros', 'array', 'where']
__all__ += ['vdot', 'dot', 'matrixmultiply', 'ravel', 'indices',
'arange', 'concatenate', 'all', 'allclose', 'alltrue', 'and_',
'any', 'argmax', 'argmin', 'argsort', 'around', 'array_equal',
'array_equiv', 'arrayrange', 'array_str', 'array_repr',
'array2list', 'average', 'choose', 'CLIP', 'RAISE', 'WRAP',
'clip', 'compress', 'copy', 'copy_reg',
'diagonal', 'divide_remainder', 'e', 'explicit_type', 'pi',
'flush_caches', 'fromfile', 'os', 'sys', 'STRICT',
'SLOPPY', 'WARN', 'EarlyEOFError', 'SizeMismatchError',
'SizeMismatchWarning', 'FileSeekWarning', 'fromstring',
'fromfunction', 'fromlist', 'getShape', 'getTypeObject',
'identity', 'info', 'innerproduct', 'inputarray',
'isBigEndian', 'kroneckerproduct', 'lexsort', 'math',
'operator', 'outerproduct', 'put', 'putmask', 'rank',
'repeat', 'reshape', 'resize', 'round', 'searchsorted',
'shape', 'size', 'sometrue', 'sort', 'swapaxes', 'take',
'tcode', 'tname', 'tensormultiply', 'trace', 'transpose',
'types', 'value', 'cumsum', 'cumproduct', 'nonzero', 'newobj',
'togglebyteorder'
]
import copy
import copy_reg
import types
import os
import sys
import math
import operator
from numpy import dot as matrixmultiply, dot, vdot, ravel, concatenate, all,\
allclose, any, argsort, array_equal, array_equiv,\
array_str, array_repr, CLIP, RAISE, WRAP, clip, concatenate, \
diagonal, e, pi, inner as innerproduct, nonzero, \
outer as outerproduct, kron as kroneckerproduct, lexsort, putmask, rank, \
resize, searchsorted, shape, size, sort, swapaxes, trace, transpose
import numpy as np
from numerictypes import typefrom
if sys.version_info[0] >= 3:
import copyreg as copy_reg
isBigEndian = sys.byteorder != 'little'
value = tcode = 'f'
tname = 'Float32'
# If dtype is not None, then it is used
# If type is not None, then it is used
# If typecode is not None then it is used
# If use_default is True, then the default
# data-type is returned if all are None
def type2dtype(typecode, type, dtype, use_default=True):
if dtype is None:
if type is None:
if use_default or typecode is not None:
dtype = np.dtype(typecode)
else:
dtype = np.dtype(type)
if use_default and dtype is None:
dtype = np.dtype('int')
return dtype
def fromfunction(shape, dimensions, type=None, typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, 1)
return np.fromfunction(shape, dimensions, dtype=dtype)
def ones(shape, type=None, typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, 1)
return np.ones(shape, dtype)
def zeros(shape, type=None, typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, 1)
return np.zeros(shape, dtype)
def where(condition, x=None, y=None, out=None):
if x is None and y is None:
arr = np.where(condition)
else:
arr = np.where(condition, x, y)
if out is not None:
out[...] = arr
return out
return arr
def indices(shape, type=None):
return np.indices(shape, type)
def arange(a1, a2=None, stride=1, type=None, shape=None,
typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, 0)
return np.arange(a1, a2, stride, dtype)
arrayrange = arange
def alltrue(x, axis=0):
return np.alltrue(x, axis)
def and_(a, b):
"""Same as a & b
"""
return a & b
def divide_remainder(a, b):
a, b = asarray(a), asarray(b)
return (a/b,a%b)
def around(array, digits=0, output=None):
ret = np.around(array, digits, output)
if output is None:
return ret
return
def array2list(arr):
return arr.tolist()
def choose(selector, population, outarr=None, clipmode=RAISE):
a = np.asarray(selector)
ret = a.choose(population, out=outarr, mode=clipmode)
if outarr is None:
return ret
return
def compress(condition, a, axis=0):
return np.compress(condition, a, axis)
# only returns a view
def explicit_type(a):
x = a.view()
return x
# stub
def flush_caches():
pass
class EarlyEOFError(Exception):
"Raised in fromfile() if EOF unexpectedly occurs."
pass
class SizeMismatchError(Exception):
"Raised in fromfile() if file size does not match shape."
pass
class SizeMismatchWarning(Warning):
"Issued in fromfile() if file size does not match shape."
pass
class FileSeekWarning(Warning):
"Issued in fromfile() if there is unused data and seek() fails"
pass
STRICT, SLOPPY, WARN = range(3)
_BLOCKSIZE=1024
# taken and adapted directly from numarray
def fromfile(infile, type=None, shape=None, sizing=STRICT,
typecode=None, dtype=None):
if isinstance(infile, (str, unicode)):
infile = open(infile, 'rb')
dtype = type2dtype(typecode, type, dtype, True)
if shape is None:
shape = (-1,)
if not isinstance(shape, tuple):
shape = (shape,)
if (list(shape).count(-1)>1):
raise ValueError("At most one unspecified dimension in shape")
if -1 not in shape:
if sizing != STRICT:
raise ValueError("sizing must be STRICT if size complete")
arr = np.empty(shape, dtype)
bytesleft=arr.nbytes
bytesread=0
while(bytesleft > _BLOCKSIZE):
data = infile.read(_BLOCKSIZE)
if len(data) != _BLOCKSIZE:
raise EarlyEOFError("Unexpected EOF reading data for size complete array")
arr.data[bytesread:bytesread+_BLOCKSIZE]=data
bytesread += _BLOCKSIZE
bytesleft -= _BLOCKSIZE
if bytesleft > 0:
data = infile.read(bytesleft)
if len(data) != bytesleft:
raise EarlyEOFError("Unexpected EOF reading data for size complete array")
arr.data[bytesread:bytesread+bytesleft]=data
return arr
##shape is incompletely specified
##read until EOF
##implementation 1: naively use memory blocks
##problematic because memory allocation can be double what is
##necessary (!)
##the most common case, namely reading in data from an unchanging
##file whose size may be determined before allocation, should be
##quick -- only one allocation will be needed.
recsize = dtype.itemsize * np.product([i for i in shape if i != -1])
blocksize = max(_BLOCKSIZE/recsize, 1)*recsize
##try to estimate file size
try:
curpos=infile.tell()
infile.seek(0,2)
endpos=infile.tell()
infile.seek(curpos)
except (AttributeError, IOError):
initsize=blocksize
else:
initsize=max(1,(endpos-curpos)/recsize)*recsize
buf = np.newbuffer(initsize)
bytesread=0
while 1:
data=infile.read(blocksize)
if len(data) != blocksize: ##eof
break
##do we have space?
if len(buf) < bytesread+blocksize:
buf=_resizebuf(buf,len(buf)+blocksize)
## or rather a=resizebuf(a,2*len(a)) ?
assert len(buf) >= bytesread+blocksize
buf[bytesread:bytesread+blocksize]=data
bytesread += blocksize
if len(data) % recsize != 0:
if sizing == STRICT:
raise SizeMismatchError("Filesize does not match specified shape")
if sizing == WARN:
_warnings.warn("Filesize does not match specified shape",
SizeMismatchWarning)
try:
infile.seek(-(len(data) % recsize),1)
except AttributeError:
_warnings.warn("Could not rewind (no seek support)",
FileSeekWarning)
except IOError:
_warnings.warn("Could not rewind (IOError in seek)",
FileSeekWarning)
datasize = (len(data)/recsize) * recsize
if len(buf) != bytesread+datasize:
buf=_resizebuf(buf,bytesread+datasize)
buf[bytesread:bytesread+datasize]=data[:datasize]
##deduce shape from len(buf)
shape = list(shape)
uidx = shape.index(-1)
shape[uidx]=len(buf) / recsize
a = np.ndarray(shape=shape, dtype=type, buffer=buf)
if a.dtype.char == '?':
np.not_equal(a, 0, a)
return a
def fromstring(datastring, type=None, shape=None, typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, True)
if shape is None:
count = -1
else:
count = np.product(shape)
res = np.fromstring(datastring, dtype=dtype, count=count)
if shape is not None:
res.shape = shape
return res
# check_overflow is ignored
def fromlist(seq, type=None, shape=None, check_overflow=0, typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, False)
return np.array(seq, dtype)
def array(sequence=None, typecode=None, copy=1, savespace=0,
type=None, shape=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, 0)
if sequence is None:
if shape is None:
return None
if dtype is None:
dtype = 'l'
return np.empty(shape, dtype)
if isinstance(sequence, file):
return fromfile(sequence, dtype=dtype, shape=shape)
if isinstance(sequence, str):
return fromstring(sequence, dtype=dtype, shape=shape)
if isinstance(sequence, buffer):
arr = np.frombuffer(sequence, dtype=dtype)
else:
arr = np.array(sequence, dtype, copy=copy)
if shape is not None:
arr.shape = shape
return arr
def asarray(seq, type=None, typecode=None, dtype=None):
if isinstance(seq, np.ndarray) and type is None and \
typecode is None and dtype is None:
return seq
return array(seq, type=type, typecode=typecode, copy=0, dtype=dtype)
inputarray = asarray
def getTypeObject(sequence, type):
if type is not None:
return type
try:
return typefrom(np.array(sequence))
except:
raise TypeError("Can't determine a reasonable type from sequence")
def getShape(shape, *args):
try:
if shape is () and not args:
return ()
if len(args) > 0:
shape = (shape, ) + args
else:
shape = tuple(shape)
dummy = np.array(shape)
if not issubclass(dummy.dtype.type, np.integer):
raise TypeError
if len(dummy) > np.MAXDIMS:
raise TypeError
except:
raise TypeError("Shape must be a sequence of integers")
return shape
def identity(n, type=None, typecode=None, dtype=None):
dtype = type2dtype(typecode, type, dtype, True)
return np.identity(n, dtype)
def info(obj, output=sys.stdout, numpy=0):
if numpy:
bp = lambda x: x
else:
bp = lambda x: int(x)
cls = getattr(obj, '__class__', type(obj))
if numpy:
nm = getattr(cls, '__name__', cls)
else:
nm = cls
print >> output, "class: ", nm
print >> output, "shape: ", obj.shape
strides = obj.strides
print >> output, "strides: ", strides
if not numpy:
print >> output, "byteoffset: 0"
if len(strides) > 0:
bs = obj.strides[0]
else:
bs = obj.itemsize
print >> output, "bytestride: ", bs
print >> output, "itemsize: ", obj.itemsize
print >> output, "aligned: ", bp(obj.flags.aligned)
print >> output, "contiguous: ", bp(obj.flags.contiguous)
if numpy:
print >> output, "fortran: ", obj.flags.fortran
if not numpy:
print >> output, "buffer: ", repr(obj.data)
if not numpy:
extra = " (DEBUG ONLY)"
tic = "'"
else:
extra = ""
tic = ""
print >> output, "data pointer: %s%s" % (hex(obj.ctypes._as_parameter_.value), extra)
print >> output, "byteorder: ",
endian = obj.dtype.byteorder
if endian in ['|','=']:
print >> output, "%s%s%s" % (tic, sys.byteorder, tic)
byteswap = False
elif endian == '>':
print >> output, "%sbig%s" % (tic, tic)
byteswap = sys.byteorder != "big"
else:
print >> output, "%slittle%s" % (tic, tic)
byteswap = sys.byteorder != "little"
print >> output, "byteswap: ", bp(byteswap)
if not numpy:
print >> output, "type: ", typefrom(obj).name
else:
print >> output, "type: %s" % obj.dtype
#clipmode is ignored if axis is not 0 and array is not 1d
def put(array, indices, values, axis=0, clipmode=RAISE):
if not isinstance(array, np.ndarray):
raise TypeError("put only works on subclass of ndarray")
work = asarray(array)
if axis == 0:
if array.ndim == 1:
work.put(indices, values, clipmode)
else:
work[indices] = values
elif isinstance(axis, (int, long, np.integer)):
work = work.swapaxes(0, axis)
work[indices] = values
work = work.swapaxes(0, axis)
else:
def_axes = range(work.ndim)
for x in axis:
def_axes.remove(x)
axis = list(axis)+def_axes
work = work.transpose(axis)
work[indices] = values
work = work.transpose(axis)
def repeat(array, repeats, axis=0):
return np.repeat(array, repeats, axis)
def reshape(array, shape, *args):
if len(args) > 0:
shape = (shape,) + args
return np.reshape(array, shape)
import warnings as _warnings
def round(*args, **keys):
_warnings.warn("round() is deprecated. Switch to around()",
DeprecationWarning)
return around(*args, **keys)
def sometrue(array, axis=0):
return np.sometrue(array, axis)
#clipmode is ignored if axis is not an integer
def take(array, indices, axis=0, outarr=None, clipmode=RAISE):
array = np.asarray(array)
if isinstance(axis, (int, long, np.integer)):
res = array.take(indices, axis, outarr, clipmode)
if outarr is None:
return res
return
else:
def_axes = range(array.ndim)
for x in axis:
def_axes.remove(x)
axis = list(axis) + def_axes
work = array.transpose(axis)
res = work[indices]
if outarr is None:
return res
outarr[...] = res
return
def tensormultiply(a1, a2):
a1, a2 = np.asarray(a1), np.asarray(a2)
if (a1.shape[-1] != a2.shape[0]):
raise ValueError("Unmatched dimensions")
shape = a1.shape[:-1] + a2.shape[1:]
return np.reshape(dot(np.reshape(a1, (-1, a1.shape[-1])),
np.reshape(a2, (a2.shape[0],-1))),
shape)
def cumsum(a1, axis=0, out=None, type=None, dim=0):
return np.asarray(a1).cumsum(axis,dtype=type,out=out)
def cumproduct(a1, axis=0, out=None, type=None, dim=0):
return np.asarray(a1).cumprod(axis,dtype=type,out=out)
def argmax(x, axis=-1):
return np.argmax(x, axis)
def argmin(x, axis=-1):
return np.argmin(x, axis)
def newobj(self, type):
if type is None:
return np.empty_like(self)
else:
return np.empty(self.shape, type)
def togglebyteorder(self):
self.dtype=self.dtype.newbyteorder()
def average(a, axis=0, weights=None, returned=0):
return np.average(a, axis, weights, returned)
|