reduce

pickle cython class

僤鯓⒐⒋嵵緔 提交于 2019-11-30 03:38:16
问题 I have to save and load a cython class instance. My cython class is this plus several methods: import numpy as np cimport numpy as np cimport cython cdef class Perceptron_avg_my: cdef int wlen,freePos cdef np.ndarray w,wtot,wac,wtotc #np.ndarray[np.int32_t] cdef np.ndarray wmean #np.ndarray[np.float32_t] cdef public dict fpos def __cinit__(self,np.int64_t wlen=4*10**7): self.fpos= dict() self.freePos=1 self.wlen=wlen self.w=np.zeros(wlen,np.int32) self.wtot=np.zeros(wlen,np.int32) self.wac=np

Using the reduce function to return an array

此生再无相见时 提交于 2019-11-30 02:59:04
Why is it that when I want to use the push function inside the reduce function to return a new array I get an error. However, when I use the concat method inside the reduce function, it returns a new array with no problem. All I'm trying to do is pass an array to the reduce function and return the same array. var store = [0,1,2,3,4]; var stored = store.reduce(function(pV,cV,cI){ console.log("pv: ", pV); return pV.push(cV); },[]); This returns an error. But when I use concat: var store = [0,1,2,3,4]; var stored = store.reduce(function(pV,cV,cI){ console.log("pv: ", pV); return pV.concat(cV); },

What is basic difference between fold and reduce in Kotlin? When to use which?

对着背影说爱祢 提交于 2019-11-30 00:12:36
I am going through basics of Kotlin and I am pretty confused with this both functions fold() and reduce() in Kotlin, can anyone give me a concrete example which distinguishes both of them? fold takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters. For example, take the following code that calculates the sum of a list of integers: listOf(1, 2, 3).fold(0) { sum, element -> sum + element } The first call to the lambda will be with parameters 0 and 1 . Having the ability to pass in an

one-liner reduce in Python3

喜你入骨 提交于 2019-11-30 00:05:58
问题 In Python3, I am looking for a way to compute in one line a lambda function called on elements two by two. Let’s say I want to compute the LCM of a list of integers, this can be done in one line in Python2: print reduce(lambda a,b: a * b // gcd(a, b), mylist) Is it possible to do the same in one line Python3 (implied, without functools.reduce )? In Python3 I know that filter , map and reduce are gone. I don’t feel I need filter and map anymore because they can be written in Python3 in a

Reduce array to set in Swift

橙三吉。 提交于 2019-11-29 21:12:54
I am trying to reduce an array of objects to a set in Swift and this is my code: objects.reduce(Set<String>()) { $0.insert($1.URL) } However, I get an error: Type of expression is ambiguous without more context. I do not understand what the problem is, since the type of URL is definitely String. Any ideas? You don't have to reduce an array to get it into a set; just create the set with an array: let objectSet = Set(objects.map { $0.URL }) . With Swift 5.1, you can use one of the three following examples in order to solve your problem. #1. Using Array 's map(_:) method and Set 's init(_:)

Accumulate a Java Stream and only then process it

为君一笑 提交于 2019-11-29 16:46:26
I had a document that looked like the following: data.txt 100, "some text" 101, "more text" 102, "even more text" I processed it using regex and returned a new processed documents as the follow: Stream<String> lines = Files.lines(Paths.get(data.txt); Pattern regex = Pattern.compile("([\\d{1,3}]),(.*)"); List<MyClass> result = lines.map(regex::matcher) .filter(Matcher::find) .map(m -> new MyClass(m.group(1), m.group(2)) //MyClass(int id, String text) .collect(Collectors.toList()); This returns a list of MyClass processed. Can run in parallel and everything is ok. The problem is that I now have

Java 8 Stream - Reduce function's combiner not getting executed [duplicate]

こ雲淡風輕ζ 提交于 2019-11-29 12:52:40
This question already has an answer here: Java8 stream.reduce() with 3 parameters - getting transparency 2 answers I am using a simple reduce method with three arguments viz. identity, accumulator and combiner. Here is my code... Integer ageSumComb = persons .stream() .reduce(0, (sum, p) -> { System.out.println("Accumulator: Sum= "+ sum + " Person= " + p); return sum += p.age; }, (sum1, sum2) -> { System.out.format("Combiner: Sum1= " + sum1 + " Sum2= "+ sum2); return sum1 + sum2; But what is happening is the Combiner is not getting executed. I am not getting the reason behind this. Here is my

group objects in array based on value of key in object

ⅰ亾dé卋堺 提交于 2019-11-29 12:03:01
I have the following data that I want to sort based on the date - not including the timestamp. NOTE: I have access to moment for this task. My data looks like the following: const data = [ { "fixture": "AC v Inter", "kickOffTime": "2018-06-14T15:00:00Z", }, { "fixture": "DC v NYC", "kickOffTime": "2018-06-15T12:00:00Z", }, { "fixture": "AFC v LPC", "kickOffTime": "2018-06-15T15:00:00Z", }, { "fixture": "DTA v MC", "kickOffTime": "2018-06-15T18:00:00Z", }, { "fixture": "LAC v GC", "kickOffTime": "2018-06-16T18:00:00Z", } ]; I have tried a number of approaches. The final result I am hoping to

thrust reduction result on device memory

隐身守侯 提交于 2019-11-29 11:14:38
Is it possible to leave the return value of a thrust::reduce operation in device-allocated memory? In case it is, is it just as easy as assigning the value to a cudaMalloc'ed area, or should I use a thrust::device_ptr? Is it possible to leave the return value of a thrust::reduce operation in device-allocated memory? The short answer is no. thrust reduce returns a quantity, the result of the reduction. This quantity must be deposited in a host resident variable : Take for example reduce, which is synchronous and always returns its result to the CPU: template<typename Iterator, typename T> T

Reduce function with three parameters

纵饮孤独 提交于 2019-11-29 06:02:19
How does reduce function work in python3 with three parameters instead of two. So, for two, tup = (1,2,3) reduce(lambda x, y: x+y, tup) I get this one. This would just sum up all the elements in tup . However, if you give reduce function three parameters like this below, tup = (1,2,3) reduce(lambda x, y: x+y, tup, 6) this would give you a value of 12 . I checked up on the documentation for python3 and it says the third argument is an initializer. That said, then what is the default initializer if the third argument is not inserted? If you omit the third parameter, then the first value from tup