I'm trying to set up a system for solving these 5 coupled PDEs in FyPi to study the dynamics of electrons and holes in semiconductors
The system of coupled PDEs
I'm struggling with defining the terms highligted in blue as they're products of one variable with gradient of another. For example, I'm able to define the third equation like this without error messages:
eq3 = ImplicitSourceTerm(coeff=1, var=J_n) == ImplicitSourceTerm(coeff=e*mu_n*PowerLawConvectionTerm(var=phi), var=n) + PowerLawConvectionTerm(coeff=mu_n*k*T, var=n)
But I'm not sure if this is a good way. Is there a better way how to define this non-linear term, please?
Also, if I wanted to define a term that would be product of two variables (say p and n), would it be just:
ImplicitSourceTerm(p, var=n)
Or is there a different way?
I am amazed that you don't get an error from passing a PowerLawConvectionTerm as a coefficient of an ImplicitSourceTerm. It's certainly not intended to work. I suspect you would get an error if you attempted to solve().
You should substitute your flux equations into your continuity equations so that you end up with three second-order PDEs for electron drift-diffusion, hole drift-diffusion, and Poisson's equation. It will hopefully then be a bit clearer how to use FiPy Terms to represent the different elements of those equations.
That said, these equations are challenging. Please see this issue and this notebook for some pointers on how to set up and solve these equations, but realize that we provide no examples in our documentation because we haven't been able to come up with anything robust enough. Solving for pseudo-Fermi levels has worked a bit better for me than solving for electron and hole concentrations.
ImplicitSourceTerm(p, var=n) is a reasonable way to represent the n*p recombination term.
I'm teaching myself linear algebra, and I'm trying to learn the corresponding Numpy and Sympy code alongside it.
My book presented the following matrix:
example1 = Matrix([[3,5,-4,0],[-3,-2,4,0],[6,1,-8,0]])
with the instructions to determine if there is a nontrivial solution. The final solution would be x = x3 * Matrix([[4\3],[0],[1]]). (Using Jupyter's math mode, I used the following to represent the solution:)
$$\pmb{x} =
\begin{bmatrix}x_1\\x_2\\x_3\end{bmatrix} =
\begin{bmatrix}\frac{4}{3}x_3\\0\\x_3\end{bmatrix} =
x_3\begin{bmatrix}\frac{4}{3}\\0\\1\end{bmatrix} \\
= x_3\pmb{v} \text{, where }\pmb{v} = \begin{bmatrix}\frac{4}{3}\\0\\1\end{bmatrix}$$
How can I now solve this in Sympy? I've looked through the documentation, but I didn't see anything, and I'm at a bit of a loss. I know that errors tend to be thrown for free variables. Is there a way to determine nontrivial solutions and the corresponding general solution using Sympy, considering that nontrivial solutions are reliant upon free variables? Or is np.linalg generally more preferred for this type of problem?
This is a linear system so
>>> linsolve(Matrix([[3,5,-4,0],[-3,-2,4,0],[6,1,-8,0]]))
FiniteSet((4*tau0/3, 0, tau0))
The tau0 is the free parameter that you refer to as x3.
My problem is defined as below,
minΣ(||xi-Xci||^2+ λ||ci||),
s.t cii = 0,
where X is a matrix of shape d * n and C is of the shape n * n, xi and
ci means a column of X and C separately.
X is known here and based on X we want to find C.
I got several options now, I already have the version in tensorflow, which uses the AdamOptimizer. I am just wondering, is there any way that I can solve this problem more efficiently? Would cvxpy or cvxopt solve this problem better?
I would be much appreciated if any of you could give me an implementation on either of these methods other than tensorflow.
The objective function, I guess, is related to dictionary learning (e.g. your X) and sparse coding (your ci) for which there are several good libraries in Python.
Take a look at scikit-learn's sparse coding and dictionary learning. Alternatively you can use SPAMS for optimisation.
You already know your dictionary so what you need to know is the sparse codes. I think using scikit-learns sparse coders would be the easiest way to go.
If you want to have more power over the optimisation process, you can implement it in Theano (or Keras, Lasagne, TensorFlow) yourself as in this.
I have a system of stochastic differential equations that I would like to solve. I was hoping that this issue was already address. I am a bit concerned about constructing my own solver because I fear my solver would be too slow, and there could be the issues with numerical stability.
Is there a python module for such problems?
If not, is there a standard approach for solving such systems.
There is one: http://diffusion.cgu.edu.tw/ftp/sde/
Example from the site:
""" add required Python packages """
from pysde import *
from sympy import *
""" Variables acclaimed """
x,dx=symbols('x dx')
r,G,e,d=symbols('r G epsilon delta')
""" Solve Kolmogorov Forward Equation """
l=sde.KolmogorovFE_Spdf(r*(G-x),e*x*(1-x),0,1)
sol=l.subs({e:r*d})
pprint(sol)
The link in the accepted answer no longer functions. There is also sdeint:
https://pypi.org/project/sdeint/#description
Which was released a few years after the accepted answer and looks to be in semi-active development. A second example in the documentation has a system of SDE with constant coefficients. I am unsure if they have support for more complex SDE systems.
The package diffeqpy brings Julia's DifferentialEquations.jl to Python. This is capable of doing a lot of things, including stochastic differential equations.
I need to make a linear programming model. Here are the inequalities I'm using (for example):
6x + 4y <= 24
x + 2y <= 6
-x + y <= 1
y <= 2
I need to find the area described by these inequalities, and shade it in a graph, as well as keep track of the vertices of the bounding lines of this area, and draw the bounding line in a different color. See the graph below for an example of what I'm looking for.
.
I'm using Python 3.2, numpy, and matplotlib. Are there better modules for linear programming in Python?
UPDATE: The answer has become somewhat outdated in the past 4 years,
here is an update. You have many options:
If you do not have to do it Python then it is a lot more easier to
do this in a modeling langage, see Any good tools to solve
integer programs on linux?
I personally use Gurobi these
days through its Python API. It is a commercial, closed-source
product but free for academic research.
With PuLP you can create MPS and LP files and then
solve them with GLPK, COIN CLP/CBC, CPLEX, or XPRESS through their
command-line interface. This approach has its advantages and
disadvantages.
The OR-Tools from Google is an open source software suite for optimization, tuned for tackling the world's toughest problems in vehicle routing, flows, integer and linear programming, and constraint programming.
Pyomo is a Python-based, open-source optimization modeling language with a diverse set of optimization capabilities.
SciPy offers linear programming: scipy.optimize.linprog. (I have
never tried this one.)
Apparently, CVXOPT offers a Python interface to GLPK, I did
not know that. I have been using GLPK for 8 years now and I can
highly recommend GLPK. The examples and tutorial of CVXOPT seem really nice!
You can find other possibilites at in the Wikibook under
GLPK/Python. Note that many of these are not necessarily resticted
to GLPK.
I'd recommend the package cvxopt for solving convex optimization problems in Python. A short example with Python code for a linear program is in cvxopt's documentation here.
The other answers have done a good job providing a list of solvers. However, only PuLP has been mentioned as a Python library to formulating LP models.
Another great option is Pyomo. Like PuLP, you can send the problem to any solver and read the solution back into Python. You can also manipulate solver parameters. A classmate and I compared the performance of PuLP and Pyomo back in 2015 and we found Pyomo could generate .LP files for the same problem several times more quickly than PuLP.
The only time a graph is used to solve a linear program is for a homework problem. In all other cases, linear programming problems are solved through matrix linear algebra.
As for Python, while there are some pure-Python libraries, most people use a native library with Python bindings. There is a wide variety of free and commercial libraries for linear programming. For a detailed list, see Linear Programming in Wikipedia or the Linear Programming Software Survey in OR/MS Today.
Disclaimer: I currently work for Gurobi Optimization and formerly worked for ILOG, which provided CPLEX.
For solving the linear programming problem, you can use the scipy.optimize.linprog module in SciPy, which uses the Simplex algorithm.
Here is a graphical representation of the problem, inspired from How to visualize feasible region for linear programming (with arbitrary inequalities) in Numpy/MatplotLib?
import numpy as np
import matplotlib.pyplot as plt
m = np.linspace(0,5,200)
x,y = np.meshgrid(m,m)
plt.imshow(((6*x+4*y<=24)&(x+2*y<=6)&(-x+y<=1)&(y<=2)&(x>=0)&(y>=0)).astype(int),
extent=(x.min(),x.max(),y.min(),y.max()),origin='lower',cmap='Greys',alpha=0.3);
# plot constraints
x = np.linspace(0, 5, 2000)
# 6*x+4*y<=24
y0 = 6-1.5*x
# x+2*y<=6
y1 = 3-0.5*x
# -x+y<=1
y2 = 1+x
# y <= 2
y3 = (x*0) + 2
# x >= 0
y4 = x*0
plt.plot(x, y0, label=r'$6x+4y\leq24$')
plt.plot(x, y1, label=r'$x+2y\leq6$')
plt.plot(x, y2, label=r'$-x+y\leq1$')
plt.plot(x, 2*np.ones_like(x), label=r'$y\leq2$')
plt.plot(x, y4, label=r'$x\geq0$')
plt.plot([0,0],[0,3], label=r'$y\geq0$')
xv = [0,0,1,2,3,4,0]; yv = [0,1,2,2,1.5,0,0]
plt.plot(xv,yv,'ko--',markersize=7,linewidth=2)
for i in range(len(xv)):
plt.text(xv[i]+0.1,yv[i]+0.1,f'({xv[i]},{yv[i]})')
plt.xlim(0,5); plt.ylim(0,3); plt.grid(); plt.tight_layout()
plt.legend(loc=1); plt.xlabel('x'); plt.ylabel('y')
plt.show()
The problem is missing an objective function so any of the shaded points satisfy the inequalities. If it did have an objective function (e.g. Maximize x+y) then many capable Python solvers can handle this problem. Here is a Linear Programming example in GEKKO that also supports mixed integer, nonlinear, and differential constraints.
from gekko import GEKKO
m = GEKKO(remote=False)
x,y = m.Array(m.Var,2,lb=0)
m.Equations([6*x+4*y<=24,x+2*y<=6,-x+y<=1,y<=2])
m.Maximize(x+y)
m.solve(disp=False)
Large scale LP problems are solved in matrix form or in sparse matrix form where only the non-zeros of the matrices are stored. There is a tutorial on LP solutions with a few examples that I developed for a university course.
I would recommend using the PuLP python package. It has a nice interface and you can use differenty types of algorithms to solve LP.
lpsolve is the easiest to me. No need to install separate solver. It comes with in the package.