Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C28-L1264-B40DDB73

Home Dashboard
Chapter 26 · Module 2 — Numerical Computing and Linear Algebra

Dot Product and Matrix Multiplication

Priority
  • JobHigh
  • InterviewMedium
  • DevelopmentHigh
3,751
words
21
min read
2
tools

1. Learning Outcomes#

या chapter नंतर तुम्हाला:

  • vector चा dot product नेमका काय करतो ते समजेल
  • element-wise multiplication आणि matrix multiplication यातील फरक स्पष्ट होईल
  • दोन 1D arrays साठी np.dot() वापरता येईल
  • np.matmul() आणि @ operator वापरून matrix multiplication करता येईल
  • दोन matrices multiply होऊ शकतात की नाही हे त्यांच्या shape वरून ठरवता येईल
  • multiplication नंतर result ची shape आधीच predict करता येईल
  • matrix आणि vector यांचे multiplication समजेल
  • np.dot(), np.matmul(), @ आणि * यांचा योग्य उपयोग निवडता येईल
  • shape mismatch errors चे कारण ओळखता येईल
  • matrix multiplication चा result manually आणि NumPy functions वापरून validate करता येईल

2. Multiplication म्हटले की नेहमी एकच operation असते का?#

NumPy मध्ये multiplication म्हटले की दोन वेगवेगळ्या गोष्टी असू शकतात.

पहिली म्हणजे element-wise multiplication.

यात एका array मधील प्रत्येक element दुसऱ्या array मधील त्याच position च्या element सोबत multiply होतो.

दुसरी म्हणजे matrix multiplication.

यात corresponding elements फक्त multiply करून थांबत नाहीत. एका matrix मधील row आणि दुसऱ्या matrix मधील column यांच्यात calculation होते.

ही दोन्ही operations दिसायला multiplication आहेत, पण त्यांचा अर्थ वेगळा आहे.

महत्त्वाचे

NumPy मध्ये * आणि @ हे एकच काम करत नाहीत.

3. Element-Wise Multiplication vs Matrix Multiplication#

सर्वात छोट्या example पासून सुरुवात करू.

आपल्याकडे दोन 1D arrays आहेत:

PYTHON
import numpy as np

a = np.array([1, 2])
b = np.array([3, 4])

print(a * b)
print(a @ b)

Expected Output#

TEXT
[3 8]
11

पहिल्या operation मध्ये:

a * b

NumPy ने corresponding elements multiply केले.

पहिले elements:

1 \times 3 = 3

दुसरे elements:

2 \times 4 = 8

म्हणून result:

[3, 8]

पण a @ b मध्ये वेगळी calculation झाली.

(1 \times 3) + (2 \times 4) = 3 + 8 = 11

म्हणजे multiply झालेल्या values शेवटी add झाल्या.

यालाच या 1D case मध्ये dot product म्हणतो.

Operation Meaning Result
a * b Corresponding elements multiply Array
a @ b Multiply corresponding elements आणि sum Scalar
np.dot(a, b) 1D arrays साठी dot product Scalar
Remember

* म्हणजे element-by-element multiplication. @ म्हणजे matrix multiplication semantics.

4. Dot Product म्हणजे काय?#

दोन समान-length vectors घ्या.

उदाहरण:

a = [1, 2, 3]

b = [4, 5, 6]

Dot product काढताना:

  1. पहिल्या position मधील values multiply करतो
  2. दुसऱ्या position मधील values multiply करतो
  3. तिसऱ्या position मधील values multiply करतो
  4. सर्व products add करतो

General form:

\mathbf{a}\cdot\mathbf{b} = \sum_{i=1}^{n} a_i b_i

या formula मध्ये n म्हणजे vector मधील elements ची संख्या.

आपल्या example साठी:

\begin{aligned} \mathbf{a}\cdot\mathbf{b} &= (1 \times 4) + (2 \times 5) + (3 \times 6) \\ &= 4 + 10 + 18 \\ &= 32 \end{aligned}

हा result array नसून एक single value आहे.

अशा single value ला scalar म्हणतो.

Key Idea

दोन 1D arrays च्या dot product मध्ये corresponding values multiply होतात आणि सर्व products ची बेरीज केली जाते.

5. np.dot() वापरून 1D Dot Product#

NumPy मध्ये हे calculation manually करण्याची गरज नाही.

np.dot() वापरू शकतो.

PYTHON
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

result = np.dot(a, b)

print(result)

Expected Output#

