Skip to content

Commit

Permalink
Long lines, shortened
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Aug 27, 2024
1 parent 0489c96 commit 89aa979
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 42 deletions.
96 changes: 71 additions & 25 deletions src/core/quantitative.PhysicalQuantity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,31 @@ object PhysicalQuantity:

// derived units from https://en.wikipedia.org/wiki/List_of_physical_quantities

type ElectricalConductivity = Units[-3, Length] & Units[-1, Mass] & Units[3, Time] & Units[2, Current]
type ElectricalConductivity =
Units[-3, Length] & Units[-1, Mass] & Units[3, Time] & Units[2, Current]

type Permittivity = Units[-3, Length] & Units[-1, Mass] & Units[4, Time] & Units[2, Current]
type ReactionRate = Units[-3, Length] & Units[-1, Time] & Units[1, AmountOfSubstance]
type MolarConcentration = Units[-3, Length] & Units[1, AmountOfSubstance]
type ElectricChargeDensity = Units[-3, Length] & Units[1, Time] & Units[1, Current]
type MassDensity = Units[-3, Length] & Units[1, Mass]
type Reluctance = Units[-2, Length] & Units[-1, Mass] & Units[2, Time] & Units[2, Current]
type ElectricalConductance = Units[-2, Length] & Units[-1, Mass] & Units[3, Time] & Units[2, Current]
type ThermalResistance = Units[-2, Length] & Units[-1, Mass] & Units[3, Time] & Units[1, Temperature]

type ElectricalConductance =
Units[-2, Length] & Units[-1, Mass] & Units[3, Time] & Units[2, Current]

type ThermalResistance =
Units[-2, Length] & Units[-1, Mass] & Units[3, Time] & Units[1, Temperature]

type Capacitance = Units[-2, Length] & Units[-1, Mass] & Units[4, Time] & Units[1, Current]
type CurrentDensity = Units[-2, Length] & Units[1, Current]
type ElectricDisplacementField = Units[-2, Length] & Units[1, Time] & Units[1, Current]
type Illuminance = Units[-2, Length] & Units[1, Luminosity]
type AreaDensity = Units[-2, Length] & Units[1, Mass]
type ThermalResistivity = Units[-1, Length] & Units[-1, Mass] & Units[3, Time] & Units[1, Temperature]

type ThermalResistivity =
Units[-1, Length] & Units[-1, Mass] & Units[3, Time] & Units[1, Temperature]

type Magnetization = Units[-1, Length] & Units[1, Current]
type OpticalPower = Units[-1, Length]
type TempratureGradient = Units[-1, Length] & Units[1, Temperature]
Expand All @@ -70,8 +80,13 @@ object PhysicalQuantity:
type Acceleration = Units[1, Length] & Units[-2, Time]
type Velocity = Units[1, Length] & Units[-1, Time]
type ElectricDipoleMoment = Units[1, Length] & Units[1, Time] & Units[1, Current]
type ElectricFieldStrength = Units[1, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Current]
type ThermalConductivity = Units[1, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Temperature]

type ElectricFieldStrength =
Units[1, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Current]

type ThermalConductivity =
Units[1, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Temperature]

type Permeability = Units[1, Length] & Units[1, Mass] & Units[-2, Time] & Units[-2, Current]
type Force = Units[1, Length] & Units[1, Mass] & Units[-2, Time]
type Momentum = Units[1, Length] & Units[1, Mass] & Units[-1, Time]
Expand All @@ -81,24 +96,34 @@ object PhysicalQuantity:
type Area = Units[2, Length]
type MagneticMoment = Units[2, Length] & Units[1, Current]
type Impedance = Units[2, Length] & Units[1, Mass] & Units[-3, Time] & Units[-2, Current]
type ElectricalPotential = Units[2, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Current]
type ThermalConductance = Units[2, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Temperature]

type ElectricalPotential =
Units[2, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Current]

type ThermalConductance =
Units[2, Length] & Units[1, Mass] & Units[-3, Time] & Units[-1, Temperature]

