def test_where(self): bv = self.bv condition = bv >= 4.5 res = pn.where(condition)[0] for bid, blk in enumerate(res): self.assertTrue(np.allclose(blk, pn.where(bv[bid] >= 4.5))) flat_condition = condition.flatten() res = pn.where(condition, 2.0, 1.0) res_flat = pn.where(flat_condition, 2.0, 1.0) self.assertTrue(np.allclose(res.flatten(), res_flat)) res = pn.where(condition, 2.0, np.ones(bv.size)) res_flat = pn.where(flat_condition, 2.0, np.ones(bv.size)) self.assertTrue(np.allclose(res.flatten(), res_flat)) res = pn.where(condition, np.ones(bv.size) * 2.0, 1.0) res_flat = pn.where(flat_condition, np.ones(bv.size) * 2.0, 1.0) self.assertTrue(np.allclose(res.flatten(), res_flat)) res = pn.where(condition, np.ones(bv.size) * 2.0, np.ones(bv.size)) res_flat = pn.where(flat_condition, np.ones(bv.size) * 2.0, np.ones(bv.size)) self.assertTrue(np.allclose(res.flatten(), res_flat)) bones = BlockVector([np.ones(3), np.ones(4)]) res = pn.where(condition, bones * 2.0, 1.0) res_flat = pn.where(flat_condition, np.ones(bv.size) * 2.0, 1.0) self.assertTrue(np.allclose(res.flatten(), res_flat)) res = pn.where(condition, 2.0, bones) res_flat = pn.where(flat_condition, 2.0, bones) self.assertTrue(np.allclose(res.flatten(), res_flat)) res = pn.where(condition, np.ones(bv.size) * 2.0, bones) res_flat = pn.where(flat_condition, np.ones(bv.size) * 2.0, np.ones(bv.size)) self.assertTrue(np.allclose(res.flatten(), res_flat)) res = pn.where(condition, bones * 2.0, np.ones(bv.size)) res_flat = pn.where(flat_condition, np.ones(bv.size) * 2.0, np.ones(bv.size)) self.assertTrue(np.allclose(res.flatten(), res_flat))
def _create_vectors(self): # Note: This method requires the complicated vars nz to be defined beforehand # init values self._init_x = BlockVector([nlp.x_init() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double)]) self._init_y = BlockVector( [nlp.y_init() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double) for i in range(self.nblocks)]) # lower and upper bounds self._lower_x = BlockVector( [nlp.xl() for nlp in self._nlps] + [np.full(self.nz, -np.inf, dtype=np.double)]) self._upper_x = BlockVector( [nlp.xu() for nlp in self._nlps] + [np.full(self.nz, np.inf, dtype=np.double)]) self._lower_g = BlockVector( [nlp.gl() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double) for i in range(self.nblocks)]) self._upper_g = BlockVector( [nlp.gu() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double) for i in range(self.nblocks)]) # define x maps and masks self._lower_x_mask = np.isfinite(self._lower_x) self._lower_x_map = self._lower_x_mask.nonzero()[0] self._upper_x_mask = np.isfinite(self._upper_x) self._upper_x_map = self._upper_x_mask.nonzero()[0] # define gcd maps and masks bounds_difference = self._upper_g - self._lower_g abs_bounds_difference = np.absolute(bounds_difference) tolerance_equalities = 1e-8 self._c_mask = abs_bounds_difference < tolerance_equalities self._c_map = self._c_mask.nonzero()[0] self._d_mask = abs_bounds_difference >= tolerance_equalities self._d_map = self._d_mask.nonzero()[0] self._lower_g_mask = np.isfinite( self._lower_g) * self._d_mask + self._c_mask self._lower_g_map = self._lower_g_mask.nonzero()[0] self._upper_g_mask = np.isfinite( self._upper_g) * self._d_mask + self._c_mask self._upper_g_map = self._upper_g_mask.nonzero()[0] self._lower_d_mask = pn.isin(self._d_map, self._lower_g_map) self._upper_d_mask = pn.isin(self._d_map, self._upper_g_map) # remove empty vectors at the end of lower and upper d self._lower_d_mask = \ BlockVector([self._lower_d_mask[i] for i in range(self.nblocks)]) self._upper_d_mask = \ BlockVector([self._upper_d_mask[i] for i in range(self.nblocks)]) # define lower and upper d maps self._lower_d_map = pn.where(self._lower_d_mask)[0] self._upper_d_map = pn.where(self._upper_d_mask)[0] # get lower and upper d values self._lower_d = np.compress(self._d_mask, self._lower_g) self._upper_d = np.compress(self._d_mask, self._upper_g) # remove empty vectors at the end of lower and upper d self._lower_d = BlockVector( [self._lower_d[i] for i in range(self.nblocks)]) self._upper_d = BlockVector( [self._upper_d[i] for i in range(self.nblocks)])
def _create_vectors(self): # Note: This method requires the complicated vars nz to be defined beforehand # init values and lower and upper bounds self._init_x = BlockVector(len(self._nlps) + 1) self._init_y = BlockVector(len(self._nlps) + self.nblocks) self._lower_x = BlockVector(len(self._nlps) + 1) self._upper_x = BlockVector(len(self._nlps) + 1) self._lower_g = BlockVector(len(self._nlps) + self.nblocks) self._upper_g = BlockVector(len(self._nlps) + self.nblocks) ndx = 0 for nlp in self._nlps: self._init_x.set_block(ndx, nlp.x_init()) self._init_y.set_block(ndx, nlp.y_init()) self._lower_x.set_block(ndx, nlp.xl()) self._upper_x.set_block(ndx, nlp.xu()) self._lower_g.set_block(ndx, nlp.gl()) self._upper_g.set_block(ndx, nlp.gu()) ndx += 1 self._init_x.set_block(ndx, np.zeros(self.nz, dtype=np.double)) self._lower_x.set_block(ndx, np.full(self.nz, -np.inf, dtype=np.double)) self._upper_x.set_block(ndx, np.full(self.nz, np.inf, dtype=np.double)) for i in range(self.nblocks): self._init_y.set_block(ndx, np.zeros(self.nz, dtype=np.double)) self._lower_g.set_block(ndx, np.zeros(self.nz, dtype=np.double)) self._upper_g.set_block(ndx, np.zeros(self.nz, dtype=np.double)) ndx += 1 # define x maps and masks self._lower_x_mask = np.isfinite(self._lower_x) self._lower_x_map = self._lower_x_mask.nonzero()[0] self._upper_x_mask = np.isfinite(self._upper_x) self._upper_x_map = self._upper_x_mask.nonzero()[0] # define gcd maps and masks bounds_difference = self._upper_g - self._lower_g abs_bounds_difference = np.absolute(bounds_difference) tolerance_equalities = 1e-8 self._c_mask = abs_bounds_difference < tolerance_equalities self._c_map = self._c_mask.nonzero()[0] self._d_mask = abs_bounds_difference >= tolerance_equalities self._d_map = self._d_mask.nonzero()[0] self._lower_g_mask = np.isfinite( self._lower_g) * self._d_mask + self._c_mask self._lower_g_map = self._lower_g_mask.nonzero()[0] self._upper_g_mask = np.isfinite( self._upper_g) * self._d_mask + self._c_mask self._upper_g_map = self._upper_g_mask.nonzero()[0] self._lower_d_mask = pn.isin(self._d_map, self._lower_g_map) self._upper_d_mask = pn.isin(self._d_map, self._upper_g_map) # remove empty vectors at the end of lower and upper d _lower_d_mask = BlockVector(self.nblocks) for i in range(self.nblocks): _lower_d_mask.set_block(i, self._lower_d_mask.get_block(i)) self._lower_d_mask = _lower_d_mask _upper_d_mask = BlockVector(self.nblocks) for i in range(self.nblocks): _upper_d_mask.set_block(i, self._upper_d_mask.get_block(i)) self._upper_d_mask = _upper_d_mask # define lower and upper d maps self._lower_d_map = pn.where(self._lower_d_mask)[0] self._upper_d_map = pn.where(self._upper_d_mask)[0] # get lower and upper d values self._lower_d = np.compress(self._d_mask, self._lower_g) self._upper_d = np.compress(self._d_mask, self._upper_g) # remove empty vectors at the end of lower and upper d _lower_d = BlockVector(self.nblocks) _upper_d = BlockVector(self.nblocks) for i in range(self.nblocks): _lower_d.set_block(i, self._lower_d.get_block(i)) _upper_d.set_block(i, self._upper_d.get_block(i)) self._lower_d = _lower_d self._upper_d = _upper_d
def _create_vectors(self): # Note: This method requires the complicated vars nz to be defined beforehand # init values self._init_x = BlockVector([nlp.x_init() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double)]) self._init_y = BlockVector([nlp.y_init() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double) for i in range(self.nblocks)]) # lower and upper bounds self._lower_x = BlockVector([nlp.xl() for nlp in self._nlps] + [np.full(self.nz, -np.inf, dtype=np.double)]) self._upper_x = BlockVector([nlp.xu() for nlp in self._nlps] + [np.full(self.nz, np.inf, dtype=np.double)]) self._lower_g = BlockVector([nlp.gl() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double) for i in range(self.nblocks)]) self._upper_g = BlockVector([nlp.gu() for nlp in self._nlps] + [np.zeros(self.nz, dtype=np.double) for i in range(self.nblocks)]) # define x maps and masks self._lower_x_mask = np.isfinite(self._lower_x) self._lower_x_map = self._lower_x_mask.nonzero()[0] self._upper_x_mask = np.isfinite(self._upper_x) self._upper_x_map = self._upper_x_mask.nonzero()[0] # define gcd maps and masks bounds_difference = self._upper_g - self._lower_g abs_bounds_difference = np.absolute(bounds_difference) tolerance_equalities = 1e-8 self._c_mask = abs_bounds_difference < tolerance_equalities self._c_map = self._c_mask.nonzero()[0] self._d_mask = abs_bounds_difference >= tolerance_equalities self._d_map = self._d_mask.nonzero()[0] self._lower_g_mask = np.isfinite(self._lower_g) * self._d_mask + self._c_mask self._lower_g_map = self._lower_g_mask.nonzero()[0] self._upper_g_mask = np.isfinite(self._upper_g) * self._d_mask + self._c_mask self._upper_g_map = self._upper_g_mask.nonzero()[0] self._lower_d_mask = pn.isin(self._d_map, self._lower_g_map) self._upper_d_mask = pn.isin(self._d_map, self._upper_g_map) # remove empty vectors at the end of lower and upper d self._lower_d_mask = \ BlockVector([self._lower_d_mask[i] for i in range(self.nblocks)]) self._upper_d_mask = \ BlockVector([self._upper_d_mask[i] for i in range(self.nblocks)]) # define lower and upper d maps self._lower_d_map = pn.where(self._lower_d_mask)[0] self._upper_d_map = pn.where(self._upper_d_mask)[0] # get lower and upper d values self._lower_d = np.compress(self._d_mask, self._lower_g) self._upper_d = np.compress(self._d_mask, self._upper_g) # remove empty vectors at the end of lower and upper d self._lower_d = BlockVector([self._lower_d[i] for i in range(self.nblocks)]) self._upper_d = BlockVector([self._upper_d[i] for i in range(self.nblocks)])