OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

ValueError solving generalized eigenvalue problem with scipy

  • Thread starter Thread starter Defcon97
  • Start date Start date
D

Defcon97

Guest
Hi I am trying to solve a generalized eigenvalue problem (GEVP) on a correlator matrix.

The part of the code that is giving me problem is:

Code:
    import scipy.linalg as la


    [...]




    corr_j = simple_corr(data_jack, mask)

    
    #compute the eigenvalues
    eig_j = np.zeros((nOps, L0, L0))


    for t0 in range(2,L0//2-1):
        for t1 in range(t0+1, L0//2):

            #solve the generalized eigenvalue problem C(t) v = lambda(t) C(t0) v

            C = corr_j[:,:,t1]
            C0 = corr_j[:,:,t0]

            print(f"C = {C.shape}")
            print(f"C = {C}")
            print(f"C0 = {C0.shape}")
            print(f"C0 = {C0}")
            eig_t = la.eig(C, b = C0)

All the print calls are there for debugging purposes. And to show you that the matrices are of the correct shape and are not empty or singular. This piece of code is inside a function called gevp_jack(data,mask)that should perform this GEVP for many different jacknife samples.

When i run the code i get the following output:

Code:
C = (7, 7)
C = [[ 1.59584972 -2.15633651  1.24114058  1.75321492 -1.81777217  1.81941323
  -0.91749864]
 [-2.15633651  2.9590494  -1.68747421 -2.40047725  2.50849557 -2.51730532
   1.25051207]
 [ 1.24114058 -1.68747421  0.98367127  1.37224169 -1.40936567  1.40846551
  -0.72515541]
 [ 1.75321492 -2.40047725  1.37224169  1.9320236  -1.99770539  2.00577185
  -1.01332363]
 [-1.81777217  2.50849557 -1.40936567 -1.99770539  2.17348089 -2.16582443
   1.08016084]
 [ 1.81941323 -2.51730532  1.40846551  2.00577185 -2.16582443  2.15743968
  -1.09213161]
 [-0.91749864  1.25051207 -0.72515541 -1.01332363  1.08016084 -1.09213161
   0.51369853]]
C0 = (7, 7)
C0 = [[ 5.09266872 -6.49228588  4.14126736  5.37497589 -4.90238238  5.82926292
  -3.01593039]
 [-6.49228588  8.46178084 -5.22826411 -6.92532952  6.49513817 -7.44136205
   3.78233514]
 [ 4.14126736 -5.22826411  3.40637298  4.36963407 -3.89903505  4.7350309
  -2.4841818 ]
 [ 5.37497589 -6.92532952  4.36963407  5.68667154 -5.23611244  6.14753923
  -3.16321806]
 [-4.90238238  6.49513817 -3.89903505 -5.23611244  5.28974258 -5.64769213
   2.79528734]
 [ 5.82926292 -7.44136205  4.7350309   6.14753923 -5.64769213  6.65394343
  -3.42858045]
 [-3.01593039  3.78233514 -2.4841818  -3.16321806  2.79528734 -3.42858045
   1.80604997]]
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3526, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/tmp/ipykernel_4321/229272726.py", line 2, in <module>
    eig, err_eig = gu.gevp_jack(data, mask)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/Documents/[...]/gevp_utils.py", line 378, in gevp_jack
    eig_t = np.sort(eig_t)
            ^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in sort
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 1029, in sort
    a = asanyarray(a).copy(order="K")
        ^^^^^^^^^^^^^
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (2, 7) + inhomogeneous part.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 2120, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1435, in structured_traceback
    return FormattedTB.structured_traceback(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1326, in structured_traceback
    return VerboseTB.structured_traceback(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1173, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1088, in format_exception_as_a_whole
    frames.append(self.format_record(record))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 970, in format_record
    frame_info.lines, Colors, self.has_colors, lvals
    ^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 792, in lines
    return self._sd.lines
           ^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
             ^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
                             ^^^^^^^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ^^^^^^^^^^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
           ^^^^^
  File "/home/andrea/anaconda3/lib/python3.11/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

I don't know what i am doing wrong, it seems to refer to an array of inhomogenous size but my array are all normal innocent square matrices
<p>Hi I am trying to solve a generalized eigenvalue problem (GEVP) on a correlator matrix.</p>
<p>The part of the code that is giving me problem is:</p>
<pre><code> import scipy.linalg as la


[...]




corr_j = simple_corr(data_jack, mask)


#compute the eigenvalues
eig_j = np.zeros((nOps, L0, L0))


for t0 in range(2,L0//2-1):
for t1 in range(t0+1, L0//2):

#solve the generalized eigenvalue problem C(t) v = lambda(t) C(t0) v

C = corr_j[:,:,t1]
C0 = corr_j[:,:,t0]

print(f"C = {C.shape}")
print(f"C = {C}")
print(f"C0 = {C0.shape}")
print(f"C0 = {C0}")
eig_t = la.eig(C, b = C0)
</code></pre>
<p>All the print calls are there for debugging purposes. And to show you that the matrices are of the correct shape and are not empty or singular.
This piece of code is inside a function called <code>gevp_jack(data,mask)</code>that should perform this GEVP for many different jacknife samples.</p>
<p>When i run the code i get the following output:</p>
<pre><code>C = (7, 7)
C = [[ 1.59584972 -2.15633651 1.24114058 1.75321492 -1.81777217 1.81941323
-0.91749864]
[-2.15633651 2.9590494 -1.68747421 -2.40047725 2.50849557 -2.51730532
1.25051207]
[ 1.24114058 -1.68747421 0.98367127 1.37224169 -1.40936567 1.40846551
-0.72515541]
[ 1.75321492 -2.40047725 1.37224169 1.9320236 -1.99770539 2.00577185
-1.01332363]
[-1.81777217 2.50849557 -1.40936567 -1.99770539 2.17348089 -2.16582443
1.08016084]
[ 1.81941323 -2.51730532 1.40846551 2.00577185 -2.16582443 2.15743968
-1.09213161]
[-0.91749864 1.25051207 -0.72515541 -1.01332363 1.08016084 -1.09213161
0.51369853]]
C0 = (7, 7)
C0 = [[ 5.09266872 -6.49228588 4.14126736 5.37497589 -4.90238238 5.82926292
-3.01593039]
[-6.49228588 8.46178084 -5.22826411 -6.92532952 6.49513817 -7.44136205
3.78233514]
[ 4.14126736 -5.22826411 3.40637298 4.36963407 -3.89903505 4.7350309
-2.4841818 ]
[ 5.37497589 -6.92532952 4.36963407 5.68667154 -5.23611244 6.14753923
-3.16321806]
[-4.90238238 6.49513817 -3.89903505 -5.23611244 5.28974258 -5.64769213
2.79528734]
[ 5.82926292 -7.44136205 4.7350309 6.14753923 -5.64769213 6.65394343
-3.42858045]
[-3.01593039 3.78233514 -2.4841818 -3.16321806 2.79528734 -3.42858045
1.80604997]]
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3526, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/tmp/ipykernel_4321/229272726.py", line 2, in <module>
eig, err_eig = gu.gevp_jack(data, mask)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/Documents/[...]/gevp_utils.py", line 378, in gevp_jack
eig_t = np.sort(eig_t)
^^^^^^^^^^^^^^
File "<__array_function__ internals>", line 200, in sort
File "/home/andrea/anaconda3/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 1029, in sort
a = asanyarray(a).copy(order="K")
^^^^^^^^^^^^^
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (2, 7) + inhomogeneous part.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 2120, in showtraceback
stb = self.InteractiveTB.structured_traceback(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1435, in structured_traceback
return FormattedTB.structured_traceback(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1326, in structured_traceback
return VerboseTB.structured_traceback(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1173, in structured_traceback
formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1088, in format_exception_as_a_whole
frames.append(self.format_record(record))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 970, in format_record
frame_info.lines, Colors, self.has_colors, lvals
^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/IPython/core/ultratb.py", line 792, in lines
return self._sd.lines
^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/core.py", line 698, in lines
pieces = self.included_pieces
^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/core.py", line 649, in included_pieces
pos = scope_pieces.index(self.executing_piece)
^^^^^^^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
value = obj.__dict__[self.func.__name__] = self.func(obj)
^^^^^^^^^^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/stack_data/core.py", line 628, in executing_piece
return only(
^^^^^
File "/home/andrea/anaconda3/lib/python3.11/site-packages/executing/executing.py", line 164, in only
raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0
</code></pre>
<p>I don't know what i am doing wrong, it seems to refer to an array of inhomogenous size but my array are all normal innocent square matrices</p>
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top