def draw(self, canvas): gx, gy, gz = canvas.grid(samples=10, sparse=False) start = time.time() points = poly.polyval3d(gx, gy, gz, self.c) thorus = numpy.around(points) == 0 print("--- %s seconds ---" % (time.time() - start,)) canvas.setgrid(thorus, downsample=True)
def test_polyval3d(self): x1, x2, x3 = self.x y1, y2, y3 = self.y #test exceptions assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d) #test values tgt = y1*y2*y3 res = poly.polyval3d(x1, x2, x3, self.c3d) assert_almost_equal(res, tgt) #test shape z = np.ones((2,3)) res = poly.polyval3d(z, z, z, self.c3d) assert_(res.shape == (2, 3))
def test_polyval3d(self): x1, x2, x3 = self.x y1, y2, y3 = self.y # test exceptions assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d) # test values tgt = y1 * y2 * y3 res = poly.polyval3d(x1, x2, x3, self.c3d) assert_almost_equal(res, tgt) # test shape z = np.ones((2, 3)) res = poly.polyval3d(z, z, z, self.c3d) assert_(res.shape == (2, 3))
def create_random_poly(embedding_size=256, coefficients=None, dim=3): """ Create a polynomial embedding of the RGB vectors. If the coefficients are given then it is that polynomial, if not - some uniformly random polynomial is chosen, such that each variable is up to the given 'dim' degree :param coefficients: a 4D array, such that the 3D array coefficients[i] contains the coefficients of the 3D polynomial of the i-th coordinate of the 3D function (from (R,G,B) to another 3D space). :param dim: maximal power of the variables in the polynomial. i.e. if the dim is 3 than the monomial of the maximal degree will be x^3 * y^3 * z^3 :return: polynomial mapping of the RGB vectors. """ if coefficients is None: coefficients = np.random.uniform(low=-1, high=1, size=(3, dim, dim, dim)) rgb_vectors = create_rgb(embedding_size) embedding = np.empty_like(rgb_vectors) for i in range(3): embedding[:, i] = P.polyval3d(rgb_vectors[:, 0], rgb_vectors[:, 1], rgb_vectors[:, 2], coefficients[i]) return embedding, coefficients
def draw(self, canvas): gx, gy, gz = canvas.grid() c = numpy.zeros((2,2,2)) c[1,0,0] = self.normal[0] c[0,1,0] = self.normal[1] c[0,0,1] = self.normal[2] points = poly.polyval3d(gx, gy, gz, c) plane = numpy.around(points) == 0 canvas.setgrid(plane)
def newtReconstruct(orders, locations, coeffs,unusedNumPts): if len(locations.shape)==1: return np.array(poly.polyval(locations, coeffs)) else: if locations.shape[1] == 2: return poly.polyval2d(locations[:,0], locations[:,1], coeffs) elif locations.shape[1] == 3: return poly.polyval3d(locations[:,0],locations[:,1],locations[:,2], coeffs) else: return genericNewtVal(locations, coeffs)
def test_polyvander3d(self) : # also tests polyval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) van = poly.polyvander3d(x1, x2, x3, [1, 2, 3]) tgt = poly.polyval3d(x1, x2, x3, c) res = np.dot(van, c.flat) assert_almost_equal(res, tgt) # check shape van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3]) assert_(van.shape == (1, 5, 24))
def test_polyvander3d(self): # also tests polyval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) van = poly.polyvander3d(x1, x2, x3, [1, 2, 3]) tgt = poly.polyval3d(x1, x2, x3, c) res = np.dot(van, c.flat) assert_almost_equal(res, tgt) # check shape van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3]) assert_(van.shape == (1, 5, 24))
def __call__(self, *input_variables): def ensure_the_type(data): if isinstance(data, (numpy.number, int, float, numpy.ndarray)): return data else: return numpy.array(data) if len(input_variables) not in [1, self.variables]: raise ValueError('Got an unexpected number of input arguments') if len(input_variables) == 1: separate = False inp_vars = ensure_the_type(input_variables[0]) else: separate = True inp_vars = [ensure_the_type(entry) for entry in input_variables] # todo: should we evaluate the viability of the input? if self.variables == 1: x = (inp_vars - self._input_offsets[0]) / self._input_scales[0] value = polynomial.polyval(x, self._numerator_array) / \ polynomial.polyval(x, self._denominator_array) elif self.variables == 2: if separate: x = (inp_vars[0] - self._input_offsets[0]) / self._input_scales[0] y = (inp_vars[1] - self._input_offsets[1]) / self._input_scales[1] else: if inp_vars.shape[-1] != 2: raise ValueError( 'Final dimension of input data ({}) must match the number ' 'of variables ({}).'.format(inp_vars.shape, self.variables)) x = (inp_vars[..., 0] - self._input_offsets[0]) / self._input_scales[0] y = (inp_vars[..., 1] - self._input_offsets[1]) / self._input_scales[1] value = polynomial.polyval2d(x, y, self._numerator_array) / \ polynomial.polyval2d(x, y, self._denominator_array) elif self.variables == 3: if separate: x = (inp_vars[0] - self._input_offsets[0]) / self._input_scales[0] y = (inp_vars[1] - self._input_offsets[1]) / self._input_scales[1] z = (inp_vars[2] - self._input_offsets[2]) / self._input_scales[2] else: if inp_vars.shape[-1] != 3: raise ValueError( 'Final dimension of input data ({}) must match the number ' 'of variables ({}).'.format(inp_vars.shape, self.variables)) x = (inp_vars[..., 0] - self._input_offsets[0]) / self._input_scales[0] y = (inp_vars[..., 1] - self._input_offsets[1]) / self._input_scales[1] z = (inp_vars[..., 2] - self._input_offsets[2]) / self._input_scales[2] value = polynomial.polyval3d(x, y, z, self._numerator_array) / \ polynomial.polyval3d(x, y, z, self._denominator_array) else: raise ValueError('More than 3 variables is unsupported') return value * self._output_scale + self._output_offset
print(coeffs) print(coeffs_to_array(coeffs, COLOR_POLYN_DEGREE)) c = np.zeros((3, 3, 3, 3)) c[0][1][0][0] = 1 c[1][0][1][0] = 1 c[2][0][0][1] = 1 print(c) p = coeffs_to_array(c, COLOR_POLYN_DEGREE) print('P', p) c = array_to_coeffs(p, COLOR_POLYN_DEGREE) print('C', c) raise d = np.array([[3, 5, 7]], dtype=np.float) c = np.zeros((3, 3, 3, 3)) c[0][1][0][0] = 1 c[1][0][1][0] = 1 c[2][0][0][1] = 1 print( poly.polyval3d(d[:, 0], d[:, 1], d[:, 2], c[0]), poly.polyval3d(d[:, 0], d[:, 1], d[:, 2], c[1]), poly.polyval3d(d[:, 0], d[:, 1], d[:, 2], c[2]), ) ''' (4, 2) y^0 + y^1 + y^2 + y^3 x^1 + x*y + x*y^2 + x*y^3 x^2 '''