~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/lib/ufunclike.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Module of functions that are like ufuncs in acting on arrays and optionally
 
3
storing results in an output array.
 
4
"""
 
5
__all__ = ['fix', 'isneginf', 'isposinf', 'log2']
 
6
 
 
7
import numpy.core.numeric as nx
 
8
from numpy.core.numeric import asarray, empty, isinf, signbit
 
9
import numpy.core.umath as umath
 
10
 
 
11
def fix(x, y=None):
 
12
    """ Round x to nearest integer towards zero.
 
13
    """
 
14
    x = asarray(x)
 
15
    if y is None:
 
16
        y = nx.floor(x)
 
17
    else:
 
18
        nx.floor(x, y)
 
19
    if x.ndim == 0:
 
20
        if (x<0):
 
21
            y += 1
 
22
    else:
 
23
        y[x<0] = y[x<0]+1
 
24
    return y
 
25
 
 
26
def isposinf(x, y=None):
 
27
    """Return a boolean array y with y[i] True for x[i] = +Inf.
 
28
 
 
29
    If y is an array, the result replaces the contents of y.
 
30
    """
 
31
    if y is None:
 
32
        y = empty(x.shape, dtype=nx.bool_)
 
33
    umath.logical_and(isinf(x), ~signbit(x), y)
 
34
    return y
 
35
 
 
36
def isneginf(x, y=None):
 
37
    """Return a boolean array y with y[i] True for x[i] = -Inf.
 
38
 
 
39
    If y is an array, the result replaces the contents of y.
 
40
    """
 
41
    if y is None:
 
42
        y = empty(x.shape, dtype=nx.bool_)
 
43
    umath.logical_and(isinf(x), signbit(x), y)
 
44
    return y
 
45
 
 
46
_log2 = umath.log(2)
 
47
def log2(x, y=None):
 
48
    """Returns the base 2 logarithm of x
 
49
 
 
50
    If y is an array, the result replaces the contents of y.
 
51
    """
 
52
    x = asarray(x)
 
53
    if y is None:
 
54
        y = umath.log(x)
 
55
    else:
 
56
        umath.log(x, y)
 
57
    y /= _log2
 
58
    return y