Commit 98d390df authored by Egor Larionov's avatar Egor Larionov

Updated nalgebra + autodiff

parent 3c2d0432
......@@ -5,7 +5,7 @@ authors = ["Egor Larionov <[email protected]>"]
[dependencies]
num-traits = "0.2"
nalgebra = "*"
nalgebra = "0.17"
[dev-dependencies]
approx = "0.3"
......
......@@ -6,7 +6,7 @@ pub mod kernel;
pub use kernel::*;
use na::storage::Storage;
use na::{
norm, DMatrix, DVector, Matrix3, Matrix3x4, Matrix4, Point3, U1, U3, U4, Vector, Vector3,
DMatrix, DVector, Matrix3, Matrix3x4, Matrix4, Point3, U1, U3, U4, Vector, Vector3,
Vector4,
};
use num_traits::{Float, Zero};
......@@ -412,7 +412,7 @@ where
/// Helper function for `eval`.
fn eval_block(&self, p: Point3<T>, j: usize) -> Vector4<T> {
let x = p - self.sites[j];
let l = norm(&x);
let l = x.norm();
let w = self.kernel[j].f(l);
let g = self.grad_phi(x, l, j);
Vector4::new(w, g[0], g[1], g[2])
......@@ -432,7 +432,7 @@ where
/// multiplied by the corresponding coefficients.
fn grad_block(&self, p: Point3<T>, j: usize) -> Matrix3x4<T> {
let x = p - self.sites[j];
let l = norm(&x);
let l = x.norm();
let h = self.hess_phi(x, l, j);
let mut grad = Matrix3x4::zero();
grad.column_mut(0).copy_from(&self.grad_phi(x, l, j));
......@@ -454,7 +454,7 @@ where
#[inline]
fn hess_block_prod(&self, p: Point3<T>, b: &Vector4<T>, j: usize) -> Matrix3<T> {
let x = p - self.sites[j];
let l = norm(&x);
let l = x.norm();
let b3 = b.fixed_rows::<U3>(1);
let h = self.hess_phi(x, l, j);
h * b[0] + self.third_deriv_prod_phi(x, l, &b3, j)
......@@ -482,7 +482,7 @@ where
/// This is [g ∇g]' = [𝜙 (∇𝜙)'; ∇𝜙 ∇(∇𝜙)'] in matlab notation.
pub fn fit_block(&self, p: Point3<T>, j: usize) -> Matrix4<T> {
let x = p - self.sites[j];
let l = norm(&x);
let l = x.norm();
let w = self.kernel[j].f(l);
let g = self.grad_phi(x, l, j);
let h = self.hess_phi(x, l, j);
......@@ -510,7 +510,7 @@ where
/// this function returns the matrix ∇(Aⱼ(p)b)'
pub fn grad_fit_block_prod(&self, p: Point3<T>, b: Vector4<T>, j: usize) -> Matrix3x4<T> {
let x = p - self.sites[j];
let l = norm(&x);
let l = x.norm();
let b3 = b.fixed_rows::<U3>(1);
......@@ -533,7 +533,7 @@ where
/// where βⱼ are taken from `self.betas`
fn hess_fit_prod_block(&self, p: Point3<T>, c: Vector4<T>, j: usize) -> Matrix3<T> {
let x = p - self.sites[j];
let l = norm(&x);
let l = x.norm();
let c3 = c.fixed_rows::<U3>(1);
let a = self.betas[j][0];
......
......@@ -5,16 +5,16 @@ extern crate rand;
extern crate approx;
extern crate autodiff;
use autodiff::Num;
use autodiff::F;
use hrbf::kernel::*;
const TEST_RADIUS: f64 = 2.0;
fn test_kernel<F, K: Kernel<Num>>(ker: K, x0: f64, compare: F)
fn test_kernel<C, K: Kernel<F>>(ker: K, x0: f64, compare: C)
where
F: Fn(f64, f64),
C: Fn(f64, f64),
{
let x = Num::var(x0);
let x = F::var(x0);
let f = ker.f(x);
let df = ker.df(x);
......@@ -31,8 +31,8 @@ where
let df_l = ker.df_l(x);
let g = ker.g(x);
let g_l = ker.g_l(x);
let h3 = ker.h(x, Num::cst(3.0));
let h52 = ker.h(x, Num::cst(5.0 / 2.0));
let h3 = ker.h(x, F::cst(3.0));
let h52 = ker.h(x, F::cst(5.0 / 2.0));
compare(x0 * df_l.x, df.x);
compare(x0 * x0 * g.x, ddf.x * x0 - df.x);
compare(x0 * g_l.x, g.x);
......@@ -47,13 +47,13 @@ where
}
}
fn test_kernel_simple<K: Kernel<Num> + Copy>(kern: K) {
fn test_kernel_simple<K: Kernel<F> + Copy>(kern: K) {
for &x in [0.0, 1.0, 0.5, ::std::f64::consts::PI].iter() {
test_kernel(kern, x, ulp_compare);
}
}
fn test_kernel_random<K: Kernel<Num> + Copy>(kern: K) {
fn test_kernel_random<K: Kernel<F> + Copy>(kern: K) {
use self::rand::{distributions::Uniform, Rng, SeedableRng, StdRng};
let seed = [3; 32];
......@@ -75,49 +75,49 @@ fn rel_compare(a: f64, b: f64) {
#[test]
fn pow2_test() {
let kern = Pow2::<Num>::default();
let kern = Pow2::<F>::default();
test_kernel_simple(kern);
test_kernel_random(kern);
}
#[test]
fn pow3_test() {
let kern = Pow3::<Num>::default();
let kern = Pow3::<F>::default();
test_kernel_simple(kern);
test_kernel_random(kern);
}
#[test]
fn pow4_test() {
let kern = Pow4::<Num>::default();
let kern = Pow4::<F>::default();
test_kernel_simple(kern);
test_kernel_random(kern);
}
#[test]
fn pow5_test() {
let kern = Pow5::<Num>::default();
let kern = Pow5::<F>::default();
test_kernel_simple(kern);
test_kernel_random(kern);
}
#[test]
fn gauss_test() {
let kern = Gauss::<Num>::new(Num::cst(TEST_RADIUS));
let kern = Gauss::<F>::new(F::cst(TEST_RADIUS));
test_kernel_simple(kern);
test_kernel_random(kern);
}
#[test]
fn csrbf31_test() {
let kern = Csrbf31::<Num>::new(Num::cst(TEST_RADIUS));
let kern = Csrbf31::<F>::new(F::cst(TEST_RADIUS));
test_kernel_simple(kern);
test_kernel_random(kern);
}
#[test]
fn csrbf42_test() {
let kern = Csrbf42::<Num>::new(Num::cst(TEST_RADIUS));
let kern = Csrbf42::<F>::new(F::cst(TEST_RADIUS));
test_kernel_simple(kern);
test_kernel_random(kern);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment