sympy

How to make sin(pi) and cos(pi/2) zero?

廉价感情. 提交于 2019-12-13 03:52:56
问题 I understand that in Python sin(pi) and cos(pi/2) won't produce 0 , but I'm making calculations with matrices and I need to use those values. I'm using SymPy and at first the values of sin(pi) and cos(pi/2) are a little annoying. After some multiplications they start to get in the way. Is there a way to make those values be equal to 0 in the entire module? How can I change it in the middle of expressions? I'll use this matrix as an example: A = Matrix([ [(-sin(theta1)*sin(theta2)*cos(alpha2)

How to Sum with conditions on Sympy?

青春壹個敷衍的年華 提交于 2019-12-13 03:36:15
问题 Sorry if this sounds dumb. I'm a beginner with Sympy and really tried to get over this. Failed. So here it goes: If I do, for example: >>> i, j = Dummy('i'), Dummy('j') >>> N = symbols('N') >>> expr = Sum(KroneckerDelta(i,j), (i,0,N)) >>> expr.doit() I get Piecewise((1, And(0 <= _j ,_j <= N)), (0, otherwise)) It means there's two different results, given conditions. My question is: if I know a priori that indeed the first condition is satisfied (0<=j<=N), how do I let Sympy know of it, so it

Sympify “<” does not return boolean in custom function?

随声附和 提交于 2019-12-13 03:05:32
问题 Summary My self-written if-then-else conditional for sympy does not work for some Booleans. Code (Note: Piecewise is not an option for me, proposed here https://stackoverflow.com/a/38858444/5626139) from sympy import Function class ifte(Function): nargs = 3 @classmethod def eval(cls, a, b, c): if a > 0: return b else: return c Which works partially, for example with these three booleans: >>> print(ifte('1+2 and True and 1 != 2', 'b', 'c')) b Problem Why does the line with 0<1 evaluate

Sympy returns a ConditionSet object when solving an equation, while matlab returns a single float when the same equation is solved

邮差的信 提交于 2019-12-13 02:06:34
问题 I have 4 input variables (floats): Xmax Xmin percentage mode and I want to solve the following (rather long) equation for s: > (1/2+1/2*erf((log(Xmax)-(log(mode)+s**2))/(sqrt(2)*s))-(1/2+1/2*erf((log(Xmin)-(log(mode)+s**2))/(sqrt(2)*s))) - percentage == 0 I want to use sympy to solve the equation, but it returns a ConditionSet Object (meaning that it cannot solve the equation) My code is as follows: from sympy import * def CalcScaleParam(mode, CIfact, percentage): s = Symbol('s', Real=True)

How to know whether a function is continuous with sympy?

ε祈祈猫儿з 提交于 2019-12-12 23:49:34
问题 I need to define a function that checks if the input function is continuous at a point with sympy. I searched the sympy documents with the keyword "continuity" and there is no existing function for that. I think maybe I should consider doing it with limits, but I'm not sure how. def check_continuity(f, var, a): try: f = sympify(f) except SympifyError: return("Invaild input") else: x1 = Symbol(var, positive = True) x2 = Symbol(var, negative = True) //I don't know what to do after this 回答1: Yes

Sympy-numpy integration exists - where is it documented?

依然范特西╮ 提交于 2019-12-12 19:25:44
问题 I just accidentally discovered that I can mix sympy expressions up with numpy arrays: >>> import numpy as np >>> import sympy as sym >>> x, y, z = sym.symbols('x y z') >>> np.ones(5)*x array([1.0*x, 1.0*x, 1.0*x, 1.0*x, 1.0*x], dtype=object) # I was expecting this to throw an error! # sum works and collects terms etc. as I would expect >>> np.sum(np.array([x+0.1,y,z+y])) x + 2*y + z + 0.1 # dot works too >>> np.dot(np.array([x,y,z]),np.array([z,y,x])) 2*x*z + y**2 >>> np.dot(np.array([x,y,z])

Sympify changes order of everything

谁说胖子不能爱 提交于 2019-12-12 18:14:28
问题 I have the following symbols in my Sympy script e0 = Symbol('e0',commutative=False) e1 = Symbol('e1',commutative=False) e2 = Symbol('e2',commutative=False) e3 = Symbol('e3',commutative=False) u = Symbol('u',commutative=False) d0 = Symbol('d0',commutative=False) d1 = Symbol('d1',commutative=False) d2 = Symbol('d2',commutative=False) d3 = Symbol('d3',commutative=False) A_0 = (Symbol('a_00',commutative=False),Symbol('a_01',commutative=False),Symbol('a_02',commutative=False),Symbol('a_03'

Convert from mpf to Sympy Float without losing precision

元气小坏坏 提交于 2019-12-12 15:26:39
问题 Is there any way to do this? For example in the code below I lose precision: >>> from sympy import * >>> from sympy.mpmath import * >>> mp.dps = 50 >>> a = mpf('1.0')/mpf('3.0') >>> a mpf('0.33333333333333333333333333333333333333333333333333311') >>> b = Float(a,50) >>> b 0.33333333333333331482961625624739099293947219848633 回答1: Converting to a string first seems to do the trick: >>> from sympy import * >>> from sympy.mpmath import * >>> mp.dps = 50 >>> a = mpf('1.0')/mpf('3.0') >>> a mpf('0

sympy factor simple relationship

谁说我不能喝 提交于 2019-12-12 15:07:33
问题 I have a simple factorization problem in sympy that I cannot sort out. I've had great success with sympy working with quite complex integrals, but I'm flummoxed by something simple. How do I get phi**2 - 2*phi*phi_0 + phi_0**2 - 8 to factor to (phi - phi_0)**2 - 8 ? I've already tried the factor function factor(phi**2 - 2*phi*phi_0 + phi_0**2 - 8,phi-phi_0) which yields the same old solution. 回答1: As I noted in the comment, such "partial factorizations" are not unique (for instance, x**2 + 5

How to restrict sympy FiniteSet containing symbol

拈花ヽ惹草 提交于 2019-12-12 12:31:13
问题 I am fairly new to sympy. I tried to solve a system of linear equations with linsolve(). This yields a solution which can be reproduced with the two following lines. d = symbols("d") solution = sets.FiniteSet((d + 1, -d + 4, -d + 5, d)) My solution obeys the restriction, that all four values must be positive integers. This happens for d = 0, 1, 2, 3, 4. I was able to evaluate solution at a fixed d (e. g. d = 0) with solution.subs({d : 0}) What I would like to have restrict the set of