TEXT
32

इथे np.dot(a, b) ने internally हाच calculation केला:

1(4) + 2(5) + 3(6)

आणि result 32 मिळाला.

Dot product तेव्हाच अर्थपूर्ण असतो जेव्हा दोन्ही 1D arrays मध्ये compatible number of elements असतात.

उदाहरणार्थ तीन values असलेला vector आणि दोन values असलेला vector थेट dot product साठी compatible नाहीत.

6. Matrix Multiplication ची खरी कल्पना#

आता 2D arrays म्हणजे matrices घेऊ.

Matrix multiplication मध्ये सर्वात महत्त्वाचा mental model आहे:

Row × Column

पहिल्या matrix मधील एक row घ्या.

दुसऱ्या matrix मधील एक column घ्या.

Corresponding values multiply करा.

Products add करा.

एक result cell मिळेल.

हा process पुन्हा पुन्हा करून पूर्ण result matrix तयार होतो.

Figure 1: Matrix multiplication चे Row × Column mental model
Matrix multiplication चे Row × Column mental modelपहिल्या matrixमधील एक rowनिवडापहिल्या matrix मधील एक row निवडादुसऱ्या matrixमधील एक columnनिवडादुसऱ्या matrix मधील एक column निवडाCorrespondingvaluesmultiply कराCorresponding values multiply करासर्व productsadd करासर्व products add कराResult matrixमधील एक cellमिळवाResult matrix मधील एक cell मिळवा

या flow मधील सर्वात महत्त्वाची गोष्ट म्हणजे row आणि column मधील values ची संख्या समान असली पाहिजे.

उदाहरणार्थ row मध्ये तीन values असतील, तर त्या row सोबत multiply होणाऱ्या column मध्येही तीन values असल्या पाहिजेत.

7. एका Result Cell मागे काय calculation होते?#

समजा पहिल्या matrix मधील row आहे:

[1, 2, 3]

दुसऱ्या matrix मधील column आहे:

[7, 9, 11]

Result cell:

\begin{aligned} c_{11} &= 1(7) + 2(9) + 3(11) \\ &= 7 + 18 + 33 \\ &= 58 \end{aligned}

म्हणून त्या row आणि column च्या intersection वर result 58 येतो.

सामान्य formula असा लिहिता येतो:

c_{ij} = \sum_{k=1}^{n} a_{ik}b_{kj}

Formula मोठा दिसतो, पण त्यामागची कल्पना साधी आहे:

A मधील row घ्या → B मधील column घ्या → dot product काढा.

Remember

पूर्ण matrix multiplication म्हणजे अनेक row-column dot products चे collection आहे.

8. Matrix Dimension Compatibility#

Matrix multiplication सुरू करण्यापूर्वी code लिहिण्याआधी shape check करणे खूप महत्त्वाचे आहे.

समजा:

Matrix A ची shape:

(m,n)

Matrix B ची shape:

(n,p)

मग:

A_{m\times n}B_{n\times p} = C_{m\times p}

मधील दोन dimensions समान असणे आवश्यक आहे.

त्या म्हणजे:

A चे columns = B चे rows

Visual पद्धतीने:

Visual Diagram80%

Rendering diagram…

इथे n आणि n या middle किंवा inner dimensions आहेत.

त्या match झाल्या तर multiplication valid आहे.

Result मध्ये त्या inner dimensions गायब होतात.

Outer dimensions राहतात.

म्हणून:

(m,n) @ (n,p)

चा result:

(m,p)

असतो.

महत्त्वाचे

Matrix multiplication साठी पूर्ण shapes समान असण्याची गरज नाही. फक्त required inner dimensions match झाल्या पाहिजेत.

9. Result Shape कशी Predict करायची?#

समजा:

A.shape = (2, 3)

B.shape = (3, 4)

Inner dimensions:

3 आणि 3

त्या match आहेत.

म्हणून multiplication valid आहे.

Outer dimensions:

2 आणि 4

म्हणून:

result.shape = (2, 4)

सोपे rule: (m, n) @ (n, p) -> (m, p)

वरील line program output नाही; हा shape rule आहे:

(m,n) @ (n,p) \rightarrow (m,p)

हा rule matrix multiplication करताना नेहमी लक्षात ठेवा.

10. दोन 2D Arrays Multiply करणे#

आता actual NumPy example पाहू.

PYTHON
import numpy as np

A = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

B = np.array([
    [7, 8],
    [9, 10],
    [11, 12]
])

