Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C28-L1265-547AD850

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

Solving Linear Systems

3,927
words
22
min read
3
tools

1. Learning Outcomes#

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

  • Linear System of Equations म्हणजे काय ते समजेल.
  • अनेक equations एकत्र solve करण्याची गरज का पडते ते समजेल.
  • equations ला matrix form Ax=b मध्ये कसे पाहायचे ते समजेल.
  • coefficient matrix, unknown vector आणि result vector यांची भूमिका समजेल.
  • np.linalg.solve() वापरून square linear system solve करता येईल.
  • A आणि b च्या shapes कशा असाव्यात ते समजेल.
  • square matrix आणि full-rank requirement मागचे basic कारण समजेल.
  • singular matrix मुळे np.linalg.solve() का fail होऊ शकते ते समजेल.
  • मिळालेली solution A @ x आणि np.allclose() वापरून verify करता येईल.
  • np.linalg.solve() हे manually inverse काढून solve करण्यापेक्षा का prefer केले जाते ते समजेल.

2. Linear System म्हणजे नेमके काय?#

समजा आपल्याला दोन unknown values शोधायच्या आहेत.

त्यांना x आणि y म्हणू.

आपल्याकडे दोन equations आहेत:

\begin{aligned} 2x+y &= 5 \\ x-y &= 1 \end{aligned}

पहिली equation एकटी घेतली तर x आणि y च्या अनेक combinations मुळे उत्तर 5 येऊ शकते.

पण दुसरी equation आणखी एक condition देते.

आपल्याला अशी x आणि y values शोधायची आहेत ज्या दोन्ही equations एकाच वेळी satisfy करतील.

यालाच linear system किंवा system of linear equations म्हणतात.

Key Idea

Linear system solve करणे म्हणजे प्रत्येक equation वेगळी solve करणे नाही. सर्व equations एकत्र satisfy करणाऱ्या unknown values शोधणे होय.

आपल्या उदाहरणाचे उत्तर:

x=2,\qquad y=1

कारण:

2(2)+1=5

आणि:

2-1=1

दोन्ही equations satisfy झाल्या.

3. Data Science मध्ये याची गरज का पडते?#

Linear algebra मध्ये अनेक problems अशा स्वरूपात येतात जिथे आपल्याकडे:

  • अनेक unknown values असतात,
  • त्या values मध्ये relationships असतात,
  • आणि त्या relationships equations ने व्यक्त केलेल्या असतात.

अशा वेळी प्रत्येक equation हाताने manipulate करण्यापेक्षा matrix representation वापरणे सोपे होते.

NumPy आपल्याला हे matrix-based computation efficiently करण्यासाठी functions देते.

त्यातील महत्त्वाचे function म्हणजे:

np.linalg.solve()

पण ते वापरण्याआधी Ax=b ही कल्पना समजणे आवश्यक आहे.

4. Matrix Form Ax=b#

आपल्या equations पुन्हा पाहू:

\begin{aligned} 2x+y &= 5 \\ x-y &= 1 \end{aligned}

यांना matrix form मध्ये असे लिहिता येते:

\begin{bmatrix} 2 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 5 \\ 1 \end{bmatrix}

हेच compact form मध्ये:

Ax=b

इथे तीन मुख्य गोष्टी आहेत.

Symbol Meaning आपल्या उदाहरणात
A Coefficient Matrix \begin{bmatrix}2&1\\1&-1\end{bmatrix}
x Unknown Vector \begin{bmatrix}x\\y\end{bmatrix}
b Result Vector \begin{bmatrix}5\\1\end{bmatrix}

हा relationship visually पाहू.

Visual Diagram80%

Rendering diagram…

या flow मध्ये learner ने मुख्य गोष्ट अशी लक्षात ठेवायची:

आपल्याला A माहित आहे.

आपल्याला b माहित आहे.

आपल्याला x शोधायचा आहे.

म्हणजे:

A \times x=b

या relationship मधून unknown vector x शोधणे हे आपले काम आहे.

5. Coefficient Matrix म्हणजे काय?#

Equation पहा:

2x+y=5

इथे:

  • x चा coefficient 2 आहे.
  • y चा coefficient 1 आहे.

दुसरी equation:

x-y=1

इथे:

  • x चा coefficient 1 आहे.
  • y चा coefficient -1 आहे.

हे coefficients एकत्र ठेवले की:

A= \begin{bmatrix} 2 & 1 \\ 1 & -1 \end{bmatrix}

हा आपला coefficient matrix आहे.

Python मध्ये:

PYTHON
import numpy as np

