sympy

Sympy - fraction manipulation

怎甘沉沦 提交于 2019-12-23 17:14:11
问题 I basically want Sympy to generate latex code \frac{x-1}{3} = y but whenever I ask it generate the Tex component of things Sympy always returns \frac{x}{3} - \frac{1}{3} . How do I avoid splitting up the equations, and assign an equals operator to another variable. I have not attempted to add the "y =" part to the code yet as I wanted to clarify the fraction situation first, but since I have had to come cap in hand to stack exchange I thought I would ask both questions. I have been through

How to modify sympy's printing order?

心已入冬 提交于 2019-12-23 16:43:50
问题 I am using sympy to write algebraic expressions and perform basic calculations with them. Sympy does not keep track of the order of the variables which can be a problem when it comes to printing expressions (the issue has already been raised here and here, so this is not a duplicate). e.g. >>> from sympy import * >>> var("p,a") >>> l=p-1-a; >>> print(l); -a+p-1 However, sympy seems to print the variables in the alphabetical order. Is there a way to change the alphebetical order Python refers

Print substituted expression without numerical evaluation

不打扰是莪最后的温柔 提交于 2019-12-23 16:08:23
问题 Is it possible to print a substitute SymPy expression without calculation? I want to print both substituted express and result. e.g. x = Symbol('x') expr = x**2 pprint(expr) # this prints expression result = expr.subs({x:2}) print(result) # this print result 4 How can I print the "middle result", the expression 2**2 ? 回答1: You can pass in UnevaluatedExpr for this purpose, as shown below: result = expr.subs(x, UnevaluatedExpr(2)) print(result) # prints 2**2 result = result.doit() print(result)

Getting element-wise equations of matrix multiplication in sympy

梦想与她 提交于 2019-12-23 15:04:15
问题 I've got 2 matrices, first of which is sparse with integer coefficients. import sympy A = sympy.eye(2) A.row_op(1, lambda v, j: v + 2*A[0, j]) The 2nd is symbolic, and I perform an operation between them: M = MatrixSymbol('M', 2, 1) X = A * M + A.col(1) Now, what I'd like is to get the element-wise equations: X_{0,0} = A_{0,0} X_{0,1} = 2*A_{0,0} + A_{0,1} One way to do this is specifying a matrix in sympy with each element being an individual symbol: rows = [] for i in range(shape[0]): col =

Solve highly non-linear equation for x in Python

*爱你&永不变心* 提交于 2019-12-23 13:27:23
问题 I am trying to solve the following equation for dB (for simplicity, I stated dB as x in the question title): All of the other terms in the equation are known. I tried using SymPy to symbolically solve for dB but I kept getting time out errors. I also tried using fminbound from scipy.optimize but the answer for dB is wrong (see below for Python code using the fminbound approach). Does anyone know of a way to solve the equation for dB using Python? import numpy as np from scipy.optimize import

solving inequality in sympy

怎甘沉沦 提交于 2019-12-23 13:25:17
问题 i want to solve the following inequality in sympy (10000 / x) - 1 < 0 so I issue the command solve_poly_inequality( Poly((10000 / x) - 1 ), '<') as result I get [Interval.open(-oo, 1/10000)] However, my manual computations give either x < 0 or x > 10000. What am I missing? Due to the -1, I cannot represent it as a rational function because of the -1. Thanks in advance! 回答1: You are using a low-level solving routine. I would recommend using the higher-level routines solve or solveset , e.g. >>

Sympy: Get functions from expression

有些话、适合烂在心里 提交于 2019-12-23 12:55:48
问题 To get all variables from a sympy expression, one can call .free_symbols on the expression. I would like to retrieve all functions used in an expression. For example, from y in from sympy import * f = Function('f') g = Function('g') x = Symbol('x') y = f(x) + 2*g(x) I'd like to get f and g . Any hints? 回答1: For all functions, use atoms(Function) . In [40]: (f(x) + sin(x)).atoms(Function) Out[40]: set([f(x), sin(x)]) For only undefined functions, use atoms(AppliedUndef) . In [41]: from sympy

Sympy: Get functions from expression

南笙酒味 提交于 2019-12-23 12:55:06
问题 To get all variables from a sympy expression, one can call .free_symbols on the expression. I would like to retrieve all functions used in an expression. For example, from y in from sympy import * f = Function('f') g = Function('g') x = Symbol('x') y = f(x) + 2*g(x) I'd like to get f and g . Any hints? 回答1: For all functions, use atoms(Function) . In [40]: (f(x) + sin(x)).atoms(Function) Out[40]: set([f(x), sin(x)]) For only undefined functions, use atoms(AppliedUndef) . In [41]: from sympy

Solving Differential Equation Sympy

天大地大妈咪最大 提交于 2019-12-23 12:02:36
问题 I haven't been able to find particular solutions to this differential equation. from sympy import * m = float(raw_input('Mass:\n> ')) g = 9.8 k = float(raw_input('Drag Coefficient:\n> ')) v = Function('v') f1 = g * m t = Symbol('t') v = Function('v') equation = dsolve(f1 - k * v(t) - m * Derivative(v(t)), 0) print equation for m = 1000 and k = .2 it returns Eq(f(t), C1*exp(-0.0002*t) + 49000.0) which is correct but I want the equation solved for when v(0) = 0 which should return Eq(f(t),

SymPy: Safely parsing strings

旧巷老猫 提交于 2019-12-23 10:57:24
问题 SymPy comes equipped with the nice sympify() function which can parse arbitrary strings into SymPy expressions. But it has two major drawbacks: It is not safe, as it relies on the notorious eval() It automatically simplifies the read expression. e.g. sympify('binomial(5,3)') will return the expression 10. So my questions are: First, is there a way to "just parse" the string, without any additional computations? I want to achieve something like this effect: latex(parse('binomial(5,3)'))