sympy

Solve a system of linear equations and linear inequalities

血红的双手。 提交于 2020-01-13 07:21:27
问题 I have to get the min and max y for a linear expression, restricted by some linear inequalities in python. You can see the equation and inequalities here that I have entered into Desmos: 3x+12y = 1000 x > 30 x < 160 y < 60 y > 10 x + y > 180 I can solve them by hand by drawing and crossing out the inequalities. But I cannot do that in Python. What I have tried so far in Python is to get y=83.33 when x=0; x=333.33 when y=0; After getting the min and max x,y I then apply the inequalities 1 by 1

Solve a system of linear equations and linear inequalities

雨燕双飞 提交于 2020-01-13 07:21:10
问题 I have to get the min and max y for a linear expression, restricted by some linear inequalities in python. You can see the equation and inequalities here that I have entered into Desmos: 3x+12y = 1000 x > 30 x < 160 y < 60 y > 10 x + y > 180 I can solve them by hand by drawing and crossing out the inequalities. But I cannot do that in Python. What I have tried so far in Python is to get y=83.33 when x=0; x=333.33 when y=0; After getting the min and max x,y I then apply the inequalities 1 by 1

Why sympy lambdify function cannot identify numpy sum function and multiply function

旧巷老猫 提交于 2020-01-11 14:30:36
问题 I want to use sympy and numpy to learning machine learning. Because symoy provides very convenient partial derivative calculation. But in the process of use, I found that sympy lambdify function and can't identify the numpy sum function and multiply function. Take the following example y_ = np.sum(np.dot(w,x)+b) print(y_) y_f = lambdify((w,x,b),y_,"numpy") w_l = np.mat([1,1,1,1,1]) x_l= np.mat([1,1,1,1,1]).T b_l = np.mat([0,0,0,0,0]).T y_l = np.mat([6,6,6,6,6]).T print(y_f(w_l,x_l,b_l)) b + w

Test if matrix is invertible over finite field

假装没事ソ 提交于 2020-01-11 05:25:07
问题 I would like to test if a particular type of random matrix is invertible over a finite field, in particular F_2. I can test if a matrix is invertible over the reals using the following simple code. import random from scipy.linalg import toeplitz import numpy as np n=10 column = [random.choice([0,1]) for x in xrange(n)] row = [column[0]]+[random.choice([0,1]) for x in xrange(n-1)] matrix = toeplitz(column, row) if (np.linalg.matrix_rank(matrix) < n): print "Not invertible!" Is there some way

How to get the Gradient and Hessian | Sympy

孤人 提交于 2020-01-11 01:50:18
问题 Here is the situation: I have a symbolic function lamb which is function of the elements of the variable z and the functions elements of the variable h. Here is an image of the lamb symbolic function . Now I would like the compute the Gradient and Hessian of this function with respect to the variables eta and xi. Of course I googled for it but I could not find a straight way for doing this. What I found is here, but as I said it does not seen to be the best approach for this situation. Any

Why won't sympy take my symbols?

China☆狼群 提交于 2020-01-06 15:20:08
问题 I have symbols defined as follows: import sympy class construct(object): def __init__(self, value): self.value = value def __add__(self, symbol): return construct(self.value+symbol.value) def __mul__(self,symbol): return construct(self.value*symbol.value) a = construct(2) b = construct(10) (the above code is runnable). Now, when I try to put them in a sympy matrix, it raises an error and I can't figure out why: import sympy A= sympy.Matrix([[a],[b]]) ....SympifyError: Sympify of expression

In sympy, simplify an expression using a canonical commutation relation

╄→尐↘猪︶ㄣ 提交于 2020-01-06 04:31:27
问题 I have a ladder operator â, which satisfies this commutator relation with its own adjoint: [â, â⁺] = 1 In sympy I have written this code: import sympy from sympy import * from sympy.physics.quantum import * a = Operator('a') ad = Dagger(a) ccr = Eq( Commutator(a, ad), 1 ) Now I need to expand and simplify an expression like this: (â⁺ + â)⁴ If I just use ((ad + a)**4).expand() , sympy doesn't use the commutator relation. How do I simplify the expression while using the canonical commutator

Matrices whose entries are polynomials

ⅰ亾dé卋堺 提交于 2020-01-05 12:23:49
问题 When using the sympy library, what is the most efficient way of operating with matrices whose entries are polynomials? I tried both storing symbols and storing Poly instances as entries of a Matrix object. In each case, the performance that I get for operations like matrix multiplication seems to be an order of magnitude slower than what I get with sage . 来源: https://stackoverflow.com/questions/24931740/matrices-whose-entries-are-polynomials

conds='none' in Sympy does not work

≯℡__Kan透↙ 提交于 2020-01-05 04:32:06
问题 I have the following integral x,x1,x2,t=symbols('x x1 x2 t') f=t*x1*x2*(x-t)**(-Rational('0.5')) integrate(f,t).simplify() The result of this is a piecewise function Piecewise((2*sqrt(x)*x1*x2*(-I*t**2*sqrt((t - x)/x) - I*t*x*sqrt((t - x)/x) + 2*t*x + 2*I*x**2*sqrt((t - x)/x) - 2*x**2)/(3*(t - x)), Abs(t/x) > 1), (2*sqrt(x)*x1*x2*(-t**2*sqrt((-t + x)/x) - t*x*sqrt((-t + x)/x) + 2*t*x + 2*x**2*sqrt((-t + x)/x) - 2*x**2)/(3*(t - x)), True)) I want to ignore the first case, so the solution would

How to plot 2 variables on a plane

妖精的绣舞 提交于 2020-01-05 04:14:07
问题 Let's say I have an equation: x**2 + y**2 - 4 = 0 How can I see the circle using sympy, matplotplib or another python solution? I know in sympy I can from sympy import Plot from sympy import Symbol x = Symbol('x') y = Symbol('y') Plot(x**2 + y**2 - 4) But then I get z = x**2 + y**2 - 4 , a 3D graph instead of the planar intersection. I understand there may be a need to solve the equation. 回答1: Yes KillianDS, I now understand this is a duplicate of Is it possible to plot implicit equations