Welcome, guest | Sign In | My Account | Store | Cart

This python submodule enable you to do the vectorize calculation, such as vectorize add, and etc.

Python, 55 lines
 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
"""
   Copyright 2011 Shao-Chuan Wang <shaochuan.wang AT gmail.com>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
"""

import operator
from itertools import imap, repeat
import functools

iterable = lambda obj: isinstance(obj, basestring) or hasattr(obj, '__iter__')
def vector_op(op, x, y):
    if iterable(x) and iterable(y):
        return type(x)(imap(op, x, y))
    if not iterable(x):
        return type(y)(imap(op, repeat(x), y))
    if not iterable(y):
        return type(x)(imap(op, x, repeat(y)))

vector_add = functools.partial(vector_op, operator.add)
vector_sub = functools.partial(vector_op, operator.sub)
vector_mul = functools.partial(vector_op, operator.mul)
vector_div = functools.partial(vector_op, operator.div)
vector_and = functools.partial(vector_op, operator.and_)
vector_or  = functools.partial(vector_op, operator.or_)

def vector_sum(has_len):
    if not has_len:
        return has_len
    return reduce(vector_add, has_len)

def vector_mean(has_len):
    vsum = vector_sum(has_len)
    return type(vsum)(float(e)/float(len(has_len)) for e in vsum)

if __name__ == '__main__':
    positions = [(1,2,1), (3,4,3), (5,6,3)]
    print vector_sum(positions)
    print vector_mean(positions)

1 comment

Sunjay Varma 13 years, 2 months ago  # | flag

Fantastic use of everything!

Created by Shao-chuan Wang on Sat, 5 Feb 2011 (MIT)
Python recipes (4591)
Shao-chuan Wang's recipes (22)

Required Modules

  • (none specified)

Other Information and Tasks