Creating adjacency matrix in python using sympy - python

I want to create an adjacency matrix in python and then proceed to study networks from there on. I wish to do so using sympy as I already developed a code to study eigenvalues and eigenvectors using sympy. However, I already ran into a problem plotting the matrix using matplotlib. So far, this is what I did
#Create a simple adjacency matrix
import sympy as sym
B=sym.Matrix([[0,1,0],[0,0,1],[1,0,0]])
import matplotlib.pyplot as plt
#Define vertices
a=B.row(0)
b=B.row(1)
c=B.row(2)
for i in B:
for j in B:
if B.row(i)[j]==B.row(j)[i] and B.row(i)[j]==1:
plt.plot(B.row(i)[j], B.row(j)[i])
plt.show()
This returns an empty plot. I can more or less see that there is a faulty logic in the for loop but I can't quite put my hand around it much less how to fix it. I tried another approach where I define the vertices as just random points in the xyz space whose only meaning is their connection to one another but I couldn't figure out hot to connect such points using a for loop.
Basically my question is how do I plot an adjacency matrix built through sympy if it is at all possible.

Related

How can I create a non-rectangular wedge domain in Numpy

I am doing some computational fluid dynamics (CFD) simulations for some research, and I have come across a paper that I would like to build upon.
In principle, I am trying to simulate flows and viscosities etc inside a triangular shaped container. Now, some of the cavity-flow and Navier-Stokes equations are quite long. Therefore, some these equations have kindly been publicly written and available in python format here. The code for these equations uses numpy.meshgrid() and numpy.linspace() extensively to produce some rectangular plots in the link. There is nothing wrong with the equations and they are mathematically sound.
However, I would like to replicate these results by simulating them instead inside a triangular container. The plots for these would therefore look like the plots provided on page 28 of this paper. Note here that this is not the rectangular plots with only a triangular subsection plotted, rather the "grid" in this simulation is triangular itself.
My question is whether numpy has a specific feature that would allow for these triangular grids? My evidence of research into this question has led me to scour the documentation regarding non-rectangular arrays, however the closest that I could find was numpy.tril() and numpy.triu(), which still give me rectangular arrays with zeros in the lower and upper triangles of the array respectively. I was wondering if there was any numpy method that allows for the creation of these triangular containers to simulate fluids in.
My last hope would be to create some kind of dictionary, with keys as row numbers, and values as lists which store the column. That way I could create a triangular dictionary. But this would not integrate with the mathematical equations that have written for numpy mentioned previously.
TLDR
How can I use the existing numpy libraries to create triangular grids so that I can have plots that look like this
to then look like this

I’m trying to programme a plot of the 2D Schrödinger equation in python using the finite differences method

I have began to write a programme that calculates solutions to Schrödingers equation in 2D using the finite differences method. I would like to display the solutions graphically using a contour plot or some other graphical display by taking in user input for the dimensions and number of grid points.
I have simplified the Schrödinger equation by setting hbar^2/2m to 1 and setting the potentiel (V) equal to 0 which gives:
-(dψ^2/dx^2 + dψ^2/dy^2) = E*ψ
Using the finite differences method the left hand side of the equation becomes the matrix of the form:
enter image description here
So this now becomes an eigenvalue problem which is the part I’m having trouble implementing.
After using the command np.linalg.eig to get the eigenvalues and eigenvectors I’m unsure of how to code a graphical interpretation of these solutions in 2D. Any help will be much appreciated.
Basically I want to use the eigenvalues and eigenvector to graphically display the solutions I just don’t know which to use and how to code it.
Cheers
You need to be more precise in your question. You say you don't know which to use, eigenvalues or eigenvectors, but that depends on what you want to plot.
Do you want to plot the energies of the quantum mechanical system? Those are represented by the eigenvalues of the Hamiltonian operator.
Do you want to plot the states that you observe your system to be in when you measure the energy? Those are given by the eigenvectors.
After finding the relevant quantity, what is it you want to plot? If it's just the energies, then you can use matplotlib's heatmap tools to show the energy as a function of x and y. If it's the states of definite energy, then you could use some of the vector field tools that matplotlib provides.

How to find the critical points, jacobian matrix and eigen values in python for a set of autonomous equations ? How to get their phase space plot?

I'm new to python. i have a set of autonomous equation, trying to analyse the asymptotic behaviour using phase space analysis.
f(x,y)=a*x*y((y**2)+a+c)
g(x,y)=a+(y**3)+((y**3)+(y**2)(x+a))
where xand y are the variables ? i seek help to find the critical points, jacobian and eigen values, also to get the phase space plot ?
Use the symPy Library.
It has a built in symbolic solver for Jacobians.
You can use the Eq method to solve for your critical points.
Use the eigenvals method to find the eigenvals of your Jacobian.
Lastly you can employ the quiver module from Scipy along with pyplot to plot your phase space plot. Good luck.

Sympy mechanics, plotting vectors and reference frames in 2-space

In summary, I'd like to plot a model I made using sympy.
I'm currently trying to animate the evolution of a bicycle model along a curve in 2-space.
So far I have three matplotlib patches that represent the body and two tires of the bicycle, as well as the dynamic equations created with the sympy mechanics module.
I'm currently able to numerically integrate the equations using KanesMethod, however plotting these objects is a different challenge.
This is what I've come up with so far, and I feel like there has to be a better way to go about all this.
position_vector.subs(...).to_matrix(inertial_reference_frame)
Then I'd have to plot each of the two coordinates over time.
I don't even know where to start with plotting reference frames. I guess I'd somehow turn the reference frame into a set of 3 unit vectors, substitute numerical values, and plot them using matplotlib's quiver command.
Any insight would be greatly appreciated!

How to display a float matrix as elevation values in a 3D plot in Python?

I currently have a heat map which is a 2D float matrix (list of lists of floats to be accurate), and I can display it in 2D with matplotlib fairly easily, but I would like to display it in a 3D plot such that the column and row indices can by the X and Y values respectively, and the values in the matrix are Z (elevation) values. What can I use to do that? I tried using Axes3D but it didn't seem very suitable (or maybe I was using it wrong?). What I am looking to do is conceptually very simple, to pretend the matrix is a DEM and display it as such.
Also if possible I would like to be able to change viewing angles on-the-fly, without having to re-generate the plot.
Any ideas?
These two questions are related but don't quite answer my question:
3d plotting with python
Python: 3D contour from a 2D image - pylab and contourf
NB: The float matrix is rather large, typically 100x100 or more, and the last time I tried to plot it in 3D my system ran out of memory and started thrashing.
Your use case seems like it is tailor made for mayavi/mlab, which has a function that does exactly what you are asking and by default permits interactive 3D rotation:
import numpy as np; from mayavi import mlab
data = np.random.random((100,100))
mlab.surf(data)
mlab.show()

Categories

Resources