Gaussian Integral Approximation¶
Write a function
gaussian_integral(orgaussianIntegraldepending on the language's case convention) that takes:- a 64-bit floating point number $a$ (you may assume $a > 0$),
- a 64-bit integer $n$ (you may assume $n \ge 1$),
and returns an approximation of
$$ \int_{-a}^{a} e^{-x^2} \, dx $$
using $n$ equal-width subintervals and the left-endpoint Riemann sum.
- Let
$$ \Delta x = \frac{2a}{n} $$
and approximate the integral by
$$ \sum_{i=0}^{n-1} e^{-x_i^2} \, \Delta x, \quad x_i = -a + i \Delta x. $$
- Use:
- 64-bit floating point numbers for $a$ and the return value,
- an integer for $n$.
Note¶
As $a$ becomes large, this value approaches $\sqrt{\pi}$.
Boilerplate source files
{go,jl,ml,rs}/gaussian_integral.{go,jl,ml,rs}containing the test code is generated and shown below.Edit the source files either by opening them in a text editor (e.g., vscode), or editing the cells below and executing them.
import heytutor
1-2. Examples¶
1-2-1. A general question¶
%%hey
How to write a function in Go?
1-2-2. A hint on this specific problem¶
%%hey
Give me a hint on this problem for Rust
1-2-3. NEW: A few builtin variables¶
{file:FILENAME}is the content of FILE{bash[-1]}is the output of the last%%bash_cell,{bash[-2]}that of the second last%%bash_cell, etc.{problem}is the content of the file you specified by%%hey problem_file=foo.md{answer}is the content of the file you specified by%%hey answer_file=go/foo.go
1-2-4. Help when you struggle¶
%%hey answer_file=go/foo.go
I get this error when I compile it. What's wrong?"
My program:
{answer}
Error message:
{bash[-1]}
1-2-5. Ask feedback¶
- You are encouraged to ask a feedback once you think you are done with the problem, to know if there is a better answer. You can do so by something like:
%%hey problem_file=foo.md answer_file=go/foo.md
Give me a feedback to my answer.
Problem:
{problem}
My Answer:
{answer}
2. Go¶
2-1. Baseline code¶
import heytutor
%%writefile_ go/gaussian_integral.go
package main
import "math"
import "fmt"
/** begin my answer */
func gaussianIntegral(a float64, n int64) float64 {
dx := 2.0 * a / float64(n)
sum := 0.0
for i := int64(0); i < n; i++ {
x := -a + float64(i)*dx
sum += math.Exp(-x*x) * dx
}
return sum
}
/** end my answer */
func main() {
if !(math.Abs(gaussianIntegral(1, 1000) - 1.493648) < 1.0e-6) { panic("wrong") }
if !(math.Abs(gaussianIntegral(2, 2000) - 1.764163) < 1.0e-6) { panic("wrong") }
if !(math.Abs(gaussianIntegral(10, 10000) - math.Sqrt(math.Pi)) < 1.0e-6) { panic("wrong") }
fmt.Println("OK")
}
2-2. Compile¶
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/gaussian_integral go/gaussian_integral.go
- Note: when you run
goor other Go commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/go/bin) - You may consider adding that line in your
~/.bash_profile
2-3. Run¶
%%bash_
go/gaussian_integral
2-4. Ask Questions or Get Feedback¶
%%hey problem_file=gaussian_integral.md answer_file=go/gaussian_integral.go
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.
3. Julia¶
3-1. Baseline code¶
import heytutor
%%writefile_ jl/gaussian_integral.jl
### begin my answer
function gaussian_integral(a, n)
dx = 2a / n
s = 0.0
for i in 0:n-1
x = -a + i * dx
s += exp(-(x * x)) * dx
end
s
end
### end my answer
function main()
@assert abs(gaussian_integral(1, 1000) - 1.493648) < 1.0e-6
@assert abs(gaussian_integral(2, 2000) - 1.764163) < 1.0e-6
@assert abs(gaussian_integral(10, 10000) - sqrt(pi)) < 1.0e-6
println("OK")
end
main()
3-2. Compile¶
- Julia code is compiled "just in time" (compiled upon executed), so does not need a specific action for compilation before you run
3-3. Run¶
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/gaussian_integral.jl
- Note: when you run
juliaor other Julia commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/.juliaup/bin) - You may consider adding that line in your
~/.bash_profile
3-4. Interactive execution¶
juliacommand also serves is an interactive command for Julia programsYou can run a source code and continue interaction
$ julia -i jl/gaussian_integral.jl
- For trial and error, you may also consider creating a Julia notebook
3-5. Ask Questions or Get Feedback¶
%%hey problem_file=gaussian_integral.md answer_file=jl/gaussian_integral.jl
Problem:
{problem}
My Answer (between ### begin my answer and ### end my answer):
{answer}
Give me a feedback to my answer.
4. OCaml¶
4-1. Baseline code¶
import heytutor
%%writefile_ ml/gaussian_integral.ml
(** begin my answer *)
let gaussian_integral a n =
let dx = 2.0 *. a /. float_of_int n in
let s = ref 0.0 in
for i = 0 to n - 1 do
let x = -. a +. float_of_int i *. dx in
s := !s +. exp (-. x *. x) *. dx
done;
!s;;
(** end my answer *)
let main () =
assert (abs_float (gaussian_integral 1.0 1000) -. 1.493648 < 1.0e-6);
assert (abs_float (gaussian_integral 2.0 2000) -. 1.764163 < 1.0e-6);
assert (abs_float (gaussian_integral 10.0 10000) -. Float.sqrt Float.pi < 1.0e-6);
Printf.printf "OK\n"
;;
main()
4-2. Compile¶
%%bash_
eval $(opam env)
ocamlc ml/gaussian_integral.ml -o ml/gaussian_integral
- Note: when you run
ocamlcor other OCaml commands (see below) in a terminal (SSH or Jupyter terminal), you need to execute the first line (eval $(opam env)) - You may consider adding that line in your
~/.bash_profile
4-3. Run¶
%%bash_
ml/gaussian_integral
4-4. Interactive execution¶
ocamlcommand is an interactive command for OCaml programsIn terminal (Jupyter or SSH), you can directly run a source code
$ eval $(opam env) # once in your session or put it in ~/.bash_profile
$ ocaml ml/gaussian_integral.ml
- You can run a source code and continue interaction
$ eval $(opam env) # once in your session or put it in ~/.bash_profile
$ ocaml -init ml/gaussian_integral.ml
- For trial and error, you may also consider creating an OCaml notebook
4-5. Ask Questions or Get Feedback¶
%%hey problem_file=gaussian_integral.md answer_file=ml/gaussian_integral.ml
Problem:
{problem}
My Answer (between (** begin my answer *) and (** end my answer *)):
{answer}
Give me a feedback to my answer.
5. Rust¶
5-1. Baseline code¶
import heytutor
%%writefile_ rs/gaussian_integral.rs
/** begin my answer */
fn gaussian_integral(a: f64, n: i64) -> f64 {
let dx = 2.0 * a / (n as f64);
let mut sum = 0.0;
for i in 0..n {
let x = -a + (i as f64) * dx;
sum += (-x * x).exp() * dx;
}
sum
}
/** end my answer */
fn main() {
let pi = std::f64::consts::PI;
assert!((gaussian_integral(1.0, 1000) - 1.493648).abs() < 1.0e-6);
assert!((gaussian_integral(2.0, 2000) - 1.764163).abs() < 1.0e-6);
assert!((gaussian_integral(10.0, 10000) - pi.sqrt()).abs() < 1.0e-6);
println!("OK");
}
5-2. Compile¶
%%bash_
. ~/.cargo/env
rustc rs/gaussian_integral.rs -o rs/gaussian_integral
- Note: when you run
rustcor other Rust commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (. ~/.cargo/env) - You may consider adding that line in your
~/.bash_profile
5-3. Run¶
%%bash_
rs/gaussian_integral
5-4. Ask Questions or Get Feedback¶
%%hey problem_file=gaussian_integral.md answer_file=rs/gaussian_integral.rs
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.