python类product()的实例源码

dataset.py 文件源码 项目:fxnn 作者: khaotik 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def download(self, local_dir_=None, url_=None):
        '''
        Args:
            local_dir_: where to save downloaded file
            url_: where to download dataset, if None, use default 'http://yann.lecun.com/exdb/mnist/'
        '''
        # TODO check whether file exists
        if url_ is None:
            url_ = 'http://yann.lecun.com/exdb/mnist/'
        if local_dir_ is None:
            local_dir = self.DEFAULT_DIR
        else:
            local_dir = Path(local_dir_)
        local_dir.mkdir(parents=True, exist_ok=True)
        in_filename = '%(subset)s-%(type_s)s-idx%(ndim)s-ubyte.gz'
        for subset, (type_s, ndim) in product(
            ('train', 't10k'), zip(('images', 'labels'), (3,1))):
            filename = in_filename % locals()
            urllib.request.urlretrieve( url_ + filename, str(local_dir / filename))
serving_test.py 文件源码 项目:treecat 作者: posterior 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_server_logprob_normalized(N, V, C, M):
    model = generate_fake_model(N, V, C, M)
    config = TINY_CONFIG.copy()
    config['model_num_clusters'] = M
    model['config'] = config
    server = TreeCatServer(model)

    # The total probability of all categorical rows should be 1.
    ragged_index = model['suffstats']['ragged_index']
    factors = []
    for v in range(V):
        C = ragged_index[v + 1] - ragged_index[v]
        factors.append([one_hot(c, C) for c in range(C)])
    data = np.array(
        [np.concatenate(columns) for columns in itertools.product(*factors)],
        dtype=np.int8)
    logprobs = server.logprob(data)
    logtotal = np.logaddexp.reduce(logprobs)
    assert logtotal == pytest.approx(0.0, abs=1e-5)
test_defuzz.py 文件源码 项目:zellij 作者: nedbat 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def test_correct_distance(start, ndigits, dimensions):
    eps = 1e-10
    window = 10 ** -ndigits
    smallest_different = 1.5 * window + eps
    largest_same = 0.5 * window - eps
    step = 10.09 * window
    for i in range(10):
        num = start + i * step
        pt = (num,) * dimensions
        for signs in itertools.product([-1, 0, 1], repeat=dimensions):
            if all(s == 0 for s in signs):
                continue
            # Need a new defuzzer for each attempt, or previous "should be
            # different" points will be close to the "should be same" point.
            dfz = Defuzzer(ndigits=ndigits)
            assert dfz.defuzz(pt) == pt
            st = tuple(num + s * largest_same for s in signs)
            dfzst = dfz.defuzz(st)
            assert dfzst == pt
            dt = tuple(num + s * smallest_different for s in signs)
            dfzdt = dfz.defuzz(dt)
            assert dfzdt != pt
report.py 文件源码 项目:cellranger 作者: 10XGenomics 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __init__(self, genomes):
        self.total_reads = 0.0
        self.unmapped_reads = 0.0
        self.good_umi_reads = 0.0
        self.good_bc_reads = 0.0
        self.corrected_bc_reads = 0.0
        self.genomes = genomes + [cr_constants.MULTI_REFS_PREFIX]
        self.regions = cr_constants.REGIONS
        genome_region_dict = lambda: {(g,r): 0.0 for g,r in itertools.product(self.genomes, self.regions)}
        genome_dict = lambda: {g: 0.0 for g in self.genomes}
        self.mapped_reads = genome_region_dict()
        self.conf_mapped_reads = genome_region_dict()
        self.conf_mapped_bc_reads = genome_region_dict()
        self.antisense_reads = genome_dict()
        self.discordant_pairs = genome_dict()
        self.genome_reads = genome_dict()
report.py 文件源码 项目:cellranger 作者: 10XGenomics 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _get_metric_keys(self, name):
        metric_cls, metric_dict = self.metrics_dict[name]
        prefixes = metric_dict.get('prefixes', [])
        kwargs = metric_dict.get('kwargs', {})

        always_active = kwargs.get('always_active', False)

        parts = [[name]]
        for prefix in prefixes:
            prefix = getattr(self, prefix)

            if prefix:
                parts.append(prefix)

        # Check to make sure all specified metrics are present for metrics that are always active
        if always_active and len(parts) != len(prefixes) + 1:
            return []

        # Return the set of keys
        keys = set(itertools.product(*parts))

        # Add bare keys
        keys.add((name,))

        return keys
