sympy

Trigonometric identities

删除回忆录丶 提交于 2019-12-04 06:09:18
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)? unutbu The sympy.simplify.fu module defines a number of transformations based on trig identities: TR0 - simplify expression

How to round Matrix elements in sympy?

本小妞迷上赌 提交于 2019-12-04 04:22:29
问题 As we know from sympy import * x = sin(pi/4) y = sin(pi/5) A = Matrix([x, y]) print(x) print(A.evalf()) displays sqrt(2)/2 Matrix([[0.707106781186548], [0.587785252292473]]) So print(round(x.evalf(), 3)) print(round(y.evalf(), 3)) displays 0.707 0.588 But how can we round all the elements in a Matrix in a terse way, so that print(roundMatrix(A, 3)) can displays Matrix([[0.707], [0.588]]) 回答1: Why you do not use method evalf with args like evalf(3) ? from sympy import * x = sin(pi/4) y = sin

How to parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly?

你离开我真会死。 提交于 2019-12-04 03:54:46
I'd like to split a string like 3cm/µs² + 4e-4 sqmiles/km/h**2 into its SI unit (in this case, m/s**2 ) and its magnitude (in multiples of that unit). Since sympy provides both a parsing module and many physical units and SI prefixes , I guess using sympy would be a good idea. But what is a nice way to achieve this? I'd write an algorithm like the following, but I'd like to avoid reinventing a squared wheel: Treat the transition between a number and a letter (except for the 4e-4 like syntax) and whitespace (unless its next to an explicit operator) as multiplication, then tokenize Replace each

How to simplify logarithm of exponent in sympy?

六月ゝ 毕业季﹏ 提交于 2019-12-03 18:11:44
问题 When I type import sympy as sp x = sp.Symbol('x') sp.simplify(sp.log(sp.exp(x))) I obtain log(e^x) Instead of x . I know that "there are no guarantees" on this function. Question. Is there some specific simplification (through series expansion or whatsoever) to convert logarithm of exponent into identity function? 回答1: You have to set x to real type and your code will work: import sympy as sp x = sp.Symbol('x', real=True) print(sp.simplify(sp.log(sp.exp(x)))) Output: x . For complex x result

Error solving Matrix equation with numpy

﹥>﹥吖頭↗ 提交于 2019-12-03 13:14:06
I'm writing my own Newton-Raphson algorithm in Python using sympy and numpy . The code is below but you can ignore this and skip on to the error: CODE def newtonRhapson(fncList, varz, x0): jacob = [] for fnc in fncList: vec = [] for var in varz: res = fnc.diff(var) for i in range(len(varz)): res = res.subs(varz[i], x0[i]) vec.append(res) jacob.append(numpy.array(vec, dtype='float64')) fx0=[] for fnc in fncList: res2 = fnc for i in range(len(varz)): res2 = res2.subs(varz[i], x0[i]) fx0.append(res2) j = jacob f = fx0 print j print '' print f print numpy.linalg.solve(j,f).tolist() The arguments

Python3 - Sympy: expand products of trig functions

孤者浪人 提交于 2019-12-03 13:10:11
I cannot find a way to have SymPy expand products like cos(a)*cos(b) into sum of trig functions of sum of angles. from sympy import * init_printing() wrf,wlo,t = symbols('\omega_RF \omega_LO t') c = cos(wrf*t)*cos(wlo*t) expand_trig(c) Keeps the product intact. simplify(c) or trigsimp(c) also do not give any alternative form. I would like to have cos(a)*cos(b) to be expanded to 1/2*(cos(a+b) + cos(a-b)) ...any hints? unutbu Per the docstring, help(sympy.fu) , fu will try to minimize the objective function measure . By default this first minimizes the number of trig terms and then minimizes the

How to find all terms in an expression in Sympy

*爱你&永不变心* 提交于 2019-12-03 12:06:17
I have an expression as: 1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2 What I need is a list that contain the terms in this expression. i.e. [1/(x+1), 4*x/(x-1), 3, -4*x**2 , 10*x**2] update : It should not collect like terms. Therefore the list should have -4*x** 2 and 10*x** 2 separately and not 6*x**2 after collecting like terms. Based on the question and comments, if you can get the expression as a string, you can do something like this if you want to avoid term collection. (sympify("1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2", evaluate=False)).args this will return all the terms without collecting like

Solving Systems of Equations with SymPy

只谈情不闲聊 提交于 2019-12-03 10:37:30
I'm trying to solve a set of equations with SymPy 0.7.1: from sympy import * equations = [ Eq(S('vf'), S('vi + a*t')), Eq(S('d'), S('vi*t + 1/2*a*t**2')), Eq(S('a'), S('10')), Eq(S('d'), S('60')), Eq(S('vi'), S('5')) ] print solve(equations) produces the correct result, but in a strange order: [(-4, 10, 60, -35, 5), (3, 10, 60, 35, 5)] How can I identify which value fits which variable? The variable order seems arbitrary. The documentation suggests providing additional arguments: print solve(equations, var('a'), var('d'), var('t'), var('vi'), var('vf')) But this seems to have no effect. Thanks

IPython Notebook Sympy Math Rendering

感情迁移 提交于 2019-12-03 09:00:36
问题 I have just started with using IPython Notebook and have been fascinated by its power. I have been using a few examples available on the net to get started with. I was following this tutorial: http://nbviewer.ipython.org/url/finiterank.com/cuadernos/suavesylocas.ipynb but the maths output is not getting rendered as expected. Below is the my code and the output: In [30]: %load_ext sympyprinting %pylab inline from __future__ import division import sympy as sym from sympy import * init_printing(

Control LaTeX expression color in ipython qtconsole

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 08:18:49
I'm using a dark background for ipython. I would like to have sympy expressions pretty-printed with LaTeX. Problem is the current LaTeX font is black. How can I change LaTeX font colors in the ipython terminal? asmeurer You can set the forecolor and backcolor options in sympy.init_printing . For example, to print the equations in blue, use In [1]: init_printing(forecolor="Blue") 来源: https://stackoverflow.com/questions/18257270/control-latex-expression-color-in-ipython-qtconsole