A = np.array([
    [2, 1],
    [1, -1]
], dtype=float)

प्रत्येक row एका equation चे coefficients represent करते.

प्रत्येक column एका unknown variable शी संबंधित असतो.

पहिला column x साठी आहे.

दुसरा column y साठी आहे.

6. Unknown Vector#

आपल्याला x आणि y शोधायचे आहेत.

म्हणून unknown vector:

x= \begin{bmatrix} x \\ y \end{bmatrix}

इथे थोडा naming confusion होऊ शकतो.

Mathematics मध्ये पूर्ण unknown vector ला अनेकदा x असे नाव दिले जाते.

त्या vector मध्ये individual unknowns x_1,x_2,\ldots किंवा आपल्या छोट्या उदाहरणात x,y असू शकतात.

NumPy मध्ये solution मिळाल्यानंतर ती साधारण one-dimensional array असते.

उदाहरण:

PYTHON
solution = np.array([2.0, 1.0])

याचा अर्थ:

  • पहिली value पहिल्या unknown साठी
  • दुसरी value दुसऱ्या unknown साठी

7. Result Vector b#

Equations च्या उजव्या बाजूच्या values पाहू:

\begin{aligned} 2x+y &= 5 \\ x-y &= 1 \end{aligned}

उजवीकडील values आहेत:

b= \begin{bmatrix} 5 \\ 1 \end{bmatrix}

NumPy मध्ये:

PYTHON
b = np.array([5, 1], dtype=float)
Remember

A मध्ये unknowns चे coefficients असतात. b मध्ये equations चे right-side results असतात. solve() आपल्यासाठी unknown vector शोधते.

8. np.linalg.solve() काय करते?#

NumPy मध्ये square linear system solve करण्यासाठी:

PYTHON
np.linalg.solve(A, b)

वापरता येते.

याचा अर्थ साध्या भाषेत:

"A आणि b दिले आहेत. आता असा unknown vector शोध की A @ x केल्यावर b मिळेल."

General pattern:

PYTHON
solution = np.linalg.solve(A, b)

इथे:

  • A → coefficient matrix
  • b → result vector
  • solution → unknown values

9. 2×2 Linear System Solve करूया#

आपला system:

\begin{aligned} 2x+y &= 5 \\ x-y &= 1 \end{aligned}

Python code:

PYTHON
import numpy as np

A = np.array([
    [2, 1],
    [1, -1]
], dtype=float)

b = np.array([5, 1], dtype=float)

solution = np.linalg.solve(A, b)

print(solution)

Expected Output#

TEXT
[2. 1.]

Output मधील पहिली value x साठी आहे.

दुसरी value y साठी आहे.

म्हणून:

x=2,\qquad y=1

आता या values equations मध्ये mentally ठेवून पाहू.

पहिली equation:

2(2)+1=5

दुसरी equation:

2-1=1

दोन्ही बरोबर आहेत.

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

10. solve() मध्ये प्रत्यक्षात आपण काय देतो?#

या complete process कडे एकदा एकत्र पाहू.

Visual Diagram80%

Rendering diagram…

Flow असा आहे:

  1. equations मधून coefficients वेगळे करतो.
  2. त्यातून A बनवतो.
  3. right-side results मधून b बनवतो.
  4. np.linalg.solve(A, b) call करतो.
  5. solution vector मिळतो.
  6. solution पुन्हा original system satisfy करते का ते verify करतो.

ही शेवटची verification step खूप उपयोगी आहे.

11. Solution Matrix Multiplication ने Verify करणे#

आपल्याला माहीत आहे:

Ax=b

म्हणून solve() ने मिळालेला x खरोखर योग्य असेल तर:

A \times x

केल्यावर पुन्हा b मिळायला हवा.

NumPy मध्ये matrix multiplication साठी @ operator वापरू शकतो.

PYTHON
import numpy as np

A = np.array([
    [2, 1],
    [1, -1]
], dtype=float)

b = np.array([5, 1], dtype=float)

solution = np.linalg.solve(A, b)

check = A @ solution

print(check)

Expected Output#

TEXT
[5. 1.]

Original b होता:

PYTHON
b = np.array([5, 1], dtype=float)

आणि A @ solution मधूनही [5. 1.] मिळाले.

म्हणजे solution original equations satisfy करते.

Key Idea

Solve करणे आणि verify करणे वेगळे steps आहेत. np.linalg.solve() unknown values शोधते. A @ solution त्या values original system satisfy करतात का हे तपासायला मदत करते.

12. मग थेट == वापरायचे का?#

Floating-point calculations मध्ये अनेकदा values mathematically equal असल्या तरी computer representation मध्ये अगदी tiny difference असू शकतो.

उदाहरणार्थ mathematically 1 असलेली value internally 0.9999999999999999 सारखी दिसू शकते.

म्हणून numerical calculations compare करताना exact equality नेहमीच योग्य नसते.

NumPy मध्ये यासाठी:

np.allclose()

खूप उपयोगी आहे.

PYTHON
import numpy as np

A = np.array([
    [2, 1],
    [1, -1]
], dtype=float)

b = np.array([5, 1], dtype=float)

solution = np.linalg.solve(A, b)

is_valid = np.allclose(A @ solution, b)

print(is_valid)

Expected Output#

TEXT
True

True याचा अर्थ:

A @ solution आणि b मधील values numerical tolerance लक्षात घेतल्यावर sufficiently close आहेत.

म्हणून solution verify झाली.

13. Shape Requirements समजून घेऊया#

np.linalg.solve() वापरताना फक्त numbers बरोबर असणे पुरेसे नाही.

Arrays ची structure देखील योग्य असावी.

Single linear system साठी beginner level वर सर्वात महत्त्वाचा pattern:

A:\;(n,n)

आणि:

b:\;(n,)

उदाहरणार्थ दोन unknowns असतील तर:

PYTHON
print(A.shape)
print(b.shape)

Expected Output#

TEXT
(2, 2)
(2,)

याचा अर्थ:

  • A मध्ये 2 rows आणि 2 columns आहेत.
  • b मध्ये 2 result values आहेत.

तीन unknowns असतील तर साधारण:

A:\;(3,3)

आणि:

b:\;(3,)

असतील.

14. A Square Matrix का असावी?#

Square matrix म्हणजे rows आणि columns ची संख्या समान.

उदाहरण:

\begin{bmatrix} 2 & 1 \\ 1 & -1 \end{bmatrix}

ही 2\times2 square matrix आहे.

आपल्या current solve() use case मध्ये आपण अशा system वर काम करतो जिथे unknowns च्या संख्येला match करणारी square coefficient matrix असते.

उदाहरणार्थ:

  • 2 unknowns → 2\times2 coefficient matrix
  • 3 unknowns → 3\times3 coefficient matrix
  • 4 unknowns → 4\times4 coefficient matrix

Non-square matrix दिल्यास हा standard square-system solving pattern लागू होत नाही.

Common Mistake

b मध्ये दोन values आहेत म्हणून कोणतीही दोन-row matrix solve() ला चालेल असे नाही. A square असणे आवश्यक आहे.

15. Square असूनही प्रत्येक Matrix Solve होईल का?#

नाही.

हा खूप महत्त्वाचा मुद्दा आहे.

Square matrix असणे आवश्यक आहे.

पण ते एकटे पुरेसे नाही.

System कडे unique solution असणेही आवश्यक आहे.

Basic level वर याला full-rank requirement शी जोडून समजू शकतो.

n\times n square matrix साठी full rank म्हणजे तिचा rank n असणे.

यामागचा beginner mental model असा ठेवा:

equations एकमेकांना पुरेशी independent information देत असतील तर unique solution मिळू शकते.

जर एक equation दुसऱ्या equation ची फक्त duplicate किंवा multiple असेल, तर नवीन independent information मिळत नाही.

तेव्हा matrix singular होऊ शकते.

16. Singular Matrix म्हणजे काय?#

उदाहरण:

\begin{aligned} x+2y &= 3 \\ 2x+4y &= 6 \end{aligned}

दुसरी equation पहिल्या equation ची exactly दोनपट आहे.

म्हणून प्रत्यक्षात आपल्याकडे दोन स्वतंत्र conditions नाहीत.

Coefficient matrix:

A= \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}

ही matrix singular आहे.

Visual flow:

Visual Diagram80%

Rendering diagram…

इथे learner ने दोन gates लक्षात ठेवायचे:

पहिला gate:

A square आहे का?

दुसरा gate:

system unique solution देऊ शकतो का?

दोन्ही conditions योग्य असतील तर np.linalg.solve() वापरण्याचा standard case तयार होतो.

17. Singular Matrix Error Handle करणे#

हा code पहा:

PYTHON
import numpy as np

A = np.array([
    [1, 2],
    [2, 4]
], dtype=float)

b = np.array([3, 6], dtype=float)

try:
    solution = np.linalg.solve(A, b)
    print(solution)
except np.linalg.LinAlgError as error:
    print(error)