PathAnalysis.py 文件源码 项目:netra 作者: akshah 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def analyze(self):

        results=[]

        #Making all of the possibilities
        allGraphs = product(*self.countries)

        for g in allGraphs:
            results.append(hasCycle(g))

        #Now searching for cycles
        #results = [hasCycle(graph) for graph in allGraphs]
        numCycles = results.count(True)

        #Seeing if it's definately an anomolous path
        if numCycles == len(results):
            self.result = 1
        #Seeing if it's only potentially anomolous    
        elif numCycles > 0:
            self.result = 2
        #Everying seems to be fine
        else:
            self.result = 0
main.py 文件源码 项目:guided-filter 作者: lisabug 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_color():
    image = cv2.imread('data/Lenna.png')
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    noise = (np.random.rand(image.shape[0], image.shape[1], 3) - 0.5) * 50
    image_noise = image + noise

    radius = [1, 2, 4]
    eps = [0.005]

    combs = list(itertools.product(radius, eps))

    vis.plot_single(to_32F(image), title='origin')
    vis.plot_single(to_32F(image_noise), title='noise')

    for r, e in combs:
        GF = GuidedFilter(image, radius=r, eps=e)
        vis.plot_single(to_32F(GF.filter(image_noise)), title='r=%d, eps=%.3f' % (r, e))
lib.py 文件源码 项目:cloud-volume 作者: seung-lab 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def xyzrange(start_vec, end_vec=None, stride_vec=(1,1,1)):
  if end_vec is None:
    end_vec = start_vec
    start_vec = (0,0,0)

  start_vec = np.array(start_vec, dtype=int)
  end_vec = np.array(end_vec, dtype=int)

  rangeargs = ( (start, end, stride) for start, end, stride in zip(start_vec, end_vec, stride_vec) )
  xyzranges = [ range(*arg) for arg in rangeargs ]

  # iterate then x first, then y, then z
  # this way you process in the xy plane slice by slice
  # but you don't create process lots of prefix-adjacent keys
  # since all the keys start with X
  zyxranges = xyzranges[::-1]

  def vectorize():
    pt = Vec(0,0,0)
    for z,y,x in product(*zyxranges):
      pt.x, pt.y, pt.z = x, y, z
      yield pt

  return vectorize()
HanoiMDPClass.py 文件源码 项目:simple_rl 作者: david-abel 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, num_pegs=3, num_discs=3, gamma=0.95):
        '''
        Args:
            num_pegs (int)
            num_discs (int)
            gamma (float)
        '''
        self.num_pegs = num_pegs
        self.num_discs = num_discs
        HanoiMDP.ACTIONS = [str(x) + str(y) for x, y in itertools.product(xrange(self.num_pegs), xrange(self.num_pegs)) if x != y]

        # Setup init state.
        init_state = [" " for peg in xrange(num_pegs)]
        x = ""
        for i in xrange(num_discs):
            x += chr(97 + i)
        init_state[0] = x
        init_state = State(data=init_state)

        MDP.__init__(self, HanoiMDP.ACTIONS, self._transition_func, self._reward_func, init_state=init_state, gamma=gamma)
perms.py 文件源码 项目:dustbunny 作者: Teamworksapp 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __iter__(self):
        tally = []
        values = (pair[1] for pair in self.of)
        keys = [pair[0] for pair in self.of]

        # evaluate any transformers in order
        for i, v in enumerate(values):
            if callable(v):  # then create a permutation for everything 
                for p in [x for x in AllPerms(*tally)]:
                    tally.append((keys[i], v(**p)))
            else:
                tally.append((keys[i], v))

        values = (pair[1] for pair in tally)

        for tup in itertools.product(*values):
            yield dict(zip(keys, tup))
builder.py 文件源码 项目:otRebuilder 作者: Pal3love 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def add_ligature_subst(self, location,
                           prefix, glyphs, suffix, replacement, forceChain):
        if prefix or suffix or forceChain:
            chain = self.get_lookup_(location, ChainContextSubstBuilder)
            lookup = self.get_chained_lookup_(location, LigatureSubstBuilder)
            chain.substitutions.append((prefix, glyphs, suffix, [lookup]))
        else:
            lookup = self.get_lookup_(location, LigatureSubstBuilder)

        # OpenType feature file syntax, section 5.d, "Ligature substitution":
        # "Since the OpenType specification does not allow ligature
        # substitutions to be specified on target sequences that contain
        # glyph classes, the implementation software will enumerate
        # all specific glyph sequences if glyph classes are detected"
        for g in sorted(itertools.product(*glyphs)):
            lookup.ligatures[g] = replacement
