Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev v0.1.12 #31

Merged
merged 19 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ version = "0.1.12" # TODO - Update the cargo package version

anyhow = "1"
approx = "0.5"
itertools = { features = [], version = "0.12" }
lazy_static = "1"
# ndarray = { features = ["serde-1"], version = "0.15" }
# ndarray-linalg = { features = [], version = "0.16" }
ndarray-rand = { features = [], version = "0.14" }
ndarray-stats = { features = [], version = "0.5" }
num = { features = ["serde"], version = "0.4" }

serde = { features = ["derive"], version = "1" }
serde_json = "1"
smart-default = "0.7"
strum = { features = ["derive"], version = "0.25" }
strum = { features = ["derive"], version = "0.26" }

[workspace]
default-members = [
Expand Down
2 changes: 2 additions & 0 deletions concision/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ openblas-static = [
"concision-s4/openblas-static",
]

serde = []

[lib]
bench = true
crate-type = ["rlib"]
Expand Down
14 changes: 13 additions & 1 deletion concision/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
extern crate concision;

use concision::prelude::BoxResult;
use concision as cnc;

use cnc::core::ops::fft::*;
use cnc::prelude::{Arange, AsComplex, BoxResult};

use ndarray::prelude::*;

fn main() -> BoxResult {
println!("Welcome to concision!");
let samples = 8;

let arr = Array1::<f64>::arange(samples).mapv(AsComplex::as_re);
let buff = arr.clone().into_raw_vec();
let plan = FftPlan::new(samples);
println!("Permutations: {:?}", plan.plan());
let res = ifft(buff.as_slice(), &plan);
println!("{:?}", &res);
Ok(())
}
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ smart-default.workspace = true
strum.workspace = true

[dev-dependencies]
lazy_static.workspace = true

[package.metadata.docs.rs]
all-features = true
Expand Down
8 changes: 4 additions & 4 deletions core/src/errors/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
use serde::{Deserialize, Serialize};
use smart_default::SmartDefault;
use strum::{Display, EnumCount, EnumIs, EnumIter, EnumVariantNames};
use strum::{Display, EnumCount, EnumIs, EnumIter, VariantNames};

#[derive(
Clone,
Expand All @@ -14,14 +14,14 @@ use strum::{Display, EnumCount, EnumIs, EnumIter, EnumVariantNames};
EnumCount,
EnumIs,
EnumIter,
EnumVariantNames,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
Serialize,
SmartDefault,
VariantNames,
)]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
Expand All @@ -39,7 +39,7 @@ pub enum Errors {
IO,
Null,
Parse,
Process,
Process(ProcessError),
Runtime,
Syntax,
Unknown,
Expand All @@ -53,14 +53,14 @@ pub enum Errors {
EnumCount,
EnumIs,
EnumIter,
EnumVariantNames,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
Serialize,
SmartDefault,
VariantNames,
)]
#[non_exhaustive]
#[serde(rename_all = "lowercase")]
Expand Down
52 changes: 52 additions & 0 deletions core/src/id/ids/atomic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Appellation: atomic <mod>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct AtomicId(usize);

impl AtomicId {
pub fn new() -> Self {
use std::sync::atomic;
static COUNTER: atomic::AtomicUsize = atomic::AtomicUsize::new(1);
Self(COUNTER.fetch_add(1, atomic::Ordering::Relaxed))
}
}

impl AsRef<usize> for AtomicId {
fn as_ref(&self) -> &usize {
&self.0
}
}

impl AsMut<usize> for AtomicId {
fn as_mut(&mut self) -> &mut usize {
&mut self.0
}
}

impl Default for AtomicId {
fn default() -> Self {
Self::new()
}
}

impl std::fmt::Display for AtomicId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<usize> for AtomicId {
fn from(id: usize) -> Self {
Self(id)
}
}

impl From<AtomicId> for usize {
fn from(id: AtomicId) -> Self {
id.0
}
}
8 changes: 8 additions & 0 deletions core/src/id/ids/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Appellation: ids <mod>
Contrib: FL03 <jo3mccain@icloud.com>
*/
//! # ids
pub use self::atomic::*;

pub(crate) mod atomic;
27 changes: 25 additions & 2 deletions core/src/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
Contrib: FL03 <jo3mccain@icloud.com>
*/
//! # id
pub use self::{identity::*, utils::*};
pub use self::{identity::*, ids::*, utils::*};

pub(crate) mod identity;
pub(crate) mod ids;

