power in order of (point, eadges, surfaces, bodies, ..)
15176
def power_cubic(n, m)
      if n < 1
	raise "cubic(< 1, m) called"
      elsif n == 1
	yield( (0...m).collect{0} )# point of origin
      else
	0.upto m do |dim|
	  Combinatorial.comb(m, dim) do |s|
	    power_cubic(n-1, dim) do |a|
	      yield inject(a, s.sort, m)
	    end
	  end
	end
      end
    end
775264
private void combineHydrogenPositions(List<Integer> taken, List<List<Integer>> combinations,
            IAtomContainer skeleton, int totalMobHydrCount, List<Integer> mobHydrAttachPositions) {
        if (taken.size() != totalMobHydrCount) {
            for (int i = 0; i < mobHydrAttachPositions.size(); i++) {
                int pos = mobHydrAttachPositions.get(i);
                IAtom atom = findAtomByPosition(skeleton, pos);
                int conn = getConnectivity(atom, skeleton);
                int hCnt = 0;
                for (int t : taken)
                    if (t == pos) hCnt++;
                if (atom.getValency() - atom.getFormalCharge() > (hCnt + conn)) {
                    taken.add(pos);
                    combineHydrogenPositions(taken, combinations, skeleton, totalMobHydrCount, mobHydrAttachPositions);
                    taken.remove(taken.size() - 1);
                }
            }
        } else {
            List<Integer> addList = new ArrayList<Integer>(taken.size());
            addList.addAll(taken);
            Collections.sort(addList);
            if (!combinations.contains(addList)) {
                combinations.add(addList);
            }
        }
    }
575105
function(x,y,z,normalX,normalY,normalZ,penetration,flip){
        var p=this.points[this.numPoints++];
        p.position.x=x;
        p.position.y=y;
        p.position.z=z;
        var r=this.body1.rotation;
        var rx=x-this.body1.position.x;
        var ry=y-this.body1.position.y;
        var rz=z-this.body1.position.z;

        var tr = r.elements;
        p.localPoint1.x=rx*tr[0]+ry*tr[3]+rz*tr[6];
        p.localPoint1.y=rx*tr[1]+ry*tr[4]+rz*tr[7];
        p.localPoint1.z=rx*tr[2]+ry*tr[5]+rz*tr[8];
        r=this.body2.rotation;
        rx=x-this.body2.position.x;
        ry=y-this.body2.position.y;
        rz=z-this.body2.position.z;
        p.localPoint2.x=rx*tr[0]+ry*tr[3]+rz*tr[6];
        p.localPoint2.y=rx*tr[1]+ry*tr[4]+rz*tr[7];
        p.localPoint2.z=rx*tr[2]+ry*tr[5]+rz*tr[8];

        p.normalImpulse=0;
        if(flip){
            p.normal.x=-normalX;
            p.normal.y=-normalY;
            p.normal.z=-normalZ;
        }else{
            p.normal.x=normalX;
            p.normal.y=normalY;
            p.normal.z=normalZ;
        }
        p.penetration=penetration;
        p.warmStarted=false;
    }
1343269
function() {

            var start = this.start;
            var controlPoint1 = this.controlPoint1;
            var controlPoint2 = this.controlPoint2;
            var end = this.end;

            var x0 = start.x;
            var y0 = start.y;
            var x1 = controlPoint1.x;
            var y1 = controlPoint1.y;
            var x2 = controlPoint2.x;
            var y2 = controlPoint2.y;
            var x3 = end.x;
            var y3 = end.y;

            var points = new Array(); // local extremes
            var tvalues = new Array(); // t values of local extremes
            var bounds = [new Array(), new Array()];

            var a, b, c, t;
            var t1, t2;
            var b2ac, sqrtb2ac;

            for (var i = 0; i < 2; ++i) {

                if (i === 0) {
                    b = 6 * x0 - 12 * x1 + 6 * x2;
                    a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
                    c = 3 * x1 - 3 * x0;

                } else {
                    b = 6 * y0 - 12 * y1 + 6 * y2;
                    a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
                    c = 3 * y1 - 3 * y0;
                }

                if (abs(a) < 1e-12) { // Numerical robustness
                    if (abs(b) < 1e-12) { // Numerical robustness
                        continue;
                    }

                    t = -c / b;
                    if ((0 < t) && (t < 1)) tvalues.push(t);

                    continue;
                }

                b2ac = b * b - 4 * c * a;
                sqrtb2ac = sqrt(b2ac);

                if (b2ac < 0) continue;

                t1 = (-b + sqrtb2ac) / (2 * a);
                if ((0 < t1) && (t1 < 1)) tvalues.push(t1);

                t2 = (-b - sqrtb2ac) / (2 * a);
                if ((0 < t2) && (t2 < 1)) tvalues.push(t2);
            }

            var j = tvalues.length;
            var jlen = j;
            var mt;
            var x, y;

            while (j--) {
                t = tvalues[j];
                mt = 1 - t;

                x = (mt * mt * mt * x0) + (3 * mt * mt * t * x1) + (3 * mt * t * t * x2) + (t * t * t * x3);
                bounds[0][j] = x;

                y = (mt * mt * mt * y0) + (3 * mt * mt * t * y1) + (3 * mt * t * t * y2) + (t * t * t * y3);
                bounds[1][j] = y;

                points[j] = { X: x, Y: y };
            }

            tvalues[jlen] = 0;
            tvalues[jlen + 1] = 1;

            points[jlen] = { X: x0, Y: y0 };
            points[jlen + 1] = { X: x3, Y: y3 };

            bounds[0][jlen] = x0;
            bounds[1][jlen] = y0;

            bounds[0][jlen + 1] = x3;
            bounds[1][jlen + 1] = y3;

            tvalues.length = jlen + 2;
            bounds[0].length = jlen + 2;
            bounds[1].length = jlen + 2;
            points.length = jlen + 2;

            var left = min.apply(null, bounds[0]);
            var top = min.apply(null, bounds[1]);
            var right = max.apply(null, bounds[0]);
            var bottom = max.apply(null, bounds[1]);

            return new Rect(left, top, (right - left), (bottom - top));
        }
