Cleaned up variable names

pull/2/head
Mark 2023-04-13 09:11:20 -07:00
parent 84d2d68aa1
commit e2d49c5fb6
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 95 additions and 95 deletions

View File

@ -15,18 +15,18 @@ use crate::quantity::Scalar;
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
pub struct Quantity { pub struct Quantity {
pub v: Scalar, pub scalar: Scalar,
pub u: Unit pub unit: Unit
} }
impl ToString for Quantity { impl ToString for Quantity {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let n = self.v.to_string(); let n = self.scalar.to_string();
if self.unitless() { return n; } if self.unitless() { return n; }
let u = self.u.to_string(); let u = self.unit.to_string();
if self.is_one() { return u; }; if self.is_one() { return u; };
return format!("{n} {u}"); return format!("{n} {u}");
@ -35,10 +35,10 @@ impl ToString for Quantity {
impl Quantity { impl Quantity {
pub fn to_string_outer(&self) -> String { pub fn to_string_outer(&self) -> String {
let n = self.v.to_string(); let n = self.scalar.to_string();
if self.unitless() { return n; } if self.unitless() { return n; }
let u = self.u.to_string(); let u = self.unit.to_string();
return format!("{n} {u}"); return format!("{n} {u}");
} }
@ -47,8 +47,8 @@ impl Quantity {
if v.is_none() { return None; } if v.is_none() { return None; }
return Some(Quantity{ return Some(Quantity{
v: v.unwrap(), scalar: v.unwrap(),
u: Unit::new() unit: Unit::new()
}); });
} }
@ -57,8 +57,8 @@ impl Quantity {
if v.is_none() { return None; } if v.is_none() { return None; }
return Some(Quantity{ return Some(Quantity{
v: v.unwrap(), scalar: v.unwrap(),
u: Unit::new() unit: Unit::new()
}); });
} }
@ -67,8 +67,8 @@ impl Quantity {
if v.is_none() { return None; } if v.is_none() { return None; }
return Some(Quantity{ return Some(Quantity{
v: v.unwrap(), scalar: v.unwrap(),
u: Unit::new() unit: Unit::new()
}); });
} }
@ -77,29 +77,29 @@ impl Quantity {
if v.is_none() { return None; } if v.is_none() { return None; }
return Some(Quantity{ return Some(Quantity{
v: v.unwrap(), scalar: v.unwrap(),
u: Unit::new() unit: Unit::new()
}); });
} }
pub fn from_scalar(s: Scalar) -> Quantity { pub fn from_scalar(s: Scalar) -> Quantity {
return Quantity{ return Quantity{
v: s, scalar: s,
u: Unit::new() unit: Unit::new()
}; };
} }
pub fn insert_unit(&mut self, ui: FreeUnit, pi: Scalar) { self.u.insert(ui, pi) } pub fn insert_unit(&mut self, ui: FreeUnit, pi: Scalar) { self.unit.insert(ui, pi) }
pub fn set_unit(&mut self, u: Unit) { self.u = u; } pub fn set_unit(&mut self, u: Unit) { self.unit = u; }
pub fn convert_to(self, other: Quantity) -> Option<Quantity> { pub fn convert_to(self, other: Quantity) -> Option<Quantity> {
let fa = self.u.to_base_factor(); let fa = self.unit.to_base_factor();
let fb = other.u.to_base_factor(); let fb = other.unit.to_base_factor();
let r = self * fa / fb; let r = self * fa / fb;
// If this didn't work, units are incompatible // If this didn't work, units are incompatible
if r.u != other.u { return None; }; if r.unit != other.unit { return None; };
return Some(r); return Some(r);
} }
@ -111,8 +111,8 @@ macro_rules! quant_foward {
pub fn $x(&self) -> Quantity { pub fn $x(&self) -> Quantity {
if !self.unitless() { panic!() } if !self.unitless() { panic!() }
Quantity { Quantity {
v: self.v.$x(), scalar: self.scalar.$x(),
u: self.u.clone() unit: self.unit.clone()
} }
} }
} }
@ -120,13 +120,13 @@ macro_rules! quant_foward {
impl Quantity { impl Quantity {
pub fn is_zero(&self) -> bool { self.v.is_zero() } pub fn is_zero(&self) -> bool { self.scalar.is_zero() }
pub fn is_one(&self) -> bool { self.v.is_one() } pub fn is_one(&self) -> bool { self.scalar.is_one() }
pub fn is_nan(&self) -> bool { self.v.is_nan() } pub fn is_nan(&self) -> bool { self.scalar.is_nan() }
pub fn is_negative(&self) -> bool { self.v.is_negative() } pub fn is_negative(&self) -> bool { self.scalar.is_negative() }
pub fn is_positive(&self) -> bool { self.v.is_positive() } pub fn is_positive(&self) -> bool { self.scalar.is_positive() }
pub fn unitless(&self) -> bool { self.u.unitless() } pub fn unitless(&self) -> bool { self.unit.unitless() }
pub fn unit(&self) -> &Unit { &self.u } pub fn unit(&self) -> &Unit { &self.unit }
quant_foward!(fract); quant_foward!(fract);
quant_foward!(abs); quant_foward!(abs);
@ -153,15 +153,15 @@ impl Quantity {
pub fn log(&self, base: Quantity) -> Quantity { pub fn log(&self, base: Quantity) -> Quantity {
if !self.unitless() { panic!() } if !self.unitless() { panic!() }
Quantity { Quantity {
v: self.v.log(base.v), scalar: self.scalar.log(base.scalar),
u: self.u.clone() unit: self.unit.clone()
} }
} }
pub fn pow(&self, pwr: Quantity) -> Quantity { pub fn pow(&self, pwr: Quantity) -> Quantity {
Quantity { Quantity {
v: self.v.pow(pwr.v.clone()), scalar: self.scalar.pow(pwr.scalar.clone()),
u: self.u.pow(pwr.v) unit: self.unit.pow(pwr.scalar)
} }
} }
} }
@ -172,8 +172,8 @@ impl Neg for Quantity where {
fn neg(self) -> Self::Output { fn neg(self) -> Self::Output {
Quantity { Quantity {
v: -self.v, scalar: -self.scalar,
u: self.u unit: self.unit
} }
} }
} }
@ -182,30 +182,30 @@ impl Add for Quantity {
type Output = Self; type Output = Self;
fn add(self, other: Self) -> Self::Output { fn add(self, other: Self) -> Self::Output {
if self.u != other.u { panic!() } if self.unit != other.unit { panic!() }
let mut o = other; let mut o = other;
if !o.u.prefixes_match(&self.u) { if !o.unit.prefixes_match(&self.unit) {
o = o.convert_to(self.clone()).unwrap(); o = o.convert_to(self.clone()).unwrap();
} }
Quantity { Quantity {
v: self.v + o.v, scalar: self.scalar + o.scalar,
u: self.u unit: self.unit
} }
} }
} }
impl AddAssign for Quantity where { impl AddAssign for Quantity where {
fn add_assign(&mut self, other: Self) { fn add_assign(&mut self, other: Self) {
if self.u != other.u { panic!() } if self.unit != other.unit { panic!() }
let mut o = other; let mut o = other;
if !o.u.prefixes_match(&self.u) { if !o.unit.prefixes_match(&self.unit) {
o = o.convert_to(self.clone()).unwrap(); o = o.convert_to(self.clone()).unwrap();
} }
self.v += o.v self.scalar += o.scalar
} }
} }
@ -213,30 +213,30 @@ impl Sub for Quantity {
type Output = Self; type Output = Self;
fn sub(self, other: Self) -> Self::Output { fn sub(self, other: Self) -> Self::Output {
if self.u != other.u { panic!() } if self.unit != other.unit { panic!() }
let mut o = other; let mut o = other;
if !o.u.prefixes_match(&self.u) { if !o.unit.prefixes_match(&self.unit) {
o = o.convert_to(self.clone()).unwrap(); o = o.convert_to(self.clone()).unwrap();
} }
Quantity { Quantity {
v: self.v - o.v, scalar: self.scalar - o.scalar,
u: self.u unit: self.unit
} }
} }
} }
impl SubAssign for Quantity where { impl SubAssign for Quantity where {
fn sub_assign(&mut self, other: Self) { fn sub_assign(&mut self, other: Self) {
if self.u != other.u { panic!() } if self.unit != other.unit { panic!() }
let mut o = other; let mut o = other;
if !o.u.prefixes_match(&self.u) { if !o.unit.prefixes_match(&self.unit) {
o = o.convert_to(self.clone()).unwrap(); o = o.convert_to(self.clone()).unwrap();
} }
self.v -= o.v self.scalar -= o.scalar;
} }
} }
@ -245,21 +245,21 @@ impl Mul for Quantity {
fn mul(self, other: Self) -> Self::Output { fn mul(self, other: Self) -> Self::Output {
let f = self.u.match_prefix_factor(&other.u); let f = self.unit.match_prefix_factor(&other.unit);
Quantity { Quantity {
v: self.v * other.v * f.v, scalar: self.scalar * other.scalar * f.scalar,
u: self.u * other.u * f.u, unit: self.unit * other.unit * f.unit,
} }
} }
} }
impl MulAssign for Quantity where { impl MulAssign for Quantity where {
fn mul_assign(&mut self, other: Self) { fn mul_assign(&mut self, other: Self) {
let f = self.u.match_prefix_factor(&other.u); let f = self.unit.match_prefix_factor(&other.unit);
self.v *= other.v * f.v; self.scalar *= other.scalar * f.scalar;
self.u *= other.u * f.u; self.unit *= other.unit * f.unit;
} }
} }
@ -268,16 +268,16 @@ impl Div for Quantity {
fn div(self, other: Self) -> Self::Output { fn div(self, other: Self) -> Self::Output {
Quantity { Quantity {
v: self.v / other.v, scalar: self.scalar / other.scalar,
u: self.u / other.u unit: self.unit / other.unit
} }
} }
} }
impl DivAssign for Quantity where { impl DivAssign for Quantity where {
fn div_assign(&mut self, other: Self) { fn div_assign(&mut self, other: Self) {
self.v /= other.v; self.scalar /= other.scalar;
self.u /= other.u; self.unit /= other.unit;
} }
} }
@ -285,27 +285,27 @@ impl Rem<Quantity> for Quantity {
type Output = Self; type Output = Self;
fn rem(self, other: Quantity) -> Self::Output { fn rem(self, other: Quantity) -> Self::Output {
if !self.u.unitless() { panic!() } if !self.unit.unitless() { panic!() }
if !other.u.unitless() { panic!() } if !other.unit.unitless() { panic!() }
Quantity { Quantity {
v: self.v % other.v, scalar: self.scalar % other.scalar,
u: self.u unit: self.unit
} }
} }
} }
impl PartialEq for Quantity { impl PartialEq for Quantity {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if self.u != other.u {false} else { if self.unit != other.unit {false} else {
self.v == other.v self.scalar == other.scalar
} }
} }
} }
impl PartialOrd for Quantity { impl PartialOrd for Quantity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.u != other.u { panic!() } if self.unit != other.unit { panic!() }
self.v.partial_cmp(&other.v) self.scalar.partial_cmp(&other.scalar)
} }
} }

