我正试图为我的日震学课程开发一个图表,这个问题提供了一个分段函数,描述了恒星中“流体”的动态,就好像它是一回事,它是另一回事,它是另一回事。我一次又一次地接收到这个'Mul' object cannot be interpreted as an integer,但是我处理的是实际中的数字,而不仅仅是整数集。我不知道如何绕过这件事,需要指导。代码如下。
import sympy as sy
from sympy import *
from sympy.physics.units import Unit
import numpy as np
import sys
import math
import scipy as sp
from scipy import special
phi = Symbol('phi', Variable = True)
x = Symbol('x', Variable = True, Real = True)
t = Symbol('t', Variable = True, Real = True)
xi = Symbol('xi', Function = True)
Solar_Radius = Symbol('R', Constant = True, unit = "meters")
Sound_Speed = Symbol('c', Constant = True, unit = "meters per second", Real = True)
gamma = Symbol('gamma', Constant = True)
gravity = Symbol('g', Constant = True, unit = "meters per second per second")
Solar_Radius = 6.963 * 10 ** 6
gamma = 5/3
g = 274.8265625336
gas_constant = 8201.25
c = 8.1 * 10 ** 3
for t in range(0,x/c):
xi[x,t] = 0
for t in range(x/c,00):
xi[x,t] = (1/2)*sy.exp(gamma*g*x/(2*c**2))*mpmath.besselj(0, (gamma*g/(2*c)*sy.sqrt(t**2 - ((x/c)**2))),derivative = 0)全面回溯:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-3506376f1686> in <module>()
----> 1 for t in range(0,x/c):
2 xi[x,t] = 0
3 for t in range(x/c,00):
4 xi[x,t] = (1/2)*sy.exp(gamma*g*x/(2*c**2))*mpmath.besselj(0, (gamma*g/(2*c)*sy.sqrt(t**2 - ((x/c)**2))),derivative = 0)
TypeError: 'Mul' object cannot be interpreted as an integer发布于 2016-10-04 18:52:26
这里有很多问题:
real,它应该是小写(如Symbol('x', real=True))。其他人什么都不做。如果需要单元,则应在SymPy中使用sympy.physics.units单元模块。不需要指定符号是常量还是变量。Solar_Radius和gamma重新定义为数字。这意味着这些变量的符号定义是没有意义的。from __future__ import division,否则像1/2和5/3这样的东西将被整数除法截断(这在Python3中不是问题)。range(0, x/c)没有道理。range创建一个数字列表,如range(0, 3) -> [0, 1, 2]。但是x/c不是一个数字,它是一个符号表达式。xi[x, t] = ...没有意义。xi是一个符号,它不允许索引,当然也不允许赋值。lambdify。你想要的是Piecewise。语法为Piecewise((expr, cond), (expr, cond), ..., (expr, True)),其中expr是在cond为true时使用的表达式((expr, True)是“否则”条件)。
以你为例,我相信你想
expr = Piecewise((0, t < x/c), (sy.exp(gamma*g*x/(2*c**2))*sy.besselj(0, (gamma*g/(2*c)*sy.sqrt(t**2 - (x/c)**2)))/2, t >= x/c))如果要将其转换为x和t中的数字函数,请使用
xi = lambdify((x, t), expr)https://stackoverflow.com/questions/39839138
复制相似问题