type Power = Units[2, Length] & Units[1, Mass] & Units[-3, Time]
type Inductance = Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-2, Current]
type MagneticFlux = Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-1, Current]
type Entropy = Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-1, Temperature]

type MolarEntropy =
Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-1, Temperature] & Units[-1, AmountOfSubstance]
Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-1, Temperature] & Units[-1,
AmountOfSubstance]

type ChemicalPotential =
Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-1, AmountOfSubstance]

type ChemicalPotential = Units[2, Length] & Units[1, Mass] & Units[-2, Time] & Units[-1, AmountOfSubstance]
type Energy = Units[2, Length] & Units[1, Mass] & Units[-2, Time]
type Spin = Units[2, Length] & Units[1, Mass] & Units[-1, Time]
type MomentOfInertia = Units[2, Length] & Units[1, Mass]
type SpecificVolume = Units[3, Length] & Units[-1, Mass]
type VolumetricFlowRate = Units[3, Length] & Units[-1, Time]
type Volume = Units[3, Length]
type ElectricalResistivity = Units[3, Length] & Units[1, Mass] & Units[-3, Time] & Units[-2, Current]

type ElectricalResistivity =
Units[3, Length] & Units[1, Mass] & Units[-3, Time] & Units[-2, Current]

erased given absement: PhysicalQuantity[Absement, "absement"] = ###
erased given absorbedDoseRate: PhysicalQuantity[AbsorbedDoseRate, "absorbed dose rate"] = ###
Expand Down Expand Up @@ -148,18 +173,39 @@ object PhysicalQuantity:
erased given thermalResistivity: PhysicalQuantity[ThermalResistivity, "thermal resistivity"] = ###
erased given velocity: PhysicalQuantity[Velocity, "velocity"] = ###
erased given volume: PhysicalQuantity[Volume, "volume"] = ###
erased given electricChargeDensity: PhysicalQuantity[ElectricChargeDensity, "electric charge density"] = ###
erased given electricDipoleMoment: PhysicalQuantity[ElectricDipoleMoment, "electric dipole moment"] = ###
erased given electricFieldStrength: PhysicalQuantity[ElectricFieldStrength, "electric field strength"] = ###
erased given electricalConductance: PhysicalQuantity[ElectricalConductance, "electric conductance"] = ###
erased given electricalConductivity: PhysicalQuantity[ElectricalConductivity, "electric conductivity"] = ###
erased given electricalPotential: PhysicalQuantity[ElectricalPotential, "electric potential"] = ###
erased given electricalResistivity: PhysicalQuantity[ElectricalResistivity, "electric resistivity"] = ###
erased given magneticFluxDensity: PhysicalQuantity[MagneticFluxDensity, "magnetic flux density"] = ###
erased given specificHeatCapacity: PhysicalQuantity[SpecificHeatCapacity, "specific heat capacity"] = ###
erased given thermalConductivity: PhysicalQuantity[ThermalConductivity, "thermal conductivity"] = ###
erased given volumetricFlowRate: PhysicalQuantity[VolumetricFlowRate, "volumetric flow rate"] = ###

erased given electricDisplacementField
: PhysicalQuantity[ElectricDisplacementField, "electric displacement field"] =

erased given electricChargeDensity: PhysicalQuantity[ElectricChargeDensity,
"electric charge density"] = ###

erased given electricDipoleMoment: PhysicalQuantity[ElectricDipoleMoment,
"electric dipole moment"] = ###

erased given electricFieldStrength: PhysicalQuantity[ElectricFieldStrength,
"electric field strength"] = ###

erased given electricalConductance: PhysicalQuantity[ElectricalConductance,
"electric conductance"] = ###

erased given electricalConductivity: PhysicalQuantity[ElectricalConductivity,
"electric conductivity"] = ###

erased given electricalPotential: PhysicalQuantity[ElectricalPotential, "electric potential"] =
###

erased given electricalResistivity: PhysicalQuantity[ElectricalResistivity,
"electric resistivity"] = ###

erased given magneticFluxDensity: PhysicalQuantity[MagneticFluxDensity, "magnetic flux density"] =
###