ast.py 文件源码 项目:otRebuilder 作者: Pal3love 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def build(self, builder):
        if self.enumerated:
            g = [self.glyphs1.glyphSet(), self.glyphs2.glyphSet()]
            for glyph1, glyph2 in itertools.product(*g):
                builder.add_specific_pair_pos(
                    self.location, glyph1, self.valuerecord1,
                    glyph2, self.valuerecord2)
            return

        is_specific = (isinstance(self.glyphs1, GlyphName) and
                       isinstance(self.glyphs2, GlyphName))
        if is_specific:
            builder.add_specific_pair_pos(
                self.location, self.glyphs1.glyph, self.valuerecord1,
                self.glyphs2.glyph, self.valuerecord2)
        else:
            builder.add_class_pair_pos(
                self.location, self.glyphs1.glyphSet(), self.valuerecord1,
                self.glyphs2.glyphSet(), self.valuerecord2)
train.py 文件源码 项目:brain_segmentation 作者: Ryo-Ito 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def validate(model):
    dice_coefs = []
    for image_path, label_path in zip(df_val["image"], df_val["label"]):
        image = load_nifti(image_path)
        label = load_nifti(label_path)
        centers = [[], [], []]
        for img_len, len_out, center, n_tile in zip(image.shape, args.output_shape, centers, args.n_tiles):
            assert img_len < len_out * n_tile, "{} must be smaller than {} x {}".format(img_len, len_out, n_tile)
            stride = int((img_len - len_out) / (n_tile - 1))
            center.append(len_out / 2)
            for i in range(n_tile - 2):
                center.append(center[-1] + stride)
            center.append(img_len - len_out / 2)
        output = np.zeros((dataset["n_classes"],) + image.shape[:-1])
        for x, y, z in itertools.product(*centers):
            patch = crop_patch(image, [x, y, z], args.input_shape)
            patch = np.expand_dims(patch, 0)
            patch = xp.asarray(patch)
            slices_out = [slice(center - len_out / 2, center + len_out / 2) for len_out, center in zip(args.output_shape, [x, y, z])]
            slices_in = [slice((len_in - len_out) / 2, len_in - (len_in - len_out) / 2) for len_out, len_in, in zip(args.output_shape, args.input_shape)]
            output[slice(None), slices_out[0], slices_out[1], slices_out[2]] += chainer.cuda.to_cpu(model(patch).data[0, slice(None), slices_in[0], slices_in[1], slices_in[2]])
        y = np.argmax(output, axis=0).astype(np.int32)
        dice_coefs.append(dice_coefficients(y, label, labels=range(dataset["n_classes"])))
    dice_coefs = np.array(dice_coefs)
    return np.mean(dice_coefs, axis=0)
sample.py 文件源码 项目:CausalGAN 作者: mkocaoglu 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def take_product(do_dict):
    '''
    this function takes some dictionary like:
        {key1:1, key2:[a,b], key3:[c,d]}
    and returns the dictionary:
        {key1:[1,1,1], key2[a,a,b,b,],key3[c,d,c,d]}
    computing the product of values
    '''
    values=[]
    for v in do_dict.values():
        if hasattr(v,'__iter__'):
            values.append(v)
        else:
            values.append([v])#allows scalar to be passed

    prod_values=np.vstack(product(*values))
    return {k:np.array(v) for k,v in zip(do_dict.keys(),zip(*prod_values))}
optimalks.py 文件源码 项目:det_k_bisbm 作者: junipertcy 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _check_if_local_minimum(self, ka, kb, old_desc_len, k_th):
        '''
            The `neighborhood search` as described in the paper.
        '''
        self.is_tempfile_existed = True
        items = map(lambda x: (x[0] + ka, x[1] + kb), product(range(-k_th, k_th + 1), repeat=2))
        # if any item has values less than 1, delete it. Also, exclude the suspected point.
        items = [(i, j) for i, j in items if i >= 1 and j >= 1 and (i, j) != (ka, kb)]
        ka_moving, kb_moving = 0, 0

        for item in items:
            self._calc_and_update(item, old_desc_len)
            if self._is_this_mdl(self.confident_desc_len[(item[0], item[1])]):
                p_estimate = sorted(self.confident_desc_len, key=self.confident_desc_len.get)[0]
                self._logger.info("Found {} that gives an even lower description length ...".format(p_estimate))
                ka_moving, kb_moving, _, _ = self._back_to_where_desc_len_is_lowest()
                break
        if ka_moving * kb_moving == 0:
            return True
        else:
            return False
