def findSteepestDirRotateClasses(self, howmany, LOO = False):
     
     working_set = pyrandom.sample(range(self.size), self.size)
     Y = self.Y[working_set]
     R = self.R[ix_(working_set, working_set)]
     RY = self.RY[working_set]
     Y_Schur_RY = self.Y_Schur_RY[working_set]
     minus_diagRx2 = self.minus_diagRx2[working_set]
     classvec = self.classvec[working_set]
     size = len(working_set)
     
     cython_mmc.findSteepestDirRotateClasses(Y,
                                             R,
                                             RY,
                                             Y_Schur_RY,
                                             self.classFitnessRowVec,
                                             minus_diagRx2,
                                             self.classcounts,
                                             classvec,
                                             size,
                                             self.labelcount,
                                             howmany,
                                             self.sqrtR,
                                             self.sqrtR.shape[1])
     self.Y[working_set] = Y
     self.RY[working_set] = RY
     self.Y_Schur_RY[working_set] = Y_Schur_RY
     self.classvec[working_set] = classvec
     return
 def findSteepestDirRotateClasses(self, howmany, LOO = False):
     #print self.Y.shape,self.R.shape, self.RY.shape, self.Y_Schur_RY.shape, self.classFitnessRowVec.shape, self.mdiagRx2.shape, self.classcounts.shape, self.classvec.shape
     cython_mmc.findSteepestDirRotateClasses(self.Y,
                                             self.R,
                                             self.RY,
                                             self.Y_Schur_RY,
                                             self.classFitnessRowVec,
                                             self.mdiagRx2,
                                             self.classcounts,
                                             self.classvec,
                                             self.size,
                                             self.labelcount,
                                             howmany,
                                             self.sqrtR,
                                             self.sqrtR.shape[1])
     return
     
     #The slow python code. Use the above cython instead.
     for newclazz in range(self.labelcount):
         
         #!!!!!!!!!!!!!!!
         takenum = (self.size / self.labelcount) - self.classcounts[newclazz] + int(howmany)
         
         for h in range(takenum):
             dirsneg = self.classFitnessRowVec + (2 * self.mdiagRx2[:, None] + 4 * multiply(self.Y, self.RY))
             dirsnegdiff = dirsneg - self.classFitnessRowVec
             dirscc = dirsnegdiff[arange(self.size), self.classvec].T
             dirs = dirsnegdiff + dirscc
             dirs[arange(self.size), self.classvec] = float('Inf')
             dirs = dirs[:, newclazz]
             steepestdir = argmin(dirs)
             steepness = amin(dirs)
             oldclazz = self.classvec[steepestdir]
             self.Y[steepestdir, oldclazz] = -1.
             self.Y[steepestdir, newclazz] = 1.
             self.classvec[steepestdir] = newclazz
             self.classcounts[oldclazz] = self.classcounts[oldclazz] - 1
             self.classcounts[newclazz] = self.classcounts[newclazz] + 1
             self.RY[:, oldclazz] = self.RY[:, oldclazz] - 2 * self.R[:, steepestdir]
             self.RY[:, newclazz] = self.RY[:, newclazz] + 2 * self.R[:, steepestdir]
             
             for i in range(self.labelcount):
                 YTRY_i = self.Y[:,i].T * self.RY[:,i]
                 fitness_i = self.size - YTRY_i
                 self.classFitnessRowVec[i] = fitness_i[0, 0]
             
             self.updateA()
         #self.callback()
     return False
    def findSteepestDirRotateClasses_(self, howmany, LOO=False):
        cython_mmc.findSteepestDirRotateClasses(
            self.Y, self.R, self.RY, self.Y_Schur_RY, self.classFitnessRowVec,
            self.minus_diagRx2, self.classcounts, self.classvec, self.size,
            self.labelcount, howmany, self.sqrtR, self.sqrtR.shape[1])
        return

        #The slow python code. Use the above cython instead.
        for newclazz in range(self.labelcount):

            #!!!!!!!!!!!!!!!
            takenum = (self.size / self.labelcount
                       ) - self.classcounts[newclazz] + int(howmany)

            for h in range(takenum):
                dirsneg = self.classFitnessRowVec + (
                    2 * self.minus_diagRx2[:, None] +
                    4 * multiply(self.Y, self.RY))
                dirsnegdiff = dirsneg - self.classFitnessRowVec
                dirscc = dirsnegdiff[arange(self.size), self.classvec].T
                dirs = dirsnegdiff + dirscc
                dirs[arange(self.size), self.classvec] = float('Inf')
                dirs = dirs[:, newclazz]
                steepestdir = argmin(dirs)
                steepness = amin(dirs)
                oldclazz = self.classvec[steepestdir]
                self.Y[steepestdir, oldclazz] = -1.
                self.Y[steepestdir, newclazz] = 1.
                self.classvec[steepestdir] = newclazz
                self.classcounts[oldclazz] = self.classcounts[oldclazz] - 1
                self.classcounts[newclazz] = self.classcounts[newclazz] + 1
                self.RY[:,
                        oldclazz] = self.RY[:,
                                            oldclazz] - 2 * self.R[:,
                                                                   steepestdir]
                self.RY[:,
                        newclazz] = self.RY[:,
                                            newclazz] + 2 * self.R[:,
                                                                   steepestdir]

                for i in range(self.labelcount):
                    YTRY_i = self.Y[:, i].T * self.RY[:, i]
                    fitness_i = self.size - YTRY_i
                    self.classFitnessRowVec[i] = fitness_i[0, 0]

                self.updateA()
            #self.callback()
        return False
    def findSteepestDirRotateClasses(self, howmany, LOO=False):

        working_set = pyrandom.sample(range(self.size), self.size)
        Y = self.Y[working_set]
        R = self.R[ix_(working_set, working_set)]
        RY = self.RY[working_set]
        Y_Schur_RY = self.Y_Schur_RY[working_set]
        minus_diagRx2 = self.minus_diagRx2[working_set]
        classvec = self.classvec[working_set]
        size = len(working_set)

        cython_mmc.findSteepestDirRotateClasses(
            Y, R, RY, Y_Schur_RY, self.classFitnessRowVec, minus_diagRx2,
            self.classcounts, classvec, size, self.labelcount, howmany,
            self.sqrtR, self.sqrtR.shape[1])
        self.Y[working_set] = Y
        self.RY[working_set] = RY
        self.Y_Schur_RY[working_set] = Y_Schur_RY
        self.classvec[working_set] = classvec
        return