sympy

How can I solve system of linear equations in SymPy?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 17:12:44
Sorry, I am pretty new to sympy and python in general. I want to solve the following underdetermined linear system of equations: x + y + z = 1 x + y + 2z = 3 SymPy recently got a new Linear system solver: linsolve in sympy.solvers.solveset , you can use that as follows: In [38]: from sympy import * In [39]: from sympy.solvers.solveset import linsolve In [40]: x, y, z = symbols('x, y, z') List of Equations Form: In [41]: linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z)) Out[41]: {(-y - 1, y, 2)} Augmented Matrix Form: In [59]: linsolve(Matrix(([1, 1, 1, 1], [1, 1, 2, 3])), (x, y, z)) Out

SymPy: How to return an expression in terms of other expression(s)?

隐身守侯 提交于 2019-11-30 15:41:10
I'm fairly new to SymPy and have what might be a basic question. Or I might simply be misinterpreting how SymPy is supposed to be used. Is there a way to create an expression that is not represented by atoms, but by a combination of other expressions? Example: >>> from sympy.physics.units import * >>> expr1 = m/s >>> expr2 = mile/hour >>> expr1 m/s >>> expr2 1397*m/(3125*s) >>> expr1.in_terms_of([mile,hour]) #in my dreams? 3125*mile/(1397*hour) >>> On a side note: Can I find an 'official' PDF (or other printable) version of the complete SymPy documentation? (I'm under draconian Internet usage

Is sympy pretty printing broken in new jupyter notebook?

血红的双手。 提交于 2019-11-30 13:09:43
I have previously used pretty printing of math in the ipython notebook. After upgrading to jupyter (also upgrades many other ipython-related packages), pretty printing no longer works like before. I use this code in the top of my notebooks to set it up: import sympy as sp sp.init_printing() I have also tried this with the use_latex=True and use_latex='mathjax' arguments to init_printing , but that does not help. In all cases, expressions are printed in plain text after upgrading. See https://gist.github.com/josteinbf/78dae5085dec0aa19a48#file-sympy_pp-ipynb for a complete example in the form

Get a value from solution set returned as finiteset by Sympy

谁都会走 提交于 2019-11-29 16:43:49
问题 I`m creating a script in Python Sympy library and trying to access the result returned by solveset() and linsolve() functions. My problem is that the object returned by these functions is of type finiteset and I want to select some results automaticaly to re-enter it in other equations. Any body could help me? An example: I create a list of equations with two unknown variables: >>> a1, a2 = symbols('a1, a2') >>> eq2_1 = Eq(-3*a1/10 - 3*a2/20 + 1/12) >>> eq2_2 = Eq(-3*a1/20 - 13*a2/105 + 1/20)

How to simplify logarithm of exponent in sympy?

百般思念 提交于 2019-11-29 16:17:14
When I type import sympy as sp x = sp.Symbol('x') sp.simplify(sp.log(sp.exp(x))) I obtain log(e^x) Instead of x . I know that "there are no guarantees" on this function. Question. Is there some specific simplification (through series expansion or whatsoever) to convert logarithm of exponent into identity function? You have to set x to real type and your code will work: import sympy as sp x = sp.Symbol('x', real=True) print(sp.simplify(sp.log(sp.exp(x)))) Output: x . For complex x result of this formula is not always is equal to x . Example is here . If you want to force the simplification,

Get all positive integral solutions for a linear equation

╄→尐↘猪︶ㄣ 提交于 2019-11-29 14:27:09
A game I played has a riddle that involves solving the following equation: x*411 + y*295 + z*161 = 3200 Not wanting to think I just slapped it into sympy , which I haven’t really used up to that point: >>> from sympy import * >>> x, y, z = symbols('x y z', integer=True, positive=True) >>> solve(x*411 + y*295 + z*161 - 3200, [x, y, z]) [{x: -295*y/411 - 161*z/411 + 3200/411}] Hmm, this only gave me a dependent solution, but I want all possible solutions in the domain I constrained the variables to, e.g. (assuming there are no other solutions) [{x: 4, y: 2, z:6}] or [(4, 2, 6)] Of course I could

Force SymPy to keep the order of terms

前提是你 提交于 2019-11-29 14:25:54
I have the following code: from sympy import * init_printing() x,y = symbols('x y') u = Function('u')(x,y) ux,uy,uxx,uxy,uyy = symbols("u_x u_y u_xx u_xy u_yy") mainEvaluation = uxx - 2*sin(x)*uxy - (cos(x) ** 2) * uyy - 2*ux + (2 - cos(x) + 2*sin(x) )*uy And when the output of print(mainExpression) is -2*u_x + u_xx - 2*u_xy*sin(x) + u_y*(2*sin(x) - cos(x) + 2) - u_yy*cos(x)**2 The problem is: I want the original order of variables. u_xx - 2*u_xy*sin(x) - u_yy*cos(x)**2 - 2*u_x + u_y*(2*sin(x) - cos(x) + 2) All this is done in IPython notebook. Is there any way to keep order? Sadly, SymPy does

Calculate curl of a vector field in Python and plot it with matplotlib

青春壹個敷衍的年華 提交于 2019-11-29 14:10:43
问题 I need to calculate the curl of a vector field and plot it with matplotlib. A simple example of what I am looking for could be put like that: How can I calculate and plot the curl of the vector field in the quiver3d_demo.py in the matplotlib gallery? 回答1: You can use sympy.curl() to calculate the curl of a vector field. Example : Suppose F (x,y,z) = y 2 z i - xy j + z 2 k , then: y would be R[1] , x is R[0] and z is R[2] the unit vectors i , j , k of the 3 axes, would be respectively R.x , R

What is the best way to convert a SymPy matrix to a numpy array/matrix

不羁的心 提交于 2019-11-29 11:48:17
问题 I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice. I have a symbolic matrix like: g = sympy.Matrix( [[ x, 2*x, 3*x, 4*x, 5*x, 6*x, 7*x, 8*x, 9*x, 10*x] [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]] ) and I am converting to a numpy.array doing: g_func = lambda val: numpy.array( g.subs( {x:val} ).tolist(), dtype=float ) where I get an array for a given value of x . Is there a

problem displaying sympy rendered svg in python

时光毁灭记忆、已成空白 提交于 2019-11-29 11:18:27
I have the following program which uses sympy and svgmath to render a user's algebraic expression. It nearly works but there are a few issues: The svg is not actually produced until the program exits so obviously cannot be diplayed. Is there a way to improve the performance (not looking up 'svgmath.xml' every time etc.)? Does an actual svg file need to be produced? Can svgmath pass output directly to the QSvgWidget? Many thanks and best wishes. from __future__ import division import sys import sympy from PySide.QtGui import * from PySide.QtCore import * from PySide.QtXml import * from PySide