sympy

How to get coefficients of polynomial expression

淺唱寂寞╮ 提交于 2019-12-25 09:13:03
问题 I used rSymPy and obtained following expression: "1 - 0.7*B - 0.3*B**2" Now I want to extract the coefficients of B and Coefficients of B^2, and stored in a matrix. I tried gsub function in R, any suggestions? 回答1: Do you mean like: > x <- "1 - 0.7*B - 0.3*B**2" > m <- gregexpr("[0-9]+\\.[0-9]+",x) > out <- unlist(regmatches(x,m) ) > out [1] "0.7" "0.3" A more complex example: > x <- c("1 - 0.7*B - 0.3*B**2", "1 - 0.3*B - 0.7*B**2","1 - 1.3*B - 0.6*B**2") > m <- gregexpr("[0-9]+\\.[0-9]+",x)

Einstein-Convention like implicit summation with sympy

南楼画角 提交于 2019-12-25 07:47:01
问题 Given some simpy tensor A = sympy.tensor.array.Array( <3rd-rank data consisting of sympy expressions> ) and a matrices T and T1=T.inv() representing a basis transform, is it somehow possible to use an notation like B[i,j,k] = T[i,a] * A[a,b,c] * T1[b,j] * T1[c,k] to calculate the transformed tensor? It seems that it is, in principle, possible in sympy to use an Einstein summation convention, but I am running into multiple problems with it: A code snippet from sympy import symbols, IndexedBase

How to determine the arrangement of the polynomial when displaying it with latex?

二次信任 提交于 2019-12-25 01:49:45
问题 I am not sure if it is an issue with my python code or with the latex but it keeps rearranging my equation in the output. Code: ddx = '\\frac{{d}}{{dx}}' f = (a * x ** m) + (b * x ** n) + d df = sym.diff(f) df_string = tools.polytex(df) f_string = tools.polytex(f) question_stem = f"Find $_\\displaystyle {ddx}\\left({f_string}\\right)$_" output: In this case a = 9 , b = -4 , c = 4 , m = (-1/2) , n = 3 and I want the output to be in the order of the variable f. I have tried changing the order

SymPy: Custom print format for a custom Function expression

青春壹個敷衍的年華 提交于 2019-12-25 01:35:24
问题 What is the correct way to define classes that inherit the Function class in Sympy and create custom print statements for them? I have followed the examples from https://docs.sympy.org/latest/modules/printing.html that modify the output of Mod and it all works fine. For example: With a Custom Printer from sympy import Symbol, Mod from sympy.printing.latex import LatexPrinter class MyLatexPrinter(LatexPrinter): def _print_Mod(self, expr, exp=None): return 'hey' def print_my_latex(expr): print

Convert sympy symbolic variable to numpy array

非 Y 不嫁゛ 提交于 2019-12-25 00:18:36
问题 I want to perform a convolution that contains a sympy symbolic variable, then convert it to a numpy array. My MWE is: from numpy import pi, float64, linspace from scipy.signal import fftconvolve import matplotlib.pyplot as plt from sympy import symbols from sympy.utilities.lambdify import lambdify a = 0.657 b = 0.745 c = 0.642 d = 0.343 x = symbols('x') f = 2*b / ((x-a)**2 + b**2) g = 2*d / ((x-c)**2 + d**2) fog = fftconvolve(f,g,mode='same') fog_fun = lambdify(x,fog,'numpy') # returns a

Print a symbol for product (∏) by using SymPy package

别来无恙 提交于 2019-12-24 21:52:09
问题 I would like to know how to print the symbols in this picture by using SymPy package in Python. 回答1: Symbolic products are created with Product(expr, (index, low, high)) where high is inclusive unlike in much of Python code. For example: P00 = IndexedBase("P^{00}") P02 = IndexedBase("P^{02}") i = Idx("i") t = symbols("t") expr = Product(P00[i]*P02[t], (i, 1, t-1)) print(expr) which prints (assuming LaTeX support) as The placement of indices is suboptimal; the issue is that the superscripts

Boolean operation with symbol in Sympy

烂漫一生 提交于 2019-12-24 14:33:00
问题 Boolean operation of a Boolean variable on a symbol produces TypeError , but the reverse has no problem: >>> from sympy import * >>> x = Symbol('x', bool=True) >>> x ^ True Not(x) >>> True ^ x Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> True ^ x TypeError: unsupported operand type(s) for ^: 'bool' and 'Symbol' I can do try-catch: try : print True ^ x except TypeError: print x ^ True Not(x) But, for my present task, it is impossible to implement this with try

Partially factoring an expression in Sympy

社会主义新天地 提交于 2019-12-24 12:12:33
问题 Suppose I have an expression of the form . I know that I can simplify the expression like so: . However, sympy.simplify and sympy.factor both return the original expression. To work around this, I've been operating on the expression at a low level: factor_map = defaultdict(set) additive_terms = expr.as_coeff_add()[-1] for term1, term2 in combinations(additive_terms, 2): common_terms = ( set(term1.as_coeff_mul()[-1]) & set(term2.as_coeff_mul()[-1]) ) if common_terms: common_factor = sympy.Mul(

Sympy Error when using POLY with SQRT

五迷三道 提交于 2019-12-24 08:38:46
问题 I have the following code: from sympy import * a = Symbol('a') p = Poly(sqrt(a), domain=QQ) p.eval(a,2) What I expect after eval is the square-root of 2. however what I get is: ValueError: tuple.index(x): x not in tuple Am I misunderstanding something here? 回答1: Evaluating a polynomial means substituting for one of its generators. The problem is that you are trying to substitute for a, after creating a polynomial in sqrt(a). Essentially you are treating a polynomial as an expression that

TypeError: return arrays must be of ArrayType using lambdify of sympy in python

允我心安 提交于 2019-12-24 08:26:07
问题 I have the following code: x,y,z,t = var('x,y,z,t') d = set([t]) rule = And(Or(x,y,z),t) atoms = tuple(rule.atoms()) params = [True if i in d else False for i in atoms] lam = lambdify(atoms, rule) lam(*params) which throws: Traceback (most recent call last): File "<pyshell#142>", line 7, in <module> lam(*params) File "<string>", line 1, in <lambda> TypeError: return arrays must be of ArrayType And I can't understand why, for other rules it works great. 回答1: Update: This bug was fixed in SymPy