jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def summarize_weights(weights, sess=None):
    '''Print summary of each weight tensor in a list of weight tensors.
    Example usage:
    summarize_weights(model.trainable_weights)

    if sess is provided, also print weight min, max, and RMS
    '''

    if sess:
        vals = sess.run(weights)
    total_params = 0
    titl = '  %50s: %10s %-20s' % ('NAME', 'SIZE', 'SHAPE')
    if sess:
        titl += ' %10s, %10s, %10s' % ('MIN', 'MAX', 'RMS')
    print(titl)
    for ii,var in enumerate(weights):
        st = '  %50s: %10d %-20s' % (var.name, np.prod(var.get_shape().as_list()), var.get_shape().as_list())
        if sess:
            val = vals[ii]
            st += ' %10s, %10s, %10s' % ('%.3g' % val.min(), '%.3g' % val.max(), '%.3g' % np.sqrt((val**2).mean()))
        print(st)
        total_params += np.prod(var.get_shape().as_list())
    print('  %50s: %10d' % ('Total', total_params))
    return total_params
//  [python]
def update_params_from_1darr(self,x):
		n=0
		for i in range(0,len(self.model.layers)):
			if self.model.layers[i].trainable:
				shape = self.model.layers[i].W.shape
				mm=n.prod(shape)
				self.model.layers[i].W=x[n:n+mm].reshape(shape)
				n+=mm
				
				shape = self.model.layers[i].b.shape
				mm=n.prod(shape)
				self.model.layers[i].b=x[n:n+mm].reshape(shape)
				n+=mm
//  [python]
def update_params_from_1darr(self,x):
		n=0
		for i in range(0,len(self.model.layers)):
			if self.model.layers[i].trainable:
				shape = self.model.layers[i].W.shape
				mm=numpy.prod(shape)
				self.model.layers[i].W=x[n:n+mm].reshape(shape)
				n+=mm
				
				shape = self.model.layers[i].b.shape
				mm=numpy.prod(shape)
				self.model.layers[i].b=x[n:n+mm].reshape(shape)
				n+=mm
//  [python]
def test_rearrange_consistency_numpy(self):
        shape = [1, 2, 3, 5, 7, 11]
        x = numpy.arange(numpy.prod(shape)).reshape(shape)
        for pattern in [
            'a b c d e f -> a b c d e f',
            'b a c d e f -> a b d e f c',
            'a b c d e f -> f e d c b a',
            'a b c d e f -> (f e) d (c b a)',
            'a b c d e f -> (f e d c b a)',
        ]:
            result = rearrange(x, pattern)
            assert len(numpy.setdiff1d(x, result)) == 0
            assert result.dtype == x.dtype

        result = rearrange(x, 'a b c d e f -> a (b) (c d e) f')
        assert numpy.array_equal(x.flatten(), result.flatten())

        result = rearrange(x, 'a aa aa1 a1a1 aaaa a11 -> a aa aa1 a1a1 aaaa a11')
        assert numpy.array_equal(x, result)

        result1 = rearrange(x, 'a b c d e f -> f e d c b a')
        result2 = rearrange(x, 'f e d c b a -> a b c d e f')
        assert numpy.array_equal(result1, result2)

        result = rearrange(rearrange(x, 'a b c d e f -> (f d) c (e b) a'), '(f d) c (e b) a -> a b c d e f', b=2, d=5)
        assert numpy.array_equal(x, result)

        sizes = dict(zip('abcdef', shape))
        temp = rearrange(x, 'a b c d e f -> (f d) c (e b) a', **sizes)
        result = rearrange(temp, '(f d) c (e b) a -> a b c d e f', **sizes)
        assert numpy.array_equal(x, result)

        x2 = numpy.arange(2 * 3 * 4).reshape([2, 3, 4])
        result = rearrange(x2, 'a b c -> b c a')
        assert x2[1, 2, 3] == result[2, 3, 1]
        assert x2[0, 1, 2] == result[1, 2, 0]
//  [python]
def _diag_gauss_den(x, mu, va, log):
    """ This function is the actual implementation
    of gaussian pdf in scalar case. It assumes all args
    are conformant, so it should not be used directly
    
    Call gauss_den instead"""
    # Diagonal matrix case
    d = mu.size
    # n   = x.shape[0]
    if not log:
        inva = 1 / va[0]
        fac = (2 * N.pi) ** (-d / 2.0) * N.prod(N.sqrt(inva))
        inva *= -0.5
        x = x - mu
        x **= 2
        y = fac * N.exp(N.dot(x, inva))
    else:
        # XXX optimize log case as non log case above
        y = _scalar_gauss_den(x[:, 0], mu[0, 0], va[0, 0], log)
        for i in range(1, d):
            y += _scalar_gauss_den(x[:, i], mu[0, i], va[0, i], log)
    return y
//  [python]
Jd = (6,6)

N = NUFFT()
N.plan(om, Nd, Kd, Jd)
y = N.forward(x0)
# A.x2xx(), A.xx2x() A.k2y() A.y2k(), A.xx2k(), A.k2xx(), 
m, n, prodk = om.shape[0],n, n
//  [python]
Jd = (6,6)

N = NUFFT()
N.plan(om, Nd, Kd, Jd)
y = N.forward(x0)
# A.x2xx(), A.xx2x() A.k2y() A.y2k(), A.xx2k(), A.k2xx(), 
m, n, prodk = om.shape[0],numpy.prod(Nd), numpy.prod(Kd)
//  [python]
def pos2ind(pos, size):
  idx = 0
  for i, p in enumerate(pos):
    idx += p * n.prod(size[i + 1:])
  return idx
//  [python]
def pos2ind(pos, size):
  idx = 0
  for i, p in enumerate(pos):
    idx += p * np.prod(size[i + 1:])
  return idx
