sympy

Integrals in Python: Add object not callable

有些话、适合烂在心里 提交于 2019-12-12 12:11:54
问题 I'm trying to solve an integral of a taylor approximation of a sin(x) function by using the trapezoid rule. The code seems fine but it keep giving me the following error: "TypeError: 'Add' object is not callable" This is my code: import math import numpy import sympy as sy import numpy as np from sympy.functions import sin,cos import matplotlib.pyplot as plt x = sy.Symbol('x') f = sin(x) # Factorial function if n <= 0: return 1 else: return n*factorial(n-1) taylor_series = sin(x).series(n

substitute sympy function with arbitrary arguments

前提是你 提交于 2019-12-12 11:57:09
问题 This should be an easy task, but I'm having a hard time getting it to work in Sympy. I want to substitute an undefined function with arbitrary arguments with a specific formula for example: from sympy import * var('a b c') f= Function('f') test= f(a+b) lin= test.subs({f(c):2*(c)}) print(lin) I want this to print out 2*(a+b) However, for that I have to use lin= test.subs({f(a+b):2*(a+b)}) Do I have to define f as a class in order to do this substitution? 回答1: When you're doing advanced

Finding roots with a SymPy Matrix

萝らか妹 提交于 2019-12-12 10:51:53
问题 Edit: I found a working solution, but I would still love more explanation to what's going on here: from scipy import optimize from sympy import lambdify, DeferredVector v = DeferredVector('v') f_expr = (v[0] ** 2 + v[1] ** 2) f = lambdify(v, f_expr, 'numpy') zero = optimize.root(f, x0=[0, 0], method='krylov') zero Original question: Below we have Matrix M composed of expressions f1(x1, x2) and f2(x1, x2) . I would like to know the values of x1 and x2 when M = [f1, f2] = [0, 0] . The follow

No module named sympy

不想你离开。 提交于 2019-12-12 10:38:48
问题 Hi I'm learning linear algebra with python with an Edx course. (http://nbviewer.ipython.org/github/ULAFF/notebooks/tree/may-14-2014/). On "02.4.2.10 Practice with matrix-vector multiplication" with the first box, the code is: import generate_problems as gp print("What is the result of the matrix vector product below?") p = gp.Problem() p.new_problem() generate_problems is a module that the professor at Edx created. However, I got an error importing sympy. I got the error below: --------------

sympy: Collect symbols for matrix coefficients?

﹥>﹥吖頭↗ 提交于 2019-12-12 09:05:12
问题 I am trying to factor an expression, and separate coefficients to matrix form, such that: Closely related to Factor sympy expression to matrix coefficients?, where Wild symbols are used with match(form) to determine coefficients for its matrix form. However, I am unable to get the match(form) method to work for the following. Why does match(form) method fail? What are clean alternatives to accomplish this? #Linear Interpolation function: V(x) v_1, theta_1, v_2, theta_2, x, L = symbols("v_1,

plus/minus operator for python ±

时光怂恿深爱的人放手 提交于 2019-12-12 07:59:37
问题 I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this. Am I missing something? 回答1: Another possibility: uncertainties is a module for doing calculations with error tolerances, ie (2.1 +/- 0.05) + (0.6 +/- 0.05) # => (2.7 +/- 0.1) which would be written as from uncertainties import ufloat ufloat(2.1, 0.05) + ufloat(0.6, 0.05) Edit: I was getting some odd results, and after a bit more

Double integral with variable boundaries in python Scipy + sympy (?)

大城市里の小女人 提交于 2019-12-12 07:36:38
问题 The full mathematical problem is here. Briefly I want to integrate a function with a double integral. The inner integral has boundaries 20 and x-2 , while the outer has boundaries 22 and 30 . I know that with Scipy I can compute the double integral with scipy.integrate.nquad . I would like to do something like this: def f(x, y): return (x ** 2 + y ** 2) res = sp.integrate.nquad(f, [[22, 30], [20, x-2]]) Is it possible? Maybe using also sympy ? 回答1: I solved with sympy : from sympy import * x,

How to create a Rician random variable?

瘦欲@ 提交于 2019-12-12 07:22:22
问题 I'm trying to model a signal detection problem using Sympy, and need two random variables. One with a Rayleigh distribution to model noise, and one with a Rician distribution to model signal+noise. Sympy provides a Rayleigh distribution, but not a Rician-- or at least not one by that name. What's the best way of creating one? Does it exist under a different name? Is there a way to manipulate existing distributions into a Rician? Following advice from @asmeurer, I've implemented my own Rice

Sympy Can't differentiate wrt the variable

送分小仙女□ 提交于 2019-12-12 05:17:11
问题 I am trying to evaluate a function (second derivative of another one) but Sympy seems to have a difficulty to do that... ? from sympy import * from sympy import Symbol # Symbols theta = Symbol('theta') phi = Symbol('phi') phi0 = Symbol('phi0') H0 = Symbol('H0') # Constants a = 0.05 t = 100*1e-9 b = 0.05**2/(8*pi*1e-7) c = 0.001/(4*pi*1e-7) phi0 = 60*pi/180 H0 = -0.03/(4*pi*1e-7) def m(theta,phi): return Matrix([[sin(theta)*cos(phi), sin(theta)*cos(phi), cos(phi)]]) def h(phi0): return Matrix(

Is there an equivalent to Mathematica's “usage” function?

谁说胖子不能爱 提交于 2019-12-12 04:20:39
问题 In Mathematicy, one can define tags of a function such as f::usage = "f[x] gives (x - 1)(x + 1)"; which can then be called like: ?f Is there an equivalent in SymPy? 回答1: You can “monkey patch” the docstring as follows: import sympy f = sympy.Function("f") f.__doc__ = "A naked function without any special properties" You can retrieve the docstring in the very same way, e.g., print(f.__doc__) . In iPython and similar, you can also use f? to obtain it (together with some other information). 来源: