sympy

Numerical Integration over a Matrix of Functions, SymPy and SciPy

大兔子大兔子 提交于 2019-12-01 16:57:54
问题 From my SymPy output I have the matrix shown below, which I must integrate in 2D. Currently I am doing it element-wise as shown below. This method works but it gets too slow (for both sympy.mpmath.quad and scipy.integrate.dblquad ) for my real case (in which A and its functions are much bigger (see edit below): from sympy import Matrix, sin, cos import sympy import scipy sympy.var( 'x, t' ) A = Matrix([[(sin(2-0.1*x)*sin(t)*x+cos(2-0.1*x)*cos(t)*x)*cos(3-0.1*x)*cos(t)], [(cos(2-0.1*x)*sin(t)

Numerical Integration over a Matrix of Functions, SymPy and SciPy

五迷三道 提交于 2019-12-01 16:48:21
From my SymPy output I have the matrix shown below, which I must integrate in 2D. Currently I am doing it element-wise as shown below. This method works but it gets too slow (for both sympy.mpmath.quad and scipy.integrate.dblquad ) for my real case (in which A and its functions are much bigger (see edit below): from sympy import Matrix, sin, cos import sympy import scipy sympy.var( 'x, t' ) A = Matrix([[(sin(2-0.1*x)*sin(t)*x+cos(2-0.1*x)*cos(t)*x)*cos(3-0.1*x)*cos(t)], [(cos(2-0.1*x)*sin(t)*x+sin(2-0.1*x)*cos(t)*x)*sin(3-0.1*x)*cos(t)], [(cos(2-0.1*x)*sin(t)*x+cos(2-0.1*x)*sin(t)*x)*sin(3-0.1

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

a 夏天 提交于 2019-12-01 16:19:33
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 symbol. Should be the variable of gamma. lower - The lower bound for the domain of gamma. upper - The upper

Sympy to numpy causes the AttributeError: 'Symbol' object has no attribute 'cos'

久未见 提交于 2019-12-01 12:45:58
I am trying to do partial derivatives using sympy and I want to convert it to a function so that I can substitute values and estimate the derivatives at some values of t_1, t_2. The code I am using is as follows: import sympy as sp import numpy as np from sympy import init_printing init_printing() t_1,t_2,X_1,X_2,Y_1,Y_2,X_c1,X_c2,Y_c1,Y_c2,a_1,a_2,psi_1,psi_2,b_1,b_2= sp.symbols('t_1 t_2 X_1 X_2 Y_1 Y_2 X_c1 X_c2 Y_c1 Y_c2 a_1 a_2 psi_1 psi_2 b_1 b_2') X_1=X_c1 + (a_1 * sp.cos(t_1) * sp.cos(psi_1)) - ((b_1) * sp.sin(t_1)* sp.sin(psi_1)) X_2=X_c2 + (a_2 * sp.cos(t_2) * sp.cos(psi_2)) - ((b_2)

Sympy to numpy causes the AttributeError: 'Symbol' object has no attribute 'cos'

≯℡__Kan透↙ 提交于 2019-12-01 10:54:24
问题 I am trying to do partial derivatives using sympy and I want to convert it to a function so that I can substitute values and estimate the derivatives at some values of t_1, t_2. The code I am using is as follows: import sympy as sp import numpy as np from sympy import init_printing init_printing() t_1,t_2,X_1,X_2,Y_1,Y_2,X_c1,X_c2,Y_c1,Y_c2,a_1,a_2,psi_1,psi_2,b_1,b_2= sp.symbols('t_1 t_2 X_1 X_2 Y_1 Y_2 X_c1 X_c2 Y_c1 Y_c2 a_1 a_2 psi_1 psi_2 b_1 b_2') X_1=X_c1 + (a_1 * sp.cos(t_1) * sp.cos

Convert symbolic expressions to Python functions using SymPy

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 07:01:27
问题 I have a rather large symbolic function that is evaluated for different values of a parameter in a loop. In each iteration, after finding the expression of the function, partial derivatives are derived. Something like this: from sympy import diff, symbols,exp def lagrange_eqs(a): x,y,z= symbols('x y z') FUNC=x**2-2*x*y**2+z+a*exp(z) d_lgrng_1=diff(FUNC,x) d_lgrng_2=diff(FUNC,y) d_lgrng_3=diff(FUNC,z) return [d_lgrng_1,d_lgrng_2,d_lgrng_3] Next, I need to convert the output of this function to

Test if matrix is invertible over finite field

↘锁芯ラ 提交于 2019-12-01 05:36:39
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 to achieve the same thing but over F_2? It would be better to use Sage or some other proper tool for

Possible to add descriptions to symbols in sympy?

混江龙づ霸主 提交于 2019-12-01 05:14:34
I seek a functionality in sympy that can provide descriptions to symbols when needed. This would be something along the lines of >>> x = symbols('x') >>> x.description.set('Distance (m)') >>> t = symbols('t') >>> t.description.set('Time (s)') >>> x.description() 'Distance (m)' >>> t.description() 'Time (s)' This would be useful because it would enable me to keep track of all my variables and know what physical quantities I am dealing with. Is something like this even remotely possible in sympy? EDIT I don't think this is a duplicate because the __doc__ attribute for symbols appears to be

Multivariate Taylor approximation in sympy

限于喜欢 提交于 2019-12-01 04:40:58
I aim to write a multidimensional Taylor approximation using sympy , which uses as many builtin code as possible, computes the truncated Taylor approximation of a given function of two variables returns the result without the Big-O-remainder term, as e.g. in sin(x)=x - x**3/6 + O(x**4) . Here is what I tryed so far: Approach 1 Naively, one could just combine the series command twice for each variable, which unfortunately does not work, as this example shows for the function sin(x*cos(y)) : sp.sin(x*sp.cos(y)).series(x,x0=0,n=3).series(y,x0=0,n=3) >>> NotImplementedError: not sure of order of O

Factoring polys in sympy

回眸只為那壹抹淺笑 提交于 2019-12-01 04:05:00
I'm doing a very simple probability calculations of getting subset of X, Y, Z from set of A-Z (with corresponding probabilities x, y, z). And because of very heavy formulas, in order to handle them, I'm trying to simplify (or collect or factor - I dont know the exact definition) these polynomial expressions using sympy . So.. having this (a very simple probability calculation expression of getting subset of X,Y,Z from set of A-Z with corresponding probabilities x, y, z) import sympy as sp x, y, z = sp.symbols('x y z') expression = ( x * (1 - x) * y * (1 - x - y) * z + x * (1 - x) * z * (1 - x