//  [python]
def get_output_buffers(output_shapes, device, is_float16=False):
        """Returns a dictionary of output name as key, and 1D tensor as value. The tensor has enough space for given shape."""
        data_type = torch.float16 if is_float16 else torch.float32

        output_buffers = {}
        for name, shape in output_shapes.items():
            output_buffers[name] = torch.empty(numpy.prod(shape), dtype=data_type, device=device)
        return output_buffers
//  [python]
def test_getitem(self):
        a = cupy.arange(numpy.prod(self.shape)).reshape(self.shape)
        indexes_gpu = []
        for s in self.indexes:
            if isinstance(s, numpy.ndarray):
                s = cupy.array(s)
            indexes_gpu.append(s)
        indexes_gpu = tuple(indexes_gpu)
        b = a[indexes_gpu]
        b_cpu = a.get()[self.indexes]
        testing.assert_array_equal(b, b_cpu)
//  [python]
def test_setitem(self):
        a_cpu = numpy.arange(numpy.prod(self.shape)).reshape(self.shape)
        a = cupy.array(a_cpu)
        indexes_gpu = []
        for s in self.indexes:
            if isinstance(s, numpy.ndarray):
                s = cupy.array(s)
            indexes_gpu.append(s)
        indexes_gpu = tuple(indexes_gpu)
        a[indexes_gpu] = -1
        a_cpu[self.indexes] = -1
        testing.assert_array_equal(a, a_cpu)
//  [python]
def test_untied_biases():
    x = tensor.tensor4('x')
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)
    conv = Convolutional(filter_size, num_filters, num_channels,
                         weights_init=Constant(1.), biases_init=Constant(2.),
                         image_size=(28, 30), tied_biases=False)
    conv.initialize()

    y = conv.apply(x)
    func = function([x], y)

    # Untied biases provide a bias for every individual output
    assert_allclose(conv.b.eval().shape, (3, 26, 28))

    # Untied biases require images of a specific size
    x_val_1 = numpy.ones((batch_size, num_channels, 28, 30),
                         dtype=theano.config.floatX)

    assert_allclose(func(x_val_1),
                    n.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 26, 28)) + 2)

    x_val_2 = numpy.ones((batch_size, num_channels, 23, 19),
                         dtype=theano.config.floatX)

    def wrongsize():
        func(x_val_2)

    assert_raises_regexp(ValueError, 'Input dimension mis-match',
                         wrongsize)
//  [python]
def test_untied_biases():
    x = tensor.tensor4('x')
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)
    conv = Convolutional(filter_size, num_filters, num_channels,
                         weights_init=Constant(1.), biases_init=Constant(2.),
                         image_size=(28, 30), tied_biases=False)
    conv.initialize()

    y = conv.apply(x)
    func = function([x], y)

    # Untied biases provide a bias for every individual output
    assert_allclose(conv.b.eval().shape, (3, 26, 28))

    # Untied biases require images of a specific size
    x_val_1 = numpy.ones((batch_size, num_channels, 28, 30),
                         dtype=theano.config.floatX)

    assert_allclose(func(x_val_1),
                    numpy.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 26, 28)) + 2)

    x_val_2 = numpy.ones((batch_size, num_channels, 23, 19),
                         dtype=theano.config.floatX)

    def wrongsize():
        func(x_val_2)

    assert_raises_regexp(ValueError, 'Input dimension mis-match',
                         wrongsize)