567224
def get_dimension_permutations(entities, dimensions):
    """"""
    new_dimensions = defaultdict(int)
    for item in dimensions:
        new = entities[item['base']].dimensions
        if new:
            for new_item in new:
                new_dimensions[new_item['base']] += new_item['power'] * \
                                                 item['power']
        else:
            new_dimensions[item['base']] += item['power']

    final = [[{'base': i[0], 'power': i[1]} for i in new_dimensions.items()]]
    final.append(dimensions)
    final = [sorted(i, key=lambda x: x['base']) for i in final]

    candidates = []
    for item in final:
        if item not in candidates:
            candidates.append(item)

    return candidates
1076690
def multipointm(self, points):
        """"""
        shapeType = MULTIPOINTM
        points = [points] # nest the points inside a list to be compatible with the generic shapeparts method
        self._shapeparts(parts=points, shapeType=shapeType)
664256
def getbfs(coords, gbasis):
    """"""

    sym2powerlist = {
        'S' : [(0,0,0)],
        'P' : [(1,0,0),(0,1,0),(0,0,1)],
        'D' : [(2,0,0),(0,2,0),(0,0,2),(1,1,0),(0,1,1),(1,0,1)],
        'F' : [(3,0,0),(2,1,0),(2,0,1),(1,2,0),(1,1,1),(1,0,2),
               (0,3,0),(0,2,1),(0,1,2), (0,0,3)]
        }

    bfs = []
    for i, at_coords in enumerate(coords):
        bs = gbasis[i]
        for sym,prims in bs:
            for power in sym2powerlist[sym]:
                bf = cgbf(at_coords,power)
                for expnt,coef in prims:
                    bf.add_pgbf(expnt,coef)
                bf.normalize()
                bfs.append(bf)

    return bfs
590859
private void setHandsComp(int[] otherTiles, List<Mentsu> mentsuList) {
        System.arraycopy(otherTiles, 0, handsComp, 0, otherTiles.length);
        for (Mentsu mentsu : mentsuList) {
            int code = mentsu.getTile().getCode();

            if (mentsu.isOpen()) {
                isOpen = true;
            }

            if (mentsu instanceof Shuntsu) {
                handsComp[code - 1] += 1;
                handsComp[code] += 1;
                handsComp[code + 1] += 1;
            } else if (mentsu instanceof Kotsu) {
                handsComp[code] += 3;
            } else if (mentsu instanceof Kantsu) {
                handsComp[code] += 4;
            } else if (mentsu instanceof Toitsu) {
                handsComp[code] += 2;
            }
        }
    }
1154121
def altitudes(self):
        '''
        

        '''
        A = self.area * 2

        return [A / self.a, A / self.b, A / self.c]
430376
private int ssPartition(int PA, int first, int last, int depth) {
        int a, b;// SA pointer
        int t;
        for (a = first - 1, b = last; ; ) {
            for (; (++a < b) && ((SA[PA + SA[a]] + depth) >= (SA[PA + SA[a] + 1] + 1)); ) {
                SA[a] = ~SA[a];
            }
            for (; (a < --b) && ((SA[PA + SA[b]] + depth) < (SA[PA + SA[b] + 1] + 1)); ) {
            }
            if (b <= a) {
                break;
            }
            t = ~SA[b];
            SA[b] = SA[a];
            SA[a] = t;
        }
        if (first < a) {
            SA[first] = ~SA[first];
        }
        return a;
    }
431684
private boolean separatorDown(Rectangular pos)
    {
        if (pos.getY2() < getTopology().getTopologyHeight()-1)
        {
            Rectangular spos = getTopology().toPixelPositionAbsolute(new Rectangular(pos.getX1(), pos.getY2(), pos.getX2(), pos.getY2()));
            Rectangular epos = getTopology().toPixelPositionAbsolute(new Rectangular(pos.getX1(), pos.getY2() + 1, pos.getX2(), pos.getY2() + 1));
            return seps.isSeparatorAt(spos.midX(), spos.getY2()) ||
                   seps.isSeparatorAt(epos.midX(), epos.getY1());
        }
        else
            return true;
    }
