Commit a948527b authored by ZackOSX's avatar ZackOSX

syncrho

sysncrho 23
parent fd35e287
#+TITLE: Joli titre #+TITLE: À propos du calcul de $\pi$
#+AUTHOR: Zack Zox #+AUTHOR: Zack Zox
#+DATE: La date du jour # +DATE: La date du jour
#+OPTIONS: toc:1
#+LANGUAGE: fr #+LANGUAGE: fr
# #+PROPERTY: header-args :eval never-export # +PROPERTY: header-args :eval never-export
# +HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
# +HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
# +HTML_HEAD: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
# +HTML_HEAD: <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
# +HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
# +HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
#+HTML_HEAD: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
#+HTML_HEAD: <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
* Quelques explications
Ceci est un document org-mode avec quelques exemples de code * En demandant à la lib maths
R. Une fois ouvert dans emacs, ce document peut aisément être
exporté au format HTML, PDF, et Office. Pour plus de détails sur
org-mode vous pouvez consulter https://orgmode.org/guide/.
Lorsque vous utiliserez le raccourci =C-c C-e h o=, ce document sera Mon ordinateur m'indique que $\pi$ vaut /approximativement/
compilé en html. Tout le code contenu sera ré-exécuté, les résultats
récupérés et inclus dans un document final. Si vous ne souhaitez pas
ré-exécuter tout le code à chaque fois, il vous suffit de supprimer
le # et l'espace qui sont devant le ~#+PROPERTY:~ au début de ce
document.
Comme nous vous l'avons montré dans la vidéo, on inclut du code
R de la façon suivante (et on l'exécute en faisant ~C-c C-c~):
#+begin_src R :results output :exports both #+begin_src R :results output :exports both
print("Hello world!") pi
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: [1] "Hello world!" : [1] 3.141593
Voici la même chose, mais avec une session R (c'est le cas le * En utilisant la méthode des aiguilles de Buffon
plus courant, R étant vraiment un langage interactif), donc une Mais calculé avec la *méthode* des [[https://fr.wikipedia.org/wiki/Aiguille_de_Buffon][aiguilles de Buffon]], on obtiendrait
persistance d'un bloc à l'autre (et on l'exécute toujours en faisant comme *approximation* :
~C-c C-c~).
#+begin_src R :results output :session *R* :exports both #+begin_src R :results output :session *R* :exports both
summary(cars) set.seed(42)
#+end_src N = 100000
x = runif(N)
#+RESULTS: theta = pi/2*runif(N)
: speed dist 2/(mean(x+sin(theta)>1))
: Min. : 4.0 Min. : 2.00
: 1st Qu.:12.0 1st Qu.: 26.00
: Median :15.0 Median : 36.00
: Mean :15.4 Mean : 42.98
: 3rd Qu.:19.0 3rd Qu.: 56.00
: Max. :25.0 Max. :120.00
Et enfin, voici un exemple de sortie graphique:
#+begin_src R :results output graphics :file "./cars.png" :exports results :width 600 :height 400 :session *R*
plot(cars)
#+end_src #+end_src
#+RESULTS: #+RESULTS:
[[file:./cars.png]] :
: [1] 3.14327
Vous remarquerez le paramètre ~:exports results~ qui indique que le code
ne doit pas apparaître dans la version finale du document. Nous vous
recommandons dans le cadre de ce MOOC de ne pas changer ce paramètre
(indiquer ~both~) car l'objectif est que vos analyses de données soient
parfaitement transparentes pour être reproductibles.
Attention, la figure ainsi générée n'est pas stockée dans le document
org. C'est un fichier ordinaire, ici nommé ~cars.png~. N'oubliez pas
de le committer si vous voulez que votre analyse soit lisible et
compréhensible sur GitLab.
Enfin, pour les prochains exercices, nous ne vous fournirons pas
forcément de fichier de départ, ça sera à vous de le créer, par
exemple en repartant de ce document et de le commiter vers
gitlab. N'oubliez pas que nous vous fournissons dans les ressources de
ce MOOC une configuration avec un certain nombre de raccourcis
claviers permettant de créer rapidement les blocs de code R (en
faisant ~<r~ ou ~<R~ suivi de ~Tab~).
Maintenant, à vous de jouer! Vous pouvez effacer toutes ces
informations et les remplacer par votre document computationnel.
#+BEGIN_SRC emacs-lisp
(directory-files ".")
#+END_SRC
#+RESULTS:
| #toy_document_orgmode_R_fr.org# | . | .#toy_document_orgmode_R_fr.org | .. | cars.png | cosxsx.png | toy_document_en.Rmd | toy_document_fr.Rmd | toy_document_orgmode_R_en.org | toy_document_orgmode_R_fr.html | toy_document_orgmode_R_fr.org | toy_document_orgmode_python_en.org | toy_document_orgmode_python_fr.org | toy_notebook_en.ipynb | toy_notebook_fr.ipynb |
#+BEGIN_SRC ruby :results list
Dir.entries('.')
#+END_SRC
#+RESULTS:
- .
- ..
- #toy_document_orgmode_R_fr.org#
- .#toy_document_orgmode_R_fr.org
- cars.png
- cosxsx.png
- toy_document_en.Rmd
- toy_document_fr.Rmd
- toy_document_orgmode_python_en.org
- toy_document_orgmode_python_fr.org
- toy_document_orgmode_R_en.org
- toy_document_orgmode_R_fr.html
- toy_document_orgmode_R_fr.org
- toy_notebook_en.ipynb
- toy_notebook_fr.ipynb
#+RESULTS:
| #toy_document_orgmode_R_fr.org# | .#toy_document_orgmode_R_fr.org | cars.png | cosxsx.png | toy_document_en.Rmd | toy_document_fr.Rmd | toy_document_orgmode_python_en.org | toy_document_orgmode_python_fr.org | toy_document_orgmode_R_en.org | toy_document_orgmode_R_fr.html | toy_document_orgmode_R_fr.org | toy_notebook_en.ipynb | toy_notebook_fr.ipynb |
#+BEGIN_SRC sh
ls -1a
#+END_SRC
#+RESULTS:
| #toy_document_orgmode_R_fr.org# |
| 0 |
| .#toy_document_orgmode_R_fr.org |
| .. |
| cars.png |
| cosxsx.png |
| double-space.rb |
| primes.txt |
| toy_document_en.Rmd |
| toy_document_fr.Rmd |
| toy_document_orgmode_R_en.org |
| toy_document_orgmode_R_fr.html |
| toy_document_orgmode_R_fr.html~ |
| toy_document_orgmode_R_fr.org |
| toy_document_orgmode_R_fr.pdf |
| toy_document_orgmode_R_fr.tex |
| toy_document_orgmode_python_en.org |
| toy_document_orgmode_python_fr.org |
| toy_notebook_en.ipynb |
| toy_notebook_fr.ipynb |
#+BEGIN_SRC ruby :results output
puts 'Hello World'
5 * 6
#+END_SRC
#+RESULTS:
: Hello World
#+BEGIN_SRC ruby :results list
Dir.entries('.').sort.select do |file|
file[0] != '.'
end
#+END_SRC
#+RESULTS:
- #toy_document_orgmode_R_fr.org#
- cars.png
- cosxsx.png
- toy_document_en.Rmd
- toy_document_fr.Rmd
- toy_document_orgmode_R_en.org
- toy_document_orgmode_R_fr.html
- toy_document_orgmode_R_fr.org
- toy_document_orgmode_python_en.org
- toy_document_orgmode_python_fr.org
- toy_notebook_en.ipynb
- toy_notebook_fr.ipynb
#+BEGIN_SRC ruby :results output :file primes.txt
require 'prime'
Prime.each(5000) do |prime|
p prime
end
#+END_SRC
#+RESULTS:
[[file:primes.txt]]
#+BEGIN_SRC ruby :tangle double-space.rb * Avec un argument "fréquentiel" de surface
while s = gets
print s ; puts
end
#+END_SRC
#+RESULTS:
: nil
Precede each line in the text from standard in (or file) with the
current line number.
See [[http://benoithamelin.tumblr.com/ruby1line][one liners]].
#+BEGIN_SRC ruby :results output
while s = gets
puts "#{$<.file.lineno}: #{s}"
end
#+END_SRC
#+RESULTS:
#+PROPERTY: tangle lineno.rb
#+PROPERTY: comments org
#+BEGIN_SRC ruby
while s = gets
puts "#{$<.file.lineno}: #{s}"
end
#+END_SRC
#+RESULTS:
: nil
#+PROPERTY: tangle lineno.rb
#+PROPERTY: comments org
#+HEADER: :var a=42 d=56 :var f=23
#+HEADERS: :var b=79 e=79
#+BEGIN_SRC ruby :var c=3 g=2 :export both
[ a, b, c, d, e, f, g ]
#+END_SRC
#+RESULTS:
| 42 | 79 | 3 | 56 | 79 | 23 | 2 |
#+NAME: twelve-primes
#+BEGIN_SRC ruby
require 'prime'
Prime.first 12
#+END_SRC
#+RESULTS: twelve-primes
| 2 | 3 | 5 | 7 | 11 | 13 | 17 | 19 | 23 | 29 | 31 | 37 |
#+BEGIN_SRC python :var primes=twelve-primes
return primes[-1]
#+END_SRC
#+RESULTS:
: 37
#+NAME: cool-numbers
#+BEGIN_SRC emacs-lisp
(mapcar (lambda (i)
(list i (random 10)
(expt i 2) (random 100)
(expt i 3) (random 1000)))
(number-sequence 1 10))
#+END_SRC
#+RESULTS: cool-numbers
| 1 | 0 | 1 | 99 | 1 | 657 |
| 2 | 8 | 4 | 5 | 8 | 720 |
| 3 | 1 | 9 | 19 | 27 | 252 |
| 4 | 6 | 16 | 68 | 64 | 549 |
| 5 | 4 | 25 | 20 | 125 | 772 |
| 6 | 0 | 36 | 28 | 216 | 821 |
| 7 | 6 | 49 | 14 | 343 | 572 |
| 8 | 0 | 64 | 47 | 512 | 654 |
| 9 | 5 | 81 | 8 | 729 | 874 |
| 10 | 0 | 100 | 34 | 1000 | 462 |
#+NAME: skip_first
#+BEGIN_SRC elisp
(require 'use-package)
(use-package graphviz-dot-mode
:ensure t
:config
(setq graphviz-dot-indent-width 4))
(use-package company-graphviz-dot)
#+END_SRC
#+BEGIN_SRC sh :post skip_first(data=*this*)
ls -lhG
#+END_SRC
#+RESULTS:
| -rw-r--r-- | 1 | zackx | staff | 9,1K | 16 | jul | 02:45 | #toy_document_orgmode_R_fr.org# |
| -rw-r--r-- | 1 | zackx | staff | 21K | 16 | jul | 02:26 | cars.png |
| -rw-r--r-- | 1 | zackx | staff | 19K | 15 | jul | 17:59 | cosxsx.png |
| -rw-r--r--@ | 1 | zackx | staff | 42B | 16 | jul | 02:28 | double-space.rb |
| -rw-r--r-- | 1 | zackx | staff | 3,1K | 16 | jul | 02:25 | primes.txt |
| -rw-r--r-- | 1 | zackx | staff | 1,3K | 15 | jul | 17:59 | toy_document_en.Rmd |
| -rw-r--r-- | 1 | zackx | staff | 1,4K | 15 | jul | 17:59 | toy_document_fr.Rmd |
| -rw-r--r-- | 1 | zackx | staff | 3,2K | 15 | jul | 17:59 | toy_document_orgmode_R_en.org |
| -rw-r--r--@ | 1 | zackx | staff | 16K | 16 | jul | 02:26 | toy_document_orgmode_R_fr.html |
| -rw-r--r--@ | 1 | zackx | staff | 13K | 15 | jul | 19:24 | toy_document_orgmode_R_fr.html~ |
| -rw-r--r-- | 1 | zackx | staff | 6,1K | 16 | jul | 02:28 | toy_document_orgmode_R_fr.org |
| -rw-r--r-- | 1 | zackx | staff | 3,8K | 15 | jul | 17:59 | toy_document_orgmode_python_en.org |
| -rw-r--r-- | 1 | zackx | staff | 3,9K | 15 | jul | 17:59 | toy_document_orgmode_python_fr.org |
| -rw-r--r-- | 1 | zackx | staff | 441B | 15 | jul | 17:59 | toy_notebook_en.ipynb |
| -rw-r--r-- | 1 | zackx | staff | 441B | 15 | jul | 17:59 | toy_notebook_fr.ipynb |
#+BEGIN_SRC dot :file some-illustration.png
digraph {
a -> b;
b -> c:
c -> a;
}
#+END_SRC
#+RESULTS:
[[file:some-illustration.png]]
- Unicode References :: for instance, \alpha, \beta and \gamma.
- Subscripts :: like Hydrogen atoms, H_2, and Water, H_{2}O.
- Superscripts :: The mass of the sun is 1.989 x 10^30 kg.
- Embedded Equations :: Surrounded with either single =$=, like $a^2=b$,
or escaped parenthesis, like: \( b=\frac{1}{2} \)
- Separated equations :: Either in double =$$= or escaped brackets, like
this: $$ a=\frac{1}{2}\sqrt{2} $$ or this: \[ a=-\sqrt{2} \] or this:
\begin{equation}
x=\sqrt{b}
\end{equation}
#+OPTIONS: tex:t
Sinon, une méthode plus simple à comprendre et ne faisant pas
intervenir d'appel à la fonction sinus se base sur le fait que si
$X \sim U(0,1)$ et $Y \sim U(0,1)$ alors $P[X^2 + Y^2 ≤ 1] = \pi/4$ (voir
[[https://fr.wikipedia.org/wiki/Méthode_de_Monte-Carlo#Détermination_de_la_valeur_de_π][méthode de Monte Carlo sur Wikipedia]]). Le code suivant illustre ce fait :
#+begin_src R :results output graphics :file (org-babel-temp-file "figure" ".png") :exports both :width 600 :height 400 :session *R*
set.seed(42)
N = 1000
df = data.frame(X = runif(N), Y = runif(N))
df$Accept = (df$X**2 + df$Y**2 <=1)
library(ggplot2)
ggplot(df, aes(x=X,y=Y,color=Accept)) + geom_point(alpha=.2) + coord_fixed() + theme_bw()
#+end_src
Il est alors aisé d'obtenir une approximation (pas terrible) de $\pi$
en comptant combien de fois, en moyenne, \$X^2+Y^2 est inférieur à 1 :
#+begin_src R :results output :session *R* :exports both
4*mean(df$Accept)
#+end_src
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment