Solving equations involving ceilings - python

I would like to solve the equations of the following type in Python. Please let me know if there are any relevant libraries or methods to solve it. ceil(x) denotes the ceiling function.
ceil(x/7)+3*ceil(x/12) = x
This is just a random example, not sure if there is a solution to this.
I tried looking into Sympy library of Python but I can't find a way to use it for my case.
Any help is appreciated. Thanks!

I tried
from sympy import ceiling
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
print(solve(ceiling(x/7) + 3*ceiling(x/12) - x, x))
which ended up in
No algorithms are implemented to solve equation -x + 3*ceiling(x/12) + ceiling(x/7)
The only thing I can think of right now is a graphical solution.

Related

Invalid Syntax error in python, what is the proper way to write f(x)= 6x*(1-x)?

So i'm very new to Python and i'm not used to the syntax yet. This is probably a simple problem but then again, i'm new so I don't know what is the proper way to write this. I'm supposed to integrate f(x)= 6x*(1-x) with intervals [0,1] but I get a syntax error when I write the code. I tried changing the parenthesis but still got the same error. Again, I know this is probably very simple but i'm still trying to get used to Python so help will be very appreciated.
from sympy import Symbol
from sympy import integrate
x=Symbol('x')
print (integrate((6x)*(1-x))
f=((6x)*(1-x))
print(quad(f,0,1))
Changing what you currently have to:
from sympy import Symbol
from sympy import integrate
x = sympy.Symbol('x')
print(sympy.integrate( (6*x)* (1-x) ))
f = ((6*x)*(1-x))
print(sympy.quad(f,0,1))
Fixes your errors. For sympy, you need to write n*y, not ny, and you also had some missing parenthesis.

Python sympy module problems

I met some problems when I using sympy to solve mathematical problems, my code is like this,
eq1=Eq(0, -sympy.sqrt(x**2) + sympy.sqrt((x + 4)**2))
sympy.solve(eq1,x)
the answer for this equation ought to be -2, but I got an empty list instead
eq1=Eq(0, -sympy.sqrt(x**2) + sympy.sqrt((x + 4)**2))
sympy.solveset(eq1,x)
this should work
OUTPUT
{-2}
btw sympy got an exelent website that would have given you the answer, along with a very intuitive shell inside the browser https://docs.sympy.org/latest/tutorial/solvers.html
here is what sympy has to say on the matter:
"Please note that there is another function called solve which can also be used to solve equations. The syntax is solve(equations, variables) However, it is recommended to use solveset instead."

Using div() on multivariate polynomials in Sympy: incorrect remainder?

I want to compute remainders in Python for multivariate polynomials and I found that div() from sympy should do the trick (I also need sympy for Gröbner computations). But the problem I keep finding is that it seems that div() only checks the leading term for division because
q, r = div(x**2 + y, 2*x)
gives r=y, while
q, r = div(x**2 + y, 2*y)
gives r=x**2+y.
I want to do things like Ideal Membership, hence finding the remainder of some polynomial f on division by G={g_1,...,g_s}, where by above I now cannot rely upon div().
Whilst working with Sage I don't get this problem (using (x^2+y)%y gives x^2), but I'm more familier with Python and prefer to do this via Python.
Can someone please tell me if I'm doing something wrong? Or does someone know a better function to use for remainders?
I found a good alternative: reduced(x**2+y, [2*y]) gives the desired ([1/2], x^2).

sympy issue solving a linear system

I am using Python v.3.6 running on the Jupyter QtConsole. I am attempting to do some linear algebra on a dataset using Sympy for a personal project linking predictions with survey scores.
In essence, I set up an augmented matrix, with N = 14 linear equations and M = 5 unknowns, and am trying to solve the system. My problem is that when I use the solve_linear_system command on my augmented matrix, I don't get any output for my code:
import sympy
from sympy import *
from sympy import Matrix, solve_linear_system
from sympy.abc import x, y, z, u, v
system = Matrix(((1,1,-1,0,0,1),(1,1,-1,0,0,2),(0,0,-1,0,-1,3),
(0,0,-1,0,-1,2),(0,0,0,1,0,1),(1,0,1,1,-1,2),(0,0,-1,0,-1,2),(1,0,1,0,0,1),
(1,1,1,0,1,3),(1,1,1,0,0,2),(-1,1,0,0,-1,3),(1,-1,-1,-1,0,2),(-1,1,1,1,-1,3),
(0,-1,0,0,0,2)))
solve_linear_system(system, x, y, z, u, v)
>>
Can someone explain what might be the issue and how to remedy the situation? I have tried other matrices and it seems to work with them, so is there something fundamentally wrong with what I am asking Sympy todo or is it the method?
Thank you.
The reason is there are no solutions to the augmented system in reference.
(probably too many constraints, you could try to relax it by eliminating some of the superfluous equations)
If you stare at your matrix for a little while, you will find that there are incompatible equations, for instance, rows 2 & 3: (0,0,-1,0,-1,3), (0,0,-1,0,-1,2), or rows 0 and 1: (1,1,-1,0,0,1),(1,1,-1,0,0,2). There may also be redundant ones.

differential equations calculating

I need to calculate some phrase as below.
d/dx(sin^2(cos(x)) = 2sin(cos(x))*-sin(x)*cos(cos(x))
it is differential equations calculating.
each phrase can place in d(phrase)/dx.
is this ability in python and if is not, how can I calculate this kind of phrase?
Thanks.
See sympy, a symbolic mathematics library.
As the others have said. Take a look at SymPy. Here is a code snippet to demonstrate the relevant functions.
In [1]: from sympy import *
In [2]: x = Symbol('x')
In [3]: (sin(cos(x))**2).diff(x)
Out[3]: -2⋅sin(x)⋅sin(cos(x))⋅cos(cos(x))

Categories

Resources