1211773
def scalarcoords(self):
        """"""
        return {k: v.values for k, v in self.coords.items() if v.dims==()}
1348923
void ensureTriangleOrder(List<Point2D_I32> contour ) {
		Element<Corner> e = list.getHead();
		Corner a = e.object;e=e.next;
		Corner b = e.object;e=e.next;
		Corner c = e.object;

		int distB = CircularIndex.distanceP(a.index,b.index,contour.size());
		int distC = CircularIndex.distanceP(a.index,c.index,contour.size());

		if( distB > distC ) {
			list.reset();
			list.pushTail(a);
			list.pushTail(c);
			list.pushTail(b);
		}
	}
883357
function bezierat (a, b, c, d, t) {
   return Math.pow(1-t, 3) * a +
        3 * t * Math.pow(1-t, 2) * b +
        3 * Math.pow(t, 2) * (1 - t) * c +
        Math.pow(t, 3) * d
}
896018
function subdivideLoop(cells, positions) {
  var edges       = nskel(cells, 1)
  var e_incidence = top.incidence(edges, cells)
  var dual        = top.dual(cells, positions.length)
  var npositions  = []
  var ncells      = []
  var e_indices   = new Array(edges.length)
  var v_indices   = new Array(positions.length)
  
  for(var i=0; i<e_indices.length; ++i) {
    e_indices[i] = -1
  }
  for(var i=0; i<v_indices.length; ++i) {
    v_indices[i] = -1
  }
  
  for(var f=0; f<cells.length; ++f) {
    var face = cells[f]
    
    for(var d=0; d<3; ++d) {
      var v = face[d]
      var u = face[(d+1)%3]
      e[0] = v
      e[1] = u
      var e_ptr = top.findCell(edges, e)
      var e_idx = e_indices[e_ptr]
      
      if(e_idx >= 0) {
        e_verts[d] = e_idx
      } else {
        //Compute edge-vertex
        var wing = e_incidence[e_ptr]
        var v0 = positions[u]
        var v1 = positions[v]
        var vertex = new Array(3)
        
        if(wing.length === 2) {
          var v2 = positions[opposite(u, v, cells[wing[0]])]
          var v3 = positions[opposite(u, v, cells[wing[1]])]
          
          for(var i=0; i<3; ++i) {
            vertex[i] = (3.0 * (v0[i] + v1[i]) + v2[i] + v3[i]) / 8.0
          }
        } else {
          for(var i=0; i<3; ++i) {
            vertex[i] = 0.5 * (v0[i] + v1[i])
          }
        }
        
        //Store vertex and continue
        e_indices[e_ptr] = e_verts[d] = npositions.length
        npositions.push(vertex)
      }
      
      if(v_indices[v] >= 0) {
        v_verts[d] = v_indices[v]
      } else {
        //Compute vertex-vertex weight
        
        //First, extract vertex neighborhood (slow and stupid here)
        var star = dual[v]
        var nbhd = [v]
        for(var i=0; i<star.length; ++i) {
          var tri = cells[star[i]]
          for(var j=0; j<3; ++j) {
            if(nbhd.indexOf(tri[j]) !== -1) {
              nbhd.push(tri[j])
            }
          }
        }
        
        //Next, compute weights
        var beta = (star.length === 3 ? 3.0/16.0 : 3.0/(8.0*star.length) )
        var center_weight = 1.0 - star.length * beta
        
        //Finally sum up weights
        var pos  = positions[v]
        var vertex = [0.1,0.1,0.1]
        for(var i=0; i<3; ++i) {
          vertex[i] = center_weight * pos[i]
        }
        for(var i=1; i<nbhd.length; ++i) {
          var p = positions[nbhd[i]]
          for(var j=0; j<3; ++j) {
            vertex[j] += beta * p[j]
          }
        }

        //Store result and continue        
        v_verts[d] = v_indices[v] = npositions.length
        npositions.push(vertex)
      }
    }
    
    //Add subdivided faces
    ncells.push([v_verts[0], e_verts[0], e_verts[2]],
                [e_verts[0], e_verts[1], e_verts[2]],
                [e_verts[0], v_verts[1], e_verts[1]],
                [e_verts[1], v_verts[2], e_verts[2]])
  }
  
  return { positions: npositions, cells: ncells }
}
1286280
def sorted(cls, items, orders):
    ''''''
    return sorted(items, cmp=cls.multipleOrderComparison(orders))
1117262
def element_order(element, modulus, phi_m, phi_m_decomposition):
        
        if element == 1:
            return 1  # by definition

        if pow(element, phi_m, modulus) != 1:
            return None  # not an element of the group

        order = phi_m
        for factor, power in list(phi_m_decomposition.items()):
            for p in range(1, power + 1):
                next_order = order // factor
                if pow(element, next_order, modulus) == 1:
                    order = next_order
                else:
                    break
        return order