2

Let's suppose I have n arrays, where n is a variable (some number greater than 2, usually less than 10).

Each array has k elements.

I also have an array of length n that contains a set of weights that dictate how I would like to linearly combine all the arrays.

I am trying to create a high performance higher order function to combine these arrays in F#.

How can I do this, so that I get a function that takes an array of arrays (arrs is a sample), a weights array (weights), and then computed a weighted sum based on the weights?

let weights = [|.6;;.3;.1|]

let arrs = [| [|.0453;.065345;.07566;1.562;356.6|] ; 
              [|.0873;.075565;.07666;1.562222;3.66|] ; 
              [|.06753;.075675;.04566;1.452;3.4556|] |]

thanks for any ideas.

fs_tech
  • 193
  • 12
  • Can you give an example output? It's not clear to me if you want just one number at the end, or an array of k with the sum of the n elements. – Benjol Feb 24 '10 at 19:30

2 Answers2

6

Here's one solution:

let combine weights arrs =
  Array.map2 (fun w -> Array.map ((*) w)) weights arrs 
  |> Array.reduce (Array.map2 (+))

EDIT

Here's some (much needed) explanation of how this works. Logically, we want to do the following:

  1. Apply each weight to its corresponding row.
  2. Add together the weight-adjusted rows.

The two lines above do just that.

  1. We use the Array.map2 function to combine corresponding weights and rows; the way that we combine them is to multiply each element in the row by the weight, which is accomplished via the inner Array.map.
  2. Now we have an array of weighted rows and need to add them together. We can do this one step at a time by keeping a running sum, adding each array in turn. The way we sum two arrays pointwise is to use Array.map2 again, using (+) as the function for combining the elements from each. We wrap this in an Array.reduce to apply this addition function to each row in turn, starting with the first row.

Hopefully this is a reasonably elegant approach to the problem, though the point-free style admittedly makes it a bit tricky to follow. However, note that it's not especially performant; doing in-place updates rather than creating new arrays with each application of map, map2, and reduce would be more efficient. Unfortunately, the standard library doesn't contain nice analogues of these operations which work in-place. It would be relatively easy to create such analogues, though, and they could be used in almost exactly the same way as I've done here.

kvb
  • 54,864
  • 2
  • 91
  • 133
  • @David - thanks. There are so many useful combinators in the standard library that it's often just a question of how to combine them to get what you want. – kvb Feb 24 '10 at 20:43
  • @kvb, That could do with some explanation. It took me a while to work out where the second argument for the first map2 function went. It should be 'T1 -> 'T2 -> 'U, but I only see w. If I have understood correctly, partial application of the map2 function to weights yields another function to be applied to arrs? (Brain explodes) – Benjol Feb 25 '10 at 05:57
  • 1
    Added to my list of handy F# snippets. http://stackoverflow.com/questions/833180/handy-f-snippets/2332111#2332111 This solution blows me away because 1) it's so elegant, and 2) I'd never have found it. – Benjol Feb 25 '10 at 06:30
  • @Benjol - yes, it desperately needed explanation, which I've added above. I'm glad that you found it elegant; thanks for the kind words. – kvb Feb 25 '10 at 15:48
  • It could also be written like: "let combine w = Array.map2 ((*) >> Array.map) w >> Array.reduce (Array.map2 (+))" though your solution is easier to understand or explain. – Jake Feb 26 '10 at 19:02
1

Something like this did it for me:

let weights = [|0.6;0.3;0.1|]

let arrs = [| [|0.0453;0.065345;0.07566;1.562;356.6|] ; 
              [|0.0873;0.075565;0.07666;1.562222;3.66|] ; 
              [|0.06753;0.075675;0.04566;1.452;3.4556|] |]

let applyWeight x y = x * y

let rotate (arr:'a[][]) = 
    Array.map (fun y -> (Array.map (fun x -> arr.[x].[y])) [|0..arr.Length - 1|]) [|0..arr.[0].Length - 1|]

let weightedarray = Array.map (fun x -> Array.map(applyWeight (fst x)) (snd x)) (Array.zip weights arrs)

let newarrs = Array.map Array.sum (rotate weightedarray)

printfn "%A" newarrs

By the way.. the 0 preceding a float value is necessary.

David Morton
  • 16,338
  • 3
  • 63
  • 73