def test_scale(self): """ A new vector can be created by scaling the values of another vector. """ indices = [Dummy(i) for i in range(9)] values = list(range(9)) indices_cp = copy.copy(indices) values_cp = copy.copy(values) v1 = Vector(index=indices, value=list(range(9))) self.assertTrue(is_ordered(v1)) k = 5. v2 = scale_vector(v1, k) # Make sure the arguments do not change self.assertTrue(indices == indices_cp) self.assertTrue(values == values_cp) for i, x in v2.iteritems(): self.assertEqual(i.uid[0] * k, x) self.assertTrue(is_ordered(v2)) for i1, i2 in izip(v1.iterkeys(), v2.iterkeys()): self.assertEqual(i1.uid, i2.uid)
def test_construct(self): """ A Vector can be constructed empty and then updated or it can be constructed with named sequences, or it can be copy constructed. """ v = Vector() self.assertEqual(len(v), 0) uids = (1, 2, 3) indices = [Dummy(i) for i in uids] values = [12, 34, 45] v.extend(zip(indices, values)) d = dict(zip(indices, values)) for i, x in v.iteritems(): self.assertTrue(equivalent(x, d[i])) v = Vector(index=indices, value=values) for i, x in v.iteritems(): self.assertEqual(x, d[i]) vc = Vector(copy=v) for i, x in v.iteritems(): self.assertEqual(x, d[i])
def test_extend(self): """ Extension is the process of making sure that one Vector has all the indices in another Vector, as well as its own. The initial values of any new indices are zero. """ index1 = [Dummy(i) for i in range(9)] v1 = Vector(index=index1, value=list(range(9))) index2 = [Dummy(i) for i in range(5, 15)] v2 = Vector(index=index2, value=list(range(10))) self.assertTrue(is_ordered(v1)) self.assertTrue(is_ordered(v2)) v = extend_vector(v1, v2) v_range = [i.uid[0] for i in v.keys()] self.assertEqual(v_range, list(range(15))) self.assertTrue(is_ordered(v)) for i, x in v.iteritems(): # The values from v1 should remain, # those from v2 should be zero. if i.uid[0] < 9: self.assertEqual(x, i.uid[0]) else: self.assertEqual(0, x)
def test_append(self): """ Elements can be appended """ v = Vector() lst = [(Dummy(1), 2), (Dummy(3), 2)] v.append(*lst[0]) # First pair self.assertEqual(len(v), 1) for p in v.items(): self.assertEqual(lst[0][0], p[0]) self.assertEqual(lst[0][1], p[1]) v.append(*lst[1]) # 2nd pair self.assertEqual(len(v), 2) for i, p in enumerate(v.items()): self.assertEqual(lst[i][0], p[0]) self.assertEqual(lst[i][1], p[1]) self.assertTrue(is_ordered(v)) lst[0] = (Dummy(5), 6) self.assertNotEqual(lst[0][0], p[0]) self.assertNotEqual(lst[0][1], p[1])
def _vector_index_to_node(v): """ Change the vector index from a uid to a node """ _nodes = context._context._registered_leaf_nodes return Vector(index=[_nodes[uid_i] for uid_i in v._index], value=v._value)
def _ivector_index_to_node(i_components, _intermediate_node): """ Return a Vector containing intermediate nodes as indices """ return Vector( index=[_intermediate_node[uid_i] for uid_i in i_components.iterkeys()], value=i_components._value)
def test_merge_weighted_vectors(self): # Merge two overlapping continuous ranges rng1 = list(range(10)) index1 = [Dummy(i) for i in rng1] v1 = Vector(index=index1, value=rng1) self.assertTrue(is_ordered(v1)) k1 = 3. idx1_cp = copy.copy(index1) v1_cp = copy.copy(rng1) index2 = [Dummy(i) for i in range(5, 15)] v2 = Vector(index=index2, value=rng1) # Note that the values are the same as v1 self.assertTrue(is_ordered(v2)) k2 = -4. idx2_cp = copy.copy(index2) v = merge_weighted_vectors(v1, k1, v2, k2) self.assertTrue(is_ordered(v)) self.assertTrue(rng1 == v1_cp) self.assertTrue(index1 == idx1_cp) self.assertTrue(index2 == idx2_cp) rng = list(range(15)) rng_merged = [i.uid[0] for i in v.keys()] self.assertEqual(rng_merged, rng) for i, p in enumerate(v.items()): if i < 5: self.assertEqual(p[1], k1 * rng[i]) elif i > 4 and i < 10: self.assertEqual(p[1], k1 * rng[i] + k2 * rng[i - 5]) elif i > 9 and i < 15: self.assertEqual(p[1], k2 * rng[i - 5]) else: assert False
def _ivector_index_to_uid(i_components, _intermediate_node_to_uid): """ Return a Vector containing the uids of tagged intermediate UNs """ i_sequence = [] v_sequence = [] for i, v in i_components.iteritems(): if i in _intermediate_node_to_uid: i_sequence.append(_intermediate_node_to_uid[i]) v_sequence.append(v) return Vector(index=i_sequence, value=v_sequence)
def _vector_index_to_uid(v): """ Change the vector index from a node to a uid """ return Vector(index=[n_i.uid for n_i in v._index], value=v._value)
def json_to_archive(j): """ Called during retrieval of an archive in JSON format by `json.loads()` The function is called, when `loads()` parses the JSON record, every time an object is not a recognised JSON type. `j` is always a dict. By transforming `j` into an appropriate object we can reconstruct the elements of the archive and finally assemble the archive object. """ if 'CLASS' in j and (j['CLASS'] == Vector.__name__): # uid must be hashable, so we apply `tuple()` return Vector(index=[tuple(i) for i in j['index']], value=j['value']) elif 'CLASS' in j and (j['CLASS'] == LeafNode.__name__): return LeafNode(j) elif 'CLASS' in j and (j['CLASS'] == ElementaryReal.__name__): return ElementaryReal( j['x'], tuple(j['uid']) # Must be hashable ) elif 'CLASS' in j and (j['CLASS'] == IntermediateReal.__name__): label = j['label'] return IntermediateReal( j['value'], j['u_components'], j['d_components'], j['i_components'], label if label != "None" else None, tuple(j['uid']) # Must be hashable ) elif 'CLASS' in j and (j['CLASS'] == Complex.__name__): label = j['label'] return Complex(j['n_re'], j['n_im'], label if label != "None" else None) elif 'CLASS' in j and (j['CLASS'] == Archive.__name__): ar = Archive() if PY2: leaf_nodes_items = j['leaf_nodes'].iteritems intermediate_uids_items = j['intermediate_uids'].iteritems else: leaf_nodes_items = j['leaf_nodes'].items intermediate_uids_items = j['intermediate_uids'].items ar._leaf_nodes = { # eval(i) transforms the string repr of a UID into a tuple eval(i): o for (i, o) in leaf_nodes_items() } # Mapping uid -> (label, u) ar._intermediate_uids = { # eval(i) transforms the string repr of a UID into a tuple eval(i): tuple(args) for (i, args) in intermediate_uids_items() } ar._tagged = j['tagged'] ar._tagged_reals = j['tagged_reals'] return ar else: # Allow parsing to continue at the next level return j
def test_merge_elementary(self): v1 = Vector(index=[Dummy(1)], value=[1]) v2 = Vector(index=[Dummy(2)], value=[1]) v = merge_weighted_vectors(v1, 2, v2, 3) self.assertEqual(len(v), 2) self.assertTrue(is_ordered(v))