-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
bb14b84
84abde5
36dfac2
3700505
3112740
3552287
f8a48c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,76 +13,196 @@ | |
open Num | ||
open Interval | ||
|
||
(* | ||
input: no | ||
output: no | ||
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 | ||
output: t | ||
description: cover num to constrant (t type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cover -> converts |
||
*) | ||
let of_num n = Rat n | ||
|
||
(* | ||
input: int | ||
output: t | ||
description: cover int to constrant (t type) | ||
*) | ||
let of_int i = Rat (Int i) | ||
|
||
(* | ||
input: float | ||
output: t | ||
description: cover float to constrant (t type) | ||
*) | ||
let of_float f = Rat (More_num.num_of_float f) | ||
|
||
(* | ||
input: interval | ||
output: t | ||
description: cover interval to constrant (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 constrant 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: cover 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: cover 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: estimate an expression and return a float value, the first argument is a function which return the value (float) of a variable's name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to write: |
||
*) | ||
let eval_float_expr vars = | ||
let rec eval = function | ||
| Const c -> Const.to_float c | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of writing
input
andoutput
, it is better to add explicit type signatures: