本文整理汇总了Python中sympy.Matrix.extract方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.extract方法的具体用法?Python Matrix.extract怎么用?Python Matrix.extract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.Matrix
的用法示例。
在下文中一共展示了Matrix.extract方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_extract
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import extract [as 别名]
def test_extract():
m = Matrix(4, 3, lambda i, j: i*3 + j)
assert m.extract([0,1,3],[0,1]) == Matrix(3,2,[0,1,3,4,9,10])
assert m.extract([0,3],[0,0,2]) == Matrix(2,3,[0,0,2,9,9,11])
assert m.extract(range(4),range(3)) == m
raises(IndexError, 'm.extract([4], [0])')
raises(IndexError, 'm.extract([0], [3])')
示例2: _mat_inv_mul
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import extract [as 别名]
def _mat_inv_mul(self, A, B):
"""Internal Function
Computes A^-1 * B symbolically w/ substitution, where B is not
necessarily a vector, but can be a matrix.
"""
# Note: investigate difficulty in only creating symbols for non-zero
# entries; this could speed things up, perhaps?
r1, c1 = A.shape
r2, c2 = B.shape
temp1 = Matrix(r1, c1, lambda i, j: Symbol('x' + str(j + r1 * i)))
temp2 = Matrix(r2, c2, lambda i, j: Symbol('y' + str(j + r2 * i)))
for i in range(len(temp1)):
if A[i] == 0:
temp1[i] = 0
for i in range(len(temp2)):
if B[i] == 0:
temp2[i] = 0
temp3 = []
for i in range(c2):
temp3.append(temp1.LUsolve(temp2.extract(range(r2), [i])))
temp3 = Matrix([i.T for i in temp3]).T
if Kane.simp == True:
temp3.simplify()
return temp3.subs(dict(zip(temp1, A))).subs(dict(zip(temp2, B)))
示例3: Kane
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import extract [as 别名]
#.........这里部分代码省略.........
set_f = set([a for a in temp_f if a.args == (t,)])
set_d = set([a for a in temp_d if ((a.args[0] in set_f) and all([i == t
for i in a.variables]))])
return list(set.union(set_f, set_d) - set(insyms))
def _find_othersymbols(self, inlist, insyms=[]):
"""Finds all non-dynamic symbols in the expressions."""
return list(reduce(set.union, [i.atoms(Symbol) for i in inlist]) -
set(insyms))
def _mat_inv_mul(self, A, B):
"""Internal Function
Computes A^-1 * B symbolically w/ substitution, where B is not
necessarily a vector, but can be a matrix.
"""
# Note: investigate difficulty in only creating symbols for non-zero
# entries; this could speed things up, perhaps?
r1, c1 = A.shape
r2, c2 = B.shape
temp1 = Matrix(r1, c1, lambda i, j: Symbol('x' + str(j + r1 * i)))
temp2 = Matrix(r2, c2, lambda i, j: Symbol('y' + str(j + r2 * i)))
for i in range(len(temp1)):
if A[i] == 0:
temp1[i] = 0
for i in range(len(temp2)):
if B[i] == 0:
temp2[i] = 0
temp3 = []
for i in range(c2):
temp3.append(temp1.LUsolve(temp2.extract(range(r2), [i])))
temp3 = Matrix([i.T for i in temp3]).T
if Kane.simp == True:
temp3.simplify()
return temp3.subs(dict(zip(temp1, A))).subs(dict(zip(temp2, B)))
def coords(self, qind, qdep=[], coneqs=[]):
"""Supply all the generalized coordiantes in a list.
If some coordinates are dependent, supply them as part of qdep. Their
dependent nature will only show up in the linearization process though.
Parameters
==========
qind : list
A list of independent generalized coords
qdep : list
List of dependent coordinates
coneq : list
List of expressions which are equal to zero; these are the
configuration constraint equations
"""
if not isinstance(qind, (list, tuple)):
raise TypeError('Generalized coords. must be supplied in a list.')
self._q = qind + qdep
self._qdot = [diff(i, dynamicsymbols._t) for i in self._q]
if not isinstance(qdep, (list, tuple)):
raise TypeError('Dependent speeds and constraints must each be '
'provided in their own list.')
if len(qdep) != len(coneqs):
示例4: __states
# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import extract [as 别名]
#.........这里部分代码省略.........
states[new_level][tuple(new_weight)][
"states"].append(new_state)
except KeyError:
states[new_level][tuple(new_weight)] = {
"states" : [new_state],
"rotation_to_ob" : Matrix([[1]]),
}
except KeyError:
states[new_level] = {
tuple(new_weight) : {
"states" : [new_state],
"rotation_to_ob" : Matrix([[1]]),
}
}
#resolve degeneracy
new_level = level + 1
for weight in weights[new_level]:
states_for_this_weight = states[new_level][weight]["states"]
degeneracy = len(states_for_this_weight)
if degeneracy == 1:
continue
scalar_product_matrix = Matrix(degeneracy, degeneracy,
lambda i,j : states_for_this_weight[i]["norm"]*
states_for_this_weight[j]["norm"]*self.scalar_product(
states_for_this_weight[i]["lowering_chain"],
states_for_this_weight[j]["lowering_chain"],
simple_root_length_squared_list, cartan_matrix,
weights)
if i > j else 0)
scalar_product_matrix += scalar_product_matrix.T
scalar_product_matrix += eye(degeneracy)
rref = scalar_product_matrix.rref()
dependents = [index for index in range(degeneracy)
if index not in rref[1]]
# calculate additional matrix elements (if any)
for independent in rref[1]:
for state_num in range(degeneracy):
if state_num != independent:
# state_num's parent might be linked to independent
state_num_norm = states_for_this_weight[state_num][
"norm"]
state_num_matrix_element_information = (
states_for_this_weight[state_num][
"matrix_element_information"])
parent_level = (
state_num_matrix_element_information[0][
"level"])
parent_weight = (
state_num_matrix_element_information[0][
"weight"])
parent_state_num = (
state_num_matrix_element_information[0][
"state_num"])
direction = (
state_num_matrix_element_information[0][
"direction"])
parent_norm = states[parent_level][parent_weight][
"states"][parent_state_num]["norm"]
matrix_element = (
Rational(1,1)*scalar_product_matrix[
state_num, independent]*parent_norm/
state_num_norm)
if matrix_element == 0:
continue
states[new_level][weight]["states"][independent][
"matrix_element_information"].append(
{"level" : parent_level,
"weight" : parent_weight,
"state_num" : parent_state_num,
"direction" : direction,
"matrix_element" : matrix_element
}
)
# orthonormalize
if degeneracy == 1:
states[new_level][weight]["rotation_to_ob"] = Matrix([[1]])
else:
norm_matrix = scalar_product_matrix.extract(
rref[1], rref[1])
rotation_to_ob = gram_schmidt_rotation(norm_matrix)
states[new_level][weight]["rotation_to_ob"] = rotation_to_ob
# keep only independent states
states[new_level][weight]["states"] = [
states_for_this_weight[i] for i in rref[1]]
# add an unique index to all states
state_index = 0
for level in states:
for weight in states[level]:
states[level][weight]["start_index"] = state_index
for state_num in range(len(states[level][weight]["states"])):
states[level][weight]["states"][state_num]["index"] = (
state_index)
state_index += 1
states[level][weight]["end_index"] = state_index - 1
print("total number of states is {0}".format(state_index))
del self.scalar_products
del self.non_existent_paths
return states