Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

python - Numpy vectorize, using lists as arguments -


the numpy vectorize function useful, doesn't behave when function arguments lists rather scalars. example:

import numpy np  def f(x, a):     print "type(a)=%s, a=%s"%(type(a),a)     return sum(a)/x  x = np.linspace(1,2,10) p = [1,2,3]  f2 = np.vectorize(f)  f(x,p) f2(x,p) 

gives:

type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'numpy.int64'>, a=1  traceback (most recent call last):   file "vectorize.py", line 14, in <module>     f2(x,p)   file "/usr/local/lib/python2.6/dist-packages/numpy/lib/function_base.py", line 1824, in __call__     theout = self.thefunc(*newargs)   file "vectorize.py", line 5, in f     return sum(a)/x typeerror: 'numpy.int64' object not iterable 

i understand function f works just fine without vectorizeing it, i'd know how (in general) vectorize function arguments take in lists rather scalar.

your question doesn't make clear precisely output see vectorized function, i'm going assume same list (a) applied argument every invocation of f() (ie once each element in x array)

the vectorized version of function ensures arguments arrays, , applies numpy's broadcasting rules determine how these arguments should combined.

as np.array's wrapping of np.ndarray, coercion of arguments arrays tries helpful, automatically converting list array containing same elements, rather making array dtype=object contains list sole element. of time want, in case, "smart" behaviour coming bite you.

while there may way instruct numpy treat inputs vectors, there 2 straightforward ways behaviour you're after:

  1. manually create array dtype=object work within broadcasting rules
  2. curry value prior vectorizing function

1. dtype=object

numpy arrays derive efficiency storing 1 type of item, can still contain arbitrary python objects specifying stored data type python objects:

list_obj_array = np.ndarray((1,), dtype=object) list_obj_array[0] = [1,2,3] f2(x,list_obj_array)  # using definition above 

prints:

type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] 

and returns:

array([ 6.        ,  5.4       ,  4.90909091,  4.5       ,  4.15384615,         3.85714286,  3.6       ,  3.375     ,  3.17647059,  3.        ]) 

2. currying

since passing same list function call each item in array, can store list directly function currying before applying vectorization:

def curry_f(a):     def f_curried(x):         return f(x, a)  # using definition above     return f_curried  f2 = np.vectorize(curry_f(p)) f2(x) 

prints:

type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] type(a)=<type 'list'>, a=[1, 2, 3] 

and returns:

array([ 6.        ,  5.4       ,  4.90909091,  4.5       ,  4.15384615,         3.85714286,  3.6       ,  3.375     ,  3.17647059,  3.        ]) 

p.s. may wish have @ np.frompyfunc -- similar vectorize(), works @ lower level.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -