Minimize a Polynomial¶

  • Consider the function

$$ f(x) = x^4 - 3x^3 + 2x^2 + x + 1. $$

  • Write a function min_value (or minValue) that takes:

    • 64-bit floating point numbers $a$ and $b$ with $a < b$,
    • a 64-bit integer $n$ (you may assume $n \ge 1$),

    and returns an approximation of the minimum value of $f(x)$ over the interval $[a, b]$.

  • Use the sample points

$$ x_i = a + \frac{(b-a)i}{n}, \qquad i = 0,1,\dots,n. $$

  • Return

$$ \min_i f(x_i). $$

  • Use:

    • 64-bit floating point numbers for all real values,
    • an integer for $n$.
  • Boilerplate source files {go,jl,ml,rs}/minimize_polynomial.{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.

1. AI tutor¶

1-1. Prepare¶

  • Your personal AI tutor is provided for questions and feedback
  • Execute the following cell before you use it
In [ ]:
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¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ go/minimize_polynomial.go
package main
import "math"
import "fmt"

/** begin my answer */

func minValue(a, b float64, n int64) float64 {
	best := a*a*a*a - 3.0*a*a*a + 2.0*a*a + a + 1.0
	for i := int64(1); i <= n; i++ {
		x := a + (b-a)*float64(i)/float64(n)
		v := x*x*x*x - 3.0*x*x*x + 2.0*x*x + x + 1.0
		if v < best {
			best = v
		}
	}
	return best
}
/** end my answer */

func main() {
	if !(math.Abs(minValue(-1, 1, 1000) - 0.903266) < 1.0e-6) { panic("wrong") }
	if !(math.Abs(minValue( 1, 2, 1000) - 1.928766) < 1.0e-6) { panic("wrong") }
	fmt.Println("OK")
}

2-2. Compile¶

In [ ]:
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/minimize_polynomial go/minimize_polynomial.go
  • Note: when you run go or 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¶

In [ ]:
%%bash_
go/minimize_polynomial

2-4. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=minimize_polynomial.md answer_file=go/minimize_polynomial.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¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ jl/minimize_polynomial.jl
### begin my answer

function min_value(a, b, n)
    f(x) = x^4 - 3x^3 + 2x^2 + x + 1
    best = f(a)
    for i in 1:n
        x = a + (b - a) * i / n
        v = f(x)
        if v < best
            best = v
        end
    end
    best
end
### end my answer

function main()
    @assert abs(min_value(-1, 1, 1000) - 0.903266) < 1.0e-6
    @assert abs(min_value( 1, 2, 1000) - 1.928766) < 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¶

In [ ]:
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/minimize_polynomial.jl
  • Note: when you run julia or 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¶

  • julia command also serves is an interactive command for Julia programs

  • You can run a source code and continue interaction

$ julia -i jl/minimize_polynomial.jl
  • For trial and error, you may also consider creating a Julia notebook

3-5. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=minimize_polynomial.md answer_file=jl/minimize_polynomial.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¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ ml/minimize_polynomial.ml
(** begin my answer *)

let min_value a b n =
  let f x = x *. x *. x *. x -. 3.0 *. x *. x *. x +. 2.0 *. x *. x +. x +. 1.0 in
  let best = ref (f a) in
  for i = 1 to n do
    let x = a +. (b -. a) *. float_of_int i /. float_of_int n in
    let v = f x in
    if v < !best then best := v
  done;
  !best;;
(** end my answer *)

let main () =
  assert ((abs_float (min_value (-1.0) 1.0 1000) -. 0.903266) < 1.0e-6);
  assert ((abs_float (min_value   1.0  2.0 1000) -. 1.928766) < 1.0e-6);
  Printf.printf "OK\n"
;;

main()

4-2. Compile¶

In [ ]:
%%bash_
eval $(opam env)
ocamlc ml/minimize_polynomial.ml -o ml/minimize_polynomial
  • Note: when you run ocamlc or 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¶

In [ ]:
%%bash_
ml/minimize_polynomial

4-4. Interactive execution¶

  • ocaml command is an interactive command for OCaml programs

  • In 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/minimize_polynomial.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/minimize_polynomial.ml
  • For trial and error, you may also consider creating an OCaml notebook

4-5. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=minimize_polynomial.md answer_file=ml/minimize_polynomial.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¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ rs/minimize_polynomial.rs
/** begin my answer */

fn min_value(a: f64, b: f64, n: i64) -> f64 {
    let f = |x: f64| x*x*x*x - 3.0*x*x*x + 2.0*x*x + x + 1.0;
    let mut best = f(a);
    for i in 1..=n {
        let x = a + (b - a) * (i as f64) / (n as f64);
        let v = f(x);
        if v < best {
            best = v;
        }
    }
    best
}
/** end my answer */

fn main() {
    assert!((min_value(-1.0, 1.0, 1000) - 0.903266).abs() < 1.0e-6);
    assert!((min_value( 1.0, 2.0, 1000) - 1.928766).abs() < 1.0e-6);
    println!("OK");
}

5-2. Compile¶

In [ ]:
%%bash_
. ~/.cargo/env
rustc rs/minimize_polynomial.rs -o rs/minimize_polynomial
  • Note: when you run rustc or 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¶

In [ ]:
%%bash_
rs/minimize_polynomial

5-4. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=minimize_polynomial.md answer_file=rs/minimize_polynomial.rs

Problem:
{problem}

My Answer (between /** begin my answer */ and /** end my answer */):
{answer}

Give me a feedback to my answer.