sympy

Take Python Function and Generate All Derivatives

馋奶兔 提交于 2019-12-06 11:46:58
问题 I have a python function with variable number of arguments: F(x1, x2, ... , xN) I want to automatically generate N functions representing the derivatives of F with respect to each argument. F'_1 = dF/dx1 F'_2 = dF/dx2 ... F'_N = dF/dxN For example, I be able to give both F(x1) = sin(x1) and F(x1, x2) = sin(x1) * cos(x2) and get all the derivatives automatically. Edit2: If function F was 2 variable (fixed number of arguments), I could use def f(x,y): return sin(x)*cos(y) from sympy import * x,

Derivative of a conjugate in sympy

青春壹個敷衍的年華 提交于 2019-12-06 10:22:18
When I try to differentiate a symbol with SymPy I get the following In : x=Symbol('x') In : diff(x,x) Out: 1 When I differentiate the symbol respect to its conjugate the result is In [55]: diff(x,x.conjugate()) Out[55]: 0 However, when I try to differentiate the conjugate of the symbol SymPy doesn't do it In : diff(x.conjugate(),x) Out: Derivative(conjugate(x), x) This is still correct, but the result should be zero. How can I make SimPy perform the derivative of a conjugate? I'm not sure about the mathematics if diff(conjugate(x), x) should be zero. The fact that diff(x,x.conjugate()) gives

How to generate Fortran subroutine with SymPy codegen

血红的双手。 提交于 2019-12-06 08:35:16
问题 I want to generate a Fortran subroutine with SymPy codegen utility. I can generate a Fortran function without problem with codegen(("f", x*y*z), "f95", "filename") . But I want to generate a Fortran subroutine so I can modify input arrays. How can I do this? The documentation is very poor. 回答1: The codegen utility creates a function if there is a single scalar return value, and a subroutine otherwise. There is some support for arrays, but the array functionality won't be triggered unless you

No solutions with exponents in python sympy

最后都变了- 提交于 2019-12-06 08:23:52
When I run this program, I get no solution at the end, but there should be a solution ( I believe). Any idea what I am doing wrong? If you take away the Q from e2 equation it seems to work correctly. #!/usr/bin/python from sympy import * a,b,w,r = symbols('a b w r',real=True,positive=True) L,K,Q = symbols('L K Q',real=True,positive=True) e1=K e2=(K*Q/2)**(a) print solve(e1-e2,K) It works if we do the following: Set Q=1 or, Change e2 to e2=(K* a) (Q/2)**(a) I would still like it to work in the original way though, as my equations are more complicated than this. This is just a deficiency of

sympy arbitrary function range

拟墨画扇 提交于 2019-12-06 07:27:21
I want to define the arbitrary function f. I know that f always returns a positive number. I want sympy to be able to use this knowledge when running simplifications (especially the three power rules mentioned in the simplify documentation). Is there a way to do this? I'm looking for something like the below: f = Function("f", positive = True) g = Function("g", positive = True) x = symbols("x") y = symbols("y") n = symbols("n", real = True) test = ( f(x) * g(y) ) ** n # This should work but doesn't expand_power_base(test) Functions defined like Function('f') do not support assumptions at this

How to format contour lines from Matplotlib

…衆ロ難τιáo~ 提交于 2019-12-06 07:26:25
问题 I am working on using Matplotlib to produce plots of implicit equations (eg. y^x=x^y). With many thanks to the help I have already received I have got quite far with it. I have used a contour line to produce the plot. My remaining problem is with formatting the contour line eg width, color and especially zorder, where the contour appears behind my gridlines. These work fine when plotting a standard function of course. import matplotlib.pyplot as plt from matplotlib.ticker import

Factor sympy expression to matrix coefficients?

佐手、 提交于 2019-12-06 07:02:01
问题 I have tried to be diligent in looking through documentation and am coming up empty. I am trying to factor or eliminate terms in a expression to matrix form. My problem appears to differ from polynomial factoring (as I plan to implement a function phi(x,y,z) = a_1 + a_2*x + a_3*y + a_4*z ) import sympy from sympy import symbols, pprint from sympy.solvers import solve phi_1, phi_2, x, a_1, a_2, L = symbols("phi_1, phi_2, x, a_1, a_2, L") #Linear Interpolation function: phi(x) phi = a_1 + a_2*x

How do I define a conditional function using sympy?

泪湿孤枕 提交于 2019-12-06 03:17:46
问题 I want to be able to define an expression which takes all the values of variable where it is defined and evaluates the expression as 0 when it is not defined. Similar to this: - import numpy as np import sympy as sp def expr(k1, k2): x, y =sp.symbols('x y') if x == k1 : fn = 0 else: fn = np.divide(1,(x-k1)*(y-k2)) return fn, x, y f,x, y = expr(1,2) print(f) fx = f.subs({x:1,y:4}) print(fx) So how is the equality or conditionality going to be checked once the function has been defined? fn = 1/

input a symbolic function in a python code

我的未来我决定 提交于 2019-12-06 02:41:10
I was just wondering if there is a method to input a symbolic function in a python code? like in my code I have: from sympy import * import numpy as np import math myfunction = input("enter your function \n") l = Symbol('l') print myfunction(l**2).diff(l) If I put cos, sin or exp, as an input then I have an output of: -2*l*sin(l**2) What If I want to make the input function more general, say a polynomial or a complex function, or maybe a function of combined cos, sin and exp ??? mirk If you need good support for symbolic funcions, you should consider using sage . It is a preprocessor for

Trigonometric identities

ぐ巨炮叔叔 提交于 2019-12-06 02:09:23
问题 I have an expression which has both sines and cosines and would like to write it using only sines (or cosines), possibly using the power-reduction formula. I tried to use SymPy but I cannot make it to "rewrite" to the desired output: angle = symbols('angle') print (sin(angle)**2).rewrite(sin, cos) # (1 - cos(2*angle))/2 print ((1 - cos(2*angle))/2).rewrite(cos, sin) # sin(angle)**2 Is there any way to tell Sympy to rewrite such expression using only sines (or cosines)? 回答1: The sympy.simplify