tagfilter.py 文件源码 项目:openstack-ansible-plugins 作者: openstack 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _queue_task(self, host, task, task_vars, play_context):
        """Wipe the notification system and return for config tasks."""
        skip_handlers = task_vars.get('skip_handlers', True)
        if skip_handlers:
            task.notify = None
        skip_tags = task_vars.get('skip_tags')
        if skip_tags:
            if not hasattr(skip_tags, '__iter__'):
                skip_tags = (skip_tags,)
        else:
            skip_tags = ()
        if any([True for (i, j) in itertools.product(skip_tags, task.tags)
               if i in j]):
            return
        else:
            return super(StrategyModule, self)._queue_task(
                host,
                task,
                task_vars,
                play_context
            )
crop_and_aug.py 文件源码 项目:logodetect 作者: munibasad 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def aug_pos(annot, im):
    aug_pos_ims = []
    aug_pos_suffixes = []

    rect = get_rect(annot)
    for sx, sy in product(
            range(DATA_AUG_POS_SHIFT_MIN, DATA_AUG_POS_SHIFT_MAX),
            range(DATA_AUG_POS_SHIFT_MIN, DATA_AUG_POS_SHIFT_MAX)):
        cx = rect['cx'] + sx
        cy = rect['cy'] + sy
        cropped_im = im.crop((cx - rect['wid'] // 2, cy - rect['hgt'] // 2,
                              cx + rect['wid'] // 2, cy + rect['hgt'] // 2))
        resized_im = cropped_im.resize((CNN_IN_WIDTH, CNN_IN_HEIGHT))
        aug_pos_ims.append(resized_im)
        aug_pos_suffixes.append('p' + str(sx) + str(sy))
        cropped_im.close()

    return aug_pos_ims, aug_pos_suffixes
create_kmer_freq_vectors.py 文件源码 项目:mbin 作者: fanglab 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def kmer_freq ( ref_str, k ):
    """
    Walk through sequence and return k-mer counts plus
    a pseudocount of 1.
    """
    ref_str = ref_str.upper()
    kmers = []
    for seq in product("ATGC",repeat=k):
        kmers.append( "".join(seq) )

    kmer_counts = Counter()
    for j in range( len(ref_str)-(k-1) ):
        motif    = ref_str[j:j+k]
        kmer_counts[motif] += 1

    # Combine forward and reverse complement motifs into one count
    combined_kmer = Counter()
    for kmer in kmers:
        kmer_rc = rev_comp_motif(kmer)
        if not combined_kmer.get(kmer_rc):
            combined_kmer[kmer] = kmer_counts[kmer] + kmer_counts[kmer_rc] + 1

    return combined_kmer
read_scanner.py 文件源码 项目:mbin 作者: fanglab 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def kmer_freq ( mode, ref_str, strand, opts ):
    ref_str = ref_str.upper()
    if strand==1:
        ref_str = ref_str[::-1]
    k = opts.comp_kmer
    kmers = []
    for seq in product("ATGC",repeat=k):
        kmers.append( "".join(seq) )

    kmer_counts = Counter()
    for j in range( len(ref_str)-(k-1) ):
        motif    = ref_str[j:j+k]
        kmer_counts[motif] += 1

    # Combine forward and reverse complement motifs into one count
    combined_kmer = Counter()
    for kmer in kmers:
        kmer_rc = motif_tools.rev_comp_motif(kmer)
        if not combined_kmer.get(kmer_rc):
            combined_kmer[kmer] = kmer_counts[kmer] + kmer_counts[kmer_rc] + 1

    return combined_kmer
test_search.py 文件源码 项目:DataFS 作者: ClimateImpactLab 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_config(api1_module, local_auth_module, temp_dir_mod):

    api1_module.attach_authority('local', local_auth_module)

    temp_file = os.path.join(temp_dir_mod, 'config.yml')

    to_config_file(api1_module, config_file=temp_file, profile='myapi')

    for i, j, k in itertools.product(*tuple([range(3) for _ in range(3)])):
        arch = 'team{}_archive{}_var{}'.format(i+1, j+1, k+1)
        api1_module.create(
            arch,
            tags=list(arch.split('_')),
            metadata={
                'description': 'archive_{}_{}_{} description'.format(i, j, k)})

    yield 'myapi', temp_file
semantics.py 文件源码 项目:Lyra 作者: caterinaurban 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def slicing_access_semantics(self, stmt: SlicingAccess, state: State) -> State:
        """Semantics of a slicing access.

        :param stmt: slicing access statement to be executed
        :param state: state before executing the slicing access
        :return: state modified by the slicing access
        """
        target = self.semantics(stmt.target, state).result
        lower = self.semantics(stmt.lower, state).result
        upper = self.semantics(stmt.upper, state).result
        stride = self.semantics(stmt.stride, state).result if stmt.stride else {None}
        result = set()
        for primary, start, stop, step in itertools.product(target, lower, upper, stride):
            slicing = Slicing(primary.typ, primary, start, stop, step)
            result.add(slicing)
        state.result = result
        return state
motif.py 文件源码 项目:krait 作者: lmdu 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def mapping(self):
        bases = ['A', 'T', 'C', 'G']
        motifs = {}
        for i in range(6):
            for motif in itertools.product(bases, repeat=i+1):
                motif = "".join(list(motif))
                if not is_motif(motif):
                    continue

                smotif = self.standard(motif)
                if smotif not in motifs:
                    motifs[smotif] = []

                if motif not in motifs[smotif]:
                    motifs[smotif].append(motif)

        return motifs
dataset.py 文件源码 项目:dnc-theano 作者: khaotik 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def download(self, local_dir_=None, url_=None):
        '''
        Args:
            local_dir_: where to save downloaded file
            url_: where to download dataset, if None, use default 'http://yann.lecun.com/exdb/mnist/'
        '''
        # TODO check whether file exists
        if url_ is None:
            url_ = 'http://yann.lecun.com/exdb/mnist/'
        if local_dir_ is None:
            local_dir = self.DEFAULT_DIR
        else:
            local_dir = Path(local_dir_)
        local_dir.mkdir(parents=True, exist_ok=True)
        in_filename = '%(subset)s-%(type_s)s-idx%(ndim)s-ubyte.gz'
        for subset, (type_s, ndim) in product(
            ('train', 't10k'), zip(('images', 'labels'), (3,1))):
            filename = in_filename % locals()
            urllib.request.urlretrieve( url_ + filename, str(local_dir / filename))
baseline_PPMI1.py 文件源码 项目:EventStoryLine 作者: tommasoc80 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def cross_sentence(event_lemma_dict):
    """
    function to create all possible pairs between event mentions in a file
    :param event_lemma_dict: dictionary of event lemmas in file
    :return: counter dictionary of event pairs in a file
    """

    full_event_file = []
    pairs_circumstantial_corpus = Counter([])

    for k, v in event_lemma_dict.items():
        full_event_file.append(k)

    event_pairs_full = list(product(full_event_file, repeat=2))

    for i in event_pairs_full:
        pairs_circumstantial_corpus.update([i])

    return pairs_circumstantial_corpus
baseline_PPMI1.py 文件源码 项目:EventStoryLine 作者: tommasoc80 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def sentence_coocc(event_lemma_dict, event_same_sentence):
    """
    funtion create pairs of events in the same sentence - same sentence event pairs
    :param event_same_sentence: dictionary with list of event markable co-ccurring in same sentence
    :param event_lemma_dict: dictionary of event ids and lemmas in file
    :return: counter dictionary of event pairs in the same sentence
    """

    same_sentence_event_lemma = collections.defaultdict(list)
    pairs_circumstantial_sentence = {}

    for k, v in event_lemma_dict.items():
        for k1, v1 in event_same_sentence.items():
            if k in v1:
                event_string = "_".join(v)
                same_sentence_event_lemma[k1].append(event_string)

    for k, v in same_sentence_event_lemma.items():
        if len(v) >= 2:
            same_sent_pairs = list(product(v, repeat=2))
            pairs_circumstantial_sentence[k] = same_sent_pairs

    return pairs_circumstantial_sentence
mppovm.py 文件源码 项目:mpnum 作者: dseuss 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def probability_map(self):
        """Map that takes a raveled MPDO to the POVM probabilities

        You can use :func:`MPPovm.expectations()` or
        :func:`MPPovm.pmf()` as convenient wrappers around this map.

        If `rho` is a matrix product density operator (MPDO), then

        .. code::python

            mp.dot(a_povm.probability_map, rho.ravel())

        produces the POVM probabilities as MPA (similar to
        :func:`mpnum.povm.localpovm.POVM.probability_map`).

        """
        # See :func:`.localpovm.POVM.probability_map` for explanation
        # of the transpose.
        return self.transpose((0, 2, 1)).reshape(
            (pdim[0], -1) for pdim in self.shape)
mparray.py 文件源码 项目:mpnum 作者: dseuss 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def axis_iter(self, axes=0):
        """Returns an iterator yielding Sub-MPArrays of ``self`` by iterating
        over the specified physical axes.

        **Example:** If ``self`` represents a bipartite (i.e. length 2)
        array with 2 physical dimensions on each site ``A[(k,l), (m,n)]``,
        ``self.axis_iter(0)`` is equivalent to::

            (A[(k, :), (m, :)] for m in range(...) for k in range(...))

        :param axes: Iterable or int specifiying the physical axes to iterate
            over (default 0 for each site)
        :returns: Iterator over :class:`.MPArray`

        """
        if not isinstance(axes, collections.Iterable):
            axes = it.repeat(axes, len(self))

        ltens_iter = it.product(*(iter(np.rollaxis(lten, i + 1))
                                  for i, lten in zip(axes, self.lt)))
        return (MPArray(ltens) for ltens in ltens_iter)

    ##########################
    #  Algebraic operations  #
    ##########################
mparray.py 文件源码 项目:mpnum 作者: dseuss 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def inner(mpa1, mpa2):
    """Compute the inner product `<mpa1|mpa2>`. Both have to have the same
    physical dimensions. If these represent a MPS, ``inner(...)`` corresponds
    to the canoncial Hilbert space scalar product. If these represent a MPO,
    ``inner(...)`` corresponds to the Frobenius scalar product (with Hermitian
    conjugation in the first argument)

    :param mpa1: MPArray with same number of physical legs on each site
    :param mpa2: MPArray with same physical shape as mpa1
    :returns: <mpa1|mpa2>

    """
    assert len(mpa1) == len(mpa2), \
        "Length is not equal: {} != {}".format(len(mpa1), len(mpa2))
    ltens_new = (_local_dot(_local_ravel(l).conj(), _local_ravel(r), axes=(1, 1))
                 for l, r in zip(mpa1.lt, mpa2.lt))
    return _ltens_to_array(ltens_new)[0, ..., 0]
mparray.py 文件源码 项目:mpnum 作者: dseuss 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def chain(mpas, astype=None):
    """Computes the tensor product of MPAs given in ``*args`` by adding more
    sites to the array.

    :param mpas: Iterable of MPAs in the order as they should appear in the
        chain
    :param astype: dtype of the returned MPA. If ``None``, use the type of the
        first MPA.
    :returns: MPA of length ``len(args[0]) + ... + len(args[-1])``

    .. todo:: Make this canonicalization aware
    .. todo:: Raise warning when casting complex to real dtype

    """
    mpas = iter(mpas)
    try:
        first = next(mpas)
    except StopIteration:
        raise ValueError('Argument `mpas` is an empty list')
    rest = (lt for mpa in mpas for lt in mpa.lt)
    if astype is None:
        astype = type(first)
    return astype(it.chain(first.lt, rest))
mparray.py 文件源码 项目:mpnum 作者: dseuss 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def norm(mpa):
    """Computes the norm (Hilbert space norm for MPS, Frobenius norm for MPO)
    of the matrix product operator. In contrast to ``mparray.inner``, this can
    take advantage of the canonicalization

    WARNING This also changes the MPA inplace by normalizing.

    :param mpa: MPArray
    :returns: l2-norm of that array

    """
    mpa.canonicalize()
    current_lcanon, current_rcanon = mpa.canonical_form

    if current_rcanon == 1:
        return np.linalg.norm(mpa.lt[0])
    elif current_lcanon == len(mpa) - 1:
        return np.linalg.norm(mpa.lt[-1])
    else:
        raise ValueError("Normalization error in MPArray.norm")


问题


面经


文章

微信
公众号

扫码关注公众号