Finding a third vertex of N-dimension equilateral triangle - python-3.x

Given two vectors X and Y, where the number of elements in each is 5. Find a V vector that satisfies :
||X-V||=||Y-V||=||X-Y||
(X,Y,V) are the vertices of an equilateral triangle.
I have tried the following:
To get a vector V that is perpendicular to A and B :
import NumPy as np
# Example vectors
x = [ 0.93937874, 0.05568767, -2.05847484, -1.15965884, -0.34035054]
y = [-0.45921145, -0.55653187, 0.6027685, 0.13113272, -1.2176953 ]
# convert those vectors to a matrix to apply SVD (sure there is a shorter code to do so)
A_list=[]
A_list.append(x)
A_list.append(y)
A=np.array(A_list) # A is a Numpy matrix
u,s,vh=np.linalg.svd(A)
v=vh[-1:1]
From here, what should I do? assuming that what I have done so far is correct

Related

QR method for eigenvectors Python

I am trying to find the eigenvectors of matrix A using QR method. I found the eigenvalues and eigenvector which corresponds to the largest eigenvalue. How do I find the rest of the eigenvectors without using numpy.linalg.eig?
import numpy as np
A = np.array([
[1, 0.3],
[0.45, 1.2]
])
def eig_evec_decomp(A, max_iter=100):
A_k = A
Q_k = np.eye(A.shape[1])
for k in range(max_iter):
Q, R = np.linalg.qr(A_k)
Q_k = Q_k.dot(Q)
A_k = R.dot(Q)
eigenvalues = np.diag(A_k)
eigenvectors = Q_k
return eigenvalues, eigenvectors
evals, evecs = eig_evec_decomp(A)
print(evals)
# array([1.48078866, 0.71921134])
print(evecs)
# array([[ 0.52937334, -0.84838898],
# [ 0.84838898, 0.52937334]])
Next I check the condition:
Ax=wx
Where:
A - Original matrix;
x - eigenvector;
w - eigenvalue.
Check the conditions:
print(np.allclose(A.dot(evecs[:,0]), evals[0] * evecs[:,0]))
# True
print(np.allclose(A.dot(evecs[:,1]), evals[1] * evecs[:,1]))
# False
There is no promise in the algorithm that Q_k will have the eigenvectors as columns. It is even rather rare that there will be an orthogonal eigenbasis. This is so special that this case has a name, these are the normal matrices, characterized in that they commute with their transpose.
In general, the A_k you converge to will still be upper triangular with non-trivial content above the diagonal. Check by computing Q_k.T # A # Q_k. What is known from the structure is that the ith eigenvector is a linear combination of the first k columns of Q_k. This could simplify solving the eigen-vector equation somewhat. Or directly determine the eigenvectors of the converged A_k and transform back with Q_k.

How to find eigenvalues for two matrices

here are my two vectors-:
y1=[2,3,4,5,6,7]
y2=[1,5,3,6,7,8]
when i solve it with pen and paper!
it gives me an ans -: y1= 1.117y2
when i do that in python
import numpy as np
from numpy import linalg as LA
A = np.array([y1,y2])
w, v = LA.eig(A)
print(w)
print(v)
this error occurs LinAlgError: Last 2 dimensions of the array must be square
how can i solve this problem!
please help me , how can i do that!!
The issue here is that eigenvalues can only exist for square matrices, therefore Numpy expects to see an n x n dimensional matrix and not an n x m dimensional matrix such as the 2 x 6 matrix A in your example.

How to generate "triangles" parameter of Mayavi triangular_mesh using triangles which are known vertices coordinates

I have triangles which construct surface of sphere. I want to draw the surface using mayavi triangular_mesh function. The function has "triangles" parameter which is list of triplets (or an array) list the vertices in each triangle. I know vertices coordinate all of triangles. But I don't know how to generate this parameter.
I also tried to generate "triangles" parameter using matplotlib Triangulation function (triangles = matplotlib.tri.Triangulation(longitudes, latitudes).triangles). However, in some places of surface, the triangles are connected incorrectly (can be seen Figure 1).
Figure 1. Incorrect connection of triangles
How to generate "triangles" parameter of Mayavi triangular_mesh using triangles which are known vertices coordinates
Data Format
x, y and z are Cartesian coordinate of triangles. For example; (x[0], y[0], z[0]), (x[1], y[1], z[1]) and (x[2], y[2], z[2]) are vertices of a triangle. Next triangle has (x[3], y[3], z[3]), (x[4], y[4], z[4]) and (x[5], y[5], z[5]) vertices coordinate.
You just need to put it together using the indices.
Take a look at the shape of triangles.
It has three indices per triangle that is used
on x, y and z.
import numpy as np
import mayavi.mlab as mlab
x = np.random.rand(30)
y = np.random.rand(30)
z = np.random.rand(30)
s = np.random.rand(30)
triangles = np.random.randint(0, 30, size=(10, 3))
print(triangles)
mlab.triangular_mesh(x, y, z, triangles, scalars=s)
mlab.orientation_axes()
mlab.show()

KNN algorithm that return 2 or more nearest neighbours

For example, I have a vector x and a is it's nearest neigbour. Then, b is it's next nearest neighbour. Is there any package in Pyton or R that outputs something like [a, b] meaning that a is its nearest neighbour(maybe by majority vote), while b is it's second nearest neighbour.
This is exactly what those metric-trees are build for.
Your question reads as you are asking for something as simple as that using sklearn's KDTree (consider BallTree depending on your metric in play):
import numpy as np
from sklearn.neighbors import KDTree
X = np.array([[1,1],[2,2], [3,3]]) # 3 points in 2 dimensions
tree = KDTree(X)
dist, ind = tree.query([[1.25, 1.35]], k=2)
print(ind) # indices of 2 closest neighbors
print(dist) # distances to 2 closest neighbors
Out:
[[0 1]]
[[ 0.43011626 0.99247166]]
And just to be clear: KNN usually refers to some pre-build algorithm based on metric-trees (KDTree, BallTree) for the task of classification. Often those data-structures are the only thing one is interested in.
Edit
If i interpret your comment correctly, you want to use the manhattan / taxicab / l1 metric.
Look here for the compatibility lists of those spatial-trees.
You just would use it like that:
X = np.array([[1,1],[2,2], [3,3]]) # 3 points in 2 dimensions
tree = KDTree(X, metric='l1') # !!!
dist, ind = tree.query([[1.25, 1.35]], k=2)
print(ind) # indices of 2 closest neighbors
print(dist) # distances to 2 closest neighbors
Out:
[[0 1]]
[[ 0.6 1.4]]

Calculate the normalized dot product of two matrices using Theano

I would like to get the normalized dot product of 2 matrices using Theano. By normalized dot product of 2 matrices, I define the normalized inner product of 2 vectors as the following:
Take v_a from matrix A, and vector v_b from matrix B. AB__dot_norm = v_a * v_b / |v_a| |v_b|.
I can get the norm of v_a and v_b with the following code. I am not sure how to normalize the dot_product matrix with the normalized vectors.
import theano
from theano import tensor
dot_product = tensor.dot(in_tensor, w_tensor)
in_normalized = in_tensor / in_tensor.norm (2, axis = 1).reshape(in_tensor.shape[0],1)
w_normalized = w_tensor / w_tensor.norm (2, axis = 0).reshape(1, w_tensor.shape[1])
I have figured out a solution, I thought it might be useful to post it here.
in_norm = in_tensor / in_tensor.norm(2, axis =1 ).reshape((in_tensor.shape[0],1))
w_norm = w_tensor / w_tensor.norm(2,axis = 0).reshape((1, w_tensor.shape[1]))
output = theano.tensor.dot (in_norm, w_norm)

Resources