Skip to content
Snippets Groups Projects

Draft: parse prefix as scale factor

Closed Henrike Fleischhack requested to merge issue_prefix into main
1 file
+ 36
19
Compare changes
  • Side-by-side
  • Inline
+ 36
19
@@ -218,6 +218,13 @@ class dsiParser:
@@ -218,6 +218,13 @@ class dsiParser:
if (len(items) == 0) and (item == ''): break
if (len(items) == 0) and (item == ''): break
(prefix, unit, exponent) = ('', '', '')
(prefix, unit, exponent) = ('', '', '')
valid=True
valid=True
 
 
#Convert prefixes to scale factors
 
for node in nodes:
 
if node.prefix != "":
 
node.scaleFactor *= _dsiPrefixesScales[node.prefix]**node.exponent
 
node.prefix = ""
 
return (nodes, warningMessages)
return (nodes, warningMessages)
def info(self):
def info(self):
@@ -339,7 +346,8 @@ class dsiUnit:
@@ -339,7 +346,8 @@ class dsiUnit:
# Handle prefix (if any) and unit
# Handle prefix (if any) and unit
prefixStr = _dsiPrefixesUTF8.get(node.prefix, ''+node.prefix+'') if node.prefix else ""
prefixStr = _dsiPrefixesUTF8.get(node.prefix, ''+node.prefix+'') if node.prefix else ""
utf8Str = f"{prefixStr}{unitStr}" # Direct concatenation for compactness
scaleFactorStr = f"{node.scaleFactor}*" if node.scaleFactor != 1 else ""
 
utf8Str = f"{scaleFactorStr}{prefixStr}{unitStr}" # Direct concatenation for compactness
# Handle exponent, converting to UTF-8 subscript, if not 1
# Handle exponent, converting to UTF-8 subscript, if not 1
if node.exponent and node.exponent != 1:
if node.exponent and node.exponent != 1:
@@ -406,6 +414,12 @@ class dsiUnit:
@@ -406,6 +414,12 @@ class dsiUnit:
fractionalScaleFactor = 1 / node.scaleFactor
fractionalScaleFactor = 1 / node.scaleFactor
consolidated_nodes.append(_node(node.prefix, node.unit, invertedExponent, scaleFactor=fractionalScaleFactor))
consolidated_nodes.append(_node(node.prefix, node.unit, invertedExponent, scaleFactor=fractionalScaleFactor))
 
##Convert prefixes to scale factors
 
#for node in consolidated_nodes:
 
# if node.prefix != "":
 
# node.scaleFactor *= _dsiPrefixesScales[node.prefix]**node.exponent
 
# node.prefix = ""
 
# Consolidating nodes with the same unit
# Consolidating nodes with the same unit
i = 0
i = 0
while i < len(consolidated_nodes):
while i < len(consolidated_nodes):
@@ -429,22 +443,18 @@ class dsiUnit:
@@ -429,22 +443,18 @@ class dsiUnit:
node.scaleFactor = 1.0 # Reset scale factor for individual nodes
node.scaleFactor = 1.0 # Reset scale factor for individual nodes
# Sort nodes alphabetically by unit
# Sort nodes alphabetically by unit
consolidated_nodes.sort(key=lambda x: x.unit)
consolidated_nodes.sort(key=lambda x: x.unit)
# Apply overall scale factor to the first node, if it exists
if consolidated_nodes:
consolidated_nodes[0].scaleFactor = overall_scale_factor
nodesWOPowerZero=[]
nodesWOPowerZero=[]
for node in consolidated_nodes:
for node in consolidated_nodes:
if node.exponent != 0:
if node.exponent != 0 and node.unit != "one":
nodesWOPowerZero.append(node)
nodesWOPowerZero.append(node)
if len(nodesWOPowerZero) == 0: # ok all nodes have ben power of zero so we deleted them so we end up with one as unit and 1.0 as exponent
if len(nodesWOPowerZero) == 0: # ok all nodes have ben power of zero so we deleted them so we end up with one as unit and 1.0 as exponent
nodesWOPowerZero.append(_node("", "one", 1.0,scaleFactor=overall_scale_factor))
nodesWOPowerZero.append(_node("", "one", 1.0,scaleFactor=1))
consolidated_nodes=nodesWOPowerZero
consolidated_nodes=nodesWOPowerZero
# Check for ones and delete them if they are not the only node ad set there exponent to 1.0 since 1^x = 1
if len(consolidated_nodes) > 1:
# Apply overall scale factor to the first node
consolidated_nodes = [node for node in consolidated_nodes if node.unit != "one"]
consolidated_nodes[0].scaleFactor = overall_scale_factor
else:
if consolidated_nodes[0].unit == "one":
consolidated_nodes[0].exponent=1.0
# Create and return a new instance of _dsiTree with consolidated nodes
# Create and return a new instance of _dsiTree with consolidated nodes
return dsiUnit(self.dsiString, [consolidated_nodes], self.warnings, self._latexDefaultWrapper,
return dsiUnit(self.dsiString, [consolidated_nodes], self.warnings, self._latexDefaultWrapper,
self._latexDefaultPrefix, self._latexDefaultSuffix)
self._latexDefaultPrefix, self._latexDefaultSuffix)
@@ -454,6 +464,7 @@ class dsiUnit:
@@ -454,6 +464,7 @@ class dsiUnit:
for i,node in enumerate(self.tree[1]):
for i,node in enumerate(self.tree[1]):
#invert exponent node.
#invert exponent node.
node.exponent=node.exponent*-1
node.exponent=node.exponent*-1
 
node.scaleFactor = 1/node.scaleFactor
self.tree[0].append(node)
self.tree[0].append(node)
self.tree[1].pop(i)
self.tree[1].pop(i)
self.tree.pop(1)
self.tree.pop(1)
@@ -463,16 +474,18 @@ class dsiUnit:
@@ -463,16 +474,18 @@ class dsiUnit:
for node in self.tree[0]: # numerator
for node in self.tree[0]: # numerator
if node.exponent < 0:
if node.exponent < 0:
node.exponent = -node.exponent
node.exponent = -node.exponent
 
node.scaleFactor = 1.0/node.scaleFactor
try:
try:
self.tree[1].append(_node("", node.unit, node.exponent))
self.tree[1].append(_node("", node.unit, node.exponent, scaleFactor = node.scaleFactor))
except IndexError:# if we have only the numerator list we need to add the denominator list
except IndexError:# if we have only the numerator list we need to add the denominator list
self.tree.append([_node("", node.unit, node.exponent)])
self.tree.append([_node("", node.unit, node.exponent, scaleFactor = node.scaleFactor)])
self.tree[0].remove(node)
self.tree[0].remove(node)
if len(self.tree) ==2: # we have a numerator and a denominator so we must treat the denominator as well
if len(self.tree) ==2: # we have a numerator and a denominator so we must treat the denominator as well
for node in self.tree[1]: # numerator
for node in self.tree[1]: # numerator
if node.exponent < 0:
if node.exponent < 0:
node.exponent = -node.exponent
node.exponent = -node.exponent
self.tree[0].append(_node("", node.unit, node.exponent))
node.scaleFactor = 1.0/node.scaleFactor
 
self.tree[0].append(_node("", node.unit, node.exponent, scaleFactor = node.scaleFactor))
self.tree[1].remove(node)
self.tree[1].remove(node)
if len(self.tree[0]) == 0:
if len(self.tree[0]) == 0:
self.tree[0].append(_node("", "one", 1.0))
self.tree[0].append(_node("", "one", 1.0))
@@ -587,6 +600,7 @@ class dsiUnit:
@@ -587,6 +600,7 @@ class dsiUnit:
for unitFraction in resultNodeLIst:
for unitFraction in resultNodeLIst:
for node in unitFraction:
for node in unitFraction:
node.exponent *= other
node.exponent *= other
 
