آموزش عملیات حسابی در NumPy

3 سال پیش
آموزش عملیات حسابی در NumPy

آموزش عملیات حسابی در NumPy

 

در این درس از مجموعه آموزش برنامه نویسی سایت سورس باران، به آموزش عملیات حسابی در NumPy خواهیم پرداخت.

پیشنهاد ویژه : پکیج آموزش صفر تا صد پایتون

 

آرایه های ورودی برای انجام عملیات حسابی مانند ()add(), subtract(), multiply, و ()divide باید یک شکل باشند یا باید مطابق با قوانین پخش آرایه باشند.

مثال

import numpy as np 
a = np.arange(9, dtype = np.float_).reshape(3,3) 

print 'First array:' 
print a 
print '\n'  

print 'Second array:' 
b = np.array([10,10,10]) 
print b 
print '\n'  

print 'Add the two arrays:' 
print np.add(a,b) 
print '\n'  

print 'Subtract the two arrays:' 
print np.subtract(a,b) 
print '\n'  

print 'Multiply the two arrays:' 
print np.multiply(a,b) 
print '\n'  

print 'Divide the two arrays:' 
print np.divide(a,b)

 

این خروجی زیر را تولید می کند –

First array:
[[ ۰٫ ۱٫ ۲٫]
 [ ۳٫ ۴٫ ۵٫]
 [ ۶٫ ۷٫ ۸٫]]

Second array:
[۱۰ ۱۰ ۱۰]

Add the two arrays:
[[ ۱۰٫ ۱۱٫ ۱۲٫]
 [ ۱۳٫ ۱۴٫ ۱۵٫]
 [ ۱۶٫ ۱۷٫ ۱۸٫]]

Subtract the two arrays:
[[-۱۰٫ -۹٫ -۸٫]
 [ -۷٫ -۶٫ -۵٫]
 [ -۴٫ -۳٫ -۲٫]]

Multiply the two arrays:
[[ ۰٫ ۱۰٫ ۲۰٫]
 [ ۳۰٫ ۴۰٫ ۵۰٫]
 [ ۶۰٫ ۷۰٫ ۸۰٫]]

Divide the two arrays:
[[ ۰٫ ۰٫۱ ۰٫۲]
 [ ۰٫۳ ۰٫۴ ۰٫۵]
 [ ۰٫۶ ۰٫۷ ۰٫۸]]

 

اکنون اجازه دهید درباره برخی دیگر از توابع حسابی مهم موجود در NumPy بحث کنیم.

()numpy.reciprocal 

این تابع متقابل استدلال را از نظر عناصر برمی گرداند. برای عناصر با مقادیر مطلق بزرگتر از ۱ ، نتیجه همیشه ۰ است به دلیل روشی که پایتون از تقسیم عدد صحیح استفاده می کند. برای عدد صحیح ۰ ، اخطار سرریز صادر می شود.

مثال

import numpy as np 
a = np.array([0.25, 1.33, 1, 0, 100]) 

print 'Our array is:' 
print a 
print '\n'  

print 'After applying reciprocal function:' 
print np.reciprocal(a) 
print '\n'  

b = np.array([100], dtype = int) 
print 'The second array is:' 
print b 
print '\n'  

print 'After applying reciprocal function:' 
print np.reciprocal(b)

 

این خروجی زیر را تولید می کند –

Our array is:
[   ۰٫۲۵    ۱٫۳۳    ۱٫      ۰٫    ۱۰۰٫  ]

After applying reciprocal function:
main.py:9: RuntimeWarning: divide by zero encountered in reciprocal
  print np.reciprocal(a)
[ ۴٫         ۰٫۷۵۱۸۷۹۷  ۱٫               inf  0.01     ]

The second array is:
[۱۰۰]

After applying reciprocal function:
[۰]

 

()numpy.power 

این تابع عناصر موجود در آرایه ورودی اول را به عنوان پایه در نظر گرفته و آنها را به توان عنصر مربوطه در آرایه ورودی دوم برمی گرداند.