result = A @ B

print(result)
print(result.shape)

Expected Output#

TEXT
[[ 58  64]
 [139 154]]
(2, 2)

हे अचानक आलेले numbers नाहीत.

चला visual पद्धतीने पाहू.

Figure 2: A मधील rows आणि B मधील columns वापरून result matrix तयार होते
A मधील rows आणि B मधील columns वापरून result matrix तयार होतेA shape = (2, 3)A shape = (2, 3)c114c225c336@@B shape = (3, 2)B shape = (3, 2)c17911c281012Result shape = (2, 2)Result shape = (2, 2)c158139c264154
c_{11}=1\cdot7+2\cdot9+3\cdot11=58
c_{12}=1\cdot8+2\cdot10+3\cdot12=64

पहिल्या result row मधील 58 कसा आला हे आपण पाहिले.

आता 64:

\begin{aligned} c_{12} &= 1(8) + 2(10) + 3(12) \\ &= 8 + 20 + 36 \\ &= 64 \end{aligned}

दुसऱ्या result row साठी A मधील दुसरा row वापरला जातो.

139:

\begin{aligned} c_{21} &= 4(7) + 5(9) + 6(11) \\ &= 28 + 45 + 66 \\ &= 139 \end{aligned}

आणि 154:

\begin{aligned} c_{22} &= 4(8) + 5(10) + 6(12) \\ &= 32 + 50 + 72 \\ &= 154 \end{aligned}

म्हणून final matrix:

\begin{bmatrix} 58 & 64 \\ 139 & 154 \end{bmatrix}

मिळते.

11. np.matmul()#

NumPy मध्ये matrix multiplication साठी explicit function आहे:

np.matmul()

आपला तोच example:

PYTHON
import numpy as np

A = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

B = np.array([
    [7, 8],
    [9, 10],
    [11, 12]
])

result = np.matmul(A, B)

print(result)

Expected Output#

TEXT
[[ 58  64]
 [139 154]]

2D matrices साठी हे स्पष्टपणे matrix multiplication करते.

जेव्हा code वाचणाऱ्या व्यक्तीला operation चा intention लगेच समजावा अशी गरज असेल, तेव्हा np.matmul(A, B) किंवा A @ B दोन्ही स्पष्ट पर्याय आहेत.

12. @ Operator#

Python मध्ये @ operator matrix multiplication साठी वापरता येतो.

PYTHON
result = A @ B

NumPy arrays साठी matrix multiplication लिहिण्याची ही compact आणि readable पद्धत आहे.

याचा अर्थ:

A च्या rows आणि B च्या columns वापरून matrix multiplication करा.

उदाहरण:

PYTHON
result1 = np.matmul(A, B)
result2 = A @ B

print(np.array_equal(result1, result2))

Expected Output#

TEXT
True

इथे दोन्ही approaches ने समान result तयार केला.

Tip

Matrix-heavy NumPy code मध्ये @ पाहिल्यावर लगेच "matrix multiplication" असा विचार करा.

13. np.matmul() vs *#

ही comparison खूप महत्त्वाची आहे.

समजा:

PYTHON
A = np.array([
    [1, 2],
    [3, 4]
])

B = np.array([
    [5, 6],
    [7, 8]
])

A * B#

PYTHON
print(A * B)

Expected Output#

TEXT
[[ 5 12]
 [21 32]]

इथे:

1\times5 2\times6 3\times7 4\times8

असे corresponding positions multiply झाले.

A @ B#

PYTHON
print(A @ B)

Expected Output#

TEXT
[[19 22]
 [43 50]]

इथे row-column calculation झाली.

उदाहरणार्थ पहिला cell:

1(5)+2(7)=19

दुसरा cell:

1(6)+2(8)=22

म्हणून * आणि @ चा result वेगळा आहे.

Expression Operation
A * B Element-wise multiplication
np.multiply(A, B) Element-wise multiplication
A @ B Matrix multiplication
np.matmul(A, B) Matrix multiplication
Common Mistake

Matrix multiplication करायची असताना चुकून A * B वापरणे हा अतिशय common beginner mistake आहे.

14. np.dot() vs np.matmul()#

हा फरक थोडा काळजीपूर्वक समजून घ्या.

दोन 1D arrays#

दोन 1D arrays साठी np.dot() dot product देते.

PYTHON
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(np.dot(a, b))
print(np.matmul(a, b))

Expected Output#

TEXT
32
32

