Python Forum
additional argument missing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
additional argument missing
#1
I am trying to find the argument of a complex number.
However, the arg function I implemented is giving me the following error:

Error:
runfile('C:/Users/Desktop/python ode/ex88.py', wdir='C:/Users/Desktop/python ode') [4+6i] Traceback (most recent call last): File "<ipython-input-103-2d5582124a5f>", line 1, in <module> runfile('C:/Users/Desktop/python ode/ex88.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/ex88.py", line 67, in <module> print((c+d).arg()) File "C:/Users/Desktop/python ode/ex88.py", line 50, in arg return Complex(np.arctan(b/a)) TypeError: __init__() missing 1 required positional argument: 'imag'
This is my code

from scipy import *
import numpy as np
from numpy import array
from scipy import integrate
import matplotlib.pyplot as plt
import scipy.integrate as si
from scipy.optimize import fsolve
from math import log
import sys

class Complex(object):
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag

    def __add__(self, other):
        return Complex(self.real + other.real,
                       self.imag + other.imag)

    def __sub__(self, other):
        return Complex(self.real - other.real,
                       self.imag - other.imag)

    def __mul__(self, other):
        return Complex(self.real*other.real - self.imag*other.imag,
                       self.imag*other.real + self.real*other.imag)

    def __div__(self, other):
        a,b,c,d = self.real, self.imag, other.real, other.imag # short forms
        r = float(c**2 + d**2)
        return Complex((a*c+b*d)/r, (b*c-a*d)/r)
    
    def real1(self):
        a,b = self.real, self.imag
        return a
        
    def imag1(self):
        a,b=self.real, self.imag
        return b
    
    def arg(self):
         a,b=self.real, self.imag
         return Complex(np.arctan(b/a))
         if a>0 and b>0:
             return Complex(np.arctan(b/a))
         elif a<0 and b>0:
             return Complex(pi - np.arctan(b/a) )
         elif a<0 and b<0:
             return Complex(- pi + np.arctan(b/a))
         elif a>0 and b<0:
             return Complex(-np.arctan(b/a))
        
    
    def __repr__(self):
        return '[{}+{}i]'.format(self.real, self.imag)
    
c=Complex(1,2)
d=Complex(3,4)
print(c+d)
print((c+d).arg())
and if I just add an argument to the return of 'arg' (for example return Complex(np.arctan(b/a),a)
I have the program running, but I don't want to see this additional value. How do I fix this issue?
Reply
#2
The argument of a complex number is a real number, why convert it to Complex? Also the best way to do it is probably numpy.arctan2()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020