Skip to content

Commit 73e4d0f

Browse files
authored
Merge pull request animator#1093 from Yogeshkarma/main
Added the Universal Functions file under NumPy directory
2 parents 55882ba + 7f43373 commit 73e4d0f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

contrib/numpy/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
- [Sorting NumPy Arrays](sorting-array.md)
1212
- [NumPy Array Iteration](array-iteration.md)
1313
- [Concatenation of Arrays](concatenation-of-arrays.md)
14+
- [Universal Functions (Ufunc)](universal-functions.md)

contrib/numpy/universal-functions.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Universal functions (ufunc)
2+
3+
---
4+
5+
A `ufunc`, short for "`universal function`," is a fundamental concept in NumPy, a powerful library for numerical computing in Python. Universal functions are highly optimized, element-wise functions designed to perform operations on data stored in NumPy arrays.
6+
7+
8+
9+
## Uses of Ufuncs in NumPy
10+
11+
Universal functions (ufuncs) in NumPy provide a wide range of functionalities for efficient and powerful numerical computations. Below is a detailed explanation of their uses:
12+
13+
### 1. **Element-wise Operations**
14+
Ufuncs perform operations on each element of the arrays independently.
15+
16+
```python
17+
import numpy as np
18+
19+
A = np.array([1, 2, 3, 4])
20+
B = np.array([5, 6, 7, 8])
21+
22+
# Element-wise addition
23+
np.add(A, B) # Output: array([ 6, 8, 10, 12])
24+
```
25+
26+
### 2. **Broadcasting**
27+
Ufuncs support broadcasting, allowing operations on arrays with different shapes, making it possible to perform operations without explicitly reshaping arrays.
28+
29+
```python
30+
C = np.array([1, 2, 3])
31+
D = np.array([[1], [2], [3]])
32+
33+
# Broadcasting addition
34+
np.add(C, D) # Output: array([[2, 3, 4], [3, 4, 5], [4, 5, 6]])
35+
```
36+
37+
### 3. **Vectorization**
38+
Ufuncs are vectorized, meaning they are implemented in low-level C code, allowing for fast execution and avoiding the overhead of Python loops.
39+
40+
```python
41+
# Vectorized square root
42+
np.sqrt(A) # Output: array([1., 1.41421356, 1.73205081, 2.])
43+
```
44+
45+
### 4. **Type Flexibility**
46+
Ufuncs handle various data types and perform automatic type casting as needed.
47+
48+
```python
49+
E = np.array([1.0, 2.0, 3.0])
50+
F = np.array([4, 5, 6])
51+
52+
# Addition with type casting
53+
np.add(E, F) # Output: array([5., 7., 9.])
54+
```
55+
56+
### 5. **Reduction Operations**
57+
Ufuncs support reduction operations, such as summing all elements of an array or finding the product of all elements.
58+
59+
```python
60+
# Summing all elements
61+
np.add.reduce(A) # Output: 10
62+
63+
# Product of all elements
64+
np.multiply.reduce(A) # Output: 24
65+
```
66+
67+
### 6. **Accumulation Operations**
68+
Ufuncs can perform accumulation operations, which keep a running tally of the computation.
69+
70+
```python
71+
# Cumulative sum
72+
np.add.accumulate(A) # Output: array([ 1, 3, 6, 10])
73+
```
74+
75+
### 7. **Reduceat Operations**
76+
Ufuncs can perform segmented reductions using the `reduceat` method, which applies the ufunc at specified intervals.
77+
78+
```python
79+
G = np.array([0, 1, 2, 3, 4, 5, 6, 7])
80+
indices = [0, 2, 5]
81+
np.add.reduceat(G, indices) # Output: array([ 1, 9, 18])
82+
```
83+
84+
### 8. **Outer Product**
85+
Ufuncs can compute the outer product of two arrays, producing a matrix where each element is the result of applying the ufunc to each pair of elements from the input arrays.
86+
87+
```python
88+
# Outer product
89+
np.multiply.outer([1, 2, 3], [4, 5, 6])
90+
# Output: array([[ 4, 5, 6],
91+
# [ 8, 10, 12],
92+
# [12, 15, 18]])
93+
```
94+
95+
### 9. **Out Parameter**
96+
Ufuncs can use the `out` parameter to store results in a pre-allocated array, saving memory and improving performance.
97+
98+
```python
99+
result = np.empty_like(A)
100+
np.multiply(A, B, out=result) # Output: array([ 5, 12, 21, 32])
101+
```
102+
103+
# Create Your Own Ufunc
104+
105+
You can create custom ufuncs for specific needs using np.frompyfunc or np.vectorize, allowing Python functions to behave like ufuncs.
106+
107+
Here, we are using `frompyfunc()` which takes three argument:
108+
109+
1. function - the name of the function.
110+
2. inputs - the number of input (arrays).
111+
3. outputs - the number of output arrays.
112+
113+
```python
114+
def my_add(x, y):
115+
return x + y
116+
117+
my_add_ufunc = np.frompyfunc(my_add, 2, 1)
118+
my_add_ufunc(A, B) # Output: array([ 6, 8, 10, 12], dtype=object)
119+
```
120+
# Some Common Ufunc are
121+
122+
Here are some commonly used ufuncs in NumPy:
123+
124+
- **Arithmetic**: `np.add`, `np.subtract`, `np.multiply`, `np.divide`
125+
- **Trigonometric**: `np.sin`, `np.cos`, `np.tan`
126+
- **Exponential and Logarithmic**: `np.exp`, `np.log`, `np.log10`
127+
- **Comparison**: `np.maximum`, `np.minimum`, `np.greater`, `np.less`
128+
- **Logical**: `np.logical_and`, `np.logical_or`, `np.logical_not`
129+
130+
For more such Ufunc, address to [Universal functions (ufunc) — NumPy](https://numpy.org/doc/stable/reference/ufuncs.html)

0 commit comments

Comments
 (0)