Expected Output#

TEXT
Singular matrix

इथे problem Python syntax मध्ये नाही.

Problem आपल्या linear system च्या mathematical structure मध्ये आहे.

दुसरी row पहिल्या row ची multiple आहे.

त्यामुळे independent information कमी झाली.

np.linalg.solve() unique solution देऊ शकत नाही.

Remember

LinAlgError: Singular matrix दिसल्यावर फक्त code syntax तपासू नका. A मधील equations/rows एकमेकांवर dependent आहेत का हेही तपासा.

18. Larger Square System Solve करणे#

np.linalg.solve() फक्त 2\times2 system साठी नाही.

उदाहरणार्थ तीन unknowns असलेला system घेऊ.

\begin{aligned} 3x+y+z &= 8 \\ x+4y+2z &= 15 \\ 2x+y+5z &= 19 \end{aligned}

Coefficient matrix:

A= \begin{bmatrix} 3 & 1 & 1 \\ 1 & 4 & 2 \\ 2 & 1 & 5 \end{bmatrix}

Result vector:

b= \begin{bmatrix} 8 \\ 15 \\ 19 \end{bmatrix}

Python मध्ये:

PYTHON
import numpy as np

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

b = np.array([8, 15, 19], dtype=float)

solution = np.linalg.solve(A, b)

print(solution)

Expected Output#

TEXT
[1. 2. 3.]

म्हणजे:

x=1,\qquad y=2,\qquad z=3

आता verify करू.

PYTHON
print(A @ solution)
print(np.allclose(A @ solution, b))

Expected Output#

TEXT
[ 8. 15. 19.]
True

इथे 2×2 system पासून 3×3 system कडे गेलो.

पण core idea बदललेली नाही:

Ax=b

आणि:

PYTHON
solution = np.linalg.solve(A, b)

19. Larger System मध्ये Shape कशी बदलते?#

तीन unknowns असल्यामुळे:

PYTHON
print(A.shape)
print(b.shape)
print(solution.shape)

Expected Output#

TEXT
(3, 3)
(3,)
(3,)

म्हणून:

  • A → तीन equations आणि तीन unknown coefficients
  • b → तीन equation results
  • solution → तीन unknown values

System मोठा झाला तरी mental model तोच राहतो.

20. Full-Rank Requirement — Basic Awareness#

rank हा linear algebra मधील independent information किती आहे याशी संबंधित concept आहे.

या chapter साठी एवढे समजणे पुरेसे आहे:

जर A ही n\times n square matrix असेल आणि तिचा rank n असेल, तर तिला full rank म्हणतात.

उदाहरण:

3\times3 matrix साठी full rank म्हणजे rank 3.

Full-rank square matrix unique solution साठी आवश्यक condition देते.

जर rank कमी झाला, तर matrix singular होते आणि np.linalg.solve() unique solution देऊ शकत नाही.

महत्त्वाचे

"Square" आणि "full rank" हे एकच नाहीत. Matrix square असू शकते पण singular देखील असू शकते.

Linear system solve केल्यावर हा pattern उपयोगी आहे:

PYTHON
solution = np.linalg.solve(A, b)

is_valid = np.allclose(A @ solution, b)

याचा अर्थ:

पहिले:

Ax=b

solve करा.

नंतर मिळालेला x वापरून:

Ax

पुन्हा calculate करा.

आणि तो result b जवळ आहे का ते np.allclose() ने तपासा.

हा छोटा verification step debugging आणि numerical work मध्ये खूप उपयोगी ठरतो.

22. np.linalg.solve() vs Manual Matrix Inversion#

Mathematically आपण कधी कधी असे लिहिलेले पाहतो:

Ax=b

आणि inverse उपलब्ध असेल तर:

x=A^{-1}b

यामुळे beginner ला असे वाटू शकते:

"मग आधी inverse काढून multiply का करू नये?"

NumPy मध्ये technically असे करता येते:

PYTHON
solution = np.linalg.inv(A) @ b

पण linear system solve करण्यासाठी सामान्यतः हा preferred approach नाही.

त्याऐवजी:

PYTHON
solution = np.linalg.solve(A, b)

prefer केले जाते.

दोन्ही approaches मधील फरक#

Approach Code Practical Preference
Direct solve np.linalg.solve(A, b) Preferred
Explicit inverse np.linalg.inv(A) @ b सामान्य solving साठी टाळणे चांगले

