sympy

Rendering coefficients as fractions in sympy latex

≯℡__Kan透↙ 提交于 2021-02-19 09:01:31
问题 I would like to render a fraction multiplied by an integral. I tried: from sympy import * x, h = symbols("x h") (2/h) * Integral(x**2, (x, 0, 1)) The generated latex is: '\\frac{2 \\int\\limits_{0}^{h} x^{2}\\, dx}{h}' which looks wretched: Is it possible to keep the fraction as a coefficient, like so? This can be reproduced easily using sympy live. Thanks for any help. 回答1: You can, as @Stelios pointed out, use an unevaluated Mul to make the expression appear as you want it. To avoid

Sympy: solving differential equation with initial conditions error

落爺英雄遲暮 提交于 2021-02-19 08:09:07
问题 Since Sympy version 1.2, python Sympy has implemented the ability to solve for the constants in a simple differential equation, given some initial conditions. I was trying to test out this feature, but keep getting an error that I don't know how to solve. The documentation indicates the following format for initial conditions, and I tried to follow what was specified in the actual pull request that implemented the feature. Here is the code and the error. import sympy as sp t = sp.symbols('t')

Sympy: How to minimize the number of multiplications in multivariate expressions?

浪尽此生 提交于 2021-02-19 07:43:11
问题 I have a lot of expressions which sympy default writes as sums of products of variables. These expressions can get quite long. I would like to simplify these expressions to have as few as possible multiplications. A reduced example: from sympy import symbols, cse, factor, simplify, count_ops, collect a,b,c=symbols("a b c", integer=True, positive=True) e = a*a*b + a*a + a*b*b + a*b*c + 4*a*b + a*c + 3*a + b*b*c + 4*b*c + 3*c + 1 What I would like to get is something like: (a + b + 3) * (a + c)

OO的奇妙冒险1

我怕爱的太早我们不能终老 提交于 2021-02-17 08:44:42
OO的奇妙冒险 ~OOP入门与字符串处理~ 目录 总体分析 作业内容分析 作业内容总结 互测的收获 公测互测bug分析与总结 不太正经的个人自嗨 总体分析 公测 中测(基础与进阶): 其实在我看来,从完成作业的角度来说,中测的基础与进阶并没有任何区别,都不能挂,都不太难,都对得分没有什么影响。中测的样例总体来说非常善良,只要是测试过,几乎不会被中测阻拦。checkstyle的规则看似很多,但是在IDEA插件的支持下,见到黄色的warning直接改掉,总体来说我认为偏向于养成习惯性的举措,并不是扣分地方所在 强测(正确性): 在第一次作业之前,我十分畏惧强测的正确性,尤其是在经历了计组 手动定点爆破+10万条随机都仅是中测 的说法,但是,就前三次来看,强测的正确性并不严格。第一次作业比较简单,第二次作业我正则表达式根本就没有写对,面对这个十分巨大的问题,强测仅仅挂了我三组,我在发出强测不够强的想法的同时也暗自窃喜,而第三次作业的重点 向面向对象转移 ,对于正确性的检查更加简单,在我连爆sin(- 9),sin(++1),sin(+++1),sin(++x)4个大型bug的情况下强测一组也没挂,互测又禁掉了WF的攻击,不得不说我并不完全认同的强测数据反而救了我一命 强测(性能): 第一次作业比较简单,第二次作业我进行了初步的贪心优化,然而偷鸡不成蚀把米,优化也出现了bug

Printing a polynomial so that the powers of x or s are in ascending or descending order

帅比萌擦擦* 提交于 2021-02-17 04:41:38
问题 I can't find a method or function that will order the powers of the polynomial terms in either ascending or descending order. I am using sympy to calculate some transfer functions using Laplace transforms. I have found only collect() helps combine powers of s but it can't print the powers of s in ascending or descending order. I see print outs like this CLTF(s)=(K*Kd*ω**2*s + K*Kp*ω**2)/(K*Kp*ω**2 + 2*ζ*ω*s**2 + s**3 + s*(K*Kd*ω**2 + ω**2)) The powers of s are not in the right order in the

Printing a polynomial so that the powers of x or s are in ascending or descending order

旧巷老猫 提交于 2021-02-17 04:41:26
问题 I can't find a method or function that will order the powers of the polynomial terms in either ascending or descending order. I am using sympy to calculate some transfer functions using Laplace transforms. I have found only collect() helps combine powers of s but it can't print the powers of s in ascending or descending order. I see print outs like this CLTF(s)=(K*Kd*ω**2*s + K*Kp*ω**2)/(K*Kp*ω**2 + 2*ζ*ω*s**2 + s**3 + s*(K*Kd*ω**2 + ω**2)) The powers of s are not in the right order in the

Cannot differentiate wrt a complicated variable in Sympy

僤鯓⒐⒋嵵緔 提交于 2021-02-16 05:27:38
问题 (I understand the title makes this sound like a duplicate question to Sympy Can't differentiate wrt the variable, but I'm fairly certain it is quite different. I apologize in advance if I am mistaken) I'm trying to solve the Double Pendulum using Hamiltonian Mechanics, but Sympy is having trouble taking one of the derivatives. The code is below, but it's much easier to read the iPython Notebook over at nbviewer.ipython.org from __future__ import division from sympy import * init_session() r_1

error: 'python_ipc_popen2' undefined near line 62 column 15 When running Octave

心已入冬 提交于 2021-02-11 14:45:01
问题 Trying to run Octave on Mac. When I run the diagnose command sympref diagnose . I get that everything is configured fine: Symbolic package diagnostics ============================ Python and SymPy are needed for most features of the Symbolic package. The Python interpreter is currently: "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3". Computers may have more than one Python interpreter installed. If you need to, you can select a different one using the PYTHON environment

How to convert class 'sympy.core' to 'number' or 'float' for optimization?

霸气de小男生 提交于 2021-02-11 14:13:13
问题 I'm a Python initiator and I'd like to solve the following problems, but I don't know what the cause is.I approached the problem using 'fsolve' an optimization tool. First of all, I'm trying to solve a nonlinear equation, but I've approached it in two cases. One case worked out well. But I can't find another case. First case (complete case) from sympy import * from scipy.optimize import fsolve import numpy as np y= symbols('y') b_1, b_2 = symbols ('b_1,b_2') b = 1 f = b_1 + b_2*(y/b)**2 K1 =

Sympy : Exp(-i.H.t)

江枫思渺然 提交于 2021-02-11 14:00:03
问题 I am trying to compute Hamiltonian time evolution in sympy. H is a matrix (Hamiltonian). For example : Matrix([[1, 2], [2, 2]] t is a symbol : t=Symbol('t') How can I easily compute A = exp (-i.H.t) ? I try many things but without success : Calculation of eigenvalues, eigenvectors ... 回答1: Should be able to do something like this: import sympy t = sympy.symbols('t') H = sympy.Matrix([[1, 2], [2, 2]]) A = sympy.exp(-sympy.I * H * t) # sympy.I is imaginary constant A returns a complex sympy