Skip to content

Commit

Permalink
Merge branch 'fix' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Axect committed Oct 31, 2024
2 parents 1294707 + b2e1cea commit 0d8fb88
Show file tree
Hide file tree
Showing 66 changed files with 1,900 additions and 991 deletions.
4 changes: 1 addition & 3 deletions examples/b_spline_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ fn main() -> Result<(), Box<dyn Error>> {
vec![1f64, 2f64],
];


let spline = BSpline::clamped(degree, knots, control_points.clone())?;
let t = linspace(0f64, 3f64, 200);
let (x, y): (Vec<f64>, Vec<f64>) = spline.eval_vec(&t).into_iter().unzip();
Expand All @@ -23,8 +22,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let control_y = control_points.iter().map(|v| v[1]).collect::<Vec<f64>>();

let mut plt = Plot2D::new();
plt
.insert_pair((x.clone(), y.clone()))
plt.insert_pair((x.clone(), y.clone()))
.insert_pair((control_x.clone(), control_y.clone()))
.set_plot_type(vec![(1, PlotType::Scatter)])
.set_color(vec![(0, "darkblue"), (1, "red")])
Expand Down
13 changes: 7 additions & 6 deletions examples/broyden_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use peroxide::fuga::*;
use peroxide::numerical::root::{Pt, Intv};
use peroxide::numerical::root::{Intv, Pt};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let problem = Quadratic;
let broyden = BroydenMethod { max_iter: 100, tol: 1e-6, rtol: 1e-6 };
let broyden = BroydenMethod {
max_iter: 100,
tol: 1e-6,
rtol: 1e-6,
};
let root = broyden.find(&problem)?;
let result = problem.function(root)?;

Expand All @@ -17,10 +21,7 @@ struct Quadratic;

