Skip to content

Commit

Permalink
Merge pull request #70 from Tehforsch/diman-lib
Browse files Browse the repository at this point in the history
Diman lib
  • Loading branch information
Tehforsch authored Jan 13, 2024
2 parents 04e1dbf + fa5a025 commit 55dd4e8
Show file tree
Hide file tree
Showing 44 changed files with 413 additions and 474 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ jobs:
- name: Build
run: cargo build --verbose --all-targets
- name: Run tests (default features)
run: cargo test --tests
run: cargo test --tests --workspace
- name: Run tests (all features, no rational dimensions)
run: cargo test --tests --features glam,glam-vec2,glam-dvec2,glam-vec3,glam-dvec3,f32,f64,gen-vec-names,si,mpi,hdf5,rand,serde
run: cargo test --tests --features glam,glam-vec2,glam-dvec2,glam-vec3,glam-dvec3,f32,f64,gen-vec-names,si,mpi,hdf5,rand,serde --workspace
- name: Run tests (all features)
run: cargo test --tests --all-features
run: cargo test --tests --all-features --workspace
- name: Run tests (no std, no libm)
run: cargo test --tests --no-default-features --features f32,f64,si
run: cargo test --tests --no-default-features --features f32,f64,si --workspace
- name: Run tests (no std, libm)
run: cargo test --tests --no-default-features --features f32,f64,num-traits-libm,si
run: cargo test --tests --no-default-features --features f32,f64,num-traits-libm,si --workspace
- name: Doctests
run: cargo test --doc --all-features
run: cargo test --doc --all-features --workspace

clippy:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ once_cell = { version = "1.18.0", optional = true }
num-traits = { version = "0.2.17", default-features = false }

diman_unit_system = { path = "crates/diman_unit_system", version = "0.4", default-features = false }
diman_lib = { path = "crates/diman_lib", version = "0.4" }