node.scaleFactor = node.scaleFactor**other
resultTree =dsiUnit("", resultNodeLIst, self.warnings, self._latexDefaultWrapper, self._latexDefaultPrefix, self._latexDefaultSuffix)
resultTree =dsiUnit("", resultNodeLIst, self.warnings, self._latexDefaultWrapper, self._latexDefaultPrefix, self._latexDefaultSuffix)
resultTree = resultTree.reduceFraction()
resultTree = resultTree.reduceFraction()
if len(self.tree)==2: # check if we had a per representation
if len(self.tree)==2: # check if we had a per representation
@@ -696,7 +710,7 @@ class _node:
@@ -696,7 +710,7 @@ class _node:
"""
"""
# Adjust the scale factor for the prefix
# Adjust the scale factor for the prefix
prefixScale = _dsiPrefixesScales.get(self.prefix, 1) # Default to 1 if no prefix
prefixScale = _dsiPrefixesScales.get(self.prefix, 1) # Default to 1 if no prefix
adjustedScaleFactor = self.scaleFactor * prefixScale
adjustedScaleFactor = self.scaleFactor * prefixScale**self.exponent
# Convert to base units if it's a derived unit
# Convert to base units if it's a derived unit
if self.unit in _derivedToBaseUnits:
if self.unit in _derivedToBaseUnits:
@@ -705,7 +719,7 @@ class _node:
@@ -705,7 +719,7 @@ class _node:
for i, (baseUnit, exponent, scaleFactor) in enumerate(baseUnitsInfo):
for i, (baseUnit, exponent, scaleFactor) in enumerate(baseUnitsInfo):
# Apply the adjusted scale factor only to the first base unit
# Apply the adjusted scale factor only to the first base unit
finalScaleFactor = math.pow(adjustedScaleFactor * scaleFactor, self.exponent) if i == 0 else 1.0
finalScaleFactor = math.pow(adjustedScaleFactor * scaleFactor, self.exponent) if i == 0 else 1.0
baseUnits.append(_node('', baseUnit, exponent * self.exponent, scaleFactor=finalScaleFactor))
baseUnits.append(_node('', baseUnit, exponent = exponent, scaleFactor=finalScaleFactor))
return baseUnits
return baseUnits
elif complete:
elif complete:
# Additional logic for converting ampere, volt, and mole to kg, s, and m equivalents
# Additional logic for converting ampere, volt, and mole to kg, s, and m equivalents
@@ -714,7 +728,7 @@ class _node:
@@ -714,7 +728,7 @@ class _node:
kgsUnits = []
kgsUnits = []
for i, (kgsUnit, exponent, scaleFactor) in enumerate(kgsUnitsInfo):
for i, (kgsUnit, exponent, scaleFactor) in enumerate(kgsUnitsInfo):
finalScaleFactor = math.pow(adjustedScaleFactor * scaleFactor, self.exponent) if i == 0 else 1.0
finalScaleFactor = math.pow(adjustedScaleFactor * scaleFactor, self.exponent) if i == 0 else 1.0
kgsUnits.append(_node('', kgsUnit, exponent * self.exponent, scaleFactor=finalScaleFactor))
kgsUnits.append(_node('', kgsUnit, exponent = exponent, scaleFactor=finalScaleFactor))
return kgsUnits
return kgsUnits
# Return the node as is if it's already a base unit, with adjusted scale factor
# Return the node as is if it's already a base unit, with adjusted scale factor
@@ -727,6 +741,9 @@ class _node:
@@ -727,6 +741,9 @@ class _node:
def __str__(self):
def __str__(self):
result=''
result=''
 
if self.scaleFactor != 1:
 
result+= f"{self.scaleFactor}*"
 
if self.prefix !='':
if self.prefix !='':
result+='\\'+self.prefix
result+='\\'+self.prefix
result = result + '\\' + self.unit
result = result + '\\' + self.unit
@@ -1066,4 +1083,4 @@ _additionalConversions = {
@@ -1066,4 +1083,4 @@ _additionalConversions = {
}
}
_dsiKeyWords = {
_dsiKeyWords = {
'tothe': r'\tothe',
'tothe': r'\tothe',
'per': r'\per'}
'per': r'\per'}
\ No newline at end of file
Loading