Skip to content

Commit 2ccf6e9

Browse files
Ananya44444prakhar14-op
authored andcommitted
Added dot.md (#7837)
* added dot.md * Enhance documentation for ndarray.dot() method Updated the dot product method documentation to clarify its behavior for different dimensional arrays and improved the syntax and example sections. * Update dot.md with return value information Added return value details for the dot product operation. * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt --------- Signed-off-by: Prakhar Sharma <prakharsharma_mc24b06_001@dtu.ac.in>
1 parent 5e334a6 commit 2ccf6e9

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: byteswap()
3+
description: "Converts the endianness of an array's data items, optionally in place."
4+
keywords:
5+
- numpy
6+
- ndarray
7+
- byteswap
8+
- endianness
9+
- byte order
10+
type: "function"
11+
---
12+
13+
## Description
14+
15+
The **`.byteswap()`** method reverses the byte order (or **endianness**) of the array’s data elements. This process essentially swaps the position of bytes within each multi-byte data item (like integers, floats, or complex numbers).
16+
17+
This feature is primarily used when data must be moved between computer systems that use different byte ordering conventions:
18+
19+
* **Little-Endian (LE):** Stores the least significant byte first (e.g., Intel/AMD).
20+
* **Big-Endian (BE):** Stores the most significant byte first (e.g., older network protocols).
21+
22+
The operation is performed **in place** if the `inplace` parameter is set to `True`, modifying the original array.
23+
24+
---
25+
26+
## Syntax
27+
28+
```python
29+
ndarray.byteswap(inplace=False)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
Title: '.dot()'
3+
Description: 'Computes the dot product of two arrays.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Arrays'
9+
- 'Linear Algebra'
10+
- 'Methods'
11+
- 'NumPy'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`.dot()`** method computes the dot product of an array with another array or scalar. For one-dimensional arrays, it calculates the standard inner product of vectors. When applied to two-dimensional arrays, it performs matrix multiplication. For arrays with higher dimensions, it executes a sum-product over the last axis of the first array and the second-to-last axis of the second array.
18+
19+
## Syntax
20+
21+
```pseudo
22+
ndarray.dot(b, out=None)
23+
```
24+
25+
**Parameters:**
26+
27+
- `ndarray`: The first array (A) in the dot product operation (A $\cdot$ B).
28+
- `b`: The second array (B) or scalar in the dot product operation.
29+
- `out` (optional): An alternative output array to place the result in. It must have the same shape and buffer length as the expected output, but the type will be cast if necessary.
30+
31+
**Return value:**
32+
33+
Returns the dot product as a scalar, 2-D array, or ndarray, depending on the input dimensions.
34+
35+
## Example
36+
37+
This example shows how to use the `.dot()` method for matrix multiplication between two 2D NumPy arrays, `matrix_a` and `matrix_b`:
38+
39+
```py
40+
# Import NumPy
41+
import numpy as np
42+
43+
# Create the first 2x3 matrix
44+
matrix_a = np.array([[1, 2, 3],
45+
[4, 5, 6]])
46+
47+
# Create the second 3x2 matrix
48+
matrix_b = np.array([[7, 8],
49+
[9, 10],
50+
[11, 12]])
51+
52+
# Use the '.dot()' method for matrix multiplication (2x3 @ 3x2 = 2x2)
53+
result_matrix = matrix_a.dot(matrix_b)
54+
55+
print("Matrix A:")
56+
print(matrix_a)
57+
print("\nMatrix B:")
58+
print(matrix_b)
59+
print("\nResult (A.dot(B)):")
60+
print(result_matrix)
61+
```
62+
63+
The output of the above code will be:
64+
65+
```shell
66+
Matrix A:
67+
[[1 2 3]
68+
[4 5 6]]
69+
70+
Matrix B:
71+
[[ 7 8]
72+
[ 9 10]
73+
[11 12]]
74+
75+
Result (A.dot(B)):
76+
[[ 58 64]
77+
[139 154]]
78+
```
79+
80+
## Codebyte Example
81+
82+
In the following codebyte example, the `.dot()` method is used to calculate the inner product (dot product) of two one-dimensional vectors, `vector_x` and `vector_y`:
83+
84+
```codebyte/python
85+
import numpy as np
86+
87+
# Create two 1-D arrays (vectors)
88+
vector_x = np.array([1, 2, 3])
89+
vector_y = np.array([5, 6, 7])
90+
91+
# Calculate the inner product (dot product)
92+
dot_product = vector_x.dot(vector_y)
93+
94+
print(f"Vector x: {vector_x}")
95+
print(f"Vector y: {vector_y}")
96+
print(f"Dot product (x.dot(y)): {dot_product}")
97+
```
98+
99+
Calculation breakdown:
100+
101+
$$\vec{x} \cdot \vec{y} = (1 \times 5) + (2 \times 6) + (3 \times 7) = 5 + 12 + 21 = 38$$

0 commit comments

Comments
 (0)