Skip to content

Commit

Permalink
Merge pull request #5 from mshrtsr/develop
Browse files Browse the repository at this point in the history
version 0.3.0
  • Loading branch information
tasshi-me authored May 16, 2020
2 parents de63cb3 + 7e604d3 commit eeb0df8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 75 deletions.
17 changes: 3 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
[package]
name = "fitting"
version = "0.2.1"
version = "0.3.0"
authors = ["Masaharu TASHIRO <masatsr.kit@gmail.com>"]
edition = "2018"

# A short blurb about the package. This is not rendered in any format when
# uploaded to crates.io (aka this is not markdown).
description = "Pure Rust curve fitting library"

# These URLs point to more information about the package. These are
# intended to be webviews of the relevant data, not necessarily compatible
# with VCS tools and the like.
documentation = "https://docs.rs/fitting/"
homepage = "https://crates.io/crates/fitting"
repository = "https://github.com/mshrtsr/fitting-rs"

# This points to a file under the package root (relative to this `Cargo.toml`).
# The contents of this file are stored and indexed in the registry.
# crates.io will render this file and place the result on the crate's page.
readme = "README.md"

# This is a list of up to five keywords that describe this crate. Keywords
# are searchable on crates.io, and you may choose any words that would
# help someone find this crate.
keywords = ["fitting","statistics","probability","distribution","math"]

keywords = ["fitting", "statistics", "probability", "distribution", "math"]
# This is a list of up to five categories where this crate would fit.
# Categories are a fixed list available at crates.io/category_slugs, and
# they must match exactly.
categories = ["science"]

# This is an SPDX 2.1 license expression for this package. Currently
# crates.io will validate the license provided against a whitelist of
# known license and exception identifiers from the SPDX license list
Expand All @@ -39,23 +33,19 @@ categories = ["science"]
# is deprecated. Instead, use a license expression with AND and OR
# operators to get more explicit semantics.
license = "MIT"

# If a package is using a nonstandard license, then this key may be specified in
# lieu of the above key and must point to a file relative to this manifest
# (similar to the readme key).
# license-file = "..."

exclude = ["/.github/*", "./circleci/*", "/.travis.yml"]

[badges]
circle-ci = { repository = "mshrtsr/fitting-rs", branch = "master" }
travis-ci = { repository = "mshrtsr/fitting-rs", branch = "master" }

# Codecov: `repository` is required. `branch` is optional; default is `master`
# `service` is optional; valid values are `github` (default), `bitbucket`, and
# `gitlab`.
codecov = { repository = "mshrtsr/fitting-rs", branch = "master", service = "github" }

# Maintenance: `status` is required. Available options are:
# - `actively-developed`: New features are being added and bugs are being fixed.
# - `passively-maintained`: There are no plans for new features, but the maintainer intends to
Expand All @@ -74,8 +64,7 @@ codecov = { repository = "mshrtsr/fitting-rs", branch = "master", service = "git
maintenance = { status = "actively-developed" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ndarray = { version = "0.13.0", features = ["approx"] }
approx = "0.3.2"
failure = "0.1.6"
thiserror = "1.0.17"
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Curve fitting library for Rust

## Updates

### 0.3.0

- Migrate from the `failure` crate to `thiserror`.
- Refactor some tests.

### 0.2.1

- Error handing changed. Some functions returns Result instead of Option.
Expand Down
62 changes: 8 additions & 54 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,14 @@
use failure::{Backtrace, Context, Fail};
use std::fmt;
use std::fmt::Display;
use thiserror::Error;

#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum ErrorKind {
#[fail(display = "None error")]
//#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
#[derive(Error, Debug, Eq, PartialEq)]
pub enum Error {
#[error("None error")]
Optional,
#[fail(display = "Line Algebra error: Equations have no solutions")]
#[error("Line Algebra error: Equations have no solutions")]
LinalgSolveNoSolutions,
#[fail(display = "Line Algebra error: Equations have infinite solutions")]
#[error("Line Algebra error: Equations have infinite solutions")]
LinalgSolveInfSolutions,
#[fail(display = "Fitting error")]
#[error("Fitting error")]
Fitting,
}

impl Fail for Error {
fn cause(&self) -> Option<&dyn Fail> {
self.inner.cause()
}

fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.inner, f)
}
}

impl Error {
pub fn new(inner: Context<ErrorKind>) -> Error {
Error { inner }
}

pub fn kind(&self) -> &ErrorKind {
self.inner.get_context()
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
inner: Context::new(kind),
}
}
}

impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Error {
Error { inner }
}
}
14 changes: 7 additions & 7 deletions src/linalg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::{Error, ErrorKind};
use crate::error::Error;
use approx::{abs_diff_eq, abs_diff_ne};
use ndarray::{s, Array1, Array2, Axis};

Expand Down Expand Up @@ -79,12 +79,12 @@ pub fn solve(a: Array2<f64>, b: Array1<f64>) -> Result<Array1<f64>, Error> {

// no solutions
if rank_coef != rank_aug {
return Err(Error::from(ErrorKind::LinalgSolveNoSolutions));
return Err(Error::LinalgSolveNoSolutions);
}

// infinite solutions
if rank_coef != a.ncols() {
return Err(Error::from(ErrorKind::LinalgSolveInfSolutions));
return Err(Error::LinalgSolveInfSolutions);
}

// backward substitution
Expand Down Expand Up @@ -170,7 +170,7 @@ mod tests {
let a = array![[2., 1., -3., -2.], [2., -1., -1., 3.], [1., -1., -2., 2.]];
let b = array![4., 1., -3.];
let err = solve(a, b).unwrap_err(); //panic
assert!(err.kind() == &ErrorKind::LinalgSolveInfSolutions);
assert_eq!(err, Error::LinalgSolveInfSolutions);
}

#[test]
Expand All @@ -186,7 +186,7 @@ mod tests {
];
let b = array![2., -6. / 5., -1., 1.];
let err = solve(a, b).unwrap_err(); //panic
assert!(err.kind() == &ErrorKind::LinalgSolveInfSolutions);
assert_eq!(err, Error::LinalgSolveInfSolutions);
}

#[test]
Expand All @@ -197,7 +197,7 @@ mod tests {
let a = array![[-2., 3.], [4., 1.], [1., -3.],];
let b = array![1., 5., -1.];
let err = solve(a, b).unwrap_err(); //panic
assert!(err.kind() == &ErrorKind::LinalgSolveNoSolutions);
assert_eq!(err, Error::LinalgSolveNoSolutions);
}

#[test]
Expand All @@ -208,6 +208,6 @@ mod tests {
let a = array![[1., 3., -2.], [-1., 2., -3.], [2., -1., 3.],];
let b = array![2., -2., 3.];
let err = solve(a, b).unwrap_err(); //panic
assert!(err.kind() == &ErrorKind::LinalgSolveNoSolutions);
assert_eq!(err, Error::LinalgSolveNoSolutions);
}
}

0 comments on commit eeb0df8

Please # to comment.