impl RootFindingProblem<2, 2, Intv<2>> for Quadratic {
fn function(&self, x: Pt<2>) -> anyhow::Result<Pt<2>> {
Ok([
x[0] * x[0] + x[1] * x[1] - 1.0,
x[0] + x[1] - 2f64.sqrt()
])
Ok([x[0] * x[0] + x[1] * x[1] - 1.0, x[0] + x[1] - 2f64.sqrt()])
}

fn initial_guess(&self) -> Intv<2> {
Expand Down
2 changes: 1 addition & 1 deletion examples/chebyshev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use peroxide::fuga::*;
fn main() {
let mut c = chebyshev_polynomial(0, SpecialKind::First);
c.print();
for i in 1 .. 11 {
for i in 1..11 {
c = chebyshev_polynomial(i, SpecialKind::First);
c.print();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate peroxide;
use peroxide::fuga::*;

fn main() {
let x = c!(1,2,3,4);
let x = c!(1, 2, 3, 4);
let a = Series::new(x);
a.print();
println!("");
Expand All @@ -19,7 +19,7 @@ fn main() {
df.print();
println!("");

df["1"] = Series::new(c!(5,6,7,8));
df["1"] = Series::new(c!(5, 6, 7, 8));

df.print();
println!("");
Expand Down
4 changes: 2 additions & 2 deletions examples/df_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate peroxide;
use peroxide::fuga::*;

fn main() -> Result<(), Box<dyn Error>> {
let a = Series::new(vec![1,2,3,4]);
let a = Series::new(vec![1, 2, 3, 4]);
let b = Series::new(c!(0.1, 0.2, 0.3, 0.4));
let c = Series::new(vec![true, false, true, false]);
let d = Series::new(vec!['a', 'b', 'c', 'd']);
Expand All @@ -13,7 +13,7 @@ fn main() -> Result<(), Box<dyn Error>> {

df.print();

#[cfg(feature="csv")]
#[cfg(feature = "csv")]
{
df.write_csv("example_data/df_csv.csv")?;

Expand Down
12 changes: 9 additions & 3 deletions examples/df_nc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ extern crate peroxide;
use peroxide::fuga::*;

fn main() -> Result<(), Box<dyn Error>> {
#[cfg(feature = "nc")] {
#[cfg(feature = "nc")]
{
let a = Series::new(vec![1, 2, 3, 4]);
let b = Series::new(vec![0.1, 0.2, 0.3, 0.4]);
let c = Series::new(vec![true, false, false, true]);
let d = Series::new(vec!["a", "b", "c", "d"].into_iter().map(|x| x.to_string()).collect());
let d = Series::new(
vec!["a", "b", "c", "d"]
.into_iter()
.map(|x| x.to_string())
.collect(),
);

let mut df = DataFrame::new(vec![a,b,c,d]);
let mut df = DataFrame::new(vec![a, b, c, d]);
df.set_header(vec!["a", "b", "c", "d"]);
println!("Write:");
df.print();
Expand Down
10 changes: 5 additions & 5 deletions examples/gradient.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use peroxide::fuga::*;

fn main() {
f(2f64).print(); // x^3 = 8
f_grad(2f64).print(); // 3 * x^2 = 12
f_hess(2f64).print(); // 6 * x = 12
f(2f64).print(); // x^3 = 8
f_grad(2f64).print(); // 3 * x^2 = 12
f_hess(2f64).print(); // 6 * x = 12
}

#[ad_function] // generates f_grad, f_hess
#[ad_function] // generates f_grad, f_hess
fn f(x: f64) -> f64 {
x.powi(3) // x^3
x.powi(3) // x^3
}
9 changes: 2 additions & 7 deletions examples/lorenz_dp45.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let dp45 = DP45::new(1e-4, 0.9, 1e-6, 1e-1, 100);
let basic_ode_solver = BasicODESolver::new(dp45);
let (_, y_vec) = basic_ode_solver.solve(
&Lorenz,
(0f64, 100f64),
1e-2,
)?;
let (_, y_vec) = basic_ode_solver.solve(&Lorenz, (0f64, 100f64), 1e-2)?;
let y_mat = py_matrix(y_vec);
let y0 = y_mat.col(0);
let y2 = y_mat.col(2);

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(y0)
plt.set_domain(y0)
.insert_image(y2)
.set_xlabel(r"$y_0$")
.set_ylabel(r"$y_2$")
Expand Down
9 changes: 2 additions & 7 deletions examples/lorenz_gl4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let gl4 = GL4::new(ImplicitSolver::FixedPoint, 1e-6, 100);
let basic_ode_solver = BasicODESolver::new(gl4);
let (_, y_vec) = basic_ode_solver.solve(
&Lorenz,
(0f64, 100f64),
1e-2,
)?;
let (_, y_vec) = basic_ode_solver.solve(&Lorenz, (0f64, 100f64), 1e-2)?;
let y_mat = py_matrix(y_vec);
let y0 = y_mat.col(0);
let y2 = y_mat.col(2);

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(y0)
plt.set_domain(y0)
.insert_image(y2)
.set_xlabel(r"$y_0$")
.set_ylabel(r"$y_2$")
Expand Down
9 changes: 2 additions & 7 deletions examples/lorenz_rk4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ use peroxide::fuga::*;
#[allow(unused_variables)]
fn main() -> Result<(), Box<dyn Error>> {
let basic_ode_solver = BasicODESolver::new(RK4);
let (_, y_vec) = basic_ode_solver.solve(
&Lorenz,
(0f64, 100f64),
1e-2,
)?;
let (_, y_vec) = basic_ode_solver.solve(&Lorenz, (0f64, 100f64), 1e-2)?;
let y_mat = py_matrix(y_vec);
let y0 = y_mat.col(0);
let y2 = y_mat.col(2);

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(y0)
plt.set_domain(y0)
.insert_image(y2)
.set_xlabel(r"$y_0$")
.set_ylabel(r"$y_2$")
Expand Down
9 changes: 2 additions & 7 deletions examples/lorenz_rkf45.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let rkf45 = RKF45::new(1e-4, 0.9, 1e-6, 1e-2, 100);
let basic_ode_solver = BasicODESolver::new(rkf45);
let (_, y_vec) = basic_ode_solver.solve(
&Lorenz,
(0f64, 100f64),
1e-2,
)?;
let (_, y_vec) = basic_ode_solver.solve(&Lorenz, (0f64, 100f64), 1e-2)?;
let y_mat = py_matrix(y_vec);
let y0 = y_mat.col(0);
let y2 = y_mat.col(2);

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(y0)
plt.set_domain(y0)
.insert_image(y2)
.set_xlabel(r"$y_0$")
.set_ylabel(r"$y_2$")
Expand Down
9 changes: 2 additions & 7 deletions examples/lorenz_tsit45.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let tsit45 = TSIT45::new(1e-2, 0.9, 1e-6, 1e-1, 100);
let basic_ode_solver = BasicODESolver::new(tsit45);
let (_, y_vec) = basic_ode_solver.solve(
&Lorenz,
(0f64, 100f64),
1e-2,
)?;
let (_, y_vec) = basic_ode_solver.solve(&Lorenz, (0f64, 100f64), 1e-2)?;
let y_mat = py_matrix(y_vec);
let y0 = y_mat.col(0);
let y2 = y_mat.col(2);

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(y0)
plt.set_domain(y0)
.insert_image(y2)
.set_xlabel(r"$y_0$")
.set_ylabel(r"$y_2$")
Expand Down
6 changes: 3 additions & 3 deletions examples/matmul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ fn main() {

// Create Matrix
let m = rand(row, col);

// Create another Matrix
let n = rand(row, col);

// Matmul
let result = m * n;

result[(row/2, col/2)].print();
result[(row / 2, col / 2)].print();
}
13 changes: 3 additions & 10 deletions examples/ode_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@ fn main() -> Result<(), Box<dyn Error>> {

let c = cubic_hermite_spline(&t, &y, Quadratic)?;

let test_problem = Test {
cs: c,
};
let test_problem = Test { cs: c };
let basic_ode_solver = BasicODESolver::new(RK4);
let (t_vec, y_vec) = basic_ode_solver.solve(
&test_problem,
(0f64, 10f64),
0.01,
)?;
let (t_vec, y_vec) = basic_ode_solver.solve(&test_problem, (0f64, 10f64), 0.01)?;
let y_vec: Vec<f64> = y_vec.into_iter().flatten().collect();

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(t_vec)
plt.set_domain(t_vec)
.insert_image(y_vec)
.set_xlabel(r"$t$")
.set_ylabel(r"$y$")
Expand Down
9 changes: 2 additions & 7 deletions examples/ode_test_gl4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let gl4 = GL4::new(ImplicitSolver::FixedPoint, 1e-6, 100);
let basic_ode_solver = BasicODESolver::new(gl4);
let (t_vec, y_vec) = basic_ode_solver.solve(
&Test,
(0f64, 10f64),
0.01,
)?;
let (t_vec, y_vec) = basic_ode_solver.solve(&Test, (0f64, 10f64), 0.01)?;
let y_vec: Vec<f64> = y_vec.into_iter().flatten().collect();

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(t_vec)
plt.set_domain(t_vec)
.insert_image(y_vec)
.set_xlabel(r"$t$")
.set_ylabel(r"$y$")
Expand Down
9 changes: 2 additions & 7 deletions examples/ode_test_rk4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ use peroxide::fuga::*;
#[allow(unused_variables)]
fn main() -> Result<(), Box<dyn Error>> {
let basic_ode_solver = BasicODESolver::new(RK4);
let (t_vec, y_vec) = basic_ode_solver.solve(
&Test,
(0f64, 10f64),
1e-3,
)?;
let (t_vec, y_vec) = basic_ode_solver.solve(&Test, (0f64, 10f64), 1e-3)?;
let y_vec: Vec<f64> = y_vec.into_iter().flatten().collect();

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(t_vec)
plt.set_domain(t_vec)
.insert_image(y_vec)
.set_xlabel(r"$t$")
.set_ylabel(r"$y$")
Expand Down
9 changes: 2 additions & 7 deletions examples/ode_test_rkf45.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ use peroxide::fuga::*;
fn main() -> Result<(), Box<dyn Error>> {
let rkf = RKF45::new(1e-4, 0.9, 1e-6, 1e-1, 100);
let basic_ode_solver = BasicODESolver::new(rkf);
let (t_vec, y_vec) = basic_ode_solver.solve(
&Test,
(0f64, 10f64),
0.01,
)?;
let (t_vec, y_vec) = basic_ode_solver.solve(&Test, (0f64, 10f64), 0.01)?;
let y_vec: Vec<f64> = y_vec.into_iter().flatten().collect();

#[cfg(feature = "plot")]
{
let mut plt = Plot2D::new();
plt
.set_domain(t_vec)
plt.set_domain(t_vec)
.insert_image(y_vec)
.set_xlabel(r"$t$")
.set_ylabel(r"$y$")
Expand Down
13 changes: 5 additions & 8 deletions examples/optim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ fn main() {

// Optimizer setting
let mut opt = Optimizer::new(data, quad);
let p = opt.set_init_param(n_init)
let p = opt
.set_init_param(n_init)
.set_max_iter(50)
.set_method(LevenbergMarquardt)
.optimize();
p.print(); // Optimized parameter
opt.get_error().print(); // Optimized RMSE
p.print(); // Optimized parameter
opt.get_error().print(); // Optimized RMSE
}

fn quad(x: &Vec<f64>, n: Vec<AD>) -> Option<Vec<AD>> {
Some(
x.clone().into_iter()
.map(|t| pow_temp(t, n[0]))
.collect()
)
Some(x.clone().into_iter().map(|t| pow_temp(t, n[0])).collect())
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion examples/pseudo_inv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use peroxide::fuga::*;

fn main() {
let a = ml_matrix("2 -1 0;4 3 -2");
#[cfg(feature="O3")]
#[cfg(feature = "O3")]
{
let b = a.pseudo_inv();
assert_eq!(&a * &b, eye(2));
Expand Down
2 changes: 1 addition & 1 deletion examples/root_macro_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;
use anyhow::Result;
use peroxide::fuga::*;

fn main() -> Result<()> {
let root_bisect = bisection!(f, (0.0, 2.0), 100, 1e-6)?;
Expand Down
Loading

0 comments on commit 0d8fb88

Please sign in to comment.