mirror of
https://github.com/fverdugo/XM_40017.git
synced 2025-11-09 16:24:24 +01:00
18 lines
10 KiB
HTML
18 lines
10 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Solutions · XM_40017</title><script data-outdated-warner src="../assets/warner.js"></script><link rel="canonical" href="https://fverdugo.github.io/XM_40017/solutions_for_all_notebooks/"/><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.045/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.13.24/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../assets/documenter.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../assets/themeswap.js"></script><link href="../assets/favicon.ico" rel="icon" type="image/x-icon"/><script src="../assets/iframeResizer.min.js"></script><link href="../assets/custom.css" rel="stylesheet" type="text/css"/></head><body><div id="documenter"><nav class="docs-sidebar"><a class="docs-logo" href="../"><img src="../assets/logo.png" alt="XM_40017 logo"/></a><div class="docs-package-name"><span class="docs-autofit"><a href="../">XM_40017</a></span></div><form class="docs-search" action="../search/"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li><a class="tocitem" href="../">Home</a></li><li><a class="tocitem" href="../getting_started_with_julia/">Getting started</a></li><li><span class="tocitem">Notebooks</span><ul><li><a class="tocitem" href="../julia_basics/">Julia Basics</a></li><li><a class="tocitem" href="../julia_async/">Asynchronous programming in Julia</a></li><li><a class="tocitem" href="../julia_distributed/">Distributed computing in Julia</a></li><li><a class="tocitem" href="../mpi_tutorial/">Distributed computing with MPI</a></li><li><a class="tocitem" href="../matrix_matrix/">Matrix-matrix multiplication</a></li><li><a class="tocitem" href="../jacobi_method/">Jacobi method</a></li><li><a class="tocitem" href="../asp/">All pairs of shortest paths</a></li></ul></li><li class="is-active"><a class="tocitem" href>Solutions</a><ul class="internal"><li><a class="tocitem" href="#Julia-Basics"><span>Julia Basics</span></a></li><li><a class="tocitem" href="#Asynchronous-programming-in-Julia"><span>Asynchronous programming in Julia</span></a></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href>Solutions</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Solutions</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com/fverdugo/XM_40017/blob/main/docs/src/solutions_for_all_notebooks.md#" title="Edit on GitHub"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch">Edit on GitHub</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="Solutions"><a class="docs-heading-anchor" href="#Solutions">Solutions</a><a id="Solutions-1"></a><a class="docs-heading-anchor-permalink" href="#Solutions" title="Permalink"></a></h1><h2 id="Julia-Basics"><a class="docs-heading-anchor" href="#Julia-Basics">Julia Basics</a><a id="Julia-Basics-1"></a><a class="docs-heading-anchor-permalink" href="#Julia-Basics" title="Permalink"></a></h2><h3 id="NB1-Q1"><a class="docs-heading-anchor" href="#NB1-Q1">NB1-Q1</a><a id="NB1-Q1-1"></a><a class="docs-heading-anchor-permalink" href="#NB1-Q1" title="Permalink"></a></h3><p>In the first, line we assign a variable to a value. In the second line, we assign another variable to the same value. Thus,we have 2 variables associated with the same value. In line 3, we associate <code>y</code> to a new value (re-assignment). Thus, we have 2 variables associated with 2 different values. Variable <code>x</code> is still associated with its original value. Thus, the value at the final line is <code>x=1</code>.</p><h3 id="NB1-Q2"><a class="docs-heading-anchor" href="#NB1-Q2">NB1-Q2</a><a id="NB1-Q2-1"></a><a class="docs-heading-anchor-permalink" href="#NB1-Q2" title="Permalink"></a></h3><p>It will be <code>1</code> for very similar reasons as in the previous questions: we are reassigning a local variable, not the global variable defined outside the function.</p><h3 id="NB1-Q3"><a class="docs-heading-anchor" href="#NB1-Q3">NB1-Q3</a><a id="NB1-Q3-1"></a><a class="docs-heading-anchor-permalink" href="#NB1-Q3" title="Permalink"></a></h3><p>It will be <code>6</code>. In the returned function <code>f2</code>, <code>x</code> is equal to <code>2</code>. Thus, when calling <code>f2(3)</code> we compute <code>2*3</code>.</p><h3 id="Exercise-1"><a class="docs-heading-anchor" href="#Exercise-1">Exercise 1</a><a id="Exercise-1-1"></a><a class="docs-heading-anchor-permalink" href="#Exercise-1" title="Permalink"></a></h3><pre><code class="language-julia hljs">function ex1(a)
|
|
j = 1
|
|
m = a[j]
|
|
for (i,ai) in enumerate(a)
|
|
if m < ai
|
|
m = ai
|
|
j = i
|
|
end
|
|
end
|
|
(m,j)
|
|
end</code></pre><h3 id="Exercise-2"><a class="docs-heading-anchor" href="#Exercise-2">Exercise 2</a><a id="Exercise-2-1"></a><a class="docs-heading-anchor-permalink" href="#Exercise-2" title="Permalink"></a></h3><pre><code class="language-julia hljs">ex2(f,g) = x -> f(x) + g(x)</code></pre><h3 id="Exercise-3"><a class="docs-heading-anchor" href="#Exercise-3">Exercise 3</a><a id="Exercise-3-1"></a><a class="docs-heading-anchor-permalink" href="#Exercise-3" title="Permalink"></a></h3><pre><code class="language-julia hljs">using GLMakie
|
|
max_iters = 100
|
|
n = 1000
|
|
x = LinRange(-1.7,0.7,n)
|
|
y = LinRange(-1.2,1.2,n)
|
|
heatmap(x,y,(i,j)->mandel(i,j,max_iters))</code></pre><h2 id="Asynchronous-programming-in-Julia"><a class="docs-heading-anchor" href="#Asynchronous-programming-in-Julia">Asynchronous programming in Julia</a><a id="Asynchronous-programming-in-Julia-1"></a><a class="docs-heading-anchor-permalink" href="#Asynchronous-programming-in-Julia" title="Permalink"></a></h2><h3 id="NB2-Q1"><a class="docs-heading-anchor" href="#NB2-Q1">NB2-Q1</a><a id="NB2-Q1-1"></a><a class="docs-heading-anchor-permalink" href="#NB2-Q1" title="Permalink"></a></h3><p>Evaluating <code>compute_π(100_000_000)</code> takes about 0.25 seconds. Thus, the loop would take about 2.5 seconds since we are calling the function 10 times.</p><h3 id="NB2-Q2"><a class="docs-heading-anchor" href="#NB2-Q2">NB2-Q2</a><a id="NB2-Q2-1"></a><a class="docs-heading-anchor-permalink" href="#NB2-Q2" title="Permalink"></a></h3><p>The time in doing the loop will be almost zero since the loop just schedules 10 tasks, which should be very fast.</p><h3 id="NB2-Q3"><a class="docs-heading-anchor" href="#NB2-Q3">NB2-Q3</a><a id="NB2-Q3-1"></a><a class="docs-heading-anchor-permalink" href="#NB2-Q3" title="Permalink"></a></h3><p>It will take 2.5 seconds, like in question 1. The <code>@sync</code> macro forces to wait for all tasks we have generated with the <code>@async</code> macro. Since we have created 10 tasks and each of them takes about 0.25 seconds, the total time will be about 2.5 seconds.</p><h3 id="NB2-Q4"><a class="docs-heading-anchor" href="#NB2-Q4">NB2-Q4</a><a id="NB2-Q4-1"></a><a class="docs-heading-anchor-permalink" href="#NB2-Q4" title="Permalink"></a></h3><p>It will take about 3 seconds. The channel has buffer size 4, thus the call to <code>put!</code>will not block. The call to <code>take!</code> will not block neither since there is a value stored in the channel. The taken value is 3 and therefore we will wait for 3 seconds. </p><h3 id="NB2-Q5"><a class="docs-heading-anchor" href="#NB2-Q5">NB2-Q5</a><a id="NB2-Q5-1"></a><a class="docs-heading-anchor-permalink" href="#NB2-Q5" title="Permalink"></a></h3><p>The channel is not buffered and therefore the call to <code>put!</code> will block. The cell will run forever, since there is no other task that calls <code>take!</code> on this channel. </p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../asp/">« All pairs of shortest paths</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 0.27.25 on <span class="colophon-date" title="Friday 8 September 2023 16:24">Friday 8 September 2023</span>. Using Julia version 1.9.3.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
|