import numpy as np 
a = np.array([10,100,1000]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying power function:' 
print np.power(a,2) 
print '\n'  

print 'Second array:' 
b = np.array([1,2,3]) 
print b 
print '\n'  

print 'Applying power function again:' 
print np.power(a,b)

 

این خروجی زیر را تولید می کند –

Our array is:
[  ۱۰  ۱۰۰ ۱۰۰۰]

Applying power function:
[    ۱۰۰   ۱۰۰۰۰ ۱۰۰۰۰۰۰]

Second array:
[۱ ۲ ۳]

Applying power function again:
[        ۱۰      ۱۰۰۰۰ ۱۰۰۰۰۰۰۰۰۰]

 

()numpy.mod 

این تابع باقی مانده تقسیم عناصر مربوطه را در آرایه ورودی برمی گرداند. تابع ()numpy.remainder نیز همین نتیجه را ایجاد می کند.

import numpy as np 
a = np.array([10,20,30]) 
b = np.array([3,5,7]) 

print 'First array:' 
print a 
print '\n'  

print 'Second array:' 
print b 
print '\n'  

print 'Applying mod() function:' 
print np.mod(a,b) 
print '\n'  

print 'Applying remainder() function:' 
print np.remainder(a,b)

 

این خروجی زیر را تولید می کند –

First array:                                                                  
[۱۰ ۲۰ ۳۰]

Second array:                                                                 
[۳ ۵ ۷]

Applying mod() function:                                                      
[۱ ۰ ۲]

Applying remainder() function:                                                
[۱ ۰ ۲]

 

از توابع زیر برای انجام عملیات روی آرایه با اعداد مختلط استفاده می شود.

()numpy.real – قسمت واقعی استدلال نوع داده پیچیده را برمی گرداند.

()numpy.imag  – قسمت موهومی استدلال نوع داده پیچیده را برمی گرداند.

()numpy.conj  – مزدوج پیچیده را برمی گرداند، که با تغییر علامت قسمت خیالی بدست می آید.

()numpy.angle  – زاویه استدلال پیچیده را برمی گرداند. این تابع دارای پارامتر درجه است. در صورت درست بودن، زاویه درجه بازگردانده می شود، در غیر این صورت زاویه در رادیان است.

نسخه ی نمایشی

import numpy as np 
a = np.array([-5.6j, 0.2j, 11. , 1+1j]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying real() function:' 
print np.real(a) 
print '\n'  

print 'Applying imag() function:' 
print np.imag(a) 
print '\n'  

print 'Applying conj() function:' 
print np.conj(a) 
print '\n'  

print 'Applying angle() function:' 
print np.angle(a) 
print '\n'  

print 'Applying angle() function again (result in degrees)' 
print np.angle(a, deg = True)

 

این خروجی زیر را تولید می کند –

Our array is:
[ ۰٫-۵٫۶j 0.+0.2j 11.+0.j 1.+1.j ]

Applying real() function:
[ ۰٫ ۰٫ ۱۱٫ ۱٫]

Applying imag() function:
[-۵٫۶ ۰٫۲ ۰٫ ۱٫ ]

Applying conj() function:
[ ۰٫+۵٫۶j 0.-0.2j 11.-0.j 1.-1.j ]

Applying angle() function:
[-۱٫۵۷۰۷۹۶۳۳ ۱٫۵۷۰۷۹۶۳۳ ۰٫ ۰٫۷۸۵۳۹۸۱۶]

Applying angle() function again (result in degrees)
[-۹۰٫ ۹۰٫ ۰٫ ۴۵٫]

 

منبع.

 

 

لیست جلسات قبل آموزش NumPy

  1. آموزش NumPy
  2. معرفی NumPy
  3. آموزش محیط کار NumPy
  4. آموزش شی Ndarray در NumPy
  5. آموزش انواع داده ها در NumPy
  6. آموزش ویژگی های آرایه در NumPy
  7. آموزش روال ایجاد آرایه در NumPy
  8. آموزش ایجاد آرایه از داده های موجود در NumPy
  9. آموزش ایجاد آرایه از محدوده های عددی در NumPy
  10. آموزش شاخص گذاری و برش در NumPy
  11. آموزش شاخص گذاری پیشرفته در NumPy
  12. آموزش Broadcasting در NumPy
  13. آموزش تکرار در یک آرایه در NumPy
  14. آموزش دستکاری آرایه در NumPy
  15. آموزش اپراتورهای دودویی در NumPy
  16. آموزش توابع رشته ای در NumPy
  17. آموزش توابع ریاضی در NumPy
0
برچسب ها :
نویسنده مطلب erfan molaei

دیدگاه شما

بدون دیدگاه