title: Gremlin Big Pi operation (product of a sequence of terms) followed by id-based summation
body: <p>I have a dataset that looks something like this:</p>
  
  <pre><code>V1 = name:"some name1"
  V2 = name:"some name2"
  V3 = name:"some name3"
  V4 = name:"some name4"
  
  E1 = weight:0.2, StartVertex:V1, EndVertex:V2
  E2 = weight:0.3, StartVertex:V1, EndVertex:V3
  E3 = weight:0.4, StartVertex:V1, EndVertex:V4
  E4 = weight:0.5, StartVertex:V2, EndVertex:V1
  E5 = weight:0.6, StartVertex:V2, EndVertex:V3
  ...
  </code></pre>
  
  <p>I have a gremlin query that finds some paths between these vertices. </p>
  
  <p>There is two things I would like to do there. </p>
  
  <blockquote>
    <p>1: I would like to be able to find the product of all the weights in a
    path (path_edge1.weight * path_edge2.weight * ...)</p>
    
    <p>2: I would like to be able to sum the resulting products for each path
    based on the end vertex.</p>
  </blockquote>
  
  <p>Pseudo code for what I want to achieve:</p>
  
  <pre><code>g.V().has('name',REGEX,\".+some_query.+\").inE.outV.inE.FindingAPathSomehow.path{path_score = 1 : foreach edge e: path_score = path_score * e.weight}{it.lastV().id}.sumWhereIdIsEqual(it[1])
  </code></pre>
  
  <p>Hopefully this is somewhat understandable.</p>
  
  <p>I would like to be able to do everything in a pure gremlin/groovy script since I am using RexPro.</p>
  
  <p>I have looked far and wide for an answer, but have not been able to find a way to do this yet.</p>
  
  <hr>
  
  <p><strong>Further explanation if the above is unclear:</strong></p>
  
  <p>When querying I am looking for vertices with a substring equal to the "some_query". This will give me a set of start vertices. </p>
  
  <p>With these vertices I am looking for a specific path in my graph that will give me several paths that might look like this:</p>
  
  <pre><code>V = Vertex
  E = Edge
  
  Path1 = V3 - E2 - V1
  Path2 = V4 - E5 - V7 - E1 - V1
  </code></pre>
  
  <p>Each of these edges has a weight property. With this I want to get what is called <a href="http://en.wikipedia.org/wiki/Multiplication#Capital_Pi_notation" rel="nofollow">"Big Pi" or "Capital Pi"</a> which is the product of a sequence. Think summation (Σ) but with multiplication in stead of addition. </p>
  
  <p>The result of <code>Path1</code> would be the weight of E2, or <code>0.3</code> in the example above. While <code>Path2</code> would have the weight of <code>E5.weight * E1.weight</code> which in the example above would be <code>0.6 * 0.2 = 0.12</code>.</p>
  
  <p>In this case we start at vertices <code>V3</code> and <code>V4</code>, and both end at <code>V1</code>. In this case I would like to sum the weights of <code>Path1</code> and <code>Path2</code> because both end vertices are <code>V1</code>. This would give the total score of <code>V1</code> as <code>0.3 + 0.12 = 0.42</code>. If there had been a <code>Path3</code> with end Vertex <code>V2</code> and score <code>0.34</code>, then the resulting list would have to elements in it; <code>{[V1, 0.42], [V2,0.34] }</code>.</p>