How to work out ComplexWarning: Casting complex values to real discards the imaginary part?

2024/9/30 19:35:00

I would like to use a matrix with complex entries to construct a new matrix, but it gives me the warning

"ComplexWarning: Casting complex values to real discards the imaginary part".

As a result, the new matrix's entries are all real numbers. How to make all the entries keep their imaginary part?

This is the image containing my original code

This is the image containing my original code

Answer

I know where I make this warning happen.

I need to add dtype = "complex_", when I construct a new empty matrix.

For example,

mat__ = np.zeros((k*nboxes,k*nboxes),dtype = 'complex_')

If we just write

mat__ = np.zeros((k*nboxes,k*nboxes))

and do some manipulations on that like using a complex matrix to forming a new matrix, then the new matrix will discard the imaginary part.

https://en.xdnf.cn/q/71043.html

Related Q&A

Is it possible to use POD(plain old documentation) with Python?

I was wondering if it is possible to use POD(plain old documentation) with Python? And how should I do it?

ctypes error AttributeError symbol not found, OS X 10.7.5

I have a simple test function on C++:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> #include <wchar.h>char fun() {printf( "%i", 1…

Filtering negative timedeltas

Consider a series holding timedelta64[ns] that measures at the time difference between two events A and B:> time_deltas499900 -1 days +23:45:13 499916 -1 days +23:50:57 499917 00:03:2…

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array.How do I do that in Matlab with a generator like syntax as in Python?

Convert from CMYK to RGB

Im having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. Ive tried reading through the Wand docs, but havent found anythi…

TopologicalError: The operation GEOSIntersection_r could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assemb…

Plot a function during debugging in Python

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function. In Python I can…

getting last n items from queue

everything I see is about lists but this is about events = queue.queue() which is a queue with objects that I want to extract, but how would I go about getting the last N elements from that queue?

Paramiko ValueError p must be exactly 1024, 2048, or 3072 bits long

I am trying to connect SFTP using Python script. Im unable to connect due to "p error".import paramiko client = paramiko.SSHClient() client.load_system_host_keys() client.connect(####.com, us…

WindowsError: [Error 5] Access is denied using urllib2

Im getting a "WindowsError: [Error 5] Access is denied" message when reading a website with urllib2. from urllib2 import urlopen, Request from bs4 import BeautifulSouphdr = {User-Agent: Mozil…