-1

I have been trying to insert $e^ix$ as matrix element. The main aim is to find the eigenvalue of a matrix which has many complex functions as elements. Can anyone help me how to insert it? My failed attempt is below:

for i in range(0,size):
                    
            H[i,i]=-2*(cmath.exp((i+1)*aj))
            H[i,i+1]=1.0
            H[i,i-1]=1.0

'a' is defined earlier in the program. The error flagged shows that aj is not defined. Using cmath I thought a complex number can be expontiated as (x+yj). Unfortunately, I couldn't figure out the right way to use it. Any help would be appreciated

7
  • What type is a? If you're trying to use the imaginary component of a you can refer to it as a.imag. Commented Aug 8, 2021 at 3:44
  • What is H, a ? In python 1j is imaginary. 2+4j is complex. A numpy array has to be created with complex elements, or initialized wth complex dtype. Commented Aug 8, 2021 at 3:58
  • numpy.org/doc/stable/reference/generated/numpy.exp.html has a complex example Commented Aug 8, 2021 at 4:05
  • a is array of real elements @sj95126 Commented Aug 8, 2021 at 4:38
  • H is a hamiltonian matrix which is a square matrix of chosen dimension. a values will be picked from an array I define separately. The array consists only real numbers a. Something like $H_i,i=-i*exp(aj)$ where a is to be picked from the array and exponentiated to be an Euler number. @hpaulj Commented Aug 8, 2021 at 4:44

1 Answer 1

0

Define a small float array:

In [214]: H = np.eye(3)
In [215]: H
Out[215]: 
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

Create a complex number:

In [216]: 1+3j
Out[216]: (1+3j)
In [217]: np.exp(1+3j)
Out[217]: (-2.6910786138197937+0.383603953541131j)

Trying to assign it to H:

In [218]: H[1,1]=np.exp(1+3j)
<ipython-input-218-6c0b228d2833>:1: ComplexWarning: Casting complex values to real discards the imaginary part
  H[1,1]=np.exp(1+3j)
In [219]: H
Out[219]: 
array([[ 1.        ,  0.        ,  0.        ],
       [ 0.        , -2.69107861,  0.        ],
       [ 0.        ,  0.        ,  1.        ]])

Now make an complex dtype array:

In [221]: H = np.eye(3).astype( complex)
In [222]: H[1,1]=np.exp(1+3j)
In [223]: H
Out[223]: 
array([[ 1.        +0.j        ,  0.        +0.j        ,
         0.        +0.j        ],
       [ 0.        +0.j        , -2.69107861+0.38360395j,
         0.        +0.j        ],
       [ 0.        +0.j        ,  0.        +0.j        ,
         1.        +0.j        ]])

edit

For an array of values:

In [225]: a = np.array([1,2,3])
In [226]: np.exp(a+1j*a)
Out[226]: 
array([  1.46869394+2.28735529j,  -3.07493232+6.7188497j ,
       -19.88453084+2.83447113j])

In [228]: H[:,0]=np.exp(a+1j*a)
In [229]: H
Out[229]: 
array([[  1.46869394+2.28735529j,   0.        +0.j        ,
          0.        +0.j        ],
       [ -3.07493232+6.7188497j ,  -2.69107861+0.38360395j,
          0.        +0.j        ],
       [-19.88453084+2.83447113j,   0.        +0.j        ,
          1.        +0.j        ]])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @hpaulj. This clears up one part of my problem. However, I am wondering if you could kindly help me to understand how to do the next part. I have a 1D matrix 'a' which has x number of elements. I want to insert each of these x elements in matrix H which is a x*x matrix.
I added an example of setting a column of values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.