sympy

Any way to do integer division in sympy?

放肆的年华 提交于 2019-12-08 16:37:23
问题 I have a very long expression that I think can be simplified, and I thought sympy would be the perfect way to do it. Unfortunately the formula relies on a couple of integer divides, and I can't find any way to represent those in sympy . >>> x=Symbol('x') >>> (x+1)/2 x/2 + 1/2 Clearly not what I want, 1/2 isn't an integer. >>> (x+1)//2 TypeError: unsupported operand type(s) for //: 'Add' and 'int' Obviously sympy doesn't handle // . >>> Integer((x+1)/2) # A long list of error messages, ending

lambdify a sympy expression that contains a Derivative of UndefinedFunction

僤鯓⒐⒋嵵緔 提交于 2019-12-08 03:24:00
问题 I have several expressions of an undefined function some of which contain the corresponding (undefined) derivatives of that function. Both the function and its derivatives exist only as numerical data. I want to make functions out of my expressions and then call that function with the corresponding numerical data to numerically compute the expression. Unfortunately I have run into a problem with lambdify. Consider the following simplified example: import sympy import numpy # define a

SymPy simplify relational (inequality) expression

给你一囗甜甜゛ 提交于 2019-12-08 03:04:22
问题 Why doesn't the following simplification work, or how could it be fixed: >>> x = Symbol('x', real=True) >>> y = Symbol('y', real=True) >>> simplify(x - 1 < y - 1) x - 1 < y - 1 But this works: >>> simplify(x - 1 - (y - 1) < 0) x - y < 0 Can somehow the first expression be simplified to x < y ? Thanks 回答1: You could solve for x: import sympy as sy x, y = sy.symbols('x,y', real=True) print(sy.solve(x - 1 < y - 1, x)) yields x < y x, y, z = sy.symbols('x,y,z', real=True) print(sy.solve(x - 1 < y

Python- Sympy Issue with expression equality check when evaluate=False

狂风中的少年 提交于 2019-12-07 23:10:20
问题 In my project I have to use evaluate=false at the time when i am creating any Add or Mul objects. In this case I am facing a problem when I apply equality checks on these objects. The issue is because of the ordering of the arguments. Please consider the example below: k2=Mul(*[x,y,2],evaluate=False) k1=Mul(*[x,2,y],evaluate=False) print k1==k2 The result is false as k2.args are (x,y,2) and k1.args are (x,2,y) . So, while the comparison checks for tuple equality it returns false . Is there

Derivative of a conjugate in sympy

我怕爱的太早我们不能终老 提交于 2019-12-07 18:58:13
问题 When I try to differentiate a symbol with SymPy I get the following In : x=Symbol('x') In : diff(x,x) Out: 1 When I differentiate the symbol respect to its conjugate the result is In [55]: diff(x,x.conjugate()) Out[55]: 0 However, when I try to differentiate the conjugate of the symbol SymPy doesn't do it In : diff(x.conjugate(),x) Out: Derivative(conjugate(x), x) This is still correct, but the result should be zero. How can I make SimPy perform the derivative of a conjugate? 回答1: I'm not

C++: Extracting symbols/variables of an analytical mathematical expression

北慕城南 提交于 2019-12-07 18:06:51
问题 I have expressions that can be provided by the user, such as: a*sin(w*t) a+b/c x^2+y^2/2 And I would like to just get the list of variables there. I don't need to do any substitutions. So, for the first formula it's gonna be {a,w,t} . For the second one {a,b,c} , and for the last one {x,y} . The expression is primarily written to be parsed with Sympy, but I need to be able to get the list of variables in C++ for some checks. I would like to: Avoid having to link the whole Python interpreter

How to use sympy.physics.quantum Commutator?

安稳与你 提交于 2019-12-07 17:31:13
问题 I want to work out some commutator manipulations and found this tool in sympy. It appears to work as expected (but the documentation is virtually non-existent or at least I found little, but see the comment by Dalton Bentley below), but I ran into the following problem. from sympy.physics.quantum import Commutator as Comm from sympy.physics.quantum import Operator A = Operator('A') B = Operator('B') C = Comm(Comm(Comm(A,B),A),B) D = Comm(Comm(Comm(A,B),B),A) E = (C-D).expand(commutator=true)

call lambdify in a loop, avoid explicitly call

旧城冷巷雨未停 提交于 2019-12-07 14:41:24
问题 I have this code: var = ['a','b','c'] arr = np.array([ [1,2,3,4,5,6,7,8,9], [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] ]) y = np.hsplit(arr,len(var)) newdict = {} for idx,el in enumerate(y): newdict[str(var[idx])] = el print(newdict) I am splitting the array in order to have 3 new arrays, one for each variable in the var list. Then , I am creating a new dictionary in order to assign to every variable the corresponding array.So, my result now is: {'a': array([[ 1. , 2. , 3. ], [ 0.1, 0.2, 0.3]]),

Solving a system of (more than two) linear inequalities

ε祈祈猫儿з 提交于 2019-12-07 13:59:31
问题 If I use diophantine(2*x+3*y-5*z-77) I receive this result. {(t_0, -9*t_0 - 5*t_1 + 154, -5*t_0 - 3*t_1 + 77)} Fine so far. However, on occasion one might like to constrain x, y and z to be (say) non-negative. When I use an approach like this< reduce_inequalities([0<=t_0, 0<=-9*t_0 - 5*t_1 + 154, 0<=-5*t_0 - 3*t_1 + 77],[t_0, t_1]) I get: NotImplementedError: inequality has more than one symbol of interest Does sympy, sage, prolog, haskell or some other freely available product have means for

input a symbolic function in a python code

半城伤御伤魂 提交于 2019-12-07 12:06:53
问题 I was just wondering if there is a method to input a symbolic function in a python code? like in my code I have: from sympy import * import numpy as np import math myfunction = input("enter your function \n") l = Symbol('l') print myfunction(l**2).diff(l) If I put cos, sin or exp, as an input then I have an output of: -2*l*sin(l**2) What If I want to make the input function more general, say a polynomial or a complex function, or maybe a function of combined cos, sin and exp ??? 回答1: If you