本文整理汇总了Python中sympy.utilities.iterables.uniq函数的典型用法代码示例。如果您正苦于以下问题:Python uniq函数的具体用法?Python uniq怎么用?Python uniq使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了uniq函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _eval_extract
def _eval_extract(self, rowsList, colsList):
urow = list(uniq(rowsList))
ucol = list(uniq(colsList))
smat = {}
if len(urow)*len(ucol) < len(self._smat):
# there are fewer elements requested than there are elements in the matrix
for i, r in enumerate(urow):
for j, c in enumerate(ucol):
smat[i, j] = self._smat.get((r, c), 0)
else:
# most of the request will be zeros so check all of self's entries,
# keeping only the ones that are desired
for rk, ck in self._smat:
if rk in urow and ck in ucol:
smat[(urow.index(rk), ucol.index(ck))] = self._smat[(rk, ck)]
rv = self._new(len(urow), len(ucol), smat)
# rv is nominally correct but there might be rows/cols
# which require duplication
if len(rowsList) != len(urow):
for i, r in enumerate(rowsList):
i_previous = rowsList.index(r)
if i_previous != i:
rv = rv.row_insert(i, rv.row(i_previous))
if len(colsList) != len(ucol):
for i, c in enumerate(colsList):
i_previous = colsList.index(c)
if i_previous != i:
rv = rv.col_insert(i, rv.col(i_previous))
return rv
示例2: __eq__
def __eq__(self, other):
if not isinstance(other, Subs):
return False
if (len(self.point) != len(other.point) or
self.free_symbols != other.free_symbols or
sorted(self.point) != sorted(other.point)):
return False
# non-repeated point args
selfargs = [ v[0] for v in sorted(zip(self.variables, self.point),
key = lambda v: v[1]) if list(self.point.args).count(v[1]) == 1 ]
otherargs = [ v[0] for v in sorted(zip(other.variables, other.point),
key = lambda v: v[1]) if list(other.point.args).count(v[1]) == 1 ]
# find repeated point values and subs each associated variable
# for a single symbol
selfrepargs = []
otherrepargs = []
if uniq(self.point) != self.point:
repeated = uniq([ v for v in self.point if
list(self.point.args).count(v) > 1 ])
repswap = dict(zip(repeated, [ C.Dummy() for _ in
xrange(len(repeated)) ]))
selfrepargs = [ (self.variables[i], repswap[v]) for i, v in
enumerate(self.point) if v in repeated ]
otherrepargs = [ (other.variables[i], repswap[v]) for i, v in
enumerate(other.point) if v in repeated ]
return self.expr.subs(selfrepargs) == other.expr.subs(
tuple(zip(otherargs, selfargs))).subs(otherrepargs)
示例3: bifid5_square
def bifid5_square(key):
r"""
5x5 Polybius square.
Produce the Polybius square for the `5 \times 5` Bifid cipher.
Examples
========
>>> from sympy.crypto.crypto import bifid5_square
>>> bifid5_square("gold bug")
Matrix([
[G, O, L, D, B],
[U, A, C, E, F],
[H, I, K, M, N],
[P, Q, R, S, T],
[V, W, X, Y, Z]])
"""
A = alphabet_of_cipher()
# first make sure the letters are capitalized
# and key has no spaces or duplicates
key = uniq(key)
key0 = [x.capitalize() for x in key if x.isalnum()]
# create long key
long_key = key0 + [x for x in A if (not(x in key0) and x != "J")]
f = lambda i, j: Symbol(long_key[5*i + j])
M = Matrix(5, 5, f)
return M
示例4: bifid7_square
def bifid7_square(key):
r"""
7x7 Polybius square.
Produce the Polybius square for the `7 \times 7` Bifid cipher.
Assumes alphabet of symbols is "A", ..., "Z", "0", ..., "22".
(Also, assumes you have some way of distinguishing "22"
from "2", "2" juxtaposed together for deciphering...)
Examples
========
>>> from sympy.crypto.crypto import bifid7_square
>>> bifid7_square("gold bug")
Matrix([
[ G, O, L, D, B, U, A],
[ C, E, F, H, I, J, K],
[ M, N, P, Q, R, S, T],
[ V, W, X, Y, Z, 0, 1],
[ 2, 3, 4, 5, 6, 7, 8],
[ 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22]])
"""
A = alphabet_of_cipher() + [str(a) for a in range(23)]
# first make sure the letters are capitalized
# and text has no spaces
key = uniq(key)
key0 = [x.capitalize() for x in key if x.isalnum()]
# create long key
long_key = key0 + [x for x in A if (not(x in key0))]
f = lambda i, j: Symbol(long_key[7*i + j])
M = Matrix(7, 7, f)
return M
示例5: decipher_vigenere
def decipher_vigenere(ct, key, symbols="ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
"""
Decode using the Vigenère cipher.
Examples
========
>>> from sympy.crypto.crypto import decipher_vigenere
>>> key = "encrypt"
>>> ct = "QRGK kt HRZQE BPR"
>>> decipher_vigenere(ct, key)
'MEETMEONMONDAY'
"""
symbols = "".join(symbols)
A = alphabet_of_cipher(symbols)
N = len(A) # normally, 26
key0 = uniq(key)
key0 = [x.capitalize() for x in key0 if x.isalnum()]
K = [A.index(x) for x in key0]
k = len(K)
ct0 = [x.capitalize() for x in ct if x.isalnum()]
C = [A.index(x) for x in ct0]
n = len(C)
#m = n//k
P = [(-K[i % k] + C[i]) % N for i in range(n)]
return "".join([str(A[x]) for x in P])
示例6: _run
def _run(coeffs):
# find runs in coeffs such that the difference in terms (mod 1)
# of t1, t2, ..., tn is 1/n
u = list(uniq(coeffs))
for i in range(len(u)):
dj = ([((u[j] - u[i]) % 1, j) for j in range(i + 1, len(u))])
for one, j in dj:
if one.p == 1 and one.q != 1:
n = one.q
got = [i]
get = list(range(1, n))
for d, j in dj:
m = n*d
if m.is_Integer and m in get:
get.remove(m)
got.append(j)
if not get:
break
else:
continue
for i, j in enumerate(got):
c = u[j]
coeffs.remove(c)
got[i] = c
return one.q, got[0], got[1:]
示例7: __new__
def __new__(cls, expr, variables, point, **assumptions):
if not ordered_iter(variables, Tuple):
variables = [variables]
variables = Tuple(*sympify(variables))
if uniq(variables) != variables:
repeated = repeated = [ v for v in set(variables)
if list(variables).count(v) > 1 ]
raise ValueError('cannot substitute expressions %s more than '
'once.' % repeated)
if not ordered_iter(point, Tuple):
point = [point]
point = Tuple(*sympify(point))
if len(point) != len(variables):
raise ValueError('Number of point values must be the same as '
'the number of variables.')
# it's necessary to use dummy variables internally
new_variables = Tuple(*[ arg.as_dummy() if arg.is_Symbol else
C.Dummy(str(arg)) for arg in variables ])
expr = sympify(expr).subs(tuple(zip(variables, new_variables)))
if expr.is_commutative:
assumptions['commutative'] = True
obj = Expr.__new__(cls, expr, new_variables, point, **assumptions)
return obj
示例8: bifid6_square
def bifid6_square(key):
r"""
6x6 Polybius square.
Produces the Polybius square for the `6 \times 6` Bifid cipher.
Assumes alphabet of symbols is "A", ..., "Z", "0", ..., "9".
Examples
========
>>> from sympy.crypto.crypto import bifid6_square
>>> key = "encrypt"
>>> bifid6_square(key)
Matrix([
[E, N, C, R, Y, P],
[T, A, B, D, F, G],
[H, I, J, K, L, M],
[O, Q, S, U, V, W],
[X, Z, 0, 1, 2, 3],
[4, 5, 6, 7, 8, 9]])
"""
A = alphabet_of_cipher() + [str(a) for a in range(10)]
# first make sure the letters are capitalized
# and text has no spaces
key = uniq(key)
key0 = [x.capitalize() for x in key if x.isalnum()]
# create long key
long_key = key0 + [x for x in A if not(x in key0)]
f = lambda i, j: Symbol(long_key[6*i + j])
M = Matrix(6, 6, f)
return M
示例9: test_uniq
def test_uniq():
assert list(uniq(p.copy() for p in partitions(4))) == [{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}]
assert list(uniq(x % 2 for x in range(5))) == [0, 1]
assert list(uniq("a")) == ["a"]
assert list(uniq("ababc")) == list("abc")
assert list(uniq([[1], [2, 1], [1]])) == [[1], [2, 1]]
assert list(uniq(permutations(i for i in [[1], 2, 2]))) == [([1], 2, 2), (2, [1], 2), (2, 2, [1])]
assert list(uniq([2, 3, 2, 4, [2], [1], [2], [3], [1]])) == [2, 3, 4, [2], [1], [3]]
示例10: is_concyclic
def is_concyclic(self, *args):
"""Do `self` and the given sequence of points lie in a circle?
Returns True if the set of points are concyclic and
False otherwise. A trivial value of True is returned
if there are fewer than 2 other points.
Parameters
==========
args : sequence of Points
Returns
=======
is_concyclic : boolean
Examples
========
>>> from sympy import Point
Define 4 points that are on the unit circle:
>>> p1, p2, p3, p4 = Point(1, 0), (0, 1), (-1, 0), (0, -1)
>>> p1.is_concyclic() == p1.is_concyclic(p2, p3, p4) == True
True
Define a point not on that circle:
>>> p = Point(1, 1)
>>> p.is_concyclic(p1, p2, p3)
False
"""
points = (self,) + args
points = Point._normalize_dimension(*[Point(i) for i in points])
points = list(uniq(points))
if not Point.affine_rank(*points) <= 2:
return False
origin = points[0]
points = [p - origin for p in points]
# points are concyclic if they are coplanar and
# there is a point c so that ||p_i-c|| == ||p_j-c|| for all
# i and j. Rearranging this equation gives us the following
# condition: the matrix `mat` must not a pivot in the last
# column.
mat = Matrix([list(i) + [i.dot(i)] for i in points])
rref, pivots = mat.rref()
if len(origin) not in pivots:
return True
return False
示例11: _do_ellipse_intersection
def _do_ellipse_intersection(self, o):
"""The intersection of an ellipse with another ellipse or a circle.
Private helper method for `intersection`.
"""
x = Dummy('x', real=True)
y = Dummy('y', real=True)
seq = self.equation(x, y)
oeq = o.equation(x, y)
result = solve([seq, oeq], [x, y])
return [Point(*r) for r in list(uniq(result))]
示例12: _do_ellipse_intersection
def _do_ellipse_intersection(self, o):
"""The intersection of an ellipse with another ellipse or a circle.
Private helper method for `intersection`.
"""
x = Dummy('x', real=True)
y = Dummy('y', real=True)
seq = self.equation(x, y)
oeq = o.equation(x, y)
# TODO: Replace solve with nonlinsolve, when nonlinsolve will be able to solve in real domain
result = solve([seq, oeq], x, y)
return [Point(*r) for r in list(uniq(result))]
示例13: DirectProduct
def DirectProduct(*groups):
"""
Returns the direct product of several groups as a permutation group.
This is implemented much like the __mul__ procedure for taking the direct
product of two permutation groups, but the idea of shifting the
generators is realized in the case of an arbitrary number of groups.
A call to DirectProduct(G1, G2, ..., Gn) is generally expected to be faster
than a call to G1*G2*...*Gn (and thus the need for this algorithm).
Examples
========
>>> from sympy.combinatorics.group_constructs import DirectProduct
>>> from sympy.combinatorics.named_groups import CyclicGroup
>>> C = CyclicGroup(4)
>>> G = DirectProduct(C, C, C)
>>> G.order()
64
See Also
========
__mul__
"""
degrees = []
gens_count = []
total_degree = 0
total_gens = 0
for group in groups:
current_deg = group.degree
current_num_gens = len(group.generators)
degrees.append(current_deg)
total_degree += current_deg
gens_count.append(current_num_gens)
total_gens += current_num_gens
array_gens = []
for i in range(total_gens):
array_gens.append(list(range(total_degree)))
current_gen = 0
current_deg = 0
for i in range(len(gens_count)):
for j in range(current_gen, current_gen + gens_count[i]):
gen = ((groups[i].generators)[j - current_gen]).array_form
array_gens[j][current_deg:current_deg + degrees[i]] = \
[x + current_deg for x in gen]
current_gen += gens_count[i]
current_deg += degrees[i]
perm_gens = list(uniq([_af_new(list(a)) for a in array_gens]))
return PermutationGroup(perm_gens, dups=False)
示例14: test_uniq
def test_uniq():
assert list(uniq(p.copy() for p in partitions(4))) == \
[{4: 1}, {1: 1, 3: 1}, {2: 2}, {1: 2, 2: 1}, {1: 4}]
assert list(uniq(x % 2 for x in range(5))) == [0, 1]
assert list(uniq('a')) == ['a']
assert list(uniq('ababc')) == list('abc')
assert list(uniq([[1], [2, 1], [1]])) == [[1], [2, 1], [1]]
assert list(uniq(permutations(i for i in [[1], 2, 2]))) == \
[([1], 2, 2), (2, [1], 2), (2, 2, [1]), (2, [1], 2), (2, 2, [1])]
示例15: are_concurrent
def are_concurrent(*planes):
"""Is a sequence of Planes concurrent?
Two or more Planes are concurrent if their intersections
are a common line.
Parameters
==========
planes: list
Returns
=======
Boolean
Examples
========
>>> from sympy import Plane, Point3D
>>> a = Plane(Point3D(5, 0, 0), normal_vector=(1, -1, 1))
>>> b = Plane(Point3D(0, -2, 0), normal_vector=(3, 1, 1))
>>> c = Plane(Point3D(0, -1, 0), normal_vector=(5, -1, 9))
>>> Plane.are_concurrent(a, b)
True
>>> Plane.are_concurrent(a, b, c)
False
"""
planes = list(uniq(planes))
for i in planes:
if not isinstance(i, Plane):
raise ValueError('All objects should be Planes but got %s' % i.func)
if len(planes) < 2:
return False
planes = list(planes)
first = planes.pop(0)
sol = first.intersection(planes[0])
if sol == []:
return False
else:
line = sol[0]
for i in planes[1:]:
l = first.intersection(i)
if not l or not l[0] in line:
return False
return True