या simple 1D example मध्ये दोन्ही result समान आहेत.

दोन 2D arrays#

दोन 2D arrays साठीही np.dot() आणि np.matmul() सामान्य matrix multiplication मध्ये समान numerical result देऊ शकतात.

पण functions ची semantics higher-dimensional arrays साठी वेगळी होते.

Beginner म्हणून practical rule असा ठेवा:

  • vector dot product स्पष्टपणे सांगायचा असेल → np.dot()
  • matrix multiplication स्पष्टपणे सांगायची असेल → np.matmul() किंवा @

यामुळे code चा intention अधिक स्पष्ट राहतो.

Note

Higher-dimensional np.dot() आणि np.matmul() behavior मध्ये अतिरिक्त differences आहेत. Current matrix multiplication समजण्यासाठी एवढे लक्षात ठेवणे पुरेसे आहे की @/np.matmul() matrix multiplication intention अधिक स्पष्टपणे व्यक्त करतात.

15. Matrix-Vector Multiplication#

Matrix फक्त दुसऱ्या matrix सोबतच multiply करावी लागते असे नाही.

Matrix एका vector सोबतही multiply करता येते.

समजा:

PYTHON
A = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

v = np.array([10, 20, 30])

A.shape आहे:

(2, 3)

v.shape आहे:

(3,)

Vector मध्ये तीन values आहेत.

Matrix च्या प्रत्येक row मध्येही तीन values आहेत.

म्हणून प्रत्येक row आणि vector यांचा dot product होऊ शकतो.

PYTHON
result = A @ v

print(result)
print(result.shape)

Expected Output#

TEXT
[140 320]
(2,)

पहिली value:

1(10)+2(20)+3(30) = 10+40+90 = 140

दुसरी value:

4(10)+5(20)+6(30) = 40+100+180 = 320

म्हणून result:

[140, 320]

आहे.

यातून एक useful mental model मिळतो:

Matrix मध्ये दोन rows आहेत.

प्रत्येक row vector सोबत एक dot product तयार करते.

म्हणून दोन output values मिळतात.

16. Shape Compatibility आधी तपासा#

Matrix multiplication करताना values पेक्षा shapes आधी पाहणे ही चांगली सवय आहे.

समजा:

A.shape = (2, 3)

आणि:

B.shape = (2, 2)

Multiplication:

A @ B

करायची आहे.

Rule:

पहिल्या matrix चे columns = दुसऱ्या matrix चे rows

पण इथे:

3 != 2

म्हणून multiplication valid नाही.

Code लिहिण्याआधी असे check करू शकतो:

PYTHON
A = np.ones((2, 3))
B = np.ones((2, 2))

print(A.shape)
print(B.shape)
print(A.shape[1] == B.shape[0])

Expected Output#

TEXT
(2, 3)
(2, 2)
False

शेवटचे False सांगते की inner dimensions match नाहीत.

म्हणून matrix multiplication करण्याआधीच problem लक्षात आली.

"घाईचे काम शैतानाचे" हे matrix multiplication च्या shapes बाबतीत अगदी लागू पडते. आधी shape पाहिली तर नंतरचा error सहज टाळता येतो.

17. Common Shape Mismatch Errors#

चूक 1 — Inner dimensions न पाहणे#

(2, 3) @ (2, 4)

इथे inner values:

3 आणि 2

match होत नाहीत.

म्हणून matrix multiplication invalid आहे.

चूक 2 — पूर्ण shapes समान असायलाच हव्यात असे समजणे#

ते आवश्यक नाही.

उदाहरण:

(2, 3) @ (3, 5)

पूर्ण shapes वेगळ्या आहेत.

पण inner dimensions 3 आणि 3 match आहेत.

म्हणून multiplication valid आहे.

Result shape:

(2, 5)

चूक 3 — * वापरून matrix multiplication अपेक्षित करणे#

* element-wise multiplication करते.

Matrix multiplication साठी @ किंवा np.matmul() वापरा.

चूक 4 — Result shape चुकीची predict करणे#

Rule:

(m,n) @ (n,p) \rightarrow (m,p)

Inner dimensions calculation साठी वापरल्या जातात.

Outer dimensions result मध्ये राहतात.

18. Matrix Multiplication Result Validate कसा करायचा?#

NumPy ने result दिला म्हणून blind trust करण्याची गरज नाही.

विशेषतः learning, debugging आणि data-science calculations मध्ये validation useful असते.

Method 1 — Shape validate करा#

PYTHON
A = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

B = np.array([
    [7, 8],
    [9, 10],
    [11, 12]
])

result = A @ B

print(result.shape == (2, 2))

Expected Output#

TEXT
True

आपण shapes वरून आधीच (2, 2) expect केले होते.

Actual result ने ते confirm केले.

Method 2 — एक cell manually calculate करा#

पहिला cell:

1(7)+2(9)+3(11)=58

मग NumPy result मधील:

result[0, 0]

हा 58 आहे का ते तपासा.

PYTHON
expected_first_cell = 58

print(result[0, 0] == expected_first_cell)

Expected Output#

TEXT
True

Method 3 — Expected matrix सोबत compare करा#

PYTHON
expected = np.array([
    [58, 64],
    [139, 154]
])

print(np.array_equal(result, expected))

Expected Output#

TEXT
True

Integer calculations मध्ये exact equality ठीक आहे.

Floating-point calculations मध्ये tiny rounding differences येऊ शकतात.

तेव्हा np.allclose() अधिक practical असते.

PYTHON
A = np.array([
    [0.1, 0.2],
    [0.3, 0.4]
])

B = np.array([
    [1.0, 2.0],
    [3.0, 4.0]
])

result = A @ B
expected = np.matmul(A, B)

print(np.allclose(result, expected))

Expected Output#

TEXT
True

np.allclose() values exactly character-for-character समान आहेत का हे पाहत नाही.

त्या numerical tolerance मध्ये sufficiently close आहेत का ते तपासते.

Data Science आणि numerical computing मध्ये हा validation pattern खूप उपयोगी आहे.

19. Practical Mental Checklist#

Matrix multiplication पाहिल्यावर हा छोटा checklist वापरा:

  1. Arrays ची shape काय आहे?
  2. ही * operation आहे की matrix multiplication?
  3. Matrix multiplication असेल तर inner dimensions match आहेत का?
  4. Result shape काय असणार?
  5. एक row-column calculation manually समजते का?
  6. @, np.matmul() किंवा np.dot() पैकी कोणता operation intention सर्वात स्पष्ट दाखवतो?
  7. Result validate करण्यासाठी shape, manual cell किंवा np.allclose() वापरता येईल का?

हा checklist वापरल्यावर matrix multiplication फक्त syntax राहत नाही. त्यामागची calculationही स्पष्ट होते.

20. Interview Preparation#

What is the difference between element-wise multiplication and matrix multiplication in NumPy?Show Answer

Element-wise multiplication multiplies corresponding elements, typically using * or np.multiply(). Matrix multiplication performs row-by-column products and sums, typically using @ or np.matmul().

What condition must be satisfied to multiply two 2D matrices?Show Answer

If matrix A has shape (m, n) and matrix B has shape (n, p), the inner dimensions must match. In other words, the number of columns in A must equal the number of rows in B. The result shape is (m, p).

When would you prefer np.matmul() or @ over np.dot()?Show Answer

For explicit matrix multiplication, np.matmul() or @ usually communicates the intent more clearly. np.dot() is commonly used for dot products, especially with 1D arrays, and its higher-dimensional behavior differs from np.matmul().

21. या Chapter मधून काय लक्षात ठेवायचे?#

  • Dot product मध्ये matching values multiply होतात आणि products add होतात.
  • दोन 1D arrays चा dot product एक scalar देतो.
  • np.dot() 1D vector dot product साठी सहज वापरता येते.
  • Matrix multiplication चा मुख्य mental model आहे: row × column.
  • * म्हणजे element-wise multiplication.
  • @ आणि np.matmul() matrix multiplication साठी वापरले जातात.
  • (m,n) @ (n,p) valid असेल तर result shape (m,p) असते.
  • Matrix multiplication साठी inner dimensions match असणे आवश्यक आहे.
  • Matrix-vector multiplication मध्ये matrix च्या प्रत्येक row चा vector सोबत dot product होतो.
  • Shape mismatch error समजण्यासाठी shape आधी inspect करा.
  • Integer results np.array_equal() ने आणि floating-point results np.allclose() ने validate करता येतात.
  • Matrix multiplication फक्त function लक्षात ठेवण्याचा topic नाही; result मधील प्रत्येक cell कुठून आला हे समजणे महत्त्वाचे आहे.
Interactive learning

Practice lab

Apply this chapter with guided activities, immediate feedback, explanations, and retry.

Practice loads when this section enters view.