Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .translate/state/python_by_example.md.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source-sha: 1a87942398e15e03539083cc944a78653c532607
synced-at: "2026-03-20"
model: unknown
mode: RESYNC
source-sha: 3241a07f00b1fb4d2bebc6989d1f480847e08e31
synced-at: "2026-04-25"
model: claude-sonnet-4-6
mode: UPDATE
section-count: 6
tool-version: 0.13.0
tool-version: 0.14.1
37 changes: 20 additions & 17 deletions lectures/python_by_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ translation:

پیش از شروع این درس، باید جلسه ی قبل را مطالعه کرده باشید.


## هدف:رسم یک فرآیند نویز سفید

فرض کنید می‌خواهیم فرآیند نویز سفید $\epsilon_0, \epsilon_1, \ldots, \epsilon_T$ را شبیه‌سازی و رسم کنیم، که در آن هر نقطه $\epsilon_t$ مستقل و نرمال استاندارد است.
Expand All @@ -77,7 +76,8 @@ translation:
import numpy as np
import matplotlib.pyplot as plt

ϵ_values = np.random.randn(100)
rng = np.random.default_rng()
ϵ_values = rng.standard_normal(100)
plt.plot(ϵ_values)
plt.show()
```
Expand Down Expand Up @@ -111,7 +111,6 @@ np.sqrt(4)
np.log(4)
```


#### چرا در برنامه نویسی پایتون، از Importهای متعددی استفاده می شود؟

برنامه های پایتون معمولا به چندین دستور `import` نیاز دارند.
Expand All @@ -120,7 +119,6 @@ np.log(4)

وقتی بخواهید کارهای جالب تر و پیشرفته تری با پایتون انجام دهید، تقریبا همیشه باید قابلیت های اضافی را با استفاده از `import` به برنامه اضافه کنید.


#### پکیج ها(Packages)

```{index} single: Python; Packages
Expand Down Expand Up @@ -155,7 +153,7 @@ print(np.__file__)
```{index} single: Python; Subpackages
```

به خط حاوی `ϵ_values = np.random.randn(100)` در کد توجه کنید.
به خط حاوی `rng = np.random.default_rng()` در کد توجه کنید.

در این دستور، `np` به پکیج NumPY اشاره دارد، و `random` یک زیرپکیج از NumPY است.

Expand Down Expand Up @@ -191,7 +189,7 @@ sqrt(4)
بیاید به کدی که نویز سفید رسم می کند برگردیم. سه خط بعد از دستور Import به این صورت هستند:

```{code-cell} ipython
ϵ_values = np.random.randn(100)
ϵ_values = rng.standard_normal(100)
plt.plot(ϵ_values)
Comment on lines 189 to 193
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The narrative says “three lines after the import” and the snippet starts with ϵ_values = rng.standard_normal(100), but the updated program now includes rng = np.random.default_rng() immediately after the imports. This section should be updated so the description and the displayed snippet reflect the new extra line (e.g., adjust the wording/count and/or include the rng = ... line in the snippet).

Copilot uses AI. Check for mistakes.
plt.show()
```
Expand All @@ -217,7 +215,7 @@ ts_length = 100
ϵ_values = [] # empty list

for i in range(ts_length):
e = np.random.randn()
e = rng.standard_normal()
ϵ_values.append(e)

plt.plot(ϵ_values)
Expand Down Expand Up @@ -313,7 +311,7 @@ x[1] # second element of x

```{code-cell} python3
for i in range(ts_length):
e = np.random.randn()
e = rng.standard_normal()
ϵ_values.append(e)
```

Expand Down Expand Up @@ -346,7 +344,6 @@ for variable_name in sequence:

* برای هر عنصر از دنباله `sequence`، نام متغیر `variable_name` را به آن عنصر متصل (blind) می کند و سپس بلوک کد را اجرا می کند.


### یادداشتی درباره تورفتگی (Indentation)

```{index} single: Python; Indentation
Expand Down Expand Up @@ -393,7 +390,7 @@ ts_length = 100
ϵ_values = []
i = 0
while i < ts_length:
e = np.random.randn()
e = rng.standard_normal()
ϵ_values.append(e)
i = i + 1
plt.plot(ϵ_values)
Expand Down Expand Up @@ -495,9 +492,10 @@ import matplotlib.pyplot as plt
T = 200
x = np.empty(T+1)
x[0] = 0
rng = np.random.default_rng()

for t in range(T):
x[t+1] = α * x[t] + np.random.randn()
x[t+1] = α * x[t] + rng.standard_normal()

plt.plot(x)
plt.show()
Expand Down Expand Up @@ -536,11 +534,12 @@ plt.show()
α_values = [0.0, 0.8, 0.98]
T = 200
x = np.empty(T+1)
rng = np.random.default_rng()

for α in α_values:
x[0] = 0
for t in range(T):
x[t+1] = α * x[t] + np.random.randn()
x[t+1] = α * x[t] + rng.standard_normal()
plt.plot(x, label=f'$\\alpha = {α}$')

plt.legend()
Expand Down Expand Up @@ -588,9 +587,10 @@ $$
T = 200
x = np.empty(T+1)
x[0] = 0
rng = np.random.default_rng()

for t in range(T):
x[t+1] = α * np.abs(x[t]) + np.random.randn()
x[t+1] = α * np.abs(x[t]) + rng.standard_normal()

plt.plot(x)
plt.show()
Expand Down Expand Up @@ -639,13 +639,14 @@ for x in numbers:
T = 200
x = np.empty(T+1)
x[0] = 0
rng = np.random.default_rng()

for t in range(T):
if x[t] < 0:
abs_x = - x[t]
else:
abs_x = x[t]
x[t+1] = α * abs_x + np.random.randn()
x[t+1] = α * abs_x + rng.standard_normal()

plt.plot(x)
plt.show()
Expand All @@ -658,10 +659,11 @@ plt.show()
T = 200
x = np.empty(T+1)
x[0] = 0
rng = np.random.default_rng()

for t in range(T):
abs_x = - x[t] if x[t] < 0 else x[t]
x[t+1] = α * abs_x + np.random.randn()
x[t+1] = α * abs_x + rng.standard_normal()

plt.plot(x)
plt.show()
Expand Down Expand Up @@ -721,12 +723,13 @@ import numpy as np

```{code-cell} python3
n = 1000000 # sample size for Monte Carlo simulation
rng = np.random.default_rng()

count = 0
for i in range(n):

# drawing random positions on the square
u, v = np.random.uniform(), np.random.uniform()
u, v = rng.uniform(), rng.uniform()

# check whether the point falls within the boundary
# of the unit circle centred at (0.5,0.5)
Expand All @@ -743,4 +746,4 @@ print(area_estimate * 4) # dividing by radius**2
```

```{solution-end}
```
```
Loading