erased given specificHeatCapacity: PhysicalQuantity[SpecificHeatCapacity,
"specific heat capacity"] = ###

erased given thermalConductivity: PhysicalQuantity[ThermalConductivity, "thermal conductivity"] =
###

erased given volumetricFlowRate: PhysicalQuantity[VolumetricFlowRate, "volumetric flow rate"] =
###

erased given electricDisplacementField: PhysicalQuantity[ElectricDisplacementField,
"electric displacement field"] = ###
26 changes: 18 additions & 8 deletions src/core/quantitative.Quantitative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,32 @@ object Quantitative extends Quantitative2:
case _ => quantity

object MetricUnit:
erased given [UnitsType <: Measure] => Underlying[MetricUnit[UnitsType], Double] as underlying = ###
erased given [UnitsType <: Measure] => Underlying[MetricUnit[UnitsType], Double] as underlying =
###

def apply[UnitsType <: Measure](value: Double): MetricUnit[UnitsType] = value

@targetName("makeDerivedUnit")
def apply[UnitsType <: Measure](value: Quantity[UnitsType]): MetricUnit[UnitsType] = value

object Quantity:
erased given [UnitsType <: Measure] => Underlying[Quantity[UnitsType], Double] as underlying = ###
erased given [UnitsType <: Measure] => Underlying[Quantity[UnitsType], Double] as underlying =
###

erased given [UnitsType <: Measure]: CanEqual[Quantity[UnitsType], Quantity[UnitsType]] = ###

given Quantity[Seconds[1]] is GenericDuration as genericDuration =
quantity => (quantity*1000.0).toLong

given Quantity[Seconds[1]] is SpecificDuration as specificDuration = long => Quantity(long/1000.0)
given Quantity[Seconds[1]] is SpecificDuration as specificDuration =
long => Quantity(long/1000.0)

transparent inline given [LeftType <: Measure, RightType <: Measure] => Quantity[LeftType] is Addable by Quantity[RightType] as addable =
transparent inline given [LeftType <: Measure, RightType <: Measure]
=> Quantity[LeftType] is Addable by Quantity[RightType] as addable =
${Quantitative.addTypeclass[LeftType, RightType]}

transparent inline given [LeftType <: Measure, RightType <: Measure] => Quantity[LeftType] is Subtractable by Quantity[RightType] as subtractable =
transparent inline given [LeftType <: Measure, RightType <: Measure]
=> Quantity[LeftType] is Subtractable by Quantity[RightType] as subtractable =
${Quantitative.subTypeclass[LeftType, RightType]}

transparent inline given [LeftType <: Measure, RightType <: Measure]
Expand All @@ -71,18 +78,21 @@ object Quantitative extends Quantitative2:
type Result = Quantity[LeftType]
inline def multiply(left: Quantity[LeftType], right: Double): Quantity[LeftType] = left*right

transparent inline given [LeftType <: Measure, RightType <: Measure] => Quantity[LeftType] is Divisible by Quantity[RightType] as divisible =
transparent inline given [LeftType <: Measure, RightType <: Measure]
=> Quantity[LeftType] is Divisible by Quantity[RightType] as divisible =
${Quantitative.divTypeclass[LeftType, RightType]}

given [LeftType <: Measure] => Quantity[LeftType] is Divisible:
type Result = Quantity[LeftType]
type Operand = Double
inline def divide(left: Quantity[LeftType], right: Double): Quantity[LeftType] = left/right

transparent inline given [ValueType <: Measure] => Quantity[ValueType] is Rootable[2] as squareRoot =
transparent inline given [ValueType <: Measure]
=> Quantity[ValueType] is Rootable[2] as squareRoot =
${Quantitative.sqrtTypeclass[ValueType]}

transparent inline given [ValueType <: Measure] => Quantity[ValueType] is Rootable[3] as cubeRoot =
transparent inline given [ValueType <: Measure]
=> Quantity[ValueType] is Rootable[3] as cubeRoot =
${Quantitative.cbrtTypeclass[ValueType]}

