fold

How to fold/accumulate a numpy matrix product (dot)?

☆樱花仙子☆ 提交于 2019-12-05 16:19:45
With using python library numpy, it is possible to use the function cumprod to evaluate cumulative products, e.g. a = np.array([1,2,3,4,2]) np.cumprod(a) gives array([ 1, 2, 6, 24, 48]) It is indeed possible to apply this function only along one axis. I would like to do the same with matrices (represented as numpy arrays), e.g. if I have S0 = np.array([[1, 0], [0, 1]]) Sx = np.array([[0, 1], [1, 0]]) Sy = np.array([[0, -1j], [1j, 0]]) Sz = np.array([[1, 0], [0, -1]]) and b = np.array([S0, Sx, Sy, Sz]) then I would like to have a cumprod -like function which gives np.array([S0, S0.dot(Sx), S0

How is foldl lazy?

扶醉桌前 提交于 2019-12-05 14:30:34
问题 There are lots of good questions and answers about foldl , foldr , and foldl' in Haskell. So now I know that: 1) foldl is lazy 2) don't use foldl because it can blow up the stack 3) use foldl' instead because it is strict (ish) How foldl is evaluated: 1) a whole bunch of thunks are created 2) after Haskell is done creating thunks, the thunks are reduced 3) overflow the stack if there are too many thunks What I'm confused about: 1) why does reduction have to occur after all thunk-ing? 2) why

reversing a list in OCaml using fold_left/right

匆匆过客 提交于 2019-12-05 13:02:51
UPDATE - Solution Thanks to jacobm for his help, I came up with a solution. // Folding Recursion let reverse_list_3 theList = List.fold_left (fun element recursive_call -> recursive_call::element) [] theList;; I'm learning about the different ways of recursion in OCaml (for class) and for some exercise, I'm writing a function to reverse a list using different recursion styles. // Forward Recursion let rec reverse_list_forward theList = match theList with [] -> [] | (head::tail) -> (reverse_list_1 tail) @ [head];; // Tail Recursion let rec reverse_list_tail theList result = match theList with [

Generic Poly2 Folder case for shapeless Hlist

[亡魂溺海] 提交于 2019-12-05 10:20:22
I am trying to transform the following HList Some(C(15)) :: None :: Some(B(55)) :: None :: Some(A(195)) :: HNil to C(15) :: B(55) :: A(195) :: HNil Here is what I have at the moment : import shapeless._ case class A(value: Int) case class B(value: Int) case class C(value: Int) trait folderLP extends Poly2 { implicit def default[T, L <: HList] = at[T, L]((acc, t) => acc) } object folder extends folderLP { implicit def none[T, L <: HList] = at[None.type, L]((t, acc) => acc) implicit def someDiameter[T, L <: HList] = at[Some[C], L]((t, acc) => t.get :: acc) implicit def someRatio[T, L <: HList] =

haskell fold rose tree paths

隐身守侯 提交于 2019-12-05 09:14:57
let us say we have a tree... data Tree a = Node a [Tree a] deriving (Show) and that tree has some nodes t = Node 1 [Node 2 [Node 3 []], Node 4 [], Node 5 [Node 6 []]] the following function will collect the paths in a tree. paths :: Tree a -> [[a]] paths (Node n []) = [[n]] paths (Node n ns) = map ((:) n . concat . paths) ns like so: *Main> paths t [[1,2,3],[1,4],[1,5,6]] But now how could we fold these paths? Obviously we could do this. Which folds after finding the paths. wastefullFold :: (a -> b -> b) -> b -> Tree a -> [b] wastefullFold f z (Node n ns) = map (foldr f z) $ paths (Node n ns)

How do I break out of a pure loop in Haskell without hand-written recursion?

浪子不回头ぞ 提交于 2019-12-05 01:51:52
I want to write a function that goes through a list updating an accumulator until that accumulator reaches a certain condition or I get to the end of the list. For example, a product function that stops as soon as its accumulator reaches zero. I know how to code it by writing the recursion by hand: {-# LANGUAGE BangPatterns #-} prod :: [Integer] -> Integer prod xs = go 1 xs where go 0 _ = 0 go !acc [] = acc go !acc (x:xs) = go (acc * x) xs but is there a way to code this using folds and other higher order functions? One thing that comes to mind is defining mult 0 _ = 0 mult x y = x * y and

How to implement delete with foldr in Haskell

心不动则不痛 提交于 2019-12-04 23:34:31
I've been studying folds for the past few days. I can implement simple functions with them, like length , concat and filter . What I'm stuck at is trying to implement with foldr functions like delete , take and find . I have implemented these with explicit recursion but it doesn't seem obvious to me how to convert these types of functions to right folds. I have studied the tutorials by Graham Hutton and Bernie Pope. Imitating Hutton's dropWhile , I was able to implement delete with foldr but it fails on infinite lists. From reading Implement insert in haskell with foldr , How can this function

Code folding is not saved in my vimrc

落爺英雄遲暮 提交于 2019-12-04 17:31:09
问题 I added the following code to my .vimrc: " save and restore folds when a file is closed and re-opened autocmd BufWinLeave *.* mkview autocmd BufWinEnter *.* silent loadview HTML and CSS documents save and restore their folds but code folding is not being saved in my .vimrc Any suggestions? EDIT: The following code solves the problem: au BufWinLeave ?* mkview au BufWinEnter ?* silent loadview but if I write it, the MRU files disappear from my list (and I have to open MRU twice in order to see

Fold expressions with arbitrary callable?

守給你的承諾、 提交于 2019-12-04 15:02:15
问题 Looking over the C++17 paper on folds, (and on cppreference), I'm confused as to why the choice was made to only work with operators? At first glance it seems like it would make it easier to expand (... + args) by just shoving a + token between the elements of args , but I'm unconvinced this is a great decision. Why can't a binary lambda expression work just as well and follow the same expansion as the latter above? It's jarring to me that a fold syntax would be added to a language without

Easy way to break foldl

久未见 提交于 2019-12-04 06:47:57
I need to break from foldl. Here is a dummy example how to break from fold when I count sum of values in a list and meet too big value (i.e. 10) L = [1,2,3,4,10,5,6,7], Res = try lists:foldl( fun(I, Value) -> if (I < 10) -> Value + I; true -> throw({too_big_value, Value}) end end, 0, L) catch throw:{too_big_value, Value} -> Value end, Res. I know this example is artificial but are there any nice method to break out fold (I know that fold always scan the whole structure)? Please note, that i need to retrieve correct data even if i break from fold. In this case i should get data from previous