Skip to content

Commit a1b4338

Browse files
committedFeb 9, 2018
initial commit
0 parents  commit a1b4338

13 files changed

+1612
-0
lines changed
 

‎.codeclimate.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
engines:
2+
fixme:
3+
enabled: true
4+
5+
ratings:
6+
paths:
7+
- src/**
8+
- examples/**
9+
10+
exclude_paths:
11+
- target/
12+
- extern/
13+
- Cargo.*
14+
- rustfmt.toml

‎.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
5+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
7+
Cargo.lock
8+
9+
# These are backup files generated by rustfmt
10+
**/*.rs.bk
11+
12+
# This is a debugging file used for development
13+
examples/sandbox.rs

‎.travis.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
language: rust
2+
dist: trusty
3+
sudo: false
4+
5+
addons:
6+
apt:
7+
packages:
8+
- libcurl4-openssl-dev
9+
- libelf-dev
10+
- libdw-dev
11+
- binutils-dev
12+
- gfortran
13+
- libopenblas-dev
14+
- cmake
15+
sources:
16+
- kalakris-cmake
17+
18+
rust:
19+
- stable
20+
21+
before_script:
22+
- |
23+
cargo install cargo-travis &&
24+
export PATH=$HOME/.cargo/bin:$PATH
25+
26+
script:
27+
- |
28+
cargo build &&
29+
cargo test #&&
30+
# cargo bench &&
31+
# cargo --only stable doc
32+
33+
after_success:
34+
- cargo coveralls
35+
- |
36+
[ $TRAVIS_BRANCH = master ] &&
37+
[ $TRAVIS_PULL_REQUEST = false ] &&
38+
cargo doc --no-deps &&
39+
echo "<meta http-equiv=refresh content=0;url=`echo $TRAVIS_REPO_SLUG | cut -d '/' -f 2`/index.html>" > target/doc/index.html &&
40+
pip install --user ghp-import &&
41+
/home/travis/.local/bin/ghp-import -n target/doc &&
42+
git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
43+
44+
env:
45+
global:
46+
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
47+
- secure: AcBwzPJGszAqn4hW891+ZzbcxVA5wETXUmBg61cGKegHhFKBssB7HaNTcVX1pL3lHUiNGiLKReM+xbMPbth9c3ttz1SKlVsDJbQzt/lNaOjMmdkXd/0PNC7ZWT3yXx8sIbuyz2XCL6TW3dkvYt3G2+Ioy0BxeRepEbfhm1NEMWHoQXbLVkFiH4XtpfADY4DENWiuUbVA3/8+Qwc9R7i4mIf7J8EQWc1+HcNhuy556FrmkkAIDvRU2h0sl9HhjMrTHIKPalfXuriW4pQbZHH+JbA9Xh8MbX230PMqZaELxYkh020Fxy4z2z0VvISmLoMBFQb6JSh11AWQsFno1TZkVqhG2yl1tBPvdKgaqrBLZ8MsES8bG5i/LVDv2kM+dmbtWQmRmn6PnvBmJeJW5bbYx4F2PW57mDUebK/TKEF+1HbBR9KGm2fIihk46mvhxxpQ3ZE7A8lDs17Up9ZBhhYM5RJJ174Ls1Yn+qmtdhEAe5IJ4x5xRNYraTlKJHy2FCgqTeLEKB8TzDB0DUhaDMY6EnuOJdZFjEk5QSzSZJBKNSfyqO+N43vloWt3lszscekgQtYHATvDhylYFrIdRFPrqB3/BYIYOHEzZblTCSres8zmMBTfnWyAn37Z4y4fWsq3NUXcZkOd29HfjAi8qa7vVDQ4d1e1c0RCPOSKiCb7zP0=