याची मुख्य कारणे:

  • explicit inverse वेगळा calculate करण्याची गरज पडत नाही.
  • direct solving साधारण अधिक computationally appropriate असते.
  • numerical calculations मध्ये explicit inverse अनावश्यक numerical error वाढवू शकते.
  • code मधून intention स्पष्ट दिसते — "हा system solve करायचा आहे."
Interview Point

To solve Ax=b, prefer np.linalg.solve(A, b) over explicitly computing np.linalg.inv(A) @ b when a direct solve is appropriate.

23. Common Mistakes#

Mistake 1 — Equation मधील coefficients चुकीच्या order मध्ये ठेवणे#

समजा variables ची order x,y आहे.

मग प्रत्येक row मध्ये coefficients त्याच order मध्ये असले पाहिजेत.

Equation:

2x+3y=7

तर row:

PYTHON
[2, 3]

असावी.

[3, 2] केली तर system बदलून जाईल.

Mistake 2 — b मध्ये coefficients ठेवणे#

b मध्ये right-side results असतात.

Equation:

2x+y=5

यासाठी:

PYTHON
A_row = [2, 1]
b_value = 5

असे separation असते.

Mistake 3 — A non-square असणे#

Standard np.linalg.solve() square coefficient system साठी आहे.

A.shape तपासणे ही चांगली debugging habit आहे.

Mistake 4 — Square म्हणजे automatically solvable असे समजणे#

Square matrix singular असू शकते.

म्हणून unique solution guaranteed नसते.

Mistake 5 — Solution verify न करणे#

Especially larger systems मध्ये:

PYTHON
np.allclose(A @ solution, b)

ही छोटी check खूप उपयोगी आहे.

Mistake 6 — Linear system साठी unnecessary inverse वापरणे#

फक्त A^{-1}b formula माहित आहे म्हणून:

PYTHON
np.linalg.inv(A) @ b

default approach करू नका.

Direct solve available असेल तर:

PYTHON
np.linalg.solve(A, b)

prefer करा.

24. Interview Preparation#

What does np.linalg.solve(A, b) compute?Show Answer

np.linalg.solve(A, b) computes the solution vector x for the linear system Ax=b, assuming the coefficient matrix is square and the system has a unique solution.

Key Points:

  • A is the coefficient matrix.
  • b is the right-hand-side vector.
  • The returned array contains the unknown values.
  • A singular coefficient matrix cannot produce a unique solution through solve().

Why interviewer asks this:

To check whether you understand the mathematical meaning of the NumPy API rather than only its syntax.


Why is np.linalg.solve(A, b) generally preferred over np.linalg.inv(A) @ b?Show Answer

For solving a linear system, np.linalg.solve(A, b) avoids explicitly computing the matrix inverse and is generally more computationally appropriate and numerically preferable.

Key Points:

  • No explicit inverse is required.
  • The code directly expresses the intention to solve a system.
  • Explicit inversion can add unnecessary computation and numerical error.

Why interviewer asks this:

To check whether you can choose an appropriate numerical linear algebra operation instead of mechanically applying formulas.


How can you verify a solution returned by np.linalg.solve()?Show Answer

Multiply the coefficient matrix by the computed solution and compare the result with the original right-hand-side vector, preferably using np.allclose(A @ solution, b) for floating-point values.

Key Points:

  • Reconstruct the left side using A @ solution.
  • Compare it with b.
  • np.allclose() handles small floating-point differences.

Why interviewer asks this:

To check whether you validate numerical results and understand the original equation Ax=b.

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

  • अनेक linear equations एकत्र solve करायच्या असतील तर system ला Ax=b या matrix form मध्ये पाहू शकतो.
  • A म्हणजे coefficient matrix.
  • x म्हणजे unknown vector.
  • b म्हणजे result vector.
  • NumPy मध्ये square system solve करण्यासाठी np.linalg.solve(A, b) वापरता येते.
  • single right-side vector साठी beginner pattern म्हणजे A.shape == (n, n) आणि b.shape == (n,).
  • square matrix असणे आवश्यक आहे, पण तेवढेच पुरेसे नाही.
  • unique solution साठी coefficient matrix singular नसावी; square full-rank matrix हा standard valid case आहे.
  • singular matrix असल्यास np.linalg.LinAlgError येऊ शकतो.
  • solution मिळाल्यानंतर A @ solution ने original result reconstruct करता येतो.
  • floating-point verification साठी np.allclose(A @ solution, b) उपयोगी आहे.
  • linear system solve करताना explicit inverse काढण्यापेक्षा np.linalg.solve() सामान्यतः prefer करावे.
Interactive learning

Practice lab

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

Practice loads when this section enters view.