View File

@ -145,20 +145,20 @@ impl ToString for Prefix {
#[derive(Debug)] #[derive(Debug)]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct FreeUnit { pub struct FreeUnit {
u: UnitBase, base: UnitBase,
p: Prefix prefix: Prefix
} }
impl Hash for FreeUnit { impl Hash for FreeUnit {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.u.hash(state); self.base.hash(state);
} }
} }
impl Eq for FreeUnit {} impl Eq for FreeUnit {}
impl PartialEq for FreeUnit { impl PartialEq for FreeUnit {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.u.eq(&other.u) self.base.eq(&other.base)
} }
} }
@ -166,8 +166,8 @@ impl PartialEq for FreeUnit {
macro_rules! quick_base_factor { macro_rules! quick_base_factor {
(float, $u:expr, $s:expr, $( ($x:expr, $p:expr) ),* ) => { (float, $u:expr, $s:expr, $( ($x:expr, $p:expr) ),* ) => {
Some(Quantity { Some(Quantity {
v: Scalar::new_float_from_string($s).unwrap(), scalar: Scalar::new_float_from_string($s).unwrap(),
u: Unit::from_array(&[ unit: Unit::from_array(&[
$( $(
(FreeUnit::from_base($x), Scalar::new_rational($p).unwrap()), (FreeUnit::from_base($x), Scalar::new_rational($p).unwrap()),
)* )*
@ -178,8 +178,8 @@ macro_rules! quick_base_factor {
(rational, $u:expr, $s:expr, $( ($x:expr, $p:expr) ),* ) => { (rational, $u:expr, $s:expr, $( ($x:expr, $p:expr) ),* ) => {
Some(Quantity { Some(Quantity {
v: Scalar::new_float_from_string($s).unwrap(), scalar: Scalar::new_float_from_string($s).unwrap(),
u: Unit::from_array(&[ unit: Unit::from_array(&[
$( $(
(FreeUnit::from_base($x), Scalar::new_rational($p).unwrap()), (FreeUnit::from_base($x), Scalar::new_rational($p).unwrap()),
)* )*
@ -191,21 +191,21 @@ macro_rules! quick_base_factor {
impl FreeUnit { impl FreeUnit {
pub fn from_base(u: UnitBase) -> FreeUnit { pub fn from_base(base: UnitBase) -> FreeUnit {
return FreeUnit { u, p: Prefix::None } return FreeUnit { base, prefix: Prefix::None }
} }
pub fn from_base_prefix(u: UnitBase, p: Prefix) -> FreeUnit { FreeUnit {u, p} } pub fn from_base_prefix(base: UnitBase, prefix: Prefix) -> FreeUnit { FreeUnit {base, prefix} }
pub fn set_prefix(&mut self, p: Prefix) { self.p = p; } pub fn set_prefix(&mut self, prefix: Prefix) { self.prefix = prefix; }
pub fn get_prefix(&self) -> Prefix { self.p } pub fn get_prefix(&self) -> Prefix { self.prefix }
pub fn same_with_prefix(&self, other: &FreeUnit) -> bool { pub fn same_with_prefix(&self, other: &FreeUnit) -> bool {
self.u.eq(&other.u) && self.p.eq(&other.p) self.base.eq(&other.base) && self.prefix.eq(&other.prefix)
} }
pub fn to_base_factor(&self) -> Quantity { pub fn to_base_factor(&self) -> Quantity {
let q = match self.u { let q = match self.base {
// Returns the unit we need to multiply by to get a base // Returns the unit we need to multiply by to get a base
// unit, or `None` if this is already a base unit. // unit, or `None` if this is already a base unit.
// //
@ -258,9 +258,9 @@ impl FreeUnit {
let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap()); let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap());
let mut p = self.p.to_ratio(); let mut p = self.prefix.to_ratio();
p.insert_unit(FreeUnit::from_base(self.u), Scalar::new_rational(1f64).unwrap()); p.insert_unit(FreeUnit::from_base(self.base), Scalar::new_rational(1f64).unwrap());
p.insert_unit(FreeUnit::from_base_prefix(self.u, self.p), Scalar::new_rational(-1f64).unwrap()); p.insert_unit(FreeUnit::from_base_prefix(self.base, self.prefix), Scalar::new_rational(-1f64).unwrap());
q *= p; q *= p;
return q; return q;
@ -270,7 +270,7 @@ impl FreeUnit {
impl ToString for FreeUnit { impl ToString for FreeUnit {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let s = match self.u { let s = match self.base {
UnitBase::Second => "s", UnitBase::Second => "s",
UnitBase::Meter => "m", UnitBase::Meter => "m",
UnitBase::Gram => "g", UnitBase::Gram => "g",
@ -288,7 +288,7 @@ impl ToString for FreeUnit {
UnitBase::Day => "day", UnitBase::Day => "day",
}; };
let p = self.p.to_string(); let p = self.prefix.to_string();
format!("{p}{s}") format!("{p}{s}")
} }
@ -361,6 +361,7 @@ impl Unit {
pub fn get_val(&self) -> &HashMap<FreeUnit, Scalar> { &self.val } pub fn get_val(&self) -> &HashMap<FreeUnit, Scalar> { &self.val }
pub fn get_val_mut(&mut self) -> &mut HashMap<FreeUnit, Scalar> { &mut self.val } pub fn get_val_mut(&mut self) -> &mut HashMap<FreeUnit, Scalar> { &mut self.val }
pub fn unitless(&self) -> bool { self.get_val().len() == 0 }
pub fn from_array(a: &[(FreeUnit, Scalar)]) -> Unit { pub fn from_array(a: &[(FreeUnit, Scalar)]) -> Unit {
let mut n = Unit::new(); let mut n = Unit::new();
@ -394,14 +395,14 @@ impl Unit {
let (su, _) = k.unwrap(); let (su, _) = k.unwrap();
// Conversion factor ou -> basic // Conversion factor ou -> basic
let mut p = ou.p.to_ratio(); let mut p = ou.prefix.to_ratio();
p.insert_unit(FreeUnit::from_base(ou.u), Scalar::new_rational(1f64).unwrap()); p.insert_unit(FreeUnit::from_base(ou.base), Scalar::new_rational(1f64).unwrap());
p.insert_unit(FreeUnit::from_base_prefix(ou.u, ou.p), Scalar::new_rational(-1f64).unwrap()); p.insert_unit(FreeUnit::from_base_prefix(ou.base, ou.prefix), Scalar::new_rational(-1f64).unwrap());
// Conversion factor su -> basic // Conversion factor su -> basic
let mut q = su.p.to_ratio(); let mut q = su.prefix.to_ratio();
q.insert_unit(FreeUnit::from_base(su.u), Scalar::new_rational(1f64).unwrap()); q.insert_unit(FreeUnit::from_base(su.base), Scalar::new_rational(1f64).unwrap());
q.insert_unit(FreeUnit::from_base_prefix(su.u, su.p), Scalar::new_rational(-1f64).unwrap()); q.insert_unit(FreeUnit::from_base_prefix(su.base, su.prefix), Scalar::new_rational(-1f64).unwrap());
f = f * (p / q).pow(Quantity::from_scalar(op.clone())); f = f * (p / q).pow(Quantity::from_scalar(op.clone()));
} }
@ -410,7 +411,6 @@ impl Unit {
return f; return f;
} }
pub fn unitless(&self) -> bool { self.get_val().len() == 0 }
pub fn insert(&mut self, u: FreeUnit, p: Scalar) { pub fn insert(&mut self, u: FreeUnit, p: Scalar) {
let v = self.get_val_mut(); let v = self.get_val_mut();