sympy

SymPy equivalent to HoldForm in mathematica

非 Y 不嫁゛ 提交于 2019-12-10 18:54:43
问题 In Mathematica, it is possible to prevent the system from simplifying expressions when they are entered. The syntax is as follows: HoldForm[x/x] Is it possible to do something similar with SymPy? 回答1: The following approaches achieve a similar effect. There might be others available I am not aware of. import sympy as sp x = sp.symbols('x') expr1 = x/x expr2 = sp.S('x/x', evaluate=False) expr3 = sp.Mul(x, 1/x, evaluate=False) print(expr1) print(expr2) print(expr3) 1 x/x x/x 回答2: You can also

Sympy custom function

元气小坏坏 提交于 2019-12-10 18:44:32
问题 I noticed, that there is no functions like sech(x) and csch(x). Is there any way to quickly define them as 1/cosh(x) and 1/sinh(x) respectively? Also, how can I make sympy to treat arccos as acos? I'm using parser, so 'cos(pi/2)' is correctly parsed. But I want 'arccos(pi/2)' to be parsed in a similar manner. 回答1: You can define functions that accomplish your desired behaviour. import sympy def sech(x): return sympy.cosh(x)**(-1) # sympy.sech = sech def csch(x): return sympy.sinh(x)**(-1) #

Are sympy matrices really that slow?

拥有回忆 提交于 2019-12-10 17:25:07
问题 I just had a look on sympy and it looks like it's extremely slow. Am I doing something wrong? from sympy.matrices import zeros from time import time import numpy as np t = time() M = zeros(500,500) print("Time", time()-t) t = time() M = np.zeros((500,500)) print("Time", time()-t) Sympy takes 1.2s and numpy takes 0.0006s. There is nothing really going on here. What takes so long? Edit: What I was really looking for was a library where I can use fast matrix stuff (Gauss eliminiation,

SymPy express variable in terms of another

一笑奈何 提交于 2019-12-10 17:20:28
问题 I am using SymPy lib for Python. I have two sympy symbols and expression that binds them: x = Symbol('x') y = Symbol('y') expr = 2 * x - 7 * y How can i express 'y' in terms of 'x', i.e get the equality: y = (2/7) * x Thanks. 回答1: This is how you can express this equation in terms of x : In [1]: from sympy import * In [2]: x, y = symbols('x, y') In [3]: expr = 2*x - 7*y In [4]: solve(expr, y) Out[4]: [2*x/7] This works because if the solve() function is presented with something that is not a

How to solve a degenerate system of equations in sympy

被刻印的时光 ゝ 提交于 2019-12-10 16:24:34
问题 I have lots of systems of equations, some are underspecified, and I would like to find one non-zero solution if it exists or report that there are none. However, sympy seems to hang trying to find all solutions. Here is an extreme example. from sympy import * A = Matrix([ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

Sum of partial derivatives of a product over a symbolic number of variables

假如想象 提交于 2019-12-10 16:10:19
问题 I would like SymPy to evaluate an expression like the following: How would I define the symbols and the expression so that SymPy could handle it nicely? I would like to keep N as just a symbol, i.e. not make an actual finite list of x 's. I have tried various combinations of IndexedBase and Sum / Product , but didn't get it working right. 回答1: Ideally it would be this: x = IndexedBase("x") i, j, N = symbols("i j N") expr = Sum(Product(exp(-x[j]**2), (j, 1, N)).diff(x[i]), (i, 1, N)) So far

Sympy: working with equalities manually

♀尐吖头ヾ 提交于 2019-12-10 16:06:08
问题 I'm currently doing a maths course where my aim is to understand the concepts and process rather than crunch through problem sets as fast as possible. When solving equations, I'd like to be able to poke at them myself rather than have them solved for me. Let's say we have the very simple equation z + 1 = 4 - if I were to solve this myself, I would obviously subtract 1 from both sides, but I can't figure out if sympy provides a simple way to do this. At the moment the best solution I can come

Use Sympy with Pypy

雨燕双飞 提交于 2019-12-10 15:44:30
问题 I have installed Python 2.7 and 3.5 on a Mac running with El Capitan. Moreover, I use the package Sympy (installed with pip) with python. I wanted to run my code with Pypy (installed with homebrew) but it seems that Pypy doesn't find Sympy and says: "No module named sympy" I am not an expert at all and don't know what to do now. Sympy works well with python 2 and 3 but not with Pypy. I appreciate every answer, thank you in advance. 回答1: PyPy packages are separate from Python packages. You can

Polynomial function cannot be solved by Python sympy

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:29:03
问题 I have problems by solving a polynomial function with sympy. The following example shows a case which gives an error message that I cannot manage. If the polynomial gets simpler the solver works properly. Please copy and paste the code to check the error on your system as well. import sympy from sympy import I omega = sympy.symbols('omega') def function(omega): return - 0.34*omega**4 \ + 7.44*omega**3 \ + 4.51*I*omega**3 \ + 87705.64*omega**2 \ - 53.08*I*omega**2 \ - 144140.03*omega \ - 22959

Display two Sympy plots as two Matplotlib subplots

拜拜、爱过 提交于 2019-12-10 14:51:22
问题 This code from sympy import * x=Symbol('x') p1 = plot(x**2,(x,-2,2)) p2 = plot(x**3,(x,-2,2)) results in two separate plots. Instead of two separate plots, I want to display them with matplotlib as subplots: import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122) plt.show() How can I add p1 and p2 , so that they are displayed as subplots inside the matplotlib figure? 回答1: The problem is that sympy Plot creates its own figure and axes. It is not