sympy args unexpected answer - sympy

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

Related

How can I write Collatz conjecture by SymPy?

I was going to write a SymPy function which takes a natural number and returns the step number of Collatz conjecture of the arg. The Python version is here:
def collatz(n: int):
step = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
step += 1
return step
print(collatz(27))
It prints 111.
How about SymPy version? I feel sympy.Lambda() should have a recursive feature, similar to recursive call of procedual programming. Is there a good way?
Just calling collatz() with a sympy.Symbol() instance (obviously) went into iloop.
sympy.series.sequences.RecursiveSeq() does only backward reference with constant decrements.
Let f(x) be the symbolic collatz result. Do a substitution followed by a replacement to replace any non-symbolic results:
>>> f=Function('f')
>>> eq = f(x)
>>> eq.subs(x, 270)
f(270)
>>> _.replace(lambda x: x.func == f and x.args[0].is_Integer, lambda x: collatz(x.args[0]))
42
If you don't want to do it like this and want automatic evaluation, then you will have to write a SymPy class deriving from Function that has an eval method that detects when the input is an Integer. You can look at any function to see how this is implemented, e.g. see the source code for cos.

sympy - UnevaluatedExpr and commutative property - keep a float number unevaluated

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

Sympy: Using "assumptions" to evaluate Piecewise, specifically for IndexedBase

I am trying to evaluate a certain expression under consideration of assumption. Specifically my problem is related to indexedBase objects.
See the following code:
from sympy import *
init_printing(use_latex="mathjax")
ntot = symbols("n_tot", integer = True)
i = Idx("i",(1,ntot))
k = Idx("k", (1,ntot))
j = Idx("j",(1,ntot))
x = IndexedBase("x")
As an example let's take the derivative of two summations over x[i].
expr = Sum(Sum(x[i],(i,1,ntot)),(k,1,ntot)).diff(x[j])
(NOTE: this is not possible in the current SymPy version 1.0, it is possible with the development version and will be available in future SymPy stable versions.)
I want to evaluate the expression and get a piecewise answer:
print(expr.doit())
OUTPUT: n_tot*Piecewise((1, And(1 <= j, j <= n_tot)), (0, True))
So my problem is, how can I tell sympy that I know for certain that j is between 1 and ntot. So that my result is 1:
I tried the following but with no luck:
with assuming(j==2):
expr=Sum(Sum(x[i],(i,1,ntot)),(k,1,ntot)).diff(x[j]).doit()
Assumptions on inequalities are a sorely missed feature in SymPy.
Technically the Idx object was created to allow a symbol to contain a definition range, so as to put limits on indexed symbols. Your j already has this information:
In [28]: j.upper
Out[28]: n_tot
In [29]: j.lower
Out[29]: 1
Unfortunately, the inequality class is not meant to handle Idx objects, so its range gets disregared.
You could actually try:
In [32]: simplify(expr.doit()).args[0][0]
Out[32]: n_tot
This manually extracts the first term of the Piecewise expression.
Obviously, the current algorithm needs improvement, it should already tell to Sum that j is within the correct range in order to give 1 as a result.

Calculating the value of arctan(x) in C++

I have to calculate the value of arctan(x) . I have calculated the value of this by evaluating the following series :
Arctan (x) = x – x^3/3 + x^5/5 – x^7/7 + x^9/9 - …
But the following code can not calculate the actual value. For example, calculate_angle(1) returns 38.34 . Why?
const double DEGREES_PER_RADIAN = 57.296;
double calculate_angle(double x)
{
int power=5,count=3;
double term,result,prev_res;
prev_res = x;
result= x-pow(x,3)/3;
while(abs(result-prev_res)<1e-10)
{
term = pow(x,power)/power;
if(count%2==0)
term = term*(-1);
prev_res=result;
result=result+term;
++count;
power+=2;
// if(count=99)
// break;
}
return result*DEGREES_PER_RADIAN;
}
EDIT: I found the culprit. You forgot to include stdlib.h, where the function abs resides. You must have ignored the warning about abs being implicitly declared. I checked that removing the include yields the result 38.19 and including it yields the result ~45.
The compiler is not required to stop compilation when an undeclared function is being used (in this case abs). Instead, it is allowed to make assumptions on how the function is declared (in this case, wrong one.)
Besides, like other posters already stated, your use of abs is inappropriate as it returns an int, not a double or float. The condition in the while should be >1e-100 not <1e-100. The 1e-100 is also too small.
--
You forgot to increase count and power after calculating the first two summands:
prev_res = x;
result= x-pow(x,3)/3;
count = 4; <<<<<<
power = 5; <<<<<<
while(abs(result-prev_res)<1e-100)
{
term = pow(x,power)/power;
if(count%2==1)
term = term*(-1);
Also I consider your use of the count variable counterintuitive: it is intialized with 3 like if it denotes the last used power; but then, loop iterations increase it by 1 instead of 2 and you decide the sign by count%2 == 1 as opposed to power%4 == 3
The series converges to tan^{-1} x, but not very fast. Consider the series when x=1:
1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
What is the error when truncating at the 1/9 term? It's around 1/9. To get 10^{-100} accuracy, you would need to have 10^{100} terms. The universe would end before you'd get that. And, catastrophic round-off error and truncation error would make the answer utterly unreliable. You only have 14 digits to play with for doubles.
Look at reference works like Abramowitz and Stegun [AMS 55] or the new NIST Digital Library of Mathematical Functions at http://dlmf.nist.gov to see how these are done in practice. Often, one uses Padé approximants instead of Taylor series. Even when you stick with Taylor series, you often use Chebyshev approximation to cut down on the total error.
I also recommend Numerical Methods that [Usually] Work, by Forman Acton. Or the Numerical Recipes in ... series.
Your sign is the wrong way around after the first two terms. It should be:
if(count%2==0)
term = term*(-1);
Your comparison is the wrong way around in the while condition. Also, you're expecting an unrealistically high level of precision. I would suggest something more like this:
while(fabs(result-prev_res)>1e-8)
Finally, you'll get a more accurate result with a better value for DEGREES_PER_RADIAN. Why not something like this:
const double DEGREES_PER_RADIAN = 180/M_PI;

Solving a linear equation in one variable

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.