inline def apply[UnitsType <: Measure](value: Double): Quantity[UnitsType] = value
Expand Down
23 changes: 14 additions & 9 deletions src/core/quantitative.Quantitative2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ trait Quantitative2:
Some(AppliedType(unit.ref, List(ConstantType(IntConstant(power)))))

case UnitPower(unit, power) :: more =>
Some(AndType(AppliedType(unit.ref, List(ConstantType(IntConstant(power)))), construct(more).get))
Some(AndType(AppliedType(unit.ref, List(ConstantType(IntConstant(power)))),
construct(more).get))

def sub(dimension: DimensionRef, unit: UnitRef, power: Int): UnitsMap =
new UnitsMap(map.updated(dimension, UnitPower(unit, power)))
Expand Down Expand Up @@ -319,8 +320,9 @@ trait Quantitative2:

((leftNorm*rightNorm).repr.map(_.asType): @unchecked) match
case Some('[type resultType <: Measure; resultType]) =>
'{ Multiplicable.Basic[Quantity[LeftType], Quantity[RightType], Quantity[resultType]]: (left, right) =>
${Quantitative.multiply('left, 'right, false).asExprOf[Quantity[resultType]]} }
'{ Multiplicable.Basic[Quantity[LeftType], Quantity[RightType], Quantity[resultType]] {
(left, right) =>
${Quantitative.multiply('left, 'right, false).asExprOf[Quantity[resultType]]} } }

case None =>
'{ Multiplicable.Basic[Quantity[LeftType], Quantity[RightType], Double]: (left, right) =>
Expand All @@ -338,8 +340,9 @@ trait Quantitative2:

((leftNorm/rightNorm).repr.map(_.asType): @unchecked) match
case Some('[type resultType <: Measure; resultType]) =>
'{ Divisible.Basic[Quantity[LeftType], Quantity[RightType], Quantity[resultType]]: (left, right) =>
${Quantitative.multiply('left, 'right, true).asExprOf[Quantity[resultType]]} }
'{ Divisible.Basic[Quantity[LeftType], Quantity[RightType], Quantity[resultType]] {
(left, right) =>
${Quantitative.multiply('left, 'right, true).asExprOf[Quantity[resultType]]} } }

case None =>
'{ Divisible.Basic[Quantity[LeftType], Quantity[RightType], Double]: (left, right) =>
Expand Down Expand Up @@ -435,8 +438,9 @@ trait Quantitative2:

(units.repr.map(_.asType): @unchecked) match
case Some('[type measureType <: Measure; measureType]) =>
'{ Subtractable.Basic[Quantity[LeftType], Quantity[RightType], Quantity[measureType]]: (left, right) =>
${Quantitative.add('left, 'right, '{true}).asExprOf[Quantity[measureType]]} }
'{ Subtractable.Basic[Quantity[LeftType], Quantity[RightType], Quantity[measureType]] {
(left, right) =>
${Quantitative.add('left, 'right, '{true}).asExprOf[Quantity[measureType]]} } }

def addTypeclass[LeftType <: Measure: Type, RightType <: Measure: Type](using Quotes)
: Expr[Quantity[LeftType] is Addable by Quantity[RightType]] =
Expand All @@ -445,8 +449,9 @@ trait Quantitative2:

(units.repr.map(_.asType): @unchecked) match
case Some('[type resultType <: Measure; resultType]) =>
'{ Addable.Basic[Quantity[LeftType], Quantity[RightType], Quantity[resultType]]: (left, right) =>
${Quantitative.add('left, 'right, '{false}).asExprOf[Quantity[resultType]]} }
'{ Addable.Basic[Quantity[LeftType], Quantity[RightType], Quantity[resultType]] {
(left, right) =>
${Quantitative.add('left, 'right, '{false}).asExprOf[Quantity[resultType]]} } }

def norm[UnitsType <: Measure: Type, NormType[power <: Nat] <: Units[power, ?]: Type]
(expr: Expr[Quantity[UnitsType]])
Expand Down

0 comments on commit 89aa979

Please sign in to comment.