//  [python]
def normal(self, size, avg=0.0, std=1.0, ndim=None,
               dtype=None, nstreams=None):
        """
        Parameters
        ----------
        size
            Can be a list of integers or Theano variables (ex: the shape
            of another Theano Variable).
        dtype
            The output data type. If dtype is not specified, it will be
            inferred from the dtype of low and high, but will be at
            least as precise as floatX.
        nstreams
            Number of streams.

        """
        # We need an even number of ]0,1[ samples. Then we split them
        # in two halves. First half becomes our U1's for Box-Muller,
        # second half our U2's. See Wikipedia page:
        # http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
        avg = as_tensor_variable(avg)
        std = as_tensor_variable(std)

        if dtype is None:
            dtype = scal.upcast(config.floatX, avg.dtype, std.dtype)

        avg = cast(avg, dtype)
        std = cast(std, dtype)

        evened = False
        constant = False
        if isinstance(size, tuple) and all([isinstance(i, (numpy.integer, int)) for i in size]):
            constant = True
            # Force dtype because it defaults to float when size is empty
            n_samples = n.prod(size, dtype='int64')

            if n_samples % 2 == 1:
                n_samples += 1
                evened = True
        else:
            # if even, don't change, if odd, +1
            n_samples = prod(size) + (prod(size) % 2)
        flattened = self.uniform(size=(n_samples,), dtype=dtype,
                                 nstreams=nstreams)

        if constant:
            U1 = flattened[:n_samples // 2]
            U2 = flattened[n_samples // 2:]
        else:
            U1 = flattened[:n.prod(flattened.shape) // 2]
            U2 = flattened[n.prod(flattened.shape) // 2:]

        #normal_samples = zeros_like(flattened)
        sqrt_ln_U1 = sqrt(-2.0 * log(U1))
        # TypeError: 'TensorVariable' object does not support item assignment
        # so this doesn't work...
        #normal_samples[:n_samples/2] = sqrt_ln_U1 * cos(2.0*numpy.pi*U2)
        #normal_samples[n_samples/2:] = sqrt_ln_U1 * sin(2.0*numpy.pi*U2)

        # so trying this instead
        first_half = sqrt_ln_U1 * cos(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2)
        second_half = sqrt_ln_U1 * sin(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2)
        normal_samples = join(0, first_half, second_half)

        final_samples = None
        if evened:
            final_samples = normal_samples[:-1]
        elif constant:
            final_samples = normal_samples
        else:
            final_samples = normal_samples[:prod(size)]

        if not size:
            # Force the dtype to be int64, otherwise reshape complains
            size = tensor.constant(size, dtype='int64')
        final_samples = final_samples.reshape(size)

        final_samples = avg + std * final_samples

        assert final_samples.dtype == dtype
        return final_samples
//  [python]
def normal(self, size, avg=0.0, std=1.0, ndim=None,
               dtype=None, nstreams=None):
        """
        Parameters
        ----------
        size
            Can be a list of integers or Theano variables (ex: the shape
            of another Theano Variable).
        dtype
            The output data type. If dtype is not specified, it will be
            inferred from the dtype of low and high, but will be at
            least as precise as floatX.
        nstreams
            Number of streams.

        """
        # We need an even number of ]0,1[ samples. Then we split them
        # in two halves. First half becomes our U1's for Box-Muller,
        # second half our U2's. See Wikipedia page:
        # http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
        avg = as_tensor_variable(avg)
        std = as_tensor_variable(std)

        if dtype is None:
            dtype = scal.upcast(config.floatX, avg.dtype, std.dtype)

        avg = cast(avg, dtype)
        std = cast(std, dtype)

        evened = False
        constant = False
        if isinstance(size, tuple) and all([isinstance(i, (numpy.integer, int)) for i in size]):
            constant = True
            # Force dtype because it defaults to float when size is empty
            n_samples = numpy.prod(size, dtype='int64')

            if n_samples % 2 == 1:
                n_samples += 1
                evened = True
        else:
            # if even, don't change, if odd, +1
            n_samples = prod(size) + (prod(size) % 2)
        flattened = self.uniform(size=(n_samples,), dtype=dtype,
                                 nstreams=nstreams)

        if constant:
            U1 = flattened[:n_samples // 2]
            U2 = flattened[n_samples // 2:]
        else:
            U1 = flattened[:prod(flattened.shape) // 2]
            U2 = flattened[prod(flattened.shape) // 2:]

        #normal_samples = zeros_like(flattened)
        sqrt_ln_U1 = sqrt(-2.0 * log(U1))
        # TypeError: 'TensorVariable' object does not support item assignment
        # so this doesn't work...
        #normal_samples[:n_samples/2] = sqrt_ln_U1 * cos(2.0*numpy.pi*U2)
        #normal_samples[n_samples/2:] = sqrt_ln_U1 * sin(2.0*numpy.pi*U2)

        # so trying this instead
        first_half = sqrt_ln_U1 * cos(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2)
        second_half = sqrt_ln_U1 * sin(numpy.array(2.0 * numpy.pi, dtype=dtype) * U2)
        normal_samples = join(0, first_half, second_half)

        final_samples = None
        if evened:
            final_samples = normal_samples[:-1]
        elif constant:
            final_samples = normal_samples
        else:
            final_samples = normal_samples[:prod(size)]

        if not size:
            # Force the dtype to be int64, otherwise reshape complains
            size = tensor.constant(size, dtype='int64')
        final_samples = final_samples.reshape(size)

        final_samples = avg + std * final_samples

        assert final_samples.dtype == dtype
        return final_samples
//  [python]
def test_tied_biases():
    x = tensor.tensor4('x')
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)

    # Tied biases are the default
    conv = Convolutional(filter_size, num_filters, num_channels,
                         weights_init=Constant(1.), biases_init=Constant(2.))
    conv.initialize()
    y = conv.apply(x)
    func = function([x], y)

    # Tied biases only provide one bias for each filter
    assert_allclose(conv.b.eval().shape, (3,))

    # Tied biases allows to pass images of different sizes
    x_val_1 = numpy.ones((batch_size, num_channels, 10,
                          12), dtype=theano.config.floatX)
    x_val_2 = numpy.ones((batch_size, num_channels, 23,
                          19), dtype=theano.config.floatX)

    assert_allclose(func(x_val_1),
                    n.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 8, 10)) + 2)
    assert_allclose(func(x_val_2),
                    n.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 21, 17)) + 2)
//  [python]
def test_tied_biases():
    x = tensor.tensor4('x')
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)

    # Tied biases are the default
    conv = Convolutional(filter_size, num_filters, num_channels,
                         weights_init=Constant(1.), biases_init=Constant(2.))
    conv.initialize()
    y = conv.apply(x)
    func = function([x], y)

    # Tied biases only provide one bias for each filter
    assert_allclose(conv.b.eval().shape, (3,))

    # Tied biases allows to pass images of different sizes
    x_val_1 = numpy.ones((batch_size, num_channels, 10,
                          12), dtype=theano.config.floatX)
    x_val_2 = numpy.ones((batch_size, num_channels, 23,
                          19), dtype=theano.config.floatX)

    assert_allclose(func(x_val_1),
                    numpy.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 8, 10)) + 2)
    assert_allclose(func(x_val_2),
                    numpy.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 21, 17)) + 2)
//  [python]
def ndarray_unpack(v, shapes):

    index=0
    r=[]
    for shape in shapes:
        size = numpy.prod(shape)
        r.append(v[index:(index+size)].reshape(shape))
        index += size
    return r
//  [python]
def merge_axes(x, x_shape, axes):
    assert(len(axes) > 1)

    axes.sort()

    non_axes=[]
    for i in range(len(x_shape)):
        if i not in axes:
            non_axes.append(i)

    size = numpy.prod([x_shape[axis] for axis in axes])

    y = x.dimshuffle(tuple(not_axes + axes))
    y = y.flatten(len(non_axes) + 1)

    shuffle = list(range(len(non_axes)))
    shuffle.insert(axes[0], len(non_axes)+1)
    y = y.dimshuffle(shuffle)
    return y
//  [python]
def get_values_in_store(self, indices, attributes):
        array_of_indices = self._to_arrays_of_indices(indices)
        mapping_from_attribute_to_result = {}    
        one_dimensional_array_of_indices = [x.reshape(-1) for x in array_of_indices]
        for getter in self.select_getters_for(attributes):
            result = getter.get_attribute_values(self, attributes, *one_dimensional_array_of_indices)
            mapping_from_attribute_to_result.update(result)
            
        results = []
        for attribute in attributes:
            returned_value = mapping_from_attribute_to_result[attribute]
            
            if len(array_of_indices)==0:
                value=returned_value
            elif len(array_of_indices[0].shape) == 0:
                value = returned_value[0]
            else:
                if len(returned_value)!=numpy.prod(array_of_indices[0].shape):
                    raise Exception("unexpected mismatch of array shapes")
                if isinstance(returned_value,list):
                  returned_value=numpy.asarray(returned_value)
                value = returned_value.reshape(array_of_indices[0].shape+returned_value.shape[1:])
                
            results.append(value)
            
        return results
//  [python]
def allow_sparsity_with_best_practice(name, weight_shape):
    shape_str = " x ".join(list(map(str, weight_shape)))
    if weight_shape[1] % 4 != 0:
        log(f"Ingore sparsity for {name}, {shape_str}, due to weight.shape(1)[{int(weight_shape[1])} % 4 != 0]")
        return False

    shape = list(weight_shape)
    if len(shape) == 2:
        shape += [1, 1]

    RS = n.prod(shape[2:])
    if RS > 32:
        log(f"Ingore sparsity for {name}, {shape_str} due to RS [{RS}] > 32")
        return False

    CRS = n.prod(shape[1:])
    if RS > 1 and CRS < 512 or RS == 1 and CRS < 4096:
        log(f"Ingore sparsity for {name}, {shape_str} due to RS[{RS}] > 1 and CRS[{CRS}] < 512 or RS == 1 and CRS < 4096")
        return False

    if name.find("rbr_reparam") != -1:
        log(f"Warning💡, Suspected re-parameterization module found({name}, {shape_str}). For re-parameterization modules, you must handle them specially and use set_mask/SparsityMaskFrequency to manage masks manually.")

    log(f"Enable sparsity: {name}, {shape_str}")
    return True
//  [python]
def allow_sparsity_with_best_practice(name, weight_shape):
    shape_str = " x ".join(list(map(str, weight_shape)))
    if weight_shape[1] % 4 != 0:
        log(f"Ingore sparsity for {name}, {shape_str}, due to weight.shape(1)[{int(weight_shape[1])} % 4 != 0]")
        return False

    shape = list(weight_shape)
    if len(shape) == 2:
        shape += [1, 1]

    RS = np.prod(shape[2:])
    if RS > 32:
        log(f"Ingore sparsity for {name}, {shape_str} due to RS [{RS}] > 32")
        return False

    CRS = np.prod(shape[1:])
    if RS > 1 and CRS < 512 or RS == 1 and CRS < 4096:
        log(f"Ingore sparsity for {name}, {shape_str} due to RS[{RS}] > 1 and CRS[{CRS}] < 512 or RS == 1 and CRS < 4096")
        return False

    if name.find("rbr_reparam") != -1:
        log(f"Warning💡, Suspected re-parameterization module found({name}, {shape_str}). For re-parameterization modules, you must handle them specially and use set_mask/SparsityMaskFrequency to manage masks manually.")

    log(f"Enable sparsity: {name}, {shape_str}")
    return True
//  [python]
def test_transpose(self):
        knots1 = [0,0,0.4,0.6,1,1]
        degree = 1
        basis1 = BSplineBasis(knots1,degree)

        knots2 = [0.,0.,0.5,0.6,1,1]
        degree = 1
        basis2 = BSplineBasis(knots2,degree)

        basis3 = MonomialBasis(3);

        mbasis1 = TensorBasis([basis1,basis2]);
        mbasis2 = TensorBasis([basis2,basis3]);

        shape1 = [4,4,2,4]
        shape2 = [4,4,3,3]
        coeff1 = DTensor(numpy.random.randn(n.prod(shape1),1),shape1)
        coeff2 = DTensor(numpy.random.randn(n.prod(shape2),1),shape2)
        func1 = Function(mbasis1,coeff1)
        func2 = Function(mbasis2,coeff2)

        func1t = func1.transpose();
        func2t = func2.transpose();

        self.assertEqualT(func1t.shape,[4,2])
        self.assertEqualT(func2t.shape,[3,3])
        self.assertEqualT(func2t.transpose().shape,func2.shape)
//  [python]
def test_transpose(self):
        knots1 = [0,0,0.4,0.6,1,1]
        degree = 1
        basis1 = BSplineBasis(knots1,degree)

        knots2 = [0.,0.,0.5,0.6,1,1]
        degree = 1
        basis2 = BSplineBasis(knots2,degree)

        basis3 = MonomialBasis(3);

        mbasis1 = TensorBasis([basis1,basis2]);
        mbasis2 = TensorBasis([basis2,basis3]);

        shape1 = [4,4,2,4]
        shape2 = [4,4,3,3]
        coeff1 = DTensor(numpy.random.randn(numpy.prod(shape1),1),shape1)
        coeff2 = DTensor(numpy.random.randn(numpy.prod(shape2),1),shape2)
        func1 = Function(mbasis1,coeff1)
        func2 = Function(mbasis2,coeff2)

        func1t = func1.transpose();
        func2t = func2.transpose();

        self.assertEqualT(func1t.shape,[4,2])
        self.assertEqualT(func2t.shape,[3,3])
        self.assertEqualT(func2t.transpose().shape,func2.shape)
//  [python]
def CalculateSimpleTopoIndex(mol):
    """
    #################################################################
    Calculation of the logarithm of the simple topological index by Narumi,

    which is defined as the product of the vertex degree.

    ---->Sito

    Usage:

        result=CalculateSimpleTopoIndex(mol)

        Input: mol is a molecule object

        Output: result is a numeric value
    #################################################################
    """
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    while 0 in deltas:
        deltas.remove(0)
    deltas = numpy.array(deltas, "d")

    res = 1.0
    for x in deltas:
        res *= x
    if res > 0:
        return numpy.log10(res)
    else:
        return "NaN"