[dev-dependencies]
serde_yaml = "0.9.27"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ The `unit_system!` macro also allows defining derived dimensions and units:
```rust ignore
#![allow(incomplete_features)]
#![feature(generic_const_exprs, adt_const_params)]
use diman_unit_system::unit_system;
use diman::unit_system;
unit_system!(
quantity_type Quantity;
dimension_type Dimension;
Expand Down
12 changes: 12 additions & 0 deletions crates/diman_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "diman_lib"
version = "0.4.0"
edition = "2021"
authors = [
"Toni Peter <mail@tonipeter.de>",
]
description = "Commonly used types for internal use in diman."
license = "MIT OR Apache-2.0"
repository = "https://github.com/tehforsch/diman"

[dependencies]
4 changes: 4 additions & 0 deletions crates/diman_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs, adt_const_params)]

pub mod ratio;
132 changes: 132 additions & 0 deletions crates/diman_lib/src/ratio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#[derive(
::core::cmp::PartialEq,
::core::cmp::Eq,
::core::clone::Clone,
::core::marker::Copy,
::core::fmt::Debug,
::core::marker::ConstParamTy,
)]
pub struct Ratio {
num: i64,
denom: i64,
}

const fn gcd(mut a: i64, mut b: i64) -> i64 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a.abs()
}

impl Ratio {
pub fn one() -> Self {
Self { num: 1, denom: 1 }
}

pub fn zero() -> Self {
Self { num: 0, denom: 1 }
}

pub const fn int(num: i64) -> Self {
Self { num, denom: 1 }
}

pub const fn num(&self) -> i64 {
self.num
}

pub const fn denom(&self) -> i64 {
self.denom
}

pub const fn new(num: i64, denom: i64) -> Self {
let gcd = gcd(num, denom);
Self {
num: num / gcd,
denom: denom / gcd,
}
}

pub const fn powi(self, exp: i32) -> Self {
let num = self.num * exp as i64;
let denom = self.denom * exp as i64;
Self::new(num, denom)
}

pub const fn add(self, rhs: Self) -> Self {
let num = self.num * rhs.denom + rhs.num * self.denom;
let denom = self.denom * rhs.denom;
Self::new(num, denom)
}

pub const fn sub(self, rhs: Self) -> Self {
self.add(rhs.neg())
}

pub const fn neg(self) -> Self {
Self {
num: -self.num,
denom: self.denom,
}
}

pub const fn mul(self, rhs: Self) -> Self {
let num = self.num * rhs.num;
let denom = self.denom * rhs.denom;
Self::new(num, denom)
}

pub const fn div(self, rhs: Self) -> Self {
self.mul(rhs.inv())
}

const fn inv(self) -> Self {
Self {
num: self.denom,
denom: self.num,
}
}

pub fn float_pow(num: f64, exponent: Self) -> f64 {
num.powf(exponent.num as f64 / exponent.denom as f64)
}
}

impl core::fmt::Display for Ratio {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.denom == 1 {
write!(f, "{}", self.num)
} else {
write!(f, "{}/{}", self.num, self.denom)
}
}
}

impl core::ops::Mul for Ratio {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
Self::new(self.num * rhs.num, self.denom * rhs.denom)
}
}

impl core::ops::AddAssign for Ratio {
fn add_assign(&mut self, rhs: Self) {
let num = self.num * rhs.denom + rhs.num * self.denom;
let denom = self.denom * rhs.denom;
*self = Self::new(num, denom)
}
}

impl core::ops::Neg for Ratio {
type Output = Self;

fn neg(self) -> Self::Output {
Self {
num: -self.num,
denom: self.denom,
}
}
}
1 change: 1 addition & 0 deletions crates/diman_unit_system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ default = ["f32", "f64"]
syn = { version = "2.0", features = ["full", "extra-traits"] }
quote = "1.0"
proc-macro2 = "1.0"
diman_lib = { path = "../diman_lib", version = "0.4" }

[lib]
proc-macro = true
Expand Down
13 changes: 7 additions & 6 deletions crates/diman_unit_system/src/codegen/base_dimension_type.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::types::{BaseDimensionExponent, Defs};
use super::Codegen;
use crate::types::BaseDimensionExponent;

#[cfg(feature = "rational-dimensions")]
impl Defs {
impl Codegen {
pub fn get_base_dimension_entry(
&self,
field: &Ident,
value: &BaseDimensionExponent,
) -> TokenStream {
let num = value.num;
let denom = value.denom;
quote! { #field: Ratio { num: #num, denom: #denom }, }
let num = value.num();
let denom = value.denom();
quote! { #field: Ratio::new(#num, #denom), }
}

pub fn base_dimension_type(&self) -> TokenStream {
Expand Down Expand Up @@ -78,7 +79,7 @@ impl Defs {
}

#[cfg(not(feature = "rational-dimensions"))]
impl Defs {
impl Codegen {
pub fn get_base_dimension_entry(
&self,
field: &Ident,
Expand Down
16 changes: 8 additions & 8 deletions crates/diman_unit_system/src/codegen/debug_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use quote::quote;

use crate::{
dimension_math::BaseDimensions,
types::{base_dimension::BaseDimension, Defs, Unit},
types::{base_dimension::BaseDimension, Unit},
};

impl Defs {
use super::Codegen;

impl Codegen {
pub fn units_array<'a>(&self, units: impl Iterator<Item = &'a Unit>) -> TokenStream {
let units: TokenStream = units
.filter_map(|unit| {
Expand All @@ -22,13 +24,11 @@ impl Defs {
}

pub fn gen_debug_trait_impl(&self) -> TokenStream {
let Defs {
quantity_type,
dimension_type,
..
} = &self;
let units = self.units_array(self.units.iter().filter(|unit| unit.magnitude == 1.0));
let dimension_type = &self.defs.dimension_type;
let quantity_type = &self.defs.quantity_type;
let units = self.units_array(self.defs.units.iter().filter(|unit| unit.magnitude == 1.0));
let get_base_dimension_symbols = self
.defs
.base_dimensions
.iter()
.map(|base_dim| self.get_base_dimension_symbol(base_dim))
Expand Down
Loading

0 comments on commit 55dd4e8

Please sign in to comment.