1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
#![doc(html_favicon_url = "\"> <script defer src=\"https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.js\" integrity=\"sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij\" crossorigin=\"anonymous\"></script> <script> document.addEventListener(\"DOMContentLoaded\", function () { let to_do = []; for (let e of document.getElementsByTagName(\"code\")) { if (e.classList.contains(\"language-math\")) { to_do.push(function () { let x = document.createElement('p'); katex.render(e.innerText, x, {displayMode: true, throwOnError: false}); e.parentNode.parentNode.replaceChild(x, e.parentNode); }); } else { let n = e.nextSibling; let p = e.previousSibling; if (n && p && /^\\$/.test(n.data) && /\\$$/.test(p.data)) { to_do.push(function () { let n = e.nextSibling; let p = e.previousSibling; let x = document.createElement('span'); katex.render(e.innerText, x, {throwOnError: false}); e.parentNode.replaceChild(x, e); n.splitText(1); n.remove(); p.splitText(p.data.length - 1).remove(); }); } } } for (let f of to_do) f(); }); </script> <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.css\" integrity=\"sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ\" crossorigin=\"anonymous")] //! # mathru //! //! A crate that provides mathematics functions implemented entirely in Rust. //! //! //! ## Usage //! //! The library usage is described well in the API documentation - including //! example code. //! //! //! Add this to your `Cargo.toml`: //! //! ```toml //! [dependencies] //! mathru = "0.6.*" //! ``` //! //! Then it is ready to be used: //! //!``` rust //! # #[macro_use] //! # extern crate mathru; //! # fn main() //! # { //! use mathru::algebra::linear::{Vector, Matrix}; //! use mathru::algebra::linear::matrix::{Substitute}; //! //! // Compute the LU decomposition of a 2x2 matrix //! let a: Matrix<f64> = Matrix::new(2, 2, vec![1.0, 2.0, -3.0, -7.0]); //! let b: Vector<f64> = vector![1.0; 3.0]; //! //! let (l, u, p): (Matrix<f64>, Matrix<f64>, Matrix<f64>) = a.dec_lu().unwrap().lup(); //! //! let b_hat = &p * &b; //! //! let y = u.substitute_backward(b_hat); //! //! let x = p * l.substitute_forward(y); //! //! println!("{}", x); //! # } //! ``` //! //!``` //! use mathru::*; //! use mathru::algebra::linear::{Vector, Matrix}; //! use mathru::statistics::distrib::{Distribution, Normal}; //! use mathru::optimization::{Optim, LevenbergMarquardt}; //! //! //! //y = a + b * exp(c * t) = f(t) //! pub struct Example //! { //! x: Vector<f64>, //! y: Vector<f64> //! } //! //! impl Example //! { //! pub fn new(x: Vector<f64>, y: Vector<f64>) -> Example //! { //! Example //! { //! x: x, //! y: y //! } //! } //! //! pub fn function(x: f64, beta: &Vector<f64>) -> f64 //! { //! let beta_0: f64 = *beta.get(0); //! let beta_1: f64 = *beta.get(1); //! let beta_2: f64 = *beta.get(2); //! let f_x: f64 = beta_0 + beta_1 * (beta_2 * x).exp(); //! //! return f_x; //! } //! } //! //! impl Optim<f64> for Example //! { //! // y(x_i) - f(x_i) //! fn eval(self: &Self, beta: &Vector<f64>) -> Vector<f64> //! { //! let f_x = self.x.clone().apply(&|x: &f64| Example::function(*x, beta)); //! let r: Vector<f64> = &self.y - &f_x; //! return vector![r.dotp(&r)] //! } //! //! fn jacobian(self: &Self, beta: &Vector<f64>) -> Matrix<f64> //! { //! let (x_m, _x_n) = self.x.dim(); //! let (beta_m, _beta_n) = beta.dim(); //! //! let mut jacobian_f: Matrix<f64> = Matrix::zero(x_m, beta_m); //! //! let f_x = self.x.clone().apply(&|x: &f64| Example::function(*x, beta)); //! let residual: Vector<f64> = &self.y - &f_x; //! //! for i in 0..x_m //! { //! //let beta_0: f64 = *beta.get(0); //! let beta_1: f64 = *beta.get(1); //! let beta_2: f64 = *beta.get(2); //! //! let x_i: f64 = *self.x.get(i); //! //! *jacobian_f.get_mut(i, 0) = 1.0; //! *jacobian_f.get_mut(i, 1) = (beta_2 * x_i).exp(); //! *jacobian_f.get_mut(i, 2) = beta_1 * x_i * (beta_2 * x_i).exp(); //! //! } //! //! let jacobian: Matrix<f64> = (residual.transpose() * jacobian_f * -2.0).into(); //! return jacobian; //! } //! } //! //! //! fn main() //! { //! let num_samples: usize = 100; //! //! let noise: Normal<f64> = Normal::new(0.0, 0.05); //! //! let mut t_vec: Vec<f64> = Vec::with_capacity(num_samples); //! //! // Start time //! let t_0 = 0.0f64; //! // End time //! let t_1 = 5.0f64; //! //! let mut y_vec: Vec<f64> = Vec::with_capacity(num_samples); //! //! // True function parameters //! let beta: Vector<f64> = vector![0.5; 5.0; -1.0]; //! //! for i in 0..num_samples //! { //! let t_i: f64 = (t_1 - t_0) / (num_samples as f64) * (i as f64); //! //! //Add some noise //! y_vec.push(Example::function(t_i, &beta) + noise.random()); //! //! t_vec.push(t_i); //! } //! //! let t: Vector<f64> = Vector::new_column(num_samples, t_vec.clone()); //! let y: Vector<f64> = Vector::new_column(num_samples, y_vec.clone()); //! //! let example_function = Example::new(t, y); //! //! let optim: LevenbergMarquardt<f64> = LevenbergMarquardt::new(100, 0.3, 0.95); //! //! let beta_0: Vector<f64> = vector![-1.5; 1.0; -2.0]; //! let beta_opt: Vector<f64> = optim.minimize(&example_function, &beta_0).arg(); //! //! println!("{}", beta_opt); //! } //! ``` #[cfg(feature = "blaslapack")] extern crate blas; #[cfg(feature = "blaslapack")] extern crate blas_src; #[cfg(feature = "blaslapack")] extern crate lapack; #[macro_use] pub mod algebra; pub mod analysis; pub mod elementary; pub mod num; pub mod optimization; pub mod special; pub mod statistics;