def test_power():
"""
Take units to some power.
"""
from sympy import nsimplify
pc_cgs = cm_per_pc
mK_cgs = 1e-3
u1_dims = mass * length**2 * time**-3 * temperature**4
u1 = Unit("g * pc**2 * s**-3 * mK**4")
u2 = u1**2
assert_true(u2.dimensions == u1_dims**2)
assert_allclose_units(u2.base_value, (pc_cgs**2 * mK_cgs**4)**2, 1e-12)
u3 = u1**(-1.0/3)
assert_true(u3.dimensions == nsimplify(u1_dims**(-1.0/3)))
assert_allclose_units(u3.base_value, (pc_cgs**2 * mK_cgs**4)**(-1.0/3), 1e-12)
python类nsimplify()的实例源码
def random_complex_number(a=2, b=-1, c=3, d=1, rational=False):
"""
Return a random complex number.
To reduce chance of hitting branch cuts or anything, we guarantee
b <= Im z <= d, a <= Re z <= c
"""
A, B = uniform(a, c), uniform(b, d)
if not rational:
return A + I*B
return nsimplify(A, rational=True) + I*nsimplify(B, rational=True)
def writeSbml(self, sbml_speciesReference, sbml_level=Settings.defaultSbmlLevel, sbml_version=Settings.defaultSbmlVersion):
""" Writes a species reference to a sbml file """
sbml_speciesReference.setSpecies(self.__species)
SbmlObject.writeSbml(self, sbml_speciesReference, sbml_level, sbml_version)
if sbml_level == 3 and self.__hasId:
Variable.writeSbml(self, sbml_speciesReference, sbml_level, sbml_version)
elif not sbml_speciesReference.isModifier():
if sbml_level == 1:
t_stoichiometry = nsimplify(self.stoichiometry.getInternalMathFormula())
if t_stoichiometry.is_Rational and t_stoichiometry.q != 1:
sbml_speciesReference.setStoichiometry(int(t_stoichiometry.p))
sbml_speciesReference.setDenominator(int(t_stoichiometry.q))
elif t_stoichiometry != MathFormula.ONE:
# print srepr(t_stoichiometry)
# t_stoichiometry = self.stoichiometry.getSbmlMathFormula(sbml_level, sbml_version)
sbml_speciesReference.setStoichiometry(int(t_stoichiometry))
else:
t_stoichiometry = self.stoichiometry.getSbmlMathFormula(sbml_level, sbml_version)
if t_stoichiometry.getType() == AST_REAL:
if t_stoichiometry.getReal() != 1.0 or sbml_level == 3:
sbml_speciesReference.setStoichiometry(t_stoichiometry.getReal())
elif t_stoichiometry.getType() == AST_INTEGER:
if t_stoichiometry.getInteger() != 1 or sbml_level == 3:
sbml_speciesReference.setStoichiometry(t_stoichiometry.getInteger())
elif t_stoichiometry.getType() == AST_RATIONAL:
sbml_speciesReference.setStoichiometry(t_stoichiometry.getNumerator())
sbml_speciesReference.setDenominator(t_stoichiometry.getDenominator())
else:
if sbml_level == 3:
if self.__model.listOfVariables.containsSymbol(self.stoichiometry.getInternalMathFormula()):
sbml_speciesReference.setId(t_stoichiometry.getName())
else:
sbml_speciesReference.setStoichiometry(t_stoichiometry)
else:
sbml_stoichiometry_math = sbml_speciesReference.createStoichiometryMath()
sbml_stoichiometry_math.setMath(t_stoichiometry)
if sbml_level >= 3:
sbml_speciesReference.setConstant(self.constant)