Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #16

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
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
Binary file added a.out
Binary file not shown.
496 changes: 496 additions & 0 deletions addfunction.ml

Large diffs are not rendered by default.

495 changes: 495 additions & 0 deletions addfunctiontemp.ml

Large diffs are not rendered by default.

Binary file added const.cmi
Binary file not shown.
118 changes: 118 additions & 0 deletions const.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,194 @@
open Num
open Interval

(*
description: Type for constants, which can be a num (build-in of Ocaml) or interval
*)
type t = Rat of num | Interval of interval

(*
input: num
Copy link
Member

Choose a reason for hiding this comment

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

Instead of writing input and output, it is better to add explicit type signatures:

let of_num (n : num) : t = ...

output: t
description: coverts num to constant (t type)
*)
let of_num n = Rat n

(*
input: int
output: t
description: coverts int to constant (t type)
*)
let of_int i = Rat (Int i)

(*
input: float
output: t
description: cover float to constant (t type)
*)
let of_float f = Rat (More_num.num_of_float f)

(*
input: interval
output: t
description: coverts interval to constant (t type)
*)
let of_interval v =
if v.low = v.high then of_float v.low
else if v.low < v.high && neg_infinity < v.high && v.low < infinity then Interval v
else failwith ("Const.of_interval: bad argument: " ^ sprintf_I "%.20e" v)

(*
input: t
output: bool
description: return true if the constrant is a num
*)
let is_rat = function
| Rat _ -> true
| Interval _ -> false

(*
input: interval
output: bool
description: return true if the constant is an interval
*)
let is_interval = function
| Rat _ -> false
| Interval _ -> true

(*
input: t
output: interval
description: cover a constant to an interval, if it is a num, return the interval of 2 64-bit floats that contains the num with smallest range
*)
let to_interval = function
| Interval v -> v
| Rat n -> More_num.interval_of_num n

(*
input: t
output: foat
description: cover a constant to float, if it is an interval, return the mean of 2 bounds
*)
let to_float = function
| Interval v -> 0.5 *. (v.low +. v.high)
| Rat n -> Num.float_of_num n

(*
input: t
output: num
description: cover a constant to num, if it is an interval, raise an error
*)
let to_num = function
| Rat n -> n
| Interval _ -> failwith "Const.to_num: interval constant"

(*
input: t
output: int
description: cover a constant to integer
*)
let to_int c = int_of_num (to_num c)

(*
input: t
output: num
description: coverts the lower bound of an interval constant to num, if the constant is num, just return it
*)
let low_bound_to_num = function
| Rat n -> n
| Interval v -> More_num.num_of_float v.low

(*
input: t
output: num
description: coverts the upper bound of an interval constant to num, if the constant is num, just return it
*)
let high_bound_to_num = function
| Rat n -> n
| Interval v -> More_num.num_of_float v.high

(*
input: (num->num,interval->interval), t
output: t
description: apply the unary function/operator over the constant and return the new constrant
*)
let lift_u_ops (op_n, op_i) c =
match c with
| Rat n -> of_num (op_n n)
| Interval v -> of_interval (op_i v)

(*
input: (num->num->num,interval->interval->interval), t
output: t
description: apply the binary function/operator over the constant and return the new constrant
*)
let lift_bin_ops (op_n, op_i) c1 c2 =
match (c1, c2) with
| Rat n1, Rat n2 -> of_num (op_n n1 n2)
| Interval _, _ | _, Interval _ ->
of_interval (op_i (to_interval c1) (to_interval c2))

(*
input: t
output: t
description: return the constant as the negative of the original one
*)
let neg_c = lift_u_ops (minus_num, (~-$))

(*
input: t
output: t
description: return the constant as the absolute value of the original one
*)
let abs_c = lift_u_ops (abs_num, abs_I)

(*
input: t,t
output: t
description: return the constant as the minimum of the 2 constants
*)
let min_c = lift_bin_ops (min_num, min_I_I)

(*
input: t,t
output: t
description: return the constant as the maximum of the 2 constants
*)
let max_c = lift_bin_ops (max_num, max_I_I)

(*
input: t,t
output: t
description: return the constant as the addition of the 2 constants
*)
let add_c = lift_bin_ops (add_num, (+$))

(*
input: t,t
output: t
description: return the constant as the subtraction of the 2 constants
*)
let sub_c = lift_bin_ops (sub_num, (-$))

(*
input: t,t
output: t
description: return the constant as the multiplication of the 2 constants
*)
let mul_c = lift_bin_ops (mult_num, ( *$ ))

(*
input: t,t
output: t
description: return the constant as the division of the 2 constants
*)
let div_c = lift_bin_ops (div_num, (/$))

(*
input: t, t
output: bool
description: return true if the 2 constants are the same
*)
let eq_c c1 c2 =
match (c1, c2) with
| Rat n1, Rat n2 -> n1 =/ n2
Expand Down
2 changes: 1 addition & 1 deletion default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,4 @@ nlopt-lib = -lnlopt -lm
# Extra options
#**************************

develop = false
develop = false
36 changes: 35 additions & 1 deletion eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ open Expr
(* Computes a floating-point value of an expression *)
(* vars : string -> float is a function which associates
floating-point values with variable names *)
(*
input: string->float
output: float
description: evaluate an expression and return a float value, the first argument is a function which return the value (float) of a variable's name
*)
let eval_float_expr vars =
let rec eval = function
| Const c -> Const.to_float c
Expand Down Expand Up @@ -74,12 +79,22 @@ let eval_float_expr vars =
in
eval

(*
input: expr
output: float
description: estimate a constant expression and return a float value
*)
let eval_float_const_expr =
eval_float_expr (fun v -> failwith ("eval_float_const_expr: Var " ^ v))

(* Computes a rational value of an expression *)
(* vars : string -> num is a function which associates
rational values with variable names *)
(*
input: string->num
output: num
description: estimate an expression and return a float value, the first argument is a function which return the value (num) of a variable's name
*)
let eval_num_expr vars =
let one = Int 1 in
let rec eval = function
Expand Down Expand Up @@ -125,12 +140,22 @@ let eval_num_expr vars =
in
eval

(*
input: expr
output: num
description: estimate a constant expression and return a num value
*)
let eval_num_const_expr =
eval_num_expr (fun v -> failwith ("eval_num_const_expr: Var " ^ v))

(* Computes an interval value of an expression *)
(* vars : string -> interval is a function which associates
inteval values with variable names *)
(*
input: string->interval
output: interval
description: estimate an expression and return a float value, the first argument is a function which return the value (interval) of a variable's name
*)
let eval_interval_expr vars =
let rec eval = function
| Const c -> Const.to_interval c
Expand Down Expand Up @@ -191,10 +216,19 @@ let eval_interval_expr vars =
in
eval

(*
input: expr
output: interval
description: estimate a constant expression and return an interval value
*)
let eval_interval_const_expr =
eval_interval_expr (fun v -> failwith ("eval_interval_const_expr: Var " ^ v))


(*
input: expr
output: Const.t
description: estimate a constant expression and return a constant
*)
let eval_const_expr e =
Log.report `Debug "eval_const_expr: %s" (ExprOut.Info.print_str e);
let n = eval_num_const_expr e in
Expand Down
Loading