sympy issue solving a linear system - python

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.

Related

Can the Python module gekko handle complex numbers?

I'm faced with an Hessenberg index-2 DAE, I'm trying to solve it using the python module gekko.
After a few days of trial and error, I think I'm not too far from a code that works. But I've just realized that maybe gekko is not able to handle complex numbers?
Here is a minimal working example:
import numpy as np
from gekko import GEKKO
# Define the simulation and its parameters
g = GEKKO()
g.options.IMODE = 7
g.options.NODES = 1
# define the time array
n_steps = 100
Time = np.linspace(0, 2 * np.pi, n_steps)
g.time = Time
# Initialise the variables
x = g.Var(0.0)
# Write the model's equations
g.Equation(x.dt() == 1.0j * x)
# solve the equations
g.solve(disp = False)
print(x.value)
If I try to run this code, I expect to find the standard complex exponential.
But instead, I get the following error:
File "gekko.py", line 2185, in solve
raise Exception(response)
Exception: #error: Model Expression
*** Error in syntax of function string: Missing operator
Position: 9
$v1-(((1j)*(v1)))
Could you confirm that gekko cannot handle complex numbers? And maybe suggest another python DAE solver that does?
Thank you so much!
Gekko does not natively handle complex numbers. The automatic differentiation and gradient-based solvers haven't been programmed with that in mind. As you discussed in the comments, there are workarounds to solve complex number problems by splitting the variable. There are additional suggestions at Application of complex numbers in Linear Programming?

Why is sympy unable to compute limit of Dirac delta?

I'm using the sympy function limit to compute values of functions (I'm actually only interested in the sign) which allow me to deal with Indeterminate form.
The last function tested lead to compute the limit of a Dirac delta but python never managed to find a value and got stuck on this instruction.
I try on a simplified script and the same issue arise. This is the script:
#!/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
from sympy import *
from sympy import Symbol
x = Symbol('x', real=True)
print(limit(DiracDelta(x), x, -1))
The expected answer being 0 since
lim (x -> -1) δ(x)=0
Can you tell me why python is unable to compute this limit ?
Is there a way to avoid this issue ?

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).

Solving equations involving ceilings

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.

ValueError: Linkage 'Z' uses the same cluster more than once in Python scipy fcluster

I'm getting ValueError: Linkage 'Z' uses the same cluster more than once. when trying to get flat clusters in Python with scipy.cluster.hierarchy.fcluster. This error happens only sometimes, usually only with really big matrices ie 10000x10000.
import scipy.cluster.hierarchy as sch
Z = sch.linkage(d, method="ward")
# some computation here, returning n (usually between 5-30)
clusters = sch.fcluster(Z, t=n, criterion='maxclust')
Why does it happen? How can I avoid it? Unfortunately I couldn't find any useful info by googling...
EDIT Error occurs also when trying to get dendrogram.
No such error appear if method='average' is used.
It seems using fastcluster instead of scipy.cluster.hierarchy solves the problem. In addition, fastcluster implementation is slightly faster than scipy.
For more details have a look at the paper.
import fastcluster
Z = fastcluster.linkage(d, method="ward")
# some computation here, returning n (usually between 5-30)
clusters = fastcluster.fcluster(Z, t=n, criterion='maxclust')

Categories

Resources