1.0 Tipuri numerice - Numeric Types
1.3 Numere complexe --------------- Complex Numbers
- În acest capitol veți studia:
- Număr complex
- Numere complexe literale
- Constructorul complex()
- Metodele numerelor complexe
- Funcția complex() încorporată
- In this chapter you will study:
- Complex Number(complex)
- Complex number literals
- Complex() constructor
- Complex number methods
- The Built-in complex() function
Număr complex - Complex Number
Python oferă suport nativ pentru numere complexe, care sunt reprezentate sub forma a + bj, unde a este partea reală, b este partea imaginară, iar j (sau J) unitate imaginară (echivalent cu i în matematică, j = √-1, unde j2 = -1).
Python provides native support for complex numbers, which are represented in the form a + bj, where a is the real part, b is the imaginary part, and j (or J) represents the imaginary unit (equivalent to i in mathematics, j = √-1, where j2 = -1).
Exemplu: (1 + 2j), (-3 - 4j)
Numere complexe literale - Complex number literals
În Python, literalii numerelor complexe sunt o modalitate directă de a reprezenta numerele complexe, care constau dintr-o parte reală și o parte imaginară.
In Python, complex number literals are a direct way to represent complex numbers, which consist of a real part and an imaginary part.
x = 3 + 4j
x2 = -1 + 2J # Utilizarea majusculei J este, de asemenea, validă
print(f"Partea reală: {x.real}") # Accesează partea reală
print(f"Partea Imaginară: {x.imag}") # Accesează partea imaginară
print(type(x)) ==> <class 'complex'>
print(f"Partea reală: {x2.real}") # Accesează partea reală
print(f"Partea Imaginară: {x2.imag}") # Accesează partea imaginară
print(type(x2)) ==> <class 'complex'>
Rețineți că nu trebuie să existe spațiu între coeficientul imaginar și j (sau J).
Accesarea părților: Odată ce un număr complex este creat, părțile sale reale și imaginare pot fi accesate
folosind atributele .real și .imag.
Note that there should be no space between the imaginary coefficient and the j (or J).
Accessing Parts: Once a complex number is created, its real and imaginary parts can be accessed using the
.real and .imag attributes.
Tip: Tipul unui literal complex în Python este complex.
Type: The type of a complex literal in Python is complex.
Constructorul complex() - Complex() constructor
Constructorul complex() din Python este o funcție încorporată folosită pentru a crea numere complexe. Poate fi utilizată în mai multe moduri: cu părți reale și imaginare.
Doar cu partea reală. Dacă este furnizat un singur argument, acesta este tratat ca parte reală, iar partea imaginară este implicit zero.
Dintr-un șir de caractere. Puteți converti o reprezentare sub formă de șir de caractere a unui număr complex într-un număr complex. Șirul de caractere trebuie să aibă formatul "a+bj" sau "a-bj".
Fără argumente. Dacă nu sunt furnizate argumente, returnează un număr complex cu părți atât reale, cât și imaginare, egale cu zero (0j).
The complex() constructor in Python is a built-in function used to create complex numbers. It can be used in several ways: with real and imaginary parts.
With only the real part. If only one argument is provided, it is treated as the real part, and the imaginary part defaults to zero.
From a string. You can convert a string representation of a complex number into a complex number. The string must be in the format "a+bj" or "a-bj".
Without any arguments. If no arguments are provided, it returns a complex number with both real and imaginary parts as zero (0j).
număr7 = complex(4, -7) # Creează un număr complex 4 - 7j
număr8 = complex(10) # Creează un număr complex 10 + 0j (partea imaginară are implicit valoarea 0)
număr9 = complex("1+1j") # Creează un număr complex dintr-un șir de caractere
număr10 = complex() # Creează numărul complex 0j
Metodele numerelor complexe - Complex number methods
Tipul complex încorporat în Python oferă diverse metode și atribute pentru lucrul cu numere complexe. În plus, modulul cmath oferă funcții matematice specializate pentru numere complexe.
Python's built-in complex type provides various methods and attributes for working with complex numbers. Additionally, the cmath module offers specialized mathematical functions for complex numbers.
Magnitudine (valoare absolută)
Magnitude (Absolute Value)
abs(z):
Returnează magnitudinea (modulul) numărului complex.
Returns the magnitude (modulus) of the complex number.
abs(3 + 2j) # sqrt(32 + 22) = sqrt(13) ==> 3.605551275463989
z.conjugate():
Returnează conjugatul complex al numărului.
Returns the complex conjugate of the number.
(3 + 2j).conjugate() ==> (3-2j)
Operații aritmetice - Arithmetic Operations
x1 = 2 + 3j
x2 = 1 - 4j
print(f"Adunare: {x1} + {x2} = {x1 + x2}") ==> (3-1j)
print(f"Scădere: {x1} - {x2} = {x1 - x2}") ==> (1+7j)
print(f"Înmulțire: {x1} * {x2} = {x1 * x2}") ==> (14-5j)
print(f"Împărțire: {x1} / {x2} = {x1 / x2}") ==> (-0.5882352941176471+0.6470588235294118j)
print(f"Ridicare la putere: {x1} ** {x2} = {x1 ** x2}") ==> (-98.43395342485856+155.17660426903427j)
funcțiile modulului cmath - cmath module functions
Modulul cmath oferă funcții pentru operații matematice avansate cu numere complexe, inclusiv:
- Conversie polară/dreptunghiulară:
- cmath.polar(z): Returnează coordonatele polare (magnitudine, fază) ale lui z.
- cmath.rect(r, phi): Returnează un număr complex din coordonate polare (magnitudine r, fază phi).
- Fază/Argument:
- cmath.phase(z): Returnează faza (argumentul) lui z.
- Funcții exponențiale și logaritmice:
- cmath.exp(z): Returnează e ridicat la puterea lui z.
- cmath.log(z[, base]): Returnează logaritmul lui z la baza dată (logaritm natural dacă nu este specificată nicio bază).
- Funcții trigonometrice:
- cmath.sin(z), cmath.cos(z), cmath.tan(z)
- Funcții trigonometrice inverse: cmath.asin(z), cmath.acos(z), cmath.atan(z)
- Funcții hiperbolice:
- cmath.sinh(z), cmath.cosh(z), cmath.tanh(z)
- Funcții hiperbolice inverse: cmath.asinh(z), cmath.acosh(z), cmath.atanh(z)
- Rădăcină pătrată:
- cmath.sqrt(z): Returnează rădăcina pătrată a lui z.
The cmath module provides functions for advanced mathematical operations on complex numbers, including:
- Polar/Rectangular Conversion:
- cmath.polar(z): Returns the polar coordinates (magnitude, phase) of z.
- cmath.rect(r, phi): Returns a complex number from polar coordinates (magnitude r, phase phi).
- Phase/Argument:
- cmath.phase(z): Returns the phase (argument) of z.
- Exponential and Logarithmic Functions:
- cmath.exp(z): Returns e raised to the power of z.
- cmath.log(z[, base]): Returns the logarithm of z to the given base (natural logarithm if no base is specified).
- Trigonometric Functions:
- cmath.sin(z), cmath.cos(z), cmath.tan(z)
- Inverse trigonometric functions: cmath.asin(z), cmath.acos(z), cmath.atan(z)
- Hyperbolic Functions:
- cmath.sinh(z), cmath.cosh(z), cmath.tanh(z)
- Inverse hyperbolic functions: cmath.asinh(z), cmath.acosh(z), cmath.atanh(z)
- Square Root:
- cmath.sqrt(z): Returns the square root of z.
import cmath
# Crearea unui număr complex
z = complex(3, 4)
# Accesarea atributelor
print(f"Partea reală a numarului complex {z}: {z.real}") ==> 3.0
print(f"Partea imaginară a numarului complex {z}: {z.imag}") ==> 4.0
# Magnitudine și conjugat
print(f"Magnitudinea numarului complex {z}: {abs(z)}") ==> 5.0
print(f"Conjugatul numarului complex {z}: {z.conjugate()}") ==> (3-4j)
# Utilizarea modulului cmath
coordonate_polare = cmath.polar(z)
print(f"Coordonate polare (magnitudine, fază): {coordonate_polare}") ==> (5.0, 0.9272952180016122)
fază = cmath.phase(z) # Returnează unghiul de fază în radiani
print(phase) ==> 0.9272952180016122
valoare_exponenţială = cmath.exp(z)
print(f"e^z: {valoare_exponenţială}") ==> (-13.128783081462158-15.200784463067954j)
- Considerații importante:
- Fără operatori de comparație: Numerele complexe nu acceptă operatori de comparație precum <, >, <= sau >= deoarece nu există o relație de ordine definită pentru acestea.
- Combinarea math și cmath: Modulul matematic este strict pentru numere reale. Dacă încercați să utilizați o funcție din math cu un număr complex care produce un rezultat complex (de exemplu, math.sqrt(-1)), va fi generată o eroare ValueError. Utilizați întotdeauna modulul cmath pentru operațiile cu numere complexe.
- Important considerations:
- No comparison operators: Complex numbers do not support comparison operators like <, >, <=, or >= because there is no defined ordering relation for them.
- Mixing math and cmath: The math module is strictly for real numbers. If you attempt to use a function from math with a complex number that produces a complex result (e.g., math.sqrt(-1)), a ValueError will be raised. Always use the cmath module for complex number operations.
Funcția complex() încorporată - The Built-in complex() function
- Funcția acceptă până la două argumente.
- real: Partea reală a numărului complex. Poate fi orice tip numeric (inclusiv un alt număr complex) sau un șir de caractere. Dacă acest argument este omis, implicit va fi 0.
- imag (opțional): Partea imaginară a numărului complex. Trebuie să fie un tip numeric. Dacă este omis, implicit va fi 0.
- The function accepts up to two arguments.
- real: The real part of the complex number. It can be any numeric type (including another complex number) or a string. If this argument is omitted, it defaults to 0.
- imag (optional): The imaginary part of the complex number. It must be a numeric type. If omitted, it defaults to 0.
Funcția complex() încorporată în Python servește la crearea de numere complexe sau la convertirea altor tipuri de date în numere complexe. Un număr complex este reprezentat sub forma x + iy, unde x este partea reală, y este partea imaginară, iar i (sau j în Python) este unitatea imaginară care satisface i2 = -1.
The built-in complex() function in Python serves to create complex numbers or convert other data types into complex numbers. A complex number is represented in the form x + iy, where x is the real part, y is the imaginary part, and i (or j in Python) is the imaginary unit satisfying i2 = -1.
Crearea unui număr complex cu părți reale și imaginare.
Creating a complex number with real and imaginary parts.
număr_complex = complex(7)
print(număr_complex) ==> (7+0j)
Conversia unui șir de caractere într-un număr complex.
Converting a string to a complex number.
Funcția complex() poate analiza, de asemenea, o reprezentare de tip șir de caractere a unui număr complex, cu condiția ca aceasta să fie în formatul corect (de exemplu, "2+3j" sau "2-3j"). Șirul de caractere nu trebuie să conțină spații albe în jurul operatorului + sau -.
The complex() function can also parse a string representation of a complex number, provided it is in the correct format (e.g., "2+3j" or "2-3j"). The string must not contain whitespace around the central + or - operator.
număr_complex = complex("1+2j")
print(număr_complex) ==> (1+2j)
- Caracteristici cheie:
- Returnează un obiect de tip complex.
- Părțile real și imag pot fi accesate folosind atributele .real și .imag ale obiectului număr complex.
- Acceptă operații aritmetice cu alte numere complexe, numere întregi și numere cu virgulă mobilă.
- La conversia dintr-un șir de caractere, partea imaginară este notată cu "j" (de exemplu, 2j).
- Dacă ambele argumente sunt omise (de exemplu, complex()), returnează 0j.
- Key Features:
- Returns an object of the complex type.
- The real and imag parts can be accessed using the .real and .imag attributes of the complex number object.
- Supports arithmetic operations with other complex numbers, integers, and floats.
- When converting from a string, the imaginary part is denoted by 'j' (e.g., 2j).
- If both arguments are omitted (i.e., complex()), it returns 0j.
Click CSS pentru style sheet
Click JavaScript pentru style sheet