I have the following expression: Bg = (pi / H)**2 + (2.405 / R)**2. In my computations I would like to keep the float number 2.405 unevaluated, otherwise I end up with long floats numbers scattered all around my expressions.
I thought I could use sympy UnevaluatedExpr to represent that float number. That worked nicely to further develop my expressions. The problem is that expressions containing UnevaluatedExpr are non-commutative, thus I cannot use methods like solve(), factor(), collect(), ... otherwise they throw errors complaining about the non-commutative nature of the expression.
The following code shows that the expression is non-commutative because I used UnevaluatedExpr.
import sympy as sp
R, H = sp.symbols("R, H", real=True, positive=True)
Bg = (sp.pi / H)**2 + (sp.UnevaluatedExpr(2.405) / R)**2
print(Bg)
print(Bg.is_commutative)
>>> 2.405**2/R**2 + pi**2/H**2
>>> False
whereas the following code shows that the expression is commutative, though the float number was evaluated:
Bg = (sp.pi / H)**2 + (2.405 / R)**2
print(Bg)
print(Bg.is_commutative)
>>> 5.784025/R**2 + pi**2/H**2
>>> True
Questions:
Can the UnevaluatedExpr and commutative behaviour be a bug?
What's the best way to deal with float number and prevent their evaluation? I thought about substituting the number with a symbol: that's fine for my simple example but if I work on bigger expressions it could quickly turn to a mess.
Yes, I would consider this to be a bug. I would suggest opening an issue about it https://github.com/sympy/sympy/issues/new.
You can use a Symbol. Another idea would be to use a class that explicitly wraps a float, like
class UnevaluatedFloat(Expr):
def __new__(cls, arg):
return Expr.__new__(cls, Float(arg))
def _eval_evalf(self, prec):
return self.args[0]._eval_evalf(prec)
def _sympystr(self, printer):
return printer.doprint(self.args[0])
This will create a class that gives the float when you call evalf but stays unevaluated otherwise. It also prints as the float. I added the str printer, but you can also define the other printing methods you care about in the same way, like _pretty and _latex and so on. Search for "printmethod" at https://docs.sympy.org/latest/modules/printing.html.
Example
>>> UnevaluatedFloat(1.0) + 1
1 + 1.0
>>> (UnevaluatedFloat(1.0) + 1).evalf()
2.00000000000000
Related
I have been trying to find the coefficients of some lengthy expressions, and even though they are non zero, the result I take is equal to 0.
I think the problem is that I don't get the result to be (s1+s2)/s4 on this mwe:
Sym1,Sym2,Sym3,Sym4 = sy.symbols('s1, s2, s3, s4')
xx = sy.Symbol('x')
TestExp = Sym1*xx + Sym2*xx + Sym3
print(TestExp.coeff(xx))
TestExp2 = (Sym1*xx + Sym2*xx + Sym3)/Sym4
print(TestExp2.coeff(xx))
coeff is pretty literal so if there is no term with xx as a factor then it will return 0. As your expression is, it appears as a fraction, a single term, and there is no factor of xx in that term. (But there is in the numerator.) Try expanding your expression first:
>>> print(TestExp2.expand().coeff(xx))
s1/s4 + s2/s4
And touch it with factor_terms or collect to simplify:
>>> factor_terms(_)
(s1 + s2)/s4
Given an expression, how do I find (after simplifications if needed) if the expression contains the complex number I which is the square root of −1?
In Maple, this is done using the check has(expression,I); see its help page.
In Mathematica, this is done using the check If[FreeQ[expression, Complex], for example: How to check if expression contains a Complex expression?
But I am not sure how to do similar thing in SymPy.
Using expression.is_complex does not return True even if I in the expression. Also since real is subset of complex, this is not a correct test anyway.
I need to check for an explicit I that shows up in the expression anywhere (after simplification).
Here is an example: I am using SymPy 1.5.
from sympy import *
from sympy.abc import z
ex1=(-(((1 + 3*I) + sqrt(2))*cos(z/2)) + ((1 + I) - I*sqrt(2))*sin(z/2))/(((1 + I) + sqrt(2))*cos(z/2) + I*((-1 -I) + sqrt(2))*sin(z/2))
print(ex1.is_complex)
#None
print(simplify(ex1).is_complex)
#None
This is in Maple, for reference:
restart;
result:=(-(((1 + 3*I) + sqrt(2))*cos(z/2)) + ((1 + I) - I*sqrt(2))*sin(z/2))/(((1 + I) + sqrt(2))*cos(z/2) + I*((-1 -I) + sqrt(2))*sin(z/2));
has(simplify(result),I)
Which gives
How to do the above in SymPy?
has checks whether an expression contains some subexpression, such as I:
ex1.has(I) # True
sin(z).has(I) # False
(sin(z)+I).has(I) # True
Note that this does not take into account simplifications that might get rid of the I.
As for checks like is_complex, they consider all possible values of the input variable and return None if there is no clear answer (or if SymPy does not see a relevant simplification). Also, in your case, you want to use is_real (since real numbers are also complex in SymPy’s sense, as you noted). For illustration, consider the following:
z = Symbol("z")
(z+1).is_real # None
(z+I).is_real # None
z = Symbol("z", real=True)
(z+1).is_real # True
(z+I).is_real # False
I the following code
f = lambda x: 2**(x+1)
f_sym = f(sympy.symbol.symbols('x'))
print f_sym.args
The output is (2, x+1).
Why the output is not x?
I have some functions composed with functions like the previous one and the final expressions do not simplify:
f = lambda x, y: (x/y)**.5
f_sym = f(sympy.symbol.symbols('x'), sympy.symbol.symbols('y'))
symbols = f_sym.free_symbols
aux = np.asarray([sympy.derive_by_array(f_sym, symbol) for symbol in [symbols]])
uc_matrix = np.diag(sympy.symbols(','.join(['u_{%s}'%symbol for symbol in symbols])))**2
uf = ((np.dot(np.dot(aux,uc_matrix), aux.T))**.5)[0][0]
y = uf/f_sym
print (y**2).expand().simplify()
In SymPy terms, f_sym is the expression
Pow(Integer(2), Add(Symbol('x'), Integer(1)))
as you can find from srepr(f_sym). So, the function is "raising to power" and the arguments of that function are 2 and x+1.
If you want specifically the exponent, f_sym.args[1] will return that.
It's important to recognize that f_sym is not a function. It does not take any arguments and does not return anything. It's a SymPy expression. There is no special meaning of x+1 within this expression: it's just one of its parts, like the number 2 is.
To answer your follow-up question: the expression will simplify to
u_{x}**2/(4*x**2) + u_{y}**2/(4*y**2)
if you use the rational number sympy.Rational(1, 2) instead of floating-point number .5 in the exponent. Floating point numbers are poison for SymPy. Other ways to achieve the same effect, with less to type:
use sympy.sqrt function instead of raising to power 1/2.
write sympy.S(1)/2 which creates the same Rational object because 1 is turned into a SymPy object prior to division.
Reference: Python numbers vs. SymPy Numbers
I am looking to evaluate the sum an infinite geometric series in SymPy, and want to use the fact that I know the sum has to converge. (Similar to this post: How to Sum with conditions on Sympy?)
My code:
import sympy as sp
from sympy import oo
from sympy.assumptions import assuming, Q
from sympy.assumptions.assume import global_assumptions
x,k = sp.symbols('x k')
#global_assumptions.add(Q.is_true(sp.Abs(x)<1))
with assuming(Q.is_true(sp.Abs(x)<1)):
y = sp.Sum(x**k,(k,0,oo)).doit()
print y
The result is:
Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True))
So it seems the assumption that abs(x)<1 is not taken into account.
Using the global_assumptions (commented out here) does not give the desired result.
Concretely, how do I evaluate the sum such that the result would be 1/(1-x)?
At present, the assumptions made by the assumptions module are not used by the rest of SymPy modules, which makes them less useful than one might hope.
You can sort of fake it by using .subs like this:
y = sp.Sum(x**k, (k,0,oo)).doit().subs(sp.Abs(x) < 1, True)
which returns 1/(-x + 1).
I think this is the best one can do at present. Because this is just a literal substitution of True for a condition, rather than a logical inference, it won't work when the assumption doesn't exactly match a condition in Piecewise:
y = sp.Sum(x**k, (k,0,oo)).doit().subs(sp.Abs(x) < 1/2, True) # alas :(
What would be the most efficient algorithm to solve a linear equation in one variable given as a string input to a function? For example, for input string:
"x + 9 – 2 - 4 + x = – x + 5 – 1 + 3 – x"
The output should be 1.
I am considering using a stack and pushing each string token onto it as I encounter spaces in the string. If the input was in polish notation then it would have been easier to pop numbers off the stack to get to a result, but I am not sure what approach to take here.
It is an interview question.
Solving the linear equation is (I hope) extremely easy for you once you've worked out the coefficients a and b in the equation a * x + b = 0.
So, the difficult part of the problem is parsing the expression and "evaluating" it to find the coefficients. Your example expression is extremely simple, it uses only the operators unary -, binary -, binary +. And =, which you could handle specially.
It is not clear from the question whether the solution should also handle expressions involving binary * and /, or parentheses. I'm wondering whether the interview question is intended:
to make you write some simple code, or
to make you ask what the real scope of the problem is before you write anything.
Both are important skills :-)
It could even be that the question is intended:
to separate those with lots of experience writing parsers (who will solve it as fast as they can write/type) from those with none (who might struggle to solve it at all within a few minutes, at least without some hints).
Anyway, to allow for future more complicated requirements, there are two common approaches to parsing arithmetic expressions: recursive descent or Dijkstra's shunting-yard algorithm. You can look these up, and if you only need the simple expressions in version 1.0 then you can use a simplified form of Dijkstra's algorithm. Then once you've parsed the expression, you need to evaluate it: use values that are linear expressions in x and interpret = as an operator with lowest possible precedence that means "subtract". The result is a linear expression in x that is equal to 0.
If you don't need complicated expressions then you can evaluate that simple example pretty much directly from left-to-right once you've tokenised it[*]:
x
x + 9
// set the "we've found minus sign" bit to negate the first thing that follows
x + 7 // and clear the negative bit
x + 3
2 * x + 3
// set the "we've found the equals sign" bit to negate everything that follows
3 * x + 3
3 * x - 2
3 * x - 1
3 * x - 4
4 * x - 4
Finally, solve a * x + b = 0 as x = - b/a.
[*] example tokenisation code, in Python:
acc = None
for idx, ch in enumerate(input):
if ch in '1234567890':
if acc is None: acc = 0
acc = 10 * acc + int(ch)
continue
if acc != None:
yield acc
acc = None
if ch in '+-=x':
yield ch
elif ch == ' ':
pass
else:
raise ValueError('illegal character "%s" at %d' % (ch, idx))
Alternative example tokenisation code, also in Python, assuming there will always be spaces between tokens as in the example. This leaves token validation to the parser:
return input.split()
ok some simple psuedo code that you could use to solve this problem
function(stinrgToParse){
arrayoftokens = stringToParse.match(RegexMatching);
foreach(arrayoftokens as token)
{
//now step through the tokens and determine what they are
//and store the neccesary information.
}
//Use the above information to do the arithmetic.
//count the number of times a variable appears positive and negative
//do the arithmetic.
//add up the numbers both positive and negative.
//return the result.
}
The first thing is to parse the string, to identify the various tokens (numbers, variables and operators), so that an expression tree can be formed by giving operator proper precedences.
Regular expressions can help, but that's not the only method (grammar parsers like boost::spirit are good too, and you can even run your own: its all a "find and recourse").
The tree can then be manipulated reducing the nodes executing those operation that deals with constants and by grouping variables related operations, executing them accordingly.
This goes on recursively until you remain with a variable related node and a constant node.
At the point the solution is calculated trivially.
They are basically the same principles that leads to the production of an interpreter or a compiler.
Consider:
from operator import add, sub
def ab(expr):
a, b, op = 0, 0, add
for t in expr.split():
if t == '+': op = add
elif t == '-': op = sub
elif t == 'x': a = op(a, 1)
else : b = op(b, int(t))
return a, b
Given an expression like 1 + x - 2 - x... this converts it to a canonical form ax+b and returns a pair of coefficients (a,b).
Now, let's obtain the coefficients from both parts of the equation:
le, ri = equation.split('=')
a1, b1 = ab(le)
a2, b2 = ab(ri)
and finally solve the trivial equation a1*x + b1 = a2*x + b2:
x = (b2 - b1) / (a1 - a2)
Of course, this only solves this particular example, without operator precedence or parentheses. To support the latter you'll need a parser, presumable a recursive descent one, which would be simper to code by hand.