//  [python]
def CalculateSimpleTopoIndex(mol):
    """
    #################################################################
    Calculation of the logarithm of the simple topological index by Narumi,

    which is defined as the product of the vertex degree.

    ---->Sito

    Usage:

        result=CalculateSimpleTopoIndex(mol)

        Input: mol is a molecule object

        Output: result is a numeric value
    #################################################################
    """
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    while 0 in deltas:
        deltas.remove(0)
    deltas = numpy.array(deltas, "d")

    res = numpy.prod(deltas)
    if res > 0:
        return numpy.log10(res)
    else:
        return "NaN"
//  [python]
def CalculateGeometricTopoIndex(mol):
    """
    #################################################################
    Geometric topological index by Narumi

    ---->Geto

    Usage:

        result=CalculateGeometricTopoIndex(mol)

        Input: mol is a molecule object

        Output: result is a numeric value
    #################################################################
    """
    nAtoms = mol.GetNumAtoms()
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    while 0 in deltas:
        deltas.remove(0)
    deltas = numpy.array(deltas, "d")

    temp = reduce(lambda x, y: x*y, deltas)
    res = numpy.power(temp, 1.0 / nAtoms)

    return res
//  [python]
def CalculateGeometricTopoIndex(mol):
    """
    #################################################################
    Geometric topological index by Narumi

    ---->Geto

    Usage:

        result=CalculateGeometricTopoIndex(mol)

        Input: mol is a molecule object

        Output: result is a numeric value
    #################################################################
    """
    nAtoms = mol.GetNumAtoms()
    deltas = [x.GetDegree() for x in mol.GetAtoms()]
    while 0 in deltas:
        deltas.remove(0)
    deltas = numpy.array(deltas, "d")

    temp = numpy.prod(deltas)
    res = numpy.power(temp, 1.0 / nAtoms)

    return res
//  [python]
def build_coo(self, isOptimized=True):
    if len(self.pre_size) != len(self.post_size):
      raise ValueError('The dimensions of shapes of two objects to establish connections should '
                       f'be the same. But we got dimension {len(self.pre_size)} != {len(self.post_size)}. '
                       f'Specifically, pre size = {self.pre_size}, post size = {self.post_size}')
    self.rng = np.random.RandomState(self.seed)
    numba_seed(self.seed)

    # connections
    n_dim = len(self.pre_size)
    if n_dim == 1:
      f = self._connect_1d_jit
    elif n_dim == 2:
      f = self._connect_2d_jit
    elif n_dim == 3:
      f = self._connect_3d_jit
    elif n_dim == 4:
      f = self._connect_4d_jit
    else:
      raise NotImplementedError('Does not support the network dimension bigger than 4.')

    pre_size = np.asarray(self.pre_size)
    post_size = np.asarray(self.post_size)
    connected_pres = []
    connected_posts = []
    pre_ids = np.meshgrid(*(np.arange(p) for p in self.pre_size), indexing='ij')
    pre_ids = tuple([(np.moveaxis(p, 0, 1).flatten()) if p.ndim > 1 else p.flatten() for p in pre_ids])
    size = np.multiply.reduce(pre_size)

    for i in range(size):
      pre_pos = np.asarray([p[i] for p in pre_ids])
      pres, posts = f(pre_pos, pre_size=pre_size, post_size=post_size, n_dim=n_dim)
      connected_pres.extend(pres)
      connected_posts.extend(posts)
    return np.asarray(connected_pres), np.asarray(connected_posts)
//  [python]
def build_coo(self, isOptimized=True):
    if len(self.pre_size) != len(self.post_size):
      raise ValueError('The dimensions of shapes of two objects to establish connections should '
                       f'be the same. But we got dimension {len(self.pre_size)} != {len(self.post_size)}. '
                       f'Specifically, pre size = {self.pre_size}, post size = {self.post_size}')
    self.rng = np.random.RandomState(self.seed)
    numba_seed(self.seed)

    # connections
    n_dim = len(self.pre_size)
    if n_dim == 1:
      f = self._connect_1d_jit
    elif n_dim == 2:
      f = self._connect_2d_jit
    elif n_dim == 3:
      f = self._connect_3d_jit
    elif n_dim == 4:
      f = self._connect_4d_jit
    else:
      raise NotImplementedError('Does not support the network dimension bigger than 4.')

    pre_size = np.asarray(self.pre_size)
    post_size = np.asarray(self.post_size)
    connected_pres = []
    connected_posts = []
    pre_ids = np.meshgrid(*(np.arange(p) for p in self.pre_size), indexing='ij')
    pre_ids = tuple([(np.moveaxis(p, 0, 1).flatten()) if p.ndim > 1 else p.flatten() for p in pre_ids])
    size = np.prod(pre_size)

    for i in range(size):
      pre_pos = np.asarray([p[i] for p in pre_ids])
      pres, posts = f(pre_pos, pre_size=pre_size, post_size=post_size, n_dim=n_dim)
      connected_pres.extend(pres)
      connected_posts.extend(posts)
    return np.asarray(connected_pres), np.asarray(connected_posts)
//  [python]
def CalculateSimpleTopovIndex(mol):
    """
    #################################################################
    Calculation of the logarithm of the simple topological index by Narumi,
    
    which is defined as the product of the vertex degree.
    
    ---->Sitov
    
    Usage: 
        
        result=CalculateSimpleTopovIndex(mol)
        
        Input: mol is a molecule object
        
        Output: result is a numeric value
    #################################################################
    """
    deltas=_HKDeltas(mol,skipHs=0)
    while 0 in deltas:
        deltas.remove(0)
    deltas=numpy.array(deltas,'d')
    
    res=n.prod(deltas)
    
    return numpy.log(res)
