added to json function
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
/Cargo.lock
|
/Cargo.lock
|
||||||
|
*/output.json
|
||||||
11
Cargo.toml
11
Cargo.toml
@@ -7,3 +7,14 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies.rand]
|
[dependencies.rand]
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
|
|
||||||
|
[dependencies.serde]
|
||||||
|
version = "1.0.200"
|
||||||
|
features = ["derive"]
|
||||||
|
|
||||||
|
[dependencies.axum]
|
||||||
|
version = "0.7.5"
|
||||||
|
features = ["json"]
|
||||||
|
|
||||||
|
[dependencies.serde_json]
|
||||||
|
version = "1.0.116"
|
||||||
16
src/main.rs
16
src/main.rs
@@ -1,3 +1,6 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
use crate::bounds::Bounds;
|
use crate::bounds::Bounds;
|
||||||
use crate::function::Function;
|
use crate::function::Function;
|
||||||
use crate::linear_bounds::LinearBounds;
|
use crate::linear_bounds::LinearBounds;
|
||||||
@@ -15,14 +18,14 @@ fn main() {
|
|||||||
let bounds: Bounds = Bounds::new(LinearBounds::new(0_f64, 20_f64), LinearBounds::new(-100_f64, 150000_f64));
|
let bounds: Bounds = Bounds::new(LinearBounds::new(0_f64, 20_f64), LinearBounds::new(-100_f64, 150000_f64));
|
||||||
let func: Function = Function::new();
|
let func: Function = Function::new();
|
||||||
|
|
||||||
let sample_limit: usize = 1_000;
|
let sample_limit: usize = 1_000_000_000;
|
||||||
let sub_samples: Vec<usize> = vec![1, 2, 5];
|
let sub_samples: Vec<usize> = vec![1, 2, 5];
|
||||||
let samples_per_iteration: usize = 10;
|
let samples_per_iteration: usize = 1;
|
||||||
|
|
||||||
let mut result: Plot = Plot::new();
|
let mut result: Plot = Plot::new();
|
||||||
|
|
||||||
let mut sample_cnt: usize = 1;
|
let mut sample_cnt: usize = 1;
|
||||||
while sample_cnt <= sample_limit {
|
while sample_cnt < sample_limit {
|
||||||
for sub_sample in sub_samples.clone() {
|
for sub_sample in sub_samples.clone() {
|
||||||
let samples: usize = sub_sample * sample_cnt;
|
let samples: usize = sub_sample * sample_cnt;
|
||||||
for _ in 0..samples_per_iteration {
|
for _ in 0..samples_per_iteration {
|
||||||
@@ -32,4 +35,11 @@ fn main() {
|
|||||||
sample_cnt *= 10;
|
sample_cnt *= 10;
|
||||||
}
|
}
|
||||||
result.print();
|
result.print();
|
||||||
|
|
||||||
|
let mut file: io::Result<File> = File::create("output.json");
|
||||||
|
if file.is_err() {
|
||||||
|
panic!("Error creating output file")
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(_) = file.unwrap().write(result.to_json().to_string().as_bytes()) { panic!("Error writing to file") };
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/plot.rs
20
src/plot.rs
@@ -1,6 +1,9 @@
|
|||||||
use std::iter::zip;
|
use std::iter::zip;
|
||||||
|
use axum::extract::FromRef;
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct Plot {
|
pub struct Plot {
|
||||||
x: Vec<usize>,
|
x: Vec<usize>,
|
||||||
y: Vec<Vec<f64>>,
|
y: Vec<Vec<f64>>,
|
||||||
@@ -21,9 +24,20 @@ impl Plot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print(self) -> () {
|
pub fn print(&self) -> () {
|
||||||
for (x, y) in zip(self.x, self.y) {
|
let plot: Plot = Plot::from_ref(self);
|
||||||
|
for (x, y) in zip(plot.x, plot.y) {
|
||||||
println!("{}: {:?}", x, y);
|
println!("{}: {:?}", x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_json(&self) -> Value {
|
||||||
|
return json!(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Plot {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
return Plot {x: self.x.clone(), y: self.y.clone()}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user