Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs417-lecture-examples
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Kennedy
cs417-lecture-examples
Commits
1429b6f2
Commit
1429b6f2
authored
4 years ago
by
Thomas Kennedy
Browse files
Options
Downloads
Patches
Plain Diff
Clean up argument parsing
parent
6e6036b4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MonteCarloIntegration/Rust/src/main.rs
+33
-7
33 additions, 7 deletions
MonteCarloIntegration/Rust/src/main.rs
with
33 additions
and
7 deletions
MonteCarloIntegration/Rust/src/main.rs
+
33
−
7
View file @
1429b6f2
...
...
@@ -13,7 +13,6 @@ struct Point(f64, f64);
///
/// Yields:
/// A sequence of points in the form (x, f(x))
fn
generate_random_points
(
f
:
fn
(
f64
)
->
f64
,
lower_limit
:
f64
,
...
...
@@ -31,20 +30,47 @@ fn generate_random_points(
.collect
()
}
/// Handle positional command line argument validation.
///
/// # Return
/// Tuple in the form (lower limit, upper limit, maximum magnitude)
fn
parse_cmd_args
()
->
(
f64
,
f64
,
u32
)
{
let
args
:
Vec
<
String
>
=
env
::
args
()
.collect
();
if
args
.len
()
<
4
{
eprintln!
(
"Usage:
\n
{} not_used num_points a b max_magnitude"
,
args
[
0
]
);
std
::
process
::
exit
(
1
);
}
let
limit_a
=
args
[
2
]
.parse
::
<
f64
>
()
.unwrap_or_else
(|
_err
|
std
::
process
::
exit
(
2
));
let
limit_b
=
args
[
3
]
.parse
::
<
f64
>
()
.unwrap_or_else
(|
_err
|
std
::
process
::
exit
(
3
));
let
max_magnitude
=
args
[
4
]
.parse
::
<
u32
>
()
.unwrap_or_else
(|
_err
|
std
::
process
::
exit
(
4
));
(
limit_a
,
limit_b
,
max_magnitude
)
}
/// This main demonstrates the impact of the number of points on Monte Carlo
/// integration
fn
main
()
{
let
num_points
=
env
::
args
()
.nth
(
1
)
.unwrap
()
.parse
::
<
u64
>
();
// Unused in this version of main
let
limit_a
=
env
::
args
()
.nth
(
2
)
.unwrap
()
.parse
::
<
f64
>
()
.unwrap
();
let
limit_b
=
env
::
args
()
.nth
(
3
)
.unwrap
()
.parse
::
<
f64
>
()
.unwrap
();
let
max_magnitude
=
env
::
args
()
.nth
(
4
)
.unwrap
()
.parse
::
<
u32
>
()
.unwrap
();
let
(
limit_a
,
limit_b
,
max_magnitude
)
=
parse_cmd_args
();
let
math_f
=
|
x
:
f64
|
x
.powf
(
2_f64
);
println!
(
"| {:^16} | {:^20} |"
,
"# Points"
,
"Est. f(x)"
);
let
max_num_points
:
u64
=
2_u64
.pow
(
max_magnitude
);
let
point_sequence
=
(
generate_random_points
(
math_f
,
limit_a
,
limit_b
,
max_num_points
)
)
;
let
point_sequence
=
generate_random_points
(
math_f
,
limit_a
,
limit_b
,
max_num_points
);
for
magnitude
in
0
..=
max_magnitude
{
let
num_points
=
2_u64
.pow
(
magnitude
);
...
...
@@ -52,7 +78,7 @@ fn main() {
let
sum_of_f_of_x_values
:
f64
=
point_sequence
.iter
()
.map
(|
point
|
point
.1
)
.take
(
(
num_points
as
usize
)
)
.take
(
num_points
as
usize
)
.sum
();
let
integral_result
=
(
limit_b
-
limit_a
)
/
(
num_points
as
f64
)
*
sum_of_f_of_x_values
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment