本文整理汇总了Python中sage.matrix.constructor.Matrix.ncols方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.ncols方法的具体用法?Python Matrix.ncols怎么用?Python Matrix.ncols使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.matrix.constructor.Matrix
的用法示例。
在下文中一共展示了Matrix.ncols方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sage.matrix.constructor import Matrix [as 别名]
# 或者: from sage.matrix.constructor.Matrix import ncols [as 别名]
class _FiniteBasisConverter:
def __init__(self, P, comb_mod, basis):
r"""
Basis should be a finite set of polynomials
"""
self._poly_ring = P
self._module = comb_mod
self._basis = basis
max_deg = max([self._poly_ring(b).degree() for b in self._basis])
monoms = []
for b in self._basis:
poly = self._poly_ring(b)
monoms += poly.monomials()
monoms_list = tuple(Set(monoms))
# check if the basis represented in terms of Monomials is efficient
degs = [self._poly_ring(m).degree() for m in monoms]
min_deg, max_deg = min(degs), max(degs)
monoms_obj = Monomials(self._poly_ring, (min_deg, max_deg + 1))
if monoms_obj.cardinality() < 2 * len(monoms_list):
computational_basis = monoms_obj
else:
computational_basis = monoms_list
self._monomial_module = PolynomialFreeModule(
P=self._poly_ring, basis=computational_basis)
cols = [self._monomial_module(b).to_vector() for b in self._basis]
self._basis_mat = Matrix(cols).transpose()
if self._basis_mat.ncols() > self._basis_mat.rank():
raise ValueError(
"Basis polynomials are not linearly independent")
def convert(self, p):
r"""
Algorithm is to convert all polynomials into monomials and use
linear algebra to solve for the appropriate coefficients in this
common basis.
"""
try:
p_vect = self._monomial_module(p).to_vector()
decomp = self._basis_mat.solve_right(p_vect)
except ValueError:
raise ValueError(
"Value %s is not spanned by the basis polynomials" % p)
polys = [v[1] * self._module.monomial(v[0])
for v in zip(self._basis, decomp)]
module_p = sum(polys, self._module.zero())
return module_p
示例2: Matroid
# 需要导入模块: from sage.matrix.constructor import Matrix [as 别名]
# 或者: from sage.matrix.constructor.Matrix import ncols [as 别名]
#.........这里部分代码省略.........
A[V.index(i), mm] = -1
A[V.index(j), mm] += 1 # So loops get 0
mm += 1
M = RegularMatroid(matrix=A, groundset=groundset)
want_regular = False # Save some time, since result is already regular
else:
M = GraphicMatroid(G, groundset=groundset)
# Matrices:
elif key in ['matrix', 'reduced_matrix']:
A = data
is_reduced = (key == 'reduced_matrix')
# Fix the representation
if not is_Matrix(A):
if base_ring is not None:
A = Matrix(base_ring, A)
else:
A = Matrix(A)
# Fix the ring
if base_ring is not None:
if A.base_ring() is not base_ring:
A = A.change_ring(base_ring)
elif A.base_ring() is ZZ and not want_regular: # Usually a rational matrix is intended, we presume.
A = A.change_ring(QQ)
base_ring = QQ
else:
base_ring = A.base_ring()
# Check groundset
if groundset is not None:
if not is_reduced:
if len(groundset) == A.ncols():
pass
elif len(groundset) == A.nrows() + A.ncols():
is_reduced = True
else:
raise ValueError("groundset size does not correspond to matrix size")
elif is_reduced:
if len(groundset) == A.nrows() + A.ncols():
pass
else:
raise ValueError("groundset size does not correspond to matrix size")
if is_reduced:
kw = dict(groundset=groundset, reduced_matrix=A)
else:
kw = dict(groundset=groundset, matrix=A)
if isinstance(base_ring, FiniteField):
q = base_ring.order()
else:
q = 0
if q == 2:
M = BinaryMatroid(**kw)
elif q == 3:
M = TernaryMatroid(**kw)
elif q == 4:
M = QuaternaryMatroid(**kw)
else:
M = LinearMatroid(ring=base_ring, **kw)
# Rank functions:
elif key == 'rank_function':
示例3: Matroid
# 需要导入模块: from sage.matrix.constructor import Matrix [as 别名]
# 或者: from sage.matrix.constructor.Matrix import ncols [as 别名]
#.........这里部分代码省略.........
M = RegularMatroid(matrix=A, groundset=kwds['groundset'])
want_regular = False # Save some time, since result is already regular
# Matrices:
if 'matrix' in kwds or 'reduced_matrix' in kwds:
if 'matrix' in kwds:
A = kwds['matrix']
if 'reduced_matrix' in kwds:
A = kwds['reduced_matrix']
# Fix the representation
if not isinstance(A, sage.matrix.matrix.Matrix):
try:
if base_ring is not None:
A = Matrix(base_ring, A)
else:
A = Matrix(A)
except ValueError:
raise ValueError("input does not seem to contain a matrix.")
# Fix the ring
if base_ring is not None:
if A.base_ring() != base_ring:
A = A.change_ring(base_ring)
elif A.base_ring() == ZZ and not want_regular: # Usually a rational matrix is intended, we presume.
A = A.change_ring(QQ)
base_ring = QQ
else:
base_ring = A.base_ring()
# Determine groundset:
if 'matrix' in kwds:
if 'groundset' in kwds:
if len(kwds['groundset']) == A.nrows() + A.ncols():
kwds['reduced_matrix'] = A
kwds.pop('matrix')
else:
if len(kwds['groundset']) != A.ncols():
raise ValueError("groundset size does not correspond to matrix size.")
else:
kwds['groundset'] = range(A.ncols())
if 'reduced_matrix' in kwds:
if 'groundset' in kwds:
if len(kwds['groundset']) != A.nrows() + A.ncols():
raise ValueError("groundset size does not correspond to matrix size.")
else:
kwds['groundset'] = range(A.nrows() + A.ncols())
if 'matrix' in kwds:
if base_ring == GF(2):
M = BinaryMatroid(groundset=kwds['groundset'], matrix=A)
elif base_ring == GF(3):
M = TernaryMatroid(groundset=kwds['groundset'], matrix=A)
elif base_ring.is_field() and base_ring.order() == 4: # GF(4) can have different generators.
M = QuaternaryMatroid(groundset=kwds['groundset'], matrix=A)
else:
M = LinearMatroid(groundset=kwds['groundset'], matrix=A, ring=base_ring)
if 'reduced_matrix' in kwds:
if A.base_ring() == GF(2):
M = BinaryMatroid(groundset=kwds['groundset'], reduced_matrix=A)
elif A.base_ring() == GF(3):
M = TernaryMatroid(groundset=kwds['groundset'], reduced_matrix=A)
elif A.base_ring().is_field() and A.base_ring().order() == 4: # GF(4) can have different generators.
M = QuaternaryMatroid(groundset=kwds['groundset'], reduced_matrix=A)
else:
M = LinearMatroid(groundset=kwds['groundset'], reduced_matrix=A, ring=base_ring)
示例4: apply_Up
# 需要导入模块: from sage.matrix.constructor import Matrix [as 别名]
# 或者: from sage.matrix.constructor.Matrix import ncols [as 别名]
def apply_Up(self,c,group = None,scale = 1,parallelize = False,times = 0,progress_bar = False,method = 'naive', repslocal = None, Up_reps = None, steps = 1):
r"""
Apply the Up Hecke operator operator to ``c``.
"""
assert steps >= 1
V = self.coefficient_module()
R = V.base_ring()
gammas = self.group().gens()
if Up_reps is None:
Up_reps = self.S_arithgroup().get_Up_reps()
if repslocal is None:
try:
prec = V.base_ring().precision_cap()
except AttributeError:
prec = None
repslocal = self.get_Up_reps_local(prec)
i = 0
if method == 'naive':
assert times == 0
G = self.S_arithgroup()
Gn = G.large_group()
if self.use_shapiro():
if self.coefficient_module().trivial_action():
def calculate_Up_contribution(lst, c, i, j):
return sum([c.evaluate_and_identity(tt) for sk, tt in lst])
else:
def calculate_Up_contribution(lst, c, i, j):
return sum([sk * c.evaluate_and_identity(tt) for sk, tt in lst])
input_vec = []
for j, gamma in enumerate(gammas):
for i, xi in enumerate(G.coset_reps()):
delta = Gn(G.get_coset_ti(set_immutable(xi * gamma.quaternion_rep))[0])
input_vec.append(([(sk, Gn.get_hecke_ti(g,delta)) for sk, g in zip(repslocal, Up_reps)], c, i, j))
vals = [[V.coefficient_module()(0,normalize=False) for xi in G.coset_reps()] for gamma in gammas]
if parallelize:
for inp, outp in parallel(calculate_Up_contribution)(input_vec):
vals[inp[0][-1]][inp[0][-2]] += outp
else:
for inp in input_vec:
outp = calculate_Up_contribution(*inp)
vals[inp[-1]][inp[-2]] += outp
ans = self([V(o) for o in vals])
else:
Gpn = G.small_group()
if self.trivial_action():
def calculate_Up_contribution(lst,c,num_gamma):
return sum([c.evaluate(tt) for sk, tt in lst], V(0,normalize=False))
else:
def calculate_Up_contribution(lst,c,num_gamma,pb_fraction=None):
i = 0
ans = V(0, normalize=False)
for sk, tt in lst:
ans += sk * c.evaluate(tt)
update_progress(i * pb_fraction, 'Up action')
return ans
input_vec = []
for j,gamma in enumerate(gammas):
input_vec.append(([(sk, Gpn.get_hecke_ti(g,gamma)) for sk, g in zip(repslocal, Up_reps)], c, j))
vals = [V(0,normalize=False) for gamma in gammas]
if parallelize:
for inp,outp in parallel(calculate_Up_contribution)(input_vec):
vals[inp[0][-1]] += outp
else:
for counter, inp in enumerate(input_vec):
outp = calculate_Up_contribution(*inp, pb_fraction=float(1)/float(len(repslocal) * len(input_vec)))
vals[inp[-1]] += outp
ans = self(vals)
if scale != 1:
ans = scale * ans
else:
assert method == 'bigmatrix'
verbose('Getting Up matrices...')
try:
N = len(V(0)._moments.list())
except AttributeError:
N = 1
nreps = len(Up_reps)
ngens = len(self.group().gens())
NN = ngens * N
A = Matrix(ZZ,NN,NN,0)
total_counter = ngens**2
counter = 0
iS = 0
for i,gi in enumerate(self.group().gens()):
ti = [tuple(self.group().get_hecke_ti(sk,gi).word_rep) for sk in Up_reps]
jS = 0
for ans in find_newans(self,repslocal,ti):
A.set_block(iS,jS,ans)
jS += N
if progress_bar:
counter +=1
update_progress(float(counter)/float(total_counter),'Up matrix')
iS += N
verbose('Computing 2^(%s)-th power of a %s x %s matrix'%(times,A.nrows(),A.ncols()))
for i in range(times):
A = A**2
#.........这里部分代码省略.........