sympy

How to create a random variable whose parameters are themselves random variables in SymPy?

*爱你&永不变心* 提交于 2019-12-10 23:01:32
问题 I have a random variable Y whose distribution is Poisson with parameter that is itself a random variable X, which is Poisson with parameter 10. How can I use SymPy to automatically calculate the covariance between X and Y? The code from sympy.stats import * x1 = Poisson("x1", 3) x2 = Poisson("x2", x1) print(covariance(x2,x1)) raises an error ValueError: Lambda must be positive The documentation is not clear to me on this matter, and playing around with the function given did not seem to work.

Cannot create mpf from a complex number when calling qr_solve

萝らか妹 提交于 2019-12-10 22:58:28
问题 I make the following imports: from sympy.matrices import Matrix as sy_matrix import sympy.mpmath as sy_mp Create my matrices like this: sysMat = sy_matrix([[0.0]*sz1]*sz2) resVec = sy_matrix([[0.0]]*sz2) .Populate then with python complex numbers (eg 1.0+1.0j) and then call: coeffVec = sy_mp.qr_solve(sysMat, resVec) However I get the following exception: File "..\RatSMat.py", line 69, in solve self.coeffVec = sy_mp.qr_solve(self.sysMat, self.resVec) File "C:\Python27\lib\site-packages\sympy

Sympy: Multiplications of exponential rather than exponential of sum

爱⌒轻易说出口 提交于 2019-12-10 21:27:30
问题 I'm searching how to tell SymPy to use a multiplication of exponentials rather than an exponential of a sum. That is, it currently gives me exp(a + b) and I would want to get exp(a)*exp(b). There must be a fairly easy way but I can't seem to find it. 回答1: You could use the expand() function to show the expression with multiplication of bases rather than the sum of exponents: >>> from sympy import * >>> a, b = symbols('a b') >>> expr = exp(a + b) >>> expr exp(a + b) >>> expr.expand() exp(a)

From string to sympy expression

余生颓废 提交于 2019-12-10 21:11:15
问题 Recently I was working on a web application, using Flask and sympy libraries. The user enters his equation in a textarea and Flask rechieve it as a string. I would like to have the possibility to calculate the result of this equation,by using sympy function solve(). But for this I must convert this string to an sympy expression. How could I do that? ` from flask import Flask,request,render_template,flash from sympy import * from sympy.parsing.sympy_parser import * x = symbols('x') app = Flask

Simplifying exponential representation of hyperbolic functions in sympy

Deadly 提交于 2019-12-10 20:27:25
问题 I am trying to rewrite some exponential functions in an expression to cosh and sinh. The rewrite() function works to get from a hyperbolic function to its exponential representation. But it does not work to get back. >>> import sympy >>> x=sympy.Symbol('x') >>> sympy.cosh(x).rewrite(sympy.exp) exp(x)/2 + exp(-x)/2 >>> sympy.cosh(x).rewrite(sympy.exp).rewrite(sympy.cosh) exp(x)/2 + exp(-x)/2 I would expect the result of the last command to be 'cosh(x)'. Can someone explain to me why it is not?

How to add assumptions in limit in Sympy?

混江龙づ霸主 提交于 2019-12-10 20:09:30
问题 I want to add some assumptions in limit. Suppose 0<x<1 , then $$limit_{n \to \infty} x^n = 0$$ from sympy import * x = var('x, n') limit(x**n, n, oo) But I get an error NotImplementedError: Result depends on the sign of sign(log(x)) . Is there some way in sympy to handle this? 回答1: EDIT : As pointed out in the comments the solution below fails with the same NotImplementedError as in the question (as of November 2019) saying that the answer depends on sign(log(x)). It seems that this sign

Cube root of negative real numbers

雨燕双飞 提交于 2019-12-10 19:47:47
问题 I'm trying to plot a quite complex function, i.e. log(x/(x-2))**Rational(1,3) . I'm working only with real numbers. If I try to plot it, sympy only plots the x>2 part of it. I found that actually complex numbers come into play and, for example, root(-8,3).n() gives: 1.0+1.73205080756888i Which is reasonable, even though it's not what I was looking for (because I'm only interested in the real result). Reading sympy › principle root I found that real_root(-8,3) gives -2 as expected. But I still

SymPy could not compute the eigenvalues of this matrix

拜拜、爱过 提交于 2019-12-10 19:15:52
问题 I want to compute the second eigenvalue of a Laplacian matrix to check if the corresponding graph is connected or not, but when I try to use SymPy's eigenvals , a lot of times it happens that it throws an error MatrixError: Could not compute eigenvalues for Matrix([[1.00000000000000, 0.0, 0.0, 0.0, -1.00000000000000, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.00000000000000, 0.0, 0.0, 0.0, -1.00000000000000, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.00000000000000, 0.0, 0.0, 0.0, 0.0, 0.0, -1.00000000000000,

finding least-squares integer solutions to a linear system with numpy/sympy

十年热恋 提交于 2019-12-10 19:13:23
问题 I need to solve a system of linear diophantine equations with either numpy or sympy. Is there any way to constrain numpy's linalg.solve/linalg.lstsq method to return only integer solutions? (probably not but thought I should ask) I looked into Sympy's diophantine solver and it does not seem to be applicable to solving whole systems The problem I am working on is something along the lines of P1(X) + P2(Y) = TargetPro F1(X) + F2(Y) = TargetFat C1(X) + C2(Y) = TargetCarb In this case, X,Y,Z

Double summation of matrix elements in Python

我只是一个虾纸丫 提交于 2019-12-10 19:06:38
问题 Based on the simplified example below I would like in my code from sympy import* import numpy as np init_printing() x, y = symbols('x, y') mat = Matrix([[x,1],[1,y]]) X = [1, 2, 3] Y = [[10, 20, 30], [40, 50, 60], [70, 80, 90]] to substitute the symbolic x and y with values of X and Y and ofcourse calculate the double summation of the given matrix. I'm trying to solve this but I'm having a rough time with the substitution in each step. Any help would be highly appreciated. 回答1: You've