sympy

Replace ** with Math.pow in SymPy

最后都变了- 提交于 2019-12-20 03:28:07
问题 I have a SymPy expression in Python and I would like to copy and paste it into a Java source code. Problem is that there’s a different notation for exponentiation: Java uses Math.pow(a,b) ; Python uses a**b . So my question is: Is there a way how to print the SymPy expression in the “Java format”? 回答1: SymPy has several code printers that are intended specifically for such a purpose. While there is no Java code printer, there is one for Javascript. I cannot say whether Java and Javascript are

Speeding up computation of symbolic determinant in SymPy

自古美人都是妖i 提交于 2019-12-19 19:12:31
问题 I have a 4x4 matrix A with rather long but simple symbolic expressions in each of its entries. About 30 different symbols are involved. By "simple" I mean that these symbols are combined using only addition/subtraction, multiplication/division, and integer powers. By "long" I mean that if I print out the matrix, it covers three or four screens worth. I need the determinant of this matrix. Or, to be more specific, I know that the determinant is a fourth-order polynomial in one particular

Error: function() takes at least n arguments (n given)

 ̄綄美尐妖づ 提交于 2019-12-19 17:40:45
问题 I'm trying to use SymPy to take residues, in this case the cotangent function. I've got an integrate() function: import sympy as sy import numpy as np def integrate(f, z, gamma, t, lower, upper, exact=True): ''' Integrate f(z) along the contour gamma(t): [lower, upper] --> C INPUTS: f - A SymPy expression. Should represent a function from C to C. z - A SymPy symbol. Should be the variable of f. gamma - A SymPy expression. Should represent a function from [lower, upper] to C. t - A SymPy

How to accelerate slow matrix multiplication in SymPy?

跟風遠走 提交于 2019-12-19 09:52:56
问题 I was writing a tool to solve specific recurrence equations with SymPy, and found that one of the steps that involved a matrix multiplication was taking an extraordinarily long time. For instance, if I try the following in the iPython console, In [1]: from sympy import * In [2]: A = Matrix(500, 500, lambda i,j: 2 + abs(i-j) if i-j in [-1, 0, 1] else 0) In [3]: A[0:7, 0:7] Out[3]: Matrix([ [2, 3, 0, 0, 0, 0, 0], [3, 2, 3, 0, 0, 0, 0], [0, 3, 2, 3, 0, 0, 0], [0, 0, 3, 2, 3, 0, 0], [0, 0, 0, 3,

Julia: how do I convert a symbolic expression to a function?

非 Y 不嫁゛ 提交于 2019-12-19 06:45:56
问题 I have created a symbolic expression using the SymPy package (https://github.com/jverzani/SymPy.jl). I want to now find the roots of that expression using the Roots package (https://github.com/JuliaLang/Roots.jl). However, I cannot figure out how to use the fzeros method for finding the roots, since this can only be applied on an object with the type Function rather than Sym , which is the type of my expression. Here's an example of what I'm trying to do. I create a symbolic "x" and a

SymPy: Limit symbol/variable to interval

假装没事ソ 提交于 2019-12-18 20:18:36
问题 Using SymPy, is it possible to limit the possible values of a symbol/variable to a certain range? I now I can set some properties while defining symbols, like positive=True , but I need more control, i.e. I need to set it to be in the interval [0,1]. This assumption should then be used for solving, simplifying etc. 回答1: You can specify the bounds as inequalities such as x >= lb and x <= ub , for example: from sympy.solvers import solve from sympy import Symbol x = Symbol('x') solve([x >= 0.5,

How can I solve system of linear equations in SymPy?

放肆的年华 提交于 2019-12-18 18:42:29
问题 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 回答1: 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

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

笑着哭i 提交于 2019-12-18 17:28:00
问题 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

Sympy: Solving Matrices in a finite field

家住魔仙堡 提交于 2019-12-18 16:25:08
问题 For my project, I need to solve for a matrix X given matrices Y and K. (XY=K) The elements of each matrix must be integers modulo a random 256-bit prime. My first attempt at solving this problem used SymPy's mod_inv(n) function. The problem with this is that I'm running out of memory with matrices of around size 30. My next thought was to perform matrix factorization, as that might be less heavy on memory. However, SymPy seems to contain no solver that can find matrices modulo a number. Any

Is sympy pretty printing broken in new jupyter notebook?

一笑奈何 提交于 2019-12-18 15:57:09
问题 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:/