pub(crate) mod utils {
// https://users.rust-lang.org/t/idiomatic-rust-way-to-generate-unique-id/33805
pub fn atomic_id() -> usize {
use std::sync::atomic;
static COUNTER: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
COUNTER.fetch_add(1, atomic::Ordering::Relaxed)
}

pub fn rid(length: usize) -> String {
use rand::distributions::Alphanumeric;
Expand All @@ -22,4 +29,20 @@ pub(crate) mod utils {
}

#[cfg(test)]
mod tests {}
mod tests {

use super::*;

#[test]
fn test_atomic_id() {
let id = atomic_id();
assert_eq!(id, 0);
assert_ne!(id, atomic_id());
}

#[test]
fn test_rid() {
let id = rid(10);
assert_eq!(id.len(), 10);
}
}
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub(crate) mod utils;
pub mod errors;
pub mod id;
pub mod masks;
pub mod ops;
pub mod params;
pub mod specs;
pub mod states;
Expand Down
52 changes: 27 additions & 25 deletions core/src/masks/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@
Appellation: mask <mod>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use ndarray::prelude::{Array, Array2};
use ndarray::Dimension;
use ndarray_rand::rand_distr::{uniform::SampleUniform, Uniform};
use ndarray::prelude::{Array, Array2, Dimension};
use ndarray::ScalarOperand;
use ndarray_rand::rand_distr::uniform::{SampleUniform, Uniform};
use ndarray_rand::RandomExt;
use num::Float;
use num::traits::{Float, NumOps};
use serde::{Deserialize, Serialize};
use smart_default::SmartDefault;
use std::ops;
use strum::EnumIs;

#[derive(Clone, Debug, Deserialize, EnumIs, PartialEq, Serialize, SmartDefault)]
pub enum Mask<T: Float = f64> {
pub enum Mask<T = f64> {
Masked(Array2<T>),
#[default]
Unmasked,
}

impl<T: Float> Mask<T> {
impl<T> Mask<T>
where
T: NumOps + ScalarOperand,
{
pub fn forward(&self, data: &Array2<T>) -> Array2<T> {
match self {
Self::Masked(bias) => data + bias,
Expand All @@ -28,19 +31,19 @@ impl<T: Float> Mask<T> {
}
}

impl<T: Float> Mask<T>
impl<T> Mask<T>
where
T: Float + SampleUniform,
{
pub fn masked(size: usize) -> Self {
pub fn uniform(size: usize) -> Self {
let ds = (T::from(size).unwrap()).sqrt();
let dist = Uniform::new(-ds, ds);
let mask = Array2::<T>::random((size, size), dist);
Self::Masked(mask)
}
}

impl<T: Float> From<usize> for Mask<T>
impl<T> From<usize> for Mask<T>
where
T: Float + SampleUniform,
{
Expand All @@ -52,19 +55,13 @@ where
}
}

impl<T> From<Array2<T>> for Mask<T>
where
T: Float,
{
impl<T> From<Array2<T>> for Mask<T> {
fn from(bias: Array2<T>) -> Self {
Self::Masked(bias)
}
}

impl<T> From<Option<Array2<T>>> for Mask<T>
where
T: Float,
{
impl<T> From<Option<Array2<T>>> for Mask<T> {
fn from(bias: Option<Array2<T>>) -> Self {
match bias {
Some(bias) => Self::Masked(bias),
Expand All @@ -73,10 +70,7 @@ where
}
}

impl<T> From<Mask<T>> for Option<Array2<T>>
where
T: Float,
{
impl<T> From<Mask<T>> for Option<Array2<T>> {
fn from(bias: Mask<T>) -> Self {
match bias {
Mask::Masked(bias) => Some(bias),
Expand All @@ -85,8 +79,10 @@ where
}
}

impl<D: Dimension, T: Float> ops::Add<Array<T, D>> for Mask<T>
impl<T, D> ops::Add<Array<T, D>> for Mask<T>
where
D: Dimension,
T: NumOps + ScalarOperand,
Array<T, D>: ops::Add<Array2<T>, Output = Array<T, D>>,
{
type Output = Array<T, D>;
Expand All @@ -100,8 +96,10 @@ where
}
}

impl<D: Dimension, T: Float> ops::Add<&Array<T, D>> for Mask<T>
impl<T, D> ops::Add<&Array<T, D>> for Mask<T>
where
D: Dimension,
T: NumOps + ScalarOperand,
Array<T, D>: ops::Add<Array2<T>, Output = Array<T, D>>,
{
type Output = Array<T, D>;
Expand All @@ -115,8 +113,10 @@ where
}
}

impl<D: Dimension, T: Float> ops::Add<Mask<T>> for Array<T, D>
impl<T, D> ops::Add<Mask<T>> for Array<T, D>
where
D: Dimension,
T: NumOps + ScalarOperand,
Array<T, D>: ops::Add<Array2<T>, Output = Array<T, D>>,
{
type Output = Array<T, D>;
Expand All @@ -130,8 +130,10 @@ where
}
}

impl<D: Dimension, T: Float> ops::Add<&Mask<T>> for Array<T, D>
impl<T, D> ops::Add<&Mask<T>> for Array<T, D>
where
D: Dimension,
T: NumOps + ScalarOperand,
Array<T, D>: ops::Add<Array2<T>, Output = Array<T, D>>,
{
type Output = Array<T, D>;
Expand Down
5 changes: 5 additions & 0 deletions core/src/masks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ pub use self::{mask::*, utils::*};

pub(crate) mod mask;

pub trait Masked<T> {
fn mask(&self) -> &Mask<T>;
fn mask_mut(&mut self) -> &mut Mask<T>;
}

pub(crate) mod utils {
use super::Mask;
use ndarray::prelude::Array2;
Expand Down
9 changes: 9 additions & 0 deletions core/src/ops/fft/algorithms/dft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Appellation: dft <mod>
Contrib: FL03 <jo3mccain@icloud.com>
*/
//! # Discrete Fourier Transform
//!
//!

pub struct Dft;
6 changes: 6 additions & 0 deletions core/src/ops/fft/algorithms/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use self::dft::*;

pub(crate) mod dft;

#[cfg(test)]
mod tests {}
Loading
Loading