GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Numpy automatically casting Complex to Float · Issue #551 · python-control/python-control

Numpy automatically casting Complex to Float #551

New issue
Copy link
New issue
Copy link
Closed
Closed
Numpy automatically casting Complex to Float#551
Copy link

Description

@Bruno-SW
Bruno-SW
opened on Feb 24, 2021
Issue body actions

It's been a while since i've used this awesome work you guys did.
Tried some simple things today and couldn't get it to work

I believe numpy got updated and messed some things up

Given the following code:

import control as ct
import numpy as np

# Define Poles and get it's coeficients
poles = np.array([np.complex(-7.7, 6.4), np.complex(7.7, 6.4)]) # Expecting to get these poles when G.pole() is called
coefs = np.poly(poles)

# Define the Transfer Function
num = [1]
den = coefs
G = ct.tf(num, den)

# Get the poles
G.pole()

i get these warnings:

/usr/local/lib/python3.7/dist-packages/control/xferfcn.py:957: ComplexWarning: Casting complex values to real discards the imaginary part den[j, :maxindex+1] = poly(poles[j])
/usr/local/lib/python3.7/dist-packages/control/xferfcn.py:987: ComplexWarning: Casting complex values to real discards the imaginary part num[i, j, maxindex+1-len(numpoly):maxindex+1] = numpoly array([-10.0124922, 10.0124922])

Doing a few tests i found out that

poles = np.array([np.complex(-7.7, 6.4), np.complex(7.7, 6.4)])

maxindex = 2
den = np.zeros((1, maxindex + 1), dtype=np.float)
den[0, :maxindex + 1] = np.poly(poles)

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: ComplexWarning: Casting complex values to real discards the imaginary part
will results in the same warnings, but changing dtype to np.complex it works correctly

poles = np.array([np.complex(-7.7, 6.4), np.complex(7.7, 6.4)])

maxindex = 2
den = np.zeros((1, maxindex + 1), dtype=np.complex)
den[0, :maxindex + 1] = np.poly(poles)
den

array([ 1. +0.j , 0. -12.8j, -100.25 +0.j ])

I belive changing the num and den dtype to np.complex from xferfcn.py file should solve the issue (did not look up for similar issues/solutions though)

938        den = zeros((self.inputs, maxindex + 1), dtype=np.complex)
939        num = zeros((max(1, self.outputs, self.inputs),
940                     max(1, self.outputs, self.inputs),
941                     maxindex + 1),
942                    dtype=np.complex)

Activity

  1. murrayrm commented on Feb 25, 2021

    @murrayrm
    murrayrm
    on Feb 25, 2021
    Member
    More actions

    It looks like you are trying to put poles at -7.7 + 6.4j and +7.7 + 6.4j, which gives a transfer function with complex coefficients. This isn't yet supported (but see PR #484 for some initial steps).

    If you do complex conjugate poles, everything looks OK:

    import control as ct
    import numpy as np
    
    # Define Poles and get it's coeficients
    poles = np.array([complex(-7.7, 6.4), complex(-7.7, -6.4)]) # Expecting to get these poles when G.pole() is called
    coefs = np.poly(poles)
    
    # Define the Transfer Function
    num = [1]
    den = coefs
    G = ct.tf(num, den)
    
    # Get the poles
    G.pole()
    

    yields

    array([-7.7+6.4j, -7.7-6.4j])
    
  2. murrayrm
    closed this as completedon Feb 25, 2021
  3. Bruno-SW commented on Feb 25, 2021

    @Bruno-SW
    Bruno-SW
    on Feb 25, 2021
    Author
    More actions

    I see... i messed up, thanks for the feedback :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.