sympy

Sympy TypeError: cannot determine truth value of Relational when using sympy

可紊 提交于 2020-01-05 04:13:14
问题 I'm learning SymPy now. Here is the problem I got: x = symbols('x',real=True) h = symbols('h',real=True) f = symbols('f',cls=Function) sym_dexpr = f_diff.subs(f(x), x*exp(-x**2)).doit() f_diff = f(x).diff(x,1) expr_diff = as_finite_diff(f_diff, [x, x-h,x-2*h,x-3*h]) w=Wild('w') c=Wild('c') patterns = [arg.match(c*f(w)) for arg in expr_diff.args] coefficients = [t[c] for t in sorted(patterns, key=lambda t:t[w])] print(coefficients) But I got following error : TypeError Traceback (most recent

Vectorizing nested loop with conditionals and functions

早过忘川 提交于 2020-01-05 03:54:05
问题 I have the following function: def F(x): #F receives a numpy vector (x) with size (xsize*ysize) ff = np.zeros(xsize*ysize) count=0 for i in range(xsize): for j in range(ysize): a=function(i,j,xsize,ysize) if (a>xsize): ff[count] = x[count]*a else ff[count] = x[count]*i*j count = count +1 return ff There is one nuance here which is the fact that (example for xsize =4, ysize=3) c=count x[c=0] corresponds to x00 i=0,j=0 x[c=1] x01 i=0, j=1 x[c=2] x02 i=0, j=2 (i=0, j = ysize-1) x[c=3] x10 i=1, j

Sympy: how to simplify logarithm of product into sum of logarithms?

百般思念 提交于 2020-01-04 13:39:52
问题 To motivate the question, sympy.concrete has some efficient tools to manipulate symbolic sums. In order to apply these tools to symbolic products, one has to take a logarithm. However, straightforward taking the logarithm doesn't automatically give the transformation: import sympy as sp sp.init_printing() # display math as latex z = sp.Symbol('z') j,k = sp.symbols('j,k') Prod = sp.Product( (z + sp.sqrt(1-4*j*z**2))**(-1), (j,1,k) ) sp.log(Prod) gives in all possible variations: sp.log(Prod)

sympy.geometry Point class is working slow

无人久伴 提交于 2020-01-04 07:33:39
问题 I have a code which reads unstructured mesh. I wrote wrappers around geometric entities of sympy.geometry such as: class Point: def __init__(self, x, y, parent_mesh): self.shape = sympy.geometry.Point(x,y) self.parent_mesh = parent_mesh self.parent_cell = list() Everything works fine but initialization of sympy.geometry.Point takes a lot of time for each Point . Actually, the code did not finish execution for thousands of points. Similar code written in C++ finished in a few seconds. Without

AttributeError: module 'sympy' has no attribute 'polys'

本小妞迷上赌 提交于 2020-01-04 05:59:10
问题 I'm encountering some problems using sympy, here is my code: from sympy import integrate as inter import sympy as sp from sympy import symbols def f(x): f1 = (sp.exp(-x ** 2)) / (1 + x ** 2) f2 = (2 * (sp.cos(x)) ** 2) / (1 + (x - 4) ** 2) f = f1 + f2 return(f) def integrate_f_from0(b): x = symbols('x') a = inter(f(x), (x, 0, b)) return(a) The error I get upon running the integrate_f_from0 function is: AttributeError: module 'sympy' has no attribute 'polys' What is causing this error? 回答1: In

sympy CSE: avoid pow/powf

不打扰是莪最后的温柔 提交于 2020-01-04 05:37:54
问题 When Sympy generates C code, is there a way to enforce CSE optimizations for pow (or powf) occurrences in an expression? For example, this code snippet c, s = symbols('c s') myexpr = c**6/1800 - c**5/100 - 0.00833333333333333*c**4*s**2 + 19*c**4/200 + 0.1*c**3*s**2 - 9*c**3/20 + c**2*s**4/120 - 0.57*c**2*s**2 + 43*c**2/40 - c*s**4/20 + 1.35*c*s**2 + 23*c/50 - 0.000555555555555556*s**6 + 19*s**4/200 - 1.075*s**2 - 2107/1800 import sympy from sympy.codegen.ast import real, float64 sub_exprs

sympy CSE: avoid pow/powf

五迷三道 提交于 2020-01-04 05:37:20
问题 When Sympy generates C code, is there a way to enforce CSE optimizations for pow (or powf) occurrences in an expression? For example, this code snippet c, s = symbols('c s') myexpr = c**6/1800 - c**5/100 - 0.00833333333333333*c**4*s**2 + 19*c**4/200 + 0.1*c**3*s**2 - 9*c**3/20 + c**2*s**4/120 - 0.57*c**2*s**2 + 43*c**2/40 - c*s**4/20 + 1.35*c*s**2 + 23*c/50 - 0.000555555555555556*s**6 + 19*s**4/200 - 1.075*s**2 - 2107/1800 import sympy from sympy.codegen.ast import real, float64 sub_exprs

Evaluating the result of sympy lambdify on a numpy mesgrid

柔情痞子 提交于 2020-01-04 04:02:15
问题 I would like to evaluate the output of sympy.lambdify on a numpy mgrid. I tried the following: import sympy as sp import numpy as np theta, v = sp.symbols("theta v") coeff = (-sp.sin(theta/2)*sp.sin(2*v) + sp.sin(v)*sp.cos(theta/2) + 3) kb = sp.Matrix([[coeff*sp.cos(theta), coeff*sp.sin(theta), sp.sin(theta/2)*sp.sin(v) + sp.sin(2*v)*sp.cos(theta/2)]]) f = sp.lambdify((theta, v), kb, modules='numpy') f(*np.mgrid[0:2*np.pi:101j, 0:2*np.pi:101j]) but I get an error saying the matrix must be 2

Can we replace the 'Derivative' terms in sympy coming from the differentiation of sympy.Function variables?

五迷三道 提交于 2020-01-04 02:45:21
问题 When the following code is run Derivative(Ksi(uix, uiy), uix)) and Derivative(Ksi(uix, uiy), uiy)) terms appear: In [4]: dgN Out[4]: Matrix([ [-(x1x - x2x)*(-x1y + x2y)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)*Derivative(Ksi(uix, uiy), uix) + (-x1y + x2y)*(-(-x1x + x2x)*Derivative(Ksi(uix, uiy), uix) + 1)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)], [-(-x1x + x2x)*(-x1y + x2y)*((x1x - x2x)**2 + (-x1y + x2y)**2)**(-0.5)*Derivative(Ksi(uix, uiy), uiy) + (x1x - x2x)*(-(-x1y + x2y)*Derivative

SymPy, simplification / substitution using known patterns or sub-expressions

与世无争的帅哥 提交于 2020-01-03 08:43:07
问题 I have the following expression: from sympy import pi, sin, cos, var, simplify var('j,u,v,w,vt,wt,a2,t,phi') u0 = v*a2*sin(pi*j/2 + pi*j*t*phi**(-1)/2) + pi*vt*a2*cos(pi*j/2 + pi*j*t*phi**(-1)/2)*j*phi**(-1)/2 + pi*w*a2*cos(pi*j/2 + pi*j*t*phi**(-1)/2)*j*phi**(-1) Which can be simplified: print simplify(u0) #a2*(pi*j*vt*cos(pi*j*(phi + t)/(2*phi)) + 2*pi*j*w*cos(pi*j*(phi + t)/(2*phi)) + 2*phi*v*sin(pi*j*(phi + t)/(2*phi)))/(2*phi) Given the sub-expressions: bj = pi*j*(phi + t)/(2*phi) cj = j