sympy

Can't convert expression to float

旧城冷巷雨未停 提交于 2020-01-24 12:33:30
问题 I'm trying to learn the ins and outs of symbolic manipulation in python (I'm a beginner). I have the following basic code, and the output is giving me an error telling me that it "can't convert expression to float". What's wrong with this code: from sympy import * from math import * def h(x): return log(0.75392 * x) x = symbols('x') hprime = h(x).diff(x) print(hprime) 回答1: This is a classic example of what is said in PEP-8 about wildcard imports: Wildcard imports ( from <module> import * )

Can't convert expression to float

十年热恋 提交于 2020-01-24 12:31:06
问题 I'm trying to learn the ins and outs of symbolic manipulation in python (I'm a beginner). I have the following basic code, and the output is giving me an error telling me that it "can't convert expression to float". What's wrong with this code: from sympy import * from math import * def h(x): return log(0.75392 * x) x = symbols('x') hprime = h(x).diff(x) print(hprime) 回答1: This is a classic example of what is said in PEP-8 about wildcard imports: Wildcard imports ( from <module> import * )

SymPy - substitute sybolic entries in a matrix

前提是你 提交于 2020-01-22 00:43:06
问题 I have a python function which generates a sympy.Matrix with symbolic entries. It works effectively like: import sympy as sp M = sp.Matrix([[1,0,2],[0,1,2],[1,2,0]]) def make_symbolic_matrix(M): M_sym = sp.zeros(3) syms = ['a0:3'] for i in xrange(3): for j in xrange(3): if M[i,j] == 1: M_sym = syms[i] elif M[i,j] == 2: M_sym = 1 - syms[i] return M_sym This works just fine. I get a matrix out, which I can use for all the symbolical calculations I need. My issue is that now I want to evaluate

AttributeError: 'Mul' object has no attribute 'sqrt'

≯℡__Kan透↙ 提交于 2020-01-16 08:34:58
问题 I am receiving the error stated in the title. Full error: MaxD = Cone*np.sqrt(SymsX/np.pi)*np.exp((-SymsX/(k*T))) #Define Maxwellian distribution function AttributeError: 'Mul' object has no attribute 'sqrt' Here is the code: from sympy.interactive import printing printing.init_printing(use_latex = True) import numpy as np from sympy import Eq, dsolve, Function, Symbol, symbols import sympy as sp EpNaut = 8.854187E-12 u0 = 1.256E-6 k = 1/(4*np.pi*EpNaut) NumGen = 1000 #How many solution

AttributeError: 'Mul' object has no attribute 'sqrt'

六月ゝ 毕业季﹏ 提交于 2020-01-16 08:34:49
问题 I am receiving the error stated in the title. Full error: MaxD = Cone*np.sqrt(SymsX/np.pi)*np.exp((-SymsX/(k*T))) #Define Maxwellian distribution function AttributeError: 'Mul' object has no attribute 'sqrt' Here is the code: from sympy.interactive import printing printing.init_printing(use_latex = True) import numpy as np from sympy import Eq, dsolve, Function, Symbol, symbols import sympy as sp EpNaut = 8.854187E-12 u0 = 1.256E-6 k = 1/(4*np.pi*EpNaut) NumGen = 1000 #How many solution

Should I use sympy.utilities.iterables.variations() to get permutations_with_replacement?

痴心易碎 提交于 2020-01-15 10:13:05
问题 I'm experimenting with sympy's permutations without replacement from sympy.functions.combinatorial.numbers import nP from sympy.utilities.iterables import permutations nP('abc', 2) # >>> 6 list(permutations('abc', 2)) # >>> [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')] Next, I wan't to try permutations with replacement . It seems that there isn't a permuations_with_replacement() method similar to the combinations_with_replacement() method, but there is a variations(

Modulo 2 arithmetics in SymPy

心已入冬 提交于 2020-01-15 03:47:10
问题 I'm working on transformation polynomial systems over GF(2^N) to GF(2). In a process of transformation I need to do lots of modulo 2 computations like summing and multiplying matrices. So is there way to sum and multiply matrices using modulo 2 arithmetics in Sympy? 回答1: I've had luck with sympy's trunc() function: from sympy import * var('x:z') y=25*x+12 z=trunc(y,2) # z=x 来源: https://stackoverflow.com/questions/21660852/modulo-2-arithmetics-in-sympy

SymPy outputs a numerical result when a symbolic expression is expected

佐手、 提交于 2020-01-15 01:58:10
问题 I'ld like my SymPy results to be displayed as precise results and not as decimal results. I looked through the SymPy documentation, but couldn't find anything helpful. To illustrate the problem, here is some example code: from sympy import * u = symbols("u") integrate((1+u)**(1/2), (u, 0, 1)) Output: 1.21895141649746 Expected result: (4/3)*sqrt(2)-(2/3) 回答1: The problem is that SymPy will simplify expressions containing floats and so removes those symbolic expressions. Although the expression

Integrating in Python using Sympy

淺唱寂寞╮ 提交于 2020-01-14 13:53:26
问题 I am currently using Sympy to help me perform mathematical calculations. Right now, I am trying to perform a numerical integration, but keep getting an error any time I run the script. Here is the script: from sympy import * cst = { 'qe':1.60217646*10**-19, 'm0':N(1.25663706*10**-6) } d = 3.6*10**-2 l = 20.3*10**-2 n = 217.0 I = 10.2 # Circum of loops circ = l/n; # Radius r = N( circ/(2*pi) ) # Flux through a ring a distance R from the ceter def flux(rad, I, loopRad): distFromWire = loopRad -

How to extract numerator and denominator from polynomial without evaluating?

最后都变了- 提交于 2020-01-14 08:54:08
问题 I have the following expression A=Symbol('A') x=Symbol('x') B=Symbol('B') C=Symbol('C') D=Symbol('D') expression=((A**x-B-C)/(D-1))*(D-1) n,d=fraction(expression) I am getting following result: n=A**x-B-C d=1 My expected result is n=(A**x-B-C)*(D-1) d=(D-1) Is there way in sympy or need to write customize function to handle that 回答1: Use UnevaluatedExpr() to prevent the expression from being evaluated. from sympy import symbols, fraction, UnevaluatedExpr A,x,B,C,D = symbols('A x B C D')