‎Cargo.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "spaces"
3+
description = "Vector space primitives for machine learning problems."
4+
5+
version = "0.0.1"
6+
authors = ["Tom Spooner <t.spooner@liverpool.ac.uk>"]
7+
8+
license = "MIT"
9+
readme = "README.md"
10+
11+
keywords = ["geometry", "vector", "spaces", "mahcine", "learning"]
12+
13+
repository = "https://github.com/tspooner/spaces"
14+
documentation = "https://tspooner.github.io/spaces/spaces/index.html"
15+
16+
[badges]
17+
travis-ci = { repository = "tspooner/spaces", branch = "master" }
18+
coveralls = { repository = "tspooner/spaces", branch = "master", service = "github" }
19+
20+
[dependencies]
21+
rand = "0.4"
22+
ndarray = { version = "0.11", features = ["serde-1"] }
23+
rusty-machine = "0.5"
24+
25+
serde = "1.0"
26+
serde_json = "1.0"
27+
serde_test = "1.0"
28+
serde_derive = "1.0"

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Tom Spooner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# spaces ([api](https://tspooner.github.io/spaces))
2+
3+
[![Crates.io](https://img.shields.io/crates/v/spaces.svg)](https://crates.io/crates/spaces)
4+
[![Build Status](https://travis-ci.org/tspooner/spaces.svg?branch=master)](https://travis-ci.org/tspooner/spaces)
5+
[![Coverage Status](https://coveralls.io/repos/github/tspooner/spaces/badge.svg?branch=master)](https://coveralls.io/github/tspooner/spaces?branch=master)
6+
7+
<!--
8+
## Overview
9+
10+
11+
## Installation
12+
```toml
13+
[dependencies]
14+
spaces = "0.0"
15+
```
16+
17+
18+
## Usage
19+
-->
20+
21+
22+
## Contributing
23+
Pull requests are welcome. For major changes, please open an issue first to
24+
discuss what you would like to change.
25+
26+
Please make sure to update tests as appropriate and adhere to the angularjs commit message conventions (see [here](https://gist.github.com/stephenparish/9941e89d80e2bc58a153)).
27+
28+
## License
29+
[MIT](https://choosealicense.com/licenses/mit/)

‎rustfmt.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
format_strings = false
2+
reorder_imports = true

‎src/dimensions.rs

+835
Large diffs are not rendered by default.

‎src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate rand;
2+
extern crate ndarray;
3+
4+
extern crate serde;
5+
extern crate serde_json;
6+
extern crate serde_test;
7+
#[macro_use] extern crate serde_derive;
8+
9+
10+
mod macros;
11+
12+
pub mod norms;
13+
14+
mod span;
15+
pub use self::span::Span;
16+
17+
/// 1d array type.
18+
pub type Vector<T = f64> = ndarray::Array1<T>;
19+
20+
/// 2d array type.
21+
pub type Matrix<T = f64> = ndarray::Array2<T>;
22+
23+
mod spaces;
24+
pub mod dimensions;
25+
pub use self::spaces::*;
26+
27+
extern crate rusty_machine;
28+
pub use self::rusty_machine::learning::toolkit::kernel as kernels;

‎src/macros.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![macro_use]
2+
3+
macro_rules! wrap {
4+
($lb:expr, $x:expr, $ub:expr) => {
5+
{
6+
let mut nx = $x;
7+
let diff = $ub - $lb;
8+
9+
while nx > $ub {
10+
nx -= diff;
11+
}
12+
13+
while nx < $lb {
14+
nx += diff;
15+
}
16+
17+
nx
18+
}
19+
}
20+
}
21+
22+
macro_rules! clip {
23+
($lb:expr, $x:expr, $ub:expr) => {
24+
{
25+
$lb.max($ub.min($x))
26+
}
27+
}
28+
}

‎src/norms.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Normalisation utilities module.
2+
3+
/// Computes the L1 norm.
4+
#[inline]
5+
pub fn l1(x: &[f64]) -> f64 {
6+
x.iter().fold(0.0, |acc, v| acc + v.abs())
7+
}
8+
9+
/// Computes the L2 norm.
10+
#[inline]
11+
pub fn l2(x: &[f64]) -> f64 {
12+
x.iter().fold(0.0, |acc, v| acc + (v*v).abs()).sqrt()
13+
}
14+
15+
/// Computes the Lp norm.
16+
#[inline]
17+
pub fn lp(x: &[f64], p: u8) -> f64 {
18+
x.iter().fold(0.0, |acc, v| acc + v.abs().powi(p as i32)).powf((p as f64).recip())
19+
}
20+
21+
/// Computes the infinity norm.
22+
#[inline]
23+
pub fn linf(x: &[f64]) -> f64 {
24+
x.iter().fold(0.0, |max, v| {
25+
let av = v.abs();
26+
27+
if av > max {
28+
av
29+
} else {
30+
max
31+
}
32+
})
33+
}

‎src/spaces.rs

+431
Large diffs are not rendered by default.

‎src/span.rs

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use std::ops::Mul;
2+
3+
4+
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
5+
pub enum Span {
6+
Null,
7+
Finite(usize),
8+
Infinite,
9+
}
10+
11+
impl Mul for Span {
12+
type Output = Span;
13+
14+
fn mul(self, rhs: Span) -> Span {
15+
match self {
16+
Span::Null => rhs,
17+
Span::Infinite => self,
18+
Span::Finite(ls) => {
19+
match rhs {
20+
Span::Null => self,
21+
Span::Infinite => rhs,
22+
Span::Finite(rs) => Span::Finite(ls * rs),
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
impl Into<usize> for Span {
30+
fn into(self) -> usize {
31+
match self {
32+
Span::Finite(e) => e,
33+
_ => panic!("Span type has no integer representation."),
34+
}
35+
}
36+
}
37+
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::Span;
42+
43+
#[test]
44+
fn test_equality() {
45+
assert_eq!(Span::Null, Span::Null);
46+
assert_eq!(Span::Infinite, Span::Infinite);
47+
48+
assert_eq!(Span::Finite(1), Span::Finite(1));
49+
assert_eq!(Span::Finite(5), Span::Finite(5));
50+
assert_eq!(Span::Finite(10), Span::Finite(10));
51+
}
52+
53+
#[test]
54+
fn test_inequality() {
55+
assert_ne!(Span::Null, Span::Infinite);
56+
assert_ne!(Span::Null, Span::Finite(1));
57+
58+
assert_ne!(Span::Infinite, Span::Null);
59+
assert_ne!(Span::Infinite, Span::Finite(1));
60+
61+
assert_ne!(Span::Finite(1), Span::Null);
62+
assert_ne!(Span::Finite(1), Span::Infinite);
63+
assert_ne!(Span::Finite(1), Span::Finite(10));
64+
}
65+
66+
#[test]
67+
fn test_mul() {
68+
assert_eq!(Span::Null * Span::Null, Span::Null);
69+
assert_eq!(Span::Infinite * Span::Infinite, Span::Infinite);
70+
71+
assert_eq!(Span::Null * Span::Infinite, Span::Infinite);
72+
assert_eq!(Span::Infinite * Span::Null, Span::Infinite);
73+
74+
assert_eq!(Span::Finite(1) * Span::Finite(1), Span::Finite(1));
75+
assert_eq!(Span::Finite(1) * Span::Finite(5), Span::Finite(5));
76+
assert_eq!(Span::Finite(5) * Span::Finite(1), Span::Finite(5));
77+
assert_eq!(Span::Finite(5) * Span::Finite(5), Span::Finite(25));
78+
}
79+
80+
#[test]
81+
#[should_panic]
82+
fn test_into_null() {
83+
let s = Span::Null;
84+
let _: usize = s.into();
85+
}
86+
87+
#[test]
88+
#[should_panic]
89+
fn test_into_infinite() {
90+
let s = Span::Infinite;
91+
let _: usize = s.into();
92+
}
93+
94+
#[test]
95+
fn test_into_finite() {
96+
for i in vec![1, 5, 10] {
97+
let d = Span::Finite(i);
98+
let v: usize = d.into();
99+
100+
assert_eq!(v, i);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)
Please sign in to comment.