//  [python]
def CalculateSimpleTopovIndex(mol):
    """
    #################################################################
    Calculation of the logarithm of the simple topological index by Narumi,
    
    which is defined as the product of the vertex degree.
    
    ---->Sitov
    
    Usage: 
        
        result=CalculateSimpleTopovIndex(mol)
        
        Input: mol is a molecule object
        
        Output: result is a numeric value
    #################################################################
    """
    deltas=_HKDeltas(mol,skipHs=0)
    while 0 in deltas:
        deltas.remove(0)
    deltas=numpy.array(deltas,'d')
    
    res=numpy.prod(deltas)
    
    return numpy.log(res)
//  [python]
def CalculateGeometricTopovIndex(mol):
    """
    #################################################################
    Geometric topological index by Narumi
    
    ---->Getov
    
    Usage: 
        
        result=CalculateGeometricTopovIndex(mol)
        
        Input: mol is a molecule object
        
        Output: result is a numeric value
    #################################################################
    """
    nAtoms=mol.GetNumAtoms()
    deltas=_HKDeltas(mol,skipHs=0)
    while 0 in deltas:
        deltas.remove(0)
    deltas=numpy.array(deltas,'d')
    
    temp=numpy.prod(deltas)
    res=numpy.power(temp,1./nAtoms)

    return res
//  [python]
def test_concatenation(self):
        knots1 = [0,0,0.4,1,1]
        degree = 1
        basis1 = BSplineBasis(knots1,degree)

        knots2 = [0.,0.,0.5,1,1]
        degree = 1
        basis2 = BSplineBasis(knots2,degree)

        basis3 = MonomialBasis(3);

        mbasis1 = TensorBasis([basis1,basis2]);
        mbasis2 = TensorBasis([basis2,basis3]);

        shape1 = [3,3,1,1]
        shape2 = [3,4,1,1]
        coeff1 = DTensor(numpy.random.randn(n.prod(shape1),1),shape1)
        coeff2 = DTensor(numpy.random.randn(n.prod(shape2),1),shape2)
        func1 = Function(mbasis1,coeff1)
        func2 = Function(mbasis2,coeff2)

        func3 = vertcat(func1, func2)
        self.assertEqual(func3.shape,[2,1])

        self.assertEqualTensor(func3(0.5,0.2),DTensor([func1(0.5,0.2),func2(0.5,0.2)],[2,1]))

        constant1 = Function(DTensor([2,2],[2,1]))
        func4 = horzcat(func3, constant1)
        self.assertEqual(func4.shape,[2,2])

        self.assertEqualTensor(func4(0.5,0.2),DTensor([func1(0.5,0.2),func2(0.5,0.2),2,2],[2,2]))
        constant2 = Function(DTensor([],[0,1]));
        constant3 = Function(DTensor([],[1,0]));
        func5 = vertcat(func1, constant2)
        func5 = horzcat(func5, constant3)
        self.assertEqual(func5.shape,[1,1])

        func6 = vertcat(func1, func2,  3 , 1 )
        self.assertEqualT(func6.shape, [4,1])
//  [python]
def test_concatenation(self):
        knots1 = [0,0,0.4,1,1]
        degree = 1
        basis1 = BSplineBasis(knots1,degree)

        knots2 = [0.,0.,0.5,1,1]
        degree = 1
        basis2 = BSplineBasis(knots2,degree)

        basis3 = MonomialBasis(3);

        mbasis1 = TensorBasis([basis1,basis2]);
        mbasis2 = TensorBasis([basis2,basis3]);

        shape1 = [3,3,1,1]
        shape2 = [3,4,1,1]
        coeff1 = DTensor(numpy.random.randn(numpy.prod(shape1),1),shape1)
        coeff2 = DTensor(numpy.random.randn(numpy.prod(shape2),1),shape2)
        func1 = Function(mbasis1,coeff1)
        func2 = Function(mbasis2,coeff2)

        func3 = vertcat(func1, func2)
        self.assertEqual(func3.shape,[2,1])

        self.assertEqualTensor(func3(0.5,0.2),DTensor([func1(0.5,0.2),func2(0.5,0.2)],[2,1]))

        constant1 = Function(DTensor([2,2],[2,1]))
        func4 = horzcat(func3, constant1)
        self.assertEqual(func4.shape,[2,2])

        self.assertEqualTensor(func4(0.5,0.2),DTensor([func1(0.5,0.2),func2(0.5,0.2),2,2],[2,2]))
        constant2 = Function(DTensor([],[0,1]));
        constant3 = Function(DTensor([],[1,0]));
        func5 = vertcat(func1, constant2)
        func5 = horzcat(func5, constant3)
        self.assertEqual(func5.shape,[1,1])

        func6 = vertcat(func1, func2,  3 , 1 )
        self.assertEqualT(func6.shape, [4,1])
//  [python]
def pN(self):
        return n.prod(self.N)
//  [python]
def pN(self):
        return np.prod(self.N)
//  [python]
def _shaped_ordered_params(fit):
    ef = fit.extract(permuted=False, inc_warmup=False)  # flattened, unpermuted, by (iteration, chain)
    ef = _by_chain(ef)
    ef = ef.reshape(-1, len(ef[0][0]))
    ef = ef[:, 0:len(fit.flatnames)]  # drop lp__
    shaped = {}
    idx = 0
    for dim, param_name in zip(fit.par_dims, fit.extract().keys()):
        length = int(numpy.prod(dim))
        shaped[param_name] = ef[:, idx:idx + length]
        shaped[param_name].reshape(*([-1] + dim))
        idx += length
    return shaped
//  [python]
def pdN(self):
        return n.prod(self.dN())
//  [python]
def pdN(self):
        return np.prod(self.dN())
//  [python]
def testBasic(self):
        op = OpArrayPiper(graph=Graph())
        inputData = numpy.indices((100, 100)).sum(0)
        op.Input.setValue(inputData)

        results = numpy.zeros((100, 100), dtype=numpy.int32)
        resultslock = threading.Lock()

        resultsCount = [0]

        def handleResult(roi, result):
            acquired = resultslock.acquire(False)
            assert acquired, "resultslock is contested! Access to callback is supposed to be automatically serialized."
            results[roiToSlice(*roi)] = result
            logger.debug("Got result for {}".format(roi))
            resultslock.release()
            resultsCount[0] += 1

        progressList = []

        def handleProgress(progress):
            progressList.append(progress)
            logger.debug("Progress update: {}".format(progress))

        totalVolume = numpy.multiply.reduce(inputData.shape)
        batch = BigRequestStreamer(op.Output, [(0, 0), (100, 100)], (10, 10))
        batch.resultSignal.subscribe(handleResult)
        batch.progressSignal.subscribe(handleProgress)

        batch.execute()
        logger.debug("Got {} results".format(resultsCount[0]))
        assert (results == inputData).all()

        # Progress reporting MUST start with 0 and end with 100
        assert progressList[0] == 0, "Invalid progress reporting."
        assert progressList[-1] == 100, "Invalid progress reporting."

        # There should be some intermediate progress reporting, but exactly how much is unspecified.
        assert len(progressList) >= 10

        logger.debug("FINISHED")
//  [python]
def testBasic(self):
        op = OpArrayPiper(graph=Graph())
        inputData = numpy.indices((100, 100)).sum(0)
        op.Input.setValue(inputData)

        results = numpy.zeros((100, 100), dtype=numpy.int32)
        resultslock = threading.Lock()

        resultsCount = [0]

        def handleResult(roi, result):
            acquired = resultslock.acquire(False)
            assert acquired, "resultslock is contested! Access to callback is supposed to be automatically serialized."
            results[roiToSlice(*roi)] = result
            logger.debug("Got result for {}".format(roi))
            resultslock.release()
            resultsCount[0] += 1

        progressList = []

        def handleProgress(progress):
            progressList.append(progress)
            logger.debug("Progress update: {}".format(progress))

        totalVolume = numpy.prod(inputData.shape)
        batch = BigRequestStreamer(op.Output, [(0, 0), (100, 100)], (10, 10))
        batch.resultSignal.subscribe(handleResult)
        batch.progressSignal.subscribe(handleProgress)

        batch.execute()
        logger.debug("Got {} results".format(resultsCount[0]))
        assert (results == inputData).all()

        # Progress reporting MUST start with 0 and end with 100
        assert progressList[0] == 0, "Invalid progress reporting."
        assert progressList[-1] == 100, "Invalid progress reporting."

        # There should be some intermediate progress reporting, but exactly how much is unspecified.
        assert len(progressList) >= 10

        logger.debug("FINISHED")
//  [python]
def lambdify_array_with_modules(
        symbols: tp.Tuple[sym.Symbol, ...],
        array_expression: tp.Union[sym.Array, sym.MatrixBase],
        symbolic_time_function_name_to_numeric_time_function_map:
        tp.Dict[str, tp.Callable],
        modules: tp.Tuple[str, ...]) \
        -> tp.Tuple[tp.Callable, ...]:
    """
    This function takes a SymPy array, unravals the index in Fortran order and
    lambdifies each element separately.

    Args:
        symbols: a tuple of SymPy symbols that are arguments to the function
        array_expression: a SymPy matrix or array
        symbolic_time_function_name_to_numeric_time_function_map: 
            a dictionary mapping the names of functions of time to Python 
            functions
        modules: modules that should be included

    Returns: a tuple of functions of the specified argument representing the
             elements of the symbolic array in Fortran order

    """

    if not isinstance(modules, list):
        modules = list(modules)

    if not (isinstance(array_expression, sym.Array) or
            isinstance(array_expression, sym.MatrixBase)):
        raise TypeError(f"symbolic_matrix_expression must be an instance of "
                        f"sympy.ArrayBase or sympy.MatrixBase "
                        f"but is of type {type(array_expression)}")

    n = reduce(operator.mul, array_expression.shape).item()
    result = []
    for i in range(n):
        index = tuple(np.unravel_index(i, array_expression.shape, order='F'))
        result.append(sym.lambdify(args=symbols,
                                   expr=array_expression[index],
                                   modules
                                   =[symbolic_time_function_name_to_numeric_time_function_map] +
                                     modules))

    return tuple(result)
//  [python]
def lambdify_array_with_modules(
        symbols: tp.Tuple[sym.Symbol, ...],
        array_expression: tp.Union[sym.Array, sym.MatrixBase],
        symbolic_time_function_name_to_numeric_time_function_map:
        tp.Dict[str, tp.Callable],
        modules: tp.Tuple[str, ...]) \
        -> tp.Tuple[tp.Callable, ...]:
    """
    This function takes a SymPy array, unravals the index in Fortran order and
    lambdifies each element separately.

    Args:
        symbols: a tuple of SymPy symbols that are arguments to the function
        array_expression: a SymPy matrix or array
        symbolic_time_function_name_to_numeric_time_function_map: 
            a dictionary mapping the names of functions of time to Python 
            functions
        modules: modules that should be included

    Returns: a tuple of functions of the specified argument representing the
             elements of the symbolic array in Fortran order

    """

    if not isinstance(modules, list):
        modules = list(modules)

    if not (isinstance(array_expression, sym.Array) or
            isinstance(array_expression, sym.MatrixBase)):
        raise TypeError(f"symbolic_matrix_expression must be an instance of "
                        f"sympy.ArrayBase or sympy.MatrixBase "
                        f"but is of type {type(array_expression)}")

    n = (np.prod(array_expression.shape)).item()
    result = []
    for i in range(n):
        index = tuple(np.unravel_index(i, array_expression.shape, order='F'))
        result.append(sym.lambdify(args=symbols,
                                   expr=array_expression[index],
                                   modules
                                   =[symbolic_time_function_name_to_numeric_time_function_map] +
                                     modules))

    return tuple(result)
//  [python]
def measure(self, ndim=None):
        """Return the Lebesgue measure of this interval product.

        Parameters
        ----------
        ndim : int, optional
            Dimension of the measure to apply. ``None`` is interpreted
            as `true_ndim`, which always results in a finite and
            positive result (unless the set is a single point).

        Examples
        --------
        >>> min_pt, max_pt = [-1, 2.5, 0], [-0.5, 10, 0]
        >>> rbox = IntervalProd(min_pt, max_pt)
        >>> rbox.measure()
        3.75
        >>> rbox.measure(ndim=3)
        0.0
        >>> rbox.measure(ndim=3) == rbox.volume
        True
        >>> rbox.measure(ndim=1)
        inf
        >>> rbox.measure() == rbox.squeeze().volume
        True
        """
        if self.true_ndim == 0:
            return 0.0

        if ndim is None:
            return self.measure(ndim=self.true_ndim)
        elif ndim < self.true_ndim:
            return np.inf
        elif ndim > self.true_ndim:
            return 0.0
        else:
            return n.prod(self.extent[self.nondegen_byaxis])
//  [python]
def measure(self, ndim=None):
        """Return the Lebesgue measure of this interval product.

        Parameters
        ----------
        ndim : int, optional
            Dimension of the measure to apply. ``None`` is interpreted
            as `true_ndim`, which always results in a finite and
            positive result (unless the set is a single point).

        Examples
        --------
        >>> min_pt, max_pt = [-1, 2.5, 0], [-0.5, 10, 0]
        >>> rbox = IntervalProd(min_pt, max_pt)
        >>> rbox.measure()
        3.75
        >>> rbox.measure(ndim=3)
        0.0
        >>> rbox.measure(ndim=3) == rbox.volume
        True
        >>> rbox.measure(ndim=1)
        inf
        >>> rbox.measure() == rbox.squeeze().volume
        True
        """
        if self.true_ndim == 0:
            return 0.0

        if ndim is None:
            return self.measure(ndim=self.true_ndim)
        elif ndim < self.true_ndim:
            return np.inf
        elif ndim > self.true_ndim:
            return 0.0
        else:
            return np.prod(self.extent[self.nondegen_byaxis])
//  [python]
def matrix(self):
        """
        Returns full matrix version of Matrix class.
        """
        pN=n.prod(self.N)
        proddN=self.d*pN
        matrix=np.matrix(np.zeros([proddN, proddN], dtype=np.float64))
        for ii in np.arange(self.d):
            for jj in np.arange(self.d):
                submatrix=np.diag(self.val[ii, jj].ravel())
                matrix[pN*ii:pN*(ii+1), pN*jj:pN*(jj+1)]=submatrix
        return matrix
//  [python]
def matrix(self):
        """
        Returns full matrix version of Matrix class.
        """
        pN=np.prod(self.N)
        proddN=self.d*pN
        matrix=np.matrix(np.zeros([proddN, proddN], dtype=np.float64))
        for ii in np.arange(self.d):
            for jj in np.arange(self.d):
                submatrix=np.diag(self.val[ii, jj].ravel())
                matrix[pN*ii:pN*(ii+1), pN*jj:pN*(jj+1)]=submatrix
        return matrix
//  [python]
def __init__(self, inverse=False, N=None, normalized=True, **kwargs):
        self.__dict__.update(kwargs)
        if 'name' not in list(kwargs.keys()):
            if inverse:
                self.name='iDFT'
            else:
                self.name='DFT'

        self.N=np.array(N, dtype=np.int32)
        self.inverse=inverse
        if normalized:
            self.norm_coef=n.prod(self.N)
        else:
            self.norm_coef=1.
//  [python]
def __init__(self, inverse=False, N=None, normalized=True, **kwargs):
        self.__dict__.update(kwargs)
        if 'name' not in list(kwargs.keys()):
            if inverse:
                self.name='iDFT'
            else:
                self.name='DFT'

        self.N=np.array(N, dtype=np.int32)
        self.inverse=inverse
        if normalized:
            self.norm_coef=np.prod(self.N)
        else:
            self.norm_coef=1.
//  [python]
def _compute_result_internal(R: int,
                             dimensions: tp.Tuple[int, ...],
                             arguments: tp.List[NumericArrayOrScalar],
                             functions_cpu: tp.Tuple[tp.Callable, ...],
                             functions_gpu: tp.Tuple[tp.Callable, ...],
                             pre_attach: bool,
                             gpu: bool,
                             dtype: np.generic) \
        -> NumericArray:
    """
    This function evaluates a sequence of functions with given positional
    arguments. Each argument is either a scalar or R-dimensional. The result
    Args:
        R: the number of replications (this is the length each of the input
           vectors if they are not scalars)

        dimensions:
            The shape of the output (excluding the R replications which are
            either attached to the beginning (if pre_attach is True) or to the
            end.

        arguments:
            a list of arguments to be passed to the functions to be evaluated

        functions_cpu: The sequence of functions to be evaluated on the CPU.
        functions_gpu: The sequence of functions to be evaluated on the GPU.
        pre_attach:
            whether to put the replications in the first or the last axis
        gpu: whether to evaluate on the cpu or the gpu
        dtype: the dtype of the output array

    Returns:

    """
    num_pack = select_num_pack(gpu)
    if gpu:
        functions = functions_gpu
    else:
        functions = functions_cpu

    n = reduce(lambda x, y: x * y, dimensions)

    if pre_attach:
        dimension_index = tuple([R] + list(dimensions))
    else:
        dimension_index = tuple(list(dimensions) + [R])

    result = num_pack.zeros(dimension_index, dtype=dtype)

    for i in range(n):
        index = tuple(np.unravel_index(i, dimensions, order='F'))

        function = functions[i]

        if pre_attach:
            location_index = tuple([slice(None)] + list(index))
        else:
            location_index = tuple(list(index) + [slice(None)])

        evaluated_function = function(*arguments)
        result[location_index] = evaluated_function

    return result