0

For a given matrix A with eigevalues eigval and eigevectors eigvec, here's what I want to do:

  1. Find the eigenvalues with multiplicity > 1
  2. Find the corresponding eigenvectors. Perform some calculations on these eigenvectors
  3. Create eigvec with these new vectors replacing the old ones

Most importantly, if there are say 2 degenerate sets of eigenvalues with multiplicty 3 each, I want the two sets of eigenvectors to be treated separately. Here's what I have tried so far:

import numpy as np
import numpy.linalg as linalg
A = [[1,0,0], [0, 1, 0], [6, 7, 3]]
eigval, eigvec = linalg.eig(A)
idx = eigval.argsort()[::-1]
eigval = eigval[idx]
eigvec = eigvec[:,idx]

unique, counts,indices = np.unique(eigval,return_counts=True,return_index=True)

uni_count = dict(zip(counts, indices))

qr_counter = []

for key,val in uni_count.items():
    if val > 1:
        qr_counter.append(key)

print('For the dict: the key is the index, the value is mulplicity')

non_unique_eigvec = np.empty((len(qr_counter),), dtype=object)

for int in qr_counter:
    np.concatenate(non_unique_eigvec, eigvec[int])

Now the problem is that eigvec are np arrays and I don't know how to work with modifying that.

Any help would be appreciated! Thanks!

thorium
  • 187
  • 8
  • Can you explain how exactly you desire to "modify" the eigenvectors? You are just looking to multiply them by a constant? In general numpy arrays are designed to be operated on immediately as if it were a single value, and said operation will be preformed element-wise. Though it does depend on the exact structure of the array. Explain a bit further and I can probably write an answer. Edit: as far as I can see your code looks fine so far. Is there somewhere it is crashing here? – pretzlstyle Sep 21 '17 at 21:44
  • No I actually want to QR factorize the eigenvectors. For degenerate eigenvalues the eigenvectors aren't orthogonal and I'm trying to brute-force fix it (any other ideas appreciated!). I am getting a TypeError: TypeError: only integer scalar arrays can be converted to a scalar index – thorium Sep 21 '17 at 21:55
  • I am very familiar with this error; can we see the line(s) of code that are causing it, if not above? – pretzlstyle Sep 21 '17 at 22:04
  • No this is everything, the concatenation is causing the error. – thorium Sep 21 '17 at 22:07
  • I'm 99% sure I know what's wrong, but I'd like to reproduce this first, and this code is incomplete (`A` is not defined). Can you add that? – pretzlstyle Sep 21 '17 at 22:10

1 Answers1

1

The solution to your error is very simple. If you look at the documentation for numpy.concatenate(), you'll see that the input parameters are:

1.) a sequence of the arrays to be concatenated

2.) the axis along which to be joined

so the arrays to be concatenated must simply be placed into a sequence and passed to concatenate() as a single object. So,

np.concatenate( (non_unique_eigvec, eigvec[int]) )

rather than

np.concatenate(non_unique_eigvec, eigvec[int]).

You were passing an array (eigvec[int]) in place of a parameter than is intended to be an integer (the second parameter - the axis).

Although, once fixed, this produces the array

array([None, 0.0, 0.274721127897378, 0.0], dtype=object)

which I doubt is what you want?

Finally, you should change the name of the variable int in your lower for loop, since int is already an important keyword in Python

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40