কম্পিউটার টিউটোরিয়াল

পাইথনে কমপ্লেক্স নম্বর


একটি কমপ্লেক্স নম্বর রিয়াল নম্বর থেকে তৈরি হয়। পাইথন কমপ্লেক্স নম্বর সরাসরি অ্যাসাইনমেন্ট স্টেটমেন্ট বা complex() ফাংশন ব্যবহার করে তৈরি করা যায়।

কমপ্লেক্স নম্বর সেখানে ব্যবহৃত হয় যেখানে দুটি রিয়াল নম্বর ব্যবহার করা হয়। উদাহরণস্বরূপ, একটি ইলেকট্রিক সার্কিট যা ভোল্টেজ (V) এবং কারেন্ট (C) দ্বারা ডিফাইন করা হয়, জ্যামিতি, সায়েন্টিফিক ক্যালকুলেশন এবং ক্যালকুলাসে ব্যবহৃত হয়।

সিনট্যাক্স

complex([real[, imag]])

পাইথনে একটি সিম্পল কমপ্লেক্স নম্বর তৈরি করা

>>> c = 3 + 6j
>>> print(type(c))

>>> print(c)
(3+6j)
>>>
>>> c1 = complex(3, 6)
>>> print(type(c1))

>>> print(c1)
(3+6j)

উপরের ফলাফল থেকে দেখা যাচ্ছে পাইথন কমপ্লেক্স নম্বর টাইপ complex। প্রতিটি কমপ্লেক্স নম্বর একটি রিয়াল পার্ট এবং একটি ইমেজিনারি পার্ট নিয়ে গঠিত।

পাইথন কমপ্লেক্স নম্বর - অ্যাট্রিবিউট এবং ফাংশন

>>> # Complex Number:
>>> c = (3 + 6j)
>>>
>>> # Real Part of complex number
>>> print('Complex Number: Real Part is = ', c.real)
Complex Number: Real Part is = 3.0
>>>
>>> # Imaginary Part of complex number
>>> print('Complex Number: Imaginary Part is = ', c.imag)
Complex Number: Imaginary Part is = 6.0
>>>
>>> # Conjugate of complex number
>>> print('Complex Number: conjugate Part = ', c.conjugate())
Complex Number: conjugate Part = (3-6j)

কমপ্লেক্স নম্বর উপর ম্যাথম্যাটিক্যাল ক্যালকুলেশন

কমপ্লেক্স নম্বর উপর সহজ ম্যাথম্যাটিক্যাল ক্যালকুলেশন করা যায়:

>>> # first complex number
>>> c1 = 3 + 6j
>>> # Second complex number
>>> c2 = 6 + 15j
>>>
>>> # Addition
>>> print("Addition of two complex number =", c1 + c2)
Addition of two complex number = (9+21j)
>>>
>>> # Subtraction
>>> print("Subtraction of two complex number =", c1 - c2)
Subtraction of two complex number = (-3-9j)
>>>
>>> # Multiplication
>>> print("Multiplication of two complex number =", c1 * c2)
Multiplication of two complex number = (-72+81j)
>>>
>>> # Division
>>> print("Division of two complex number =", c1 / c2)
Division of two complex number = (0.4137931034482759-0.03448275862068964j)

তবে কমপ্লেক্স নম্বর কমপারিজন অপারেটর যেমন <, >, <=, >= সাপোর্ট করে না এবং TypeError মেসেজ দেবে:

>>> c2 <= c2
Traceback (most recent call last):
File "", line 1, in 
c2 <= c2
TypeError: '<=' not supported between instances of 'complex' and 'complex'

পাইথন cmath মডিউল

পাইথন cmath মডিউল কমপ্লেক্স নম্বর জন্য ম্যাথম্যাটিক্যাল ফাংশন অ্যাক্সেস প্রদান করে। ম্যাথ মডিউল ফাংশন ব্যবহার করে কমপ্লেক্স নম্বর의 কিছু গুরুত্বপূর্ণ ফিচার দেখি।

কমপ্লেক্স নম্বারের ফেজ

একটি কমপ্লেক্স নম্বারের ফেজ হলো রিয়াল অক্ষ এবং ইমেজিনারি পার্টকে প্রতিনিধিত্ব করার ভেক্টরের মধ্যবর্তী কোণ।

math এবং cmath মডিউল দ্বারা রিটার্ন করা ফেজ রেডিয়ানে থাকে এবং আমরা numpy.degrees() ফাংশন ব্যবহার করে ডিগ্রিতে কনভার্ট করি।

import cmath, math, numpy
c = 4 + 4j
# phase
phase = cmath.phase(c)
print('4+ 4j Phase =', phase)
print('Phase in Degrees =', numpy.degrees(phase))
print('-4-4j Phase =', cmath.phase(-4-4j), 'radians. Degrees =', numpy.degrees(cmath.phase(-4-4j)))
# we can get phase using math.atan2() function too
print('Complex number phase using math.atan2() =', math.atan2(2, 1))

ফলাফল

4+ 4j Phase = 0.7853981633974483
Phase in Degrees = 45.0
-4-4j Phase = -2.356194490192345 radians. Degrees = -135.0
Complex number phase using math.atan2() = 1.1071487177940904

cmath মডিউল কনস্ট্যান্ট

cmath মডিউলে কিছু কনস্ট্যান্ট রয়েছে যা কমপ্লেক্স নম্বর ক্যালকুলেশনে ব্যবহৃত হয়:

import cmath
print('π =', cmath.pi)
print('e =', cmath.e)
print('tau =', cmath.tau)
print('Positive infinity =', cmath.inf)
print('Positive Complex infinity =', cmath.infj)
print('NaN =', cmath.nan)
print('NaN Complex =', cmath.nanj)

ফলাফল

π = 3.141592653589793
e = 2.718281828459045
tau = 6.283185307179586
Positive infinity = inf
Positive Complex infinity = infj
NaN = nan
NaN Complex = nanj

পাওয়ার এবং লগ ফাংশন

cmath() মডিউল লগারিদমিক এবং পাওয়ার অপারেশনের জন্য কিছু কার্যকর ফাংশন প্রদান করে:

import cmath
c = 1 + 2j
print('e^c =', cmath.exp(c))
print('log2(c) =', cmath.log(c, 2))
print('log10(c) =', cmath.log10(c))
print('sqrt(c) =', cmath.sqrt(c))

ফলাফল

e^c = (-1.1312043837568135+2.4717266720048188j)
log2(c) = (1.1609640474436813+1.5972779646881088j)
log10(c) = (0.3494850021680094+0.480828578784234j)
sqrt(c) = (1.272019649514069+0.7861513777574233j)

ট্রাইগোনোমেট্রিক ফাংশন

import cmath
c = 2 + 4j
print('arc sine value:\n ', cmath.asin(c))
print('arc cosine value:\n', cmath.acos(c))
print('arc tangent value of complex number c:\n', cmath.atan(c))
print('sine value:\n', cmath.sin(c))
print('cosine value:\n', cmath.cos(c))
print('tangent value:\n', cmath.tan(c))

ফলাফল

arc sine value:
 (0.4538702099631225+2.198573027920936j)
arc cosine value:
 (1.1169261168317741-2.198573027920936j)
arc tangent value of complex number c:
 (1.4670482135772953+0.20058661813123432j)
sine value:
 (24.83130584894638-11.356612711218174j)
cosine value:
 (-11.36423470640106-24.814651485634187j)
tangent value:
 (-0.0005079806234700387+1.0004385132020523j)

হাইপারবোলিক ফাংশন

import cmath
c = 2 + 4j
print('Inverse hyperbolic sine value: \n', cmath.asinh(c))
print('Inverse hyperbolic cosine value: \n', cmath.acosh(c))
print('Inverse hyperbolic tangent value: \n', cmath.atanh(c))
print('Hyperbolic sine value: \n', cmath.sinh(c))
print('Hyperbolic cosine value: \n', cmath.cosh(c))
print('Hyperbolic tangent value: \n', cmath.tanh(c))

ফলাফল

Inverse hyperbolic sine value:
 (2.183585216564564+1.096921548830143j)
Inverse hyperbolic cosine value:
 (2.198573027920936+1.1169261168317741j)
Inverse hyperbolic tangent value:
 (0.09641562020299617+1.3715351039616865j)
Hyperbolic sine value:
 (-2.370674169352002-2.8472390868488278j)
Hyperbolic cosine value:
 (-2.4591352139173837-2.744817006792154j)
Hyperbolic tangent value:
 (1.0046823121902348+0.03642336924740368j)

  1. Python-এ কমপ্লেক্স নম্বরে মডুলাস অপারেটর কীভাবে কাজ করে?

  2. পাইথনে কমপ্লেক্স নম্বর কীভাবে তৈরি করবেন?

  3. পাইথনে ইন্টিজার স্ট্রিংকে কমপ্লেক্স নাম্বারে আনপ্যাক কীভাবে করবেন?

  4. পাইথনে কমপ্লেক্স নম্বর (Complex Numbers) ব্যবহার করার পদ্ধতি