Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:38:29+00:00 2026-06-10T06:38:29+00:00

The following code multiplies a part of the array by a number def mul_by_num(a,b):

  • 0

The following code multiplies a part of the array by a number

def mul_by_num(a,b):
    a[0:2] *= b


import numpy as np
a = np.ones(5,dtype=np.float64)
mul_by_num(a,1.0)
mul_by_num(a,1j) #Generates a warning (and casts to float!)

The second call generates a warning

-c:2: ComplexWarning: Casting complex values to real discards the imaginary part

The question is, what is the most pythonic way to multiply parts of numpy arrays by complex/real numbers without messing with dtypes? I do not really want to convert an array to complex from the beginning, but the program in principle can get a complex input.

EDIT:

I do not care about copying the full array, casting it to complex; But I want to avoid checking dtypes (i.e., np.float32, np.float64, np.complex, np.int etc.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:38:31+00:00Added an answer on June 10, 2026 at 6:38 am

    You’re going to need to convert the array to complex at some point, otherwise it won’t be able to hold complex numbers.

    The easiest way to convert an array to complex is to add 0j:

    if (np.iscomplexobj(b)):
        a = a + 0j
    a[0:2] *= b
    

    note: not a += 0j as that will attempt to modify the array inplace, which won’t work if it isn’t complex already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in a view: def controller_details(request, object_id): controller = Controller.objects.get(pk=object_id)
Hi i am using the following code to load a part of page dynamically
The code has a number of following sections: int filter; #ifdef INPUTFILTER_FOO LOG4CXX_DEBUG(log, FOO
I have two array I merge both with following code: $information = array_merge($this->resInfo, $this->resName);
I have the following C++ code snippet (the C++ part is the profiler class
I have the following code to insert multiple products and prices before submitting the
I use the following code to produce multiple boxplots, ranked by the mean value
The following code creates a field with multiple checkboxes. How do I set the
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I have multiple messages in SQS. The following code always returns only one, even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.