From 73e282a2dee603e3760f96d52f95cde1dc60218d Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Wed, 3 Jan 2024 15:33:47 +0100 Subject: [PATCH 01/13] (#109) added latex for formula 9.1N --- .../formula_9_1n.py | 23 +++++++++++++++++++ .../test_formula_9_1n.py | 16 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py index 40f1b596..461b2538 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py @@ -1,6 +1,7 @@ """Formula 9.1N from NEN-EN 1992-1-1+C2:2011: Chapter 9 - Detailing of members and particular rules.""" from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula +from blueprints.codes.latex_formula import LatexFormula, latex_fraction, max_curly_brackets_latex, value_to_latex_text from blueprints.type_alias import MM, MM2, MPA from blueprints.validations import raise_if_negative @@ -57,3 +58,25 @@ def _evaluate( """For more detailed documentation see the class docstring.""" raise_if_negative(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d) return max(0.26 * (f_ctm / f_yk) * b_t * d, 0.0013 * b_t * d) + + def latex(self) -> LatexFormula: + """Returns LatexFormula object for formula 9.1N.""" + return LatexFormula( + return_symbol="A_{s,min}", + result=value_to_latex_text(self), + equation=max_curly_brackets_latex( + value_to_latex_text(0.26) + r"\cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d", + value_to_latex_text(0.0013) + r"\cdot b_t \cdot d", + ), + numeric_equation=max_curly_brackets_latex( + value_to_latex_text(0.26) + + r"\cdot" + + latex_fraction(self.f_ctm, self.f_yk) + + r"\cdot" + + value_to_latex_text(self.b_t) + + r"\cdot" + + value_to_latex_text(self.d), + value_to_latex_text(0.0013) + r"\cdot" + value_to_latex_text(self.b_t) + r"\cdot" + value_to_latex_text(self.d), + ), + comparison_operator_label="=", + ) diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py index 0fd1ca3a..c73226a3 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py @@ -81,3 +81,19 @@ def test_raise_error_when_negative_d_is_given(self) -> None: with pytest.raises(NegativeValueError): Form9Dot1NMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d) + + def test_latex(self) -> None: + """Test the latex representation.""" + # Example values + f_ctm = 2 # MPa + f_yk = 355 # MPa + b_t = 50 # mm + d = 150 # mm + form = Form9Dot1NMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d) + + assert form.latex().complete == ( + r"A_{s,min} = \max \left\{\text{0.26}\cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; \text{0.0013}\cdot b_t " + r"\cdot d\right\} = \max \left\{\text{0.26}\cdot\frac{2}{355}\cdot\text{50}\cdot\text{150}; \text{0.0013}" + r"\cdot\text{50}\cdot\text{150}\right\} = \text{10.985915492957748}" + ) + assert form.latex().short == r"A_{s,min} = \text{10.985915492957748}" From 671b3a16f3e7bdf677986d0de925ac4be1cec7cc Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Thu, 4 Jan 2024 15:10:18 +0100 Subject: [PATCH 02/13] (#109) updated 9.1N with updates --- .../formula_9_1n.py | 28 +++++++++---------- .../test_formula_9_1n.py | 8 +++--- tests/codes/test_latex_formula.py | 8 +++--- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py index 461b2538..37f9b407 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py @@ -1,7 +1,7 @@ """Formula 9.1N from NEN-EN 1992-1-1+C2:2011: Chapter 9 - Detailing of members and particular rules.""" from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula -from blueprints.codes.latex_formula import LatexFormula, latex_fraction, max_curly_brackets_latex, value_to_latex_text +from blueprints.codes.latex_formula import LatexFormula, latex_fraction, latex_max_curly_brackets, latex_value_to_text, latex_variable_with_subscript from blueprints.type_alias import MM, MM2, MPA from blueprints.validations import raise_if_negative @@ -61,22 +61,20 @@ def _evaluate( def latex(self) -> LatexFormula: """Returns LatexFormula object for formula 9.1N.""" + latex_f_ctm = latex_variable_with_subscript(variable="f", subscript="ctm") + latex_f_yk = latex_variable_with_subscript(variable="f", subscript="yk") + latex_b_t = latex_variable_with_subscript(variable="b", subscript="t") return LatexFormula( - return_symbol="A_{s,min}", - result=value_to_latex_text(self), - equation=max_curly_brackets_latex( - value_to_latex_text(0.26) + r"\cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d", - value_to_latex_text(0.0013) + r"\cdot b_t \cdot d", + return_symbol=latex_variable_with_subscript(variable="A", subscript="s,min"), + result=latex_value_to_text(self), + equation=latex_max_curly_brackets( + rf"{latex_value_to_text(0.26)} \cdot {latex_fraction(latex_f_ctm, latex_f_yk)} \cdot {latex_b_t} \cdot d", + rf"{latex_value_to_text(0.0013)} \cdot {latex_b_t} \cdot d", ), - numeric_equation=max_curly_brackets_latex( - value_to_latex_text(0.26) - + r"\cdot" - + latex_fraction(self.f_ctm, self.f_yk) - + r"\cdot" - + value_to_latex_text(self.b_t) - + r"\cdot" - + value_to_latex_text(self.d), - value_to_latex_text(0.0013) + r"\cdot" + value_to_latex_text(self.b_t) + r"\cdot" + value_to_latex_text(self.d), + numeric_equation=latex_max_curly_brackets( + rf"{latex_value_to_text(0.26)} \cdot {latex_fraction(self.f_ctm, self.f_yk)} \cdot {latex_value_to_text(self.b_t)} \cdot " + f"{latex_value_to_text(self.d)}", + rf"{latex_value_to_text(0.0013)} \cdot {latex_value_to_text(self.b_t)} \cdot {latex_value_to_text(self.d)}", ), comparison_operator_label="=", ) diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py index c73226a3..b024f62a 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py @@ -92,8 +92,8 @@ def test_latex(self) -> None: form = Form9Dot1NMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d) assert form.latex().complete == ( - r"A_{s,min} = \max \left\{\text{0.26}\cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; \text{0.0013}\cdot b_t " - r"\cdot d\right\} = \max \left\{\text{0.26}\cdot\frac{2}{355}\cdot\text{50}\cdot\text{150}; \text{0.0013}" - r"\cdot\text{50}\cdot\text{150}\right\} = \text{10.985915492957748}" + r"A_{\text{s,min}} = \max \left\{\text{0.26} \cdot \frac{f_{\text{ctm}}}{f_{\text{yk}}} \cdot b_{\text{t}} \cdot d; \text{0.0013} \cdot " + r"b_{\text{t}} \cdot d\right\} = \max \left\{\text{0.26} \cdot \frac{2}{355} \cdot \text{50} \cdot \text{150}; \text{0.0013} \cdot " + r"\text{50} \cdot \text{150}\right\} = \text{10.985915492957748}" ) - assert form.latex().short == r"A_{s,min} = \text{10.985915492957748}" + assert form.latex().short == r"A_{\text{s,min}} = \text{10.985915492957748}" diff --git a/tests/codes/test_latex_formula.py b/tests/codes/test_latex_formula.py index 88858daa..6523c764 100644 --- a/tests/codes/test_latex_formula.py +++ b/tests/codes/test_latex_formula.py @@ -52,10 +52,10 @@ def test_latex_value_to_text() -> None: ], ) def test_latex_max_curly_brackets( - arg_1: str | float, - arg_2: str | float, - arg_3: str | float, - expected_output: str, + arg_1: str | float, + arg_2: str | float, + arg_3: str | float, + expected_output: str, ) -> None: """Test the latex_max_curly_brackets function.""" result = latex_max_curly_brackets(arg_1, arg_2, arg_3) From 0f8b7ae831d338a3c392c56066250231a78e297a Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Fri, 5 Jan 2024 15:35:03 +0100 Subject: [PATCH 03/13] (#109) changed name latex functions --- .../formula_9_1n.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py index 638bb811..57ca6f44 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py @@ -1,7 +1,7 @@ """Formula 9.1N from NEN-EN 1992-1-1+C2:2011: Chapter 9 - Detailing of members and particular rules.""" from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula -from blueprints.codes.latex_formula import LatexFormula, latex_fraction, latex_max_curly_brackets, latex_value_to_text, latex_variable_with_subscript +from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets, to_text, variable_with_subscript from blueprints.type_alias import MM, MM2, MPA from blueprints.validations import raise_if_negative @@ -59,20 +59,20 @@ def _evaluate( def latex(self) -> LatexFormula: """Returns LatexFormula object for formula 9.1N.""" - latex_f_ctm = latex_variable_with_subscript(variable="f", subscript="ctm") - latex_f_yk = latex_variable_with_subscript(variable="f", subscript="yk") - latex_b_t = latex_variable_with_subscript(variable="b", subscript="t") + latex_f_ctm = variable_with_subscript(variable="f", subscript="ctm") + latex_f_yk = variable_with_subscript(variable="f", subscript="yk") + latex_b_t = variable_with_subscript(variable="b", subscript="t") return LatexFormula( - return_symbol=latex_variable_with_subscript(variable="A", subscript="s,min"), - result=latex_value_to_text(self), - equation=latex_max_curly_brackets( - rf"{latex_value_to_text(0.26)} \cdot {latex_fraction(latex_f_ctm, latex_f_yk)} \cdot {latex_b_t} \cdot d", - rf"{latex_value_to_text(0.0013)} \cdot {latex_b_t} \cdot d", + return_symbol=variable_with_subscript(variable="A", subscript="s,min"), + result=to_text(self), + equation=max_curly_brackets( + rf"{to_text(0.26)} \cdot {fraction(latex_f_ctm, latex_f_yk)} \cdot {latex_b_t} \cdot d", + rf"{to_text(0.0013)} \cdot {latex_b_t} \cdot d", ), - numeric_equation=latex_max_curly_brackets( - rf"{latex_value_to_text(0.26)} \cdot {latex_fraction(self.f_ctm, self.f_yk)} \cdot {latex_value_to_text(self.b_t)} \cdot " - f"{latex_value_to_text(self.d)}", - rf"{latex_value_to_text(0.0013)} \cdot {latex_value_to_text(self.b_t)} \cdot {latex_value_to_text(self.d)}", + numeric_equation=max_curly_brackets( + rf"{to_text(0.26)} \cdot {fraction(self.f_ctm, self.f_yk)} \cdot {to_text(self.b_t)} \cdot " + f"{to_text(self.d)}", + rf"{to_text(0.0013)} \cdot {to_text(self.b_t)} \cdot {to_text(self.d)}", ), comparison_operator_label="=", ) From 718a09ac6cb41c0e8da1902b20abdc6bca871df2 Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Fri, 5 Jan 2024 15:42:17 +0100 Subject: [PATCH 04/13] (#109) changed name latex functions --- .../chapter_4_durability_and_cover/test_formula_4_2.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py index 73da46d6..01c8440a 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py @@ -140,10 +140,3 @@ def test_latex(self) -> None: assert form.latex().complete == expected_latex_formula assert form.latex().short == r"c_{\text{min}} = \text{15.0}" - - # Possible way to display the latex string and check if it is correct - # import matplotlib.pyplot as plt - # ax = plt.axes((0, 0, 1, 1)) - # ax.axis('off') - # plt.text(0, 0.5, '$%s$' % expected_latex_formula, size=15, color="black") - # plt.show() From 9cd7f10ae617ee96ad374db65532ca5095e0d873 Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Wed, 24 Jan 2024 09:44:46 +0100 Subject: [PATCH 05/13] (#109) fixed PR --- .../formula_4_1.py | 26 +++------ .../formula_4_2.py | 57 ++++++------------- .../test_formula_4_1.py | 16 +----- .../test_formula_4_2.py | 33 ++--------- 4 files changed, 32 insertions(+), 100 deletions(-) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_1.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_1.py index 41db97b3..5be0d679 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_1.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_1.py @@ -1,13 +1,11 @@ """Formula 4.1 from NEN-EN 1992-1-1+C2:2011: Chapter 4 - Durability and cover to reinforcement.""" from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula -from blueprints.codes.latex_formula import LatexFormula, to_text, variable_with_subscript from blueprints.type_alias import MM -from blueprints.validations import raise_if_negative class Form4Dot1NominalConcreteCover(Formula): - """Class representing the formula 4.1 for the calculation of the nominal concrete cover :math:`c_{nom}` [:math:`mm`].""" + """Class representing the formula 4.1 for the calculation of the nominal concrete cover.""" label = "4.1" source_document = NEN_EN_1992_1_1_C2_2011 @@ -17,16 +15,16 @@ def __init__( c_min: MM, delta_c_dev: MM, ) -> None: - """[:math:`c_{nom}`] Calculates the nominal concrete cover [:math:`mm`]. + """[cnom] Calculates the nominal concrete cover [mm]. NEN-EN 1992-1-1+C2:2011 art.4.4.1.1 (2) - Formula (4.1) Parameters ---------- c_min: MM - [:math:`c_{min}`] Minimum concrete cover based on art. 4.4.1.2 [:math:`mm`]. + [cmin] Minimum concrete cover based on art. 4.4.1.2 [mm]. delta_c_dev: MM - [:math:`Δc_{dev}`] Construction tolerance based on art. 4.4.1.3 [:math:`mm`]. + [Δcdev] Construction tolerance based on art. 4.4.1.3 [mm]. """ super().__init__() self.c_min = c_min @@ -38,16 +36,8 @@ def _evaluate( delta_c_dev: MM, ) -> MM: """For more detailed documentation see the class docstring.""" - raise_if_negative(c_min=c_min, delta_c_dev=delta_c_dev) + if c_min < 0: + raise ValueError(f"Negative c_min: {c_min}. c_min cannot be negative") + if delta_c_dev < 0: + raise ValueError(f"Negative delta_c_dev: {delta_c_dev}. delta_c_dev cannot be negative") return c_min + delta_c_dev - - def latex(self) -> LatexFormula: - """Returns LatexFormula object for formula 4.1.""" - latex_delta_c_dev = variable_with_subscript(r"\Delta c", "dev") - return LatexFormula( - return_symbol=variable_with_subscript("c", "nom"), - result=to_text(self), - equation=f"{variable_with_subscript('c', 'min')}+{latex_delta_c_dev}", - numeric_equation=f"{to_text(self.c_min)}+{to_text(self.delta_c_dev)}", - comparison_operator_label="=", - ) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_2.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_2.py index e3b99dab..24d25b50 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_2.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/formula_4_2.py @@ -1,13 +1,11 @@ """Formula 4.2 from NEN-EN 1992-1-1+C2:2011: Chapter 4 - Durability and cover to reinforcement.""" from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula -from blueprints.codes.latex_formula import LatexFormula, max_curly_brackets, to_text, variable_with_subscript from blueprints.type_alias import MM -from blueprints.validations import raise_if_negative class Form4Dot2MinimumConcreteCover(Formula): - """Class representing the formula 4.2 for the calculation of the minimum concrete cover :math:`c_{min}` [:math:`mm`].""" + """Class representing the formula 4.2 for the calculation of the minimum concrete cover [mm].""" label = "4.2" source_document = NEN_EN_1992_1_1_C2_2011 @@ -20,22 +18,22 @@ def __init__( delta_c_dur_st: MM, delta_c_dur_add: MM, ) -> None: - """[:math:`c_{min}`] Calculates the minimum concrete cover [:math:`mm`]. + """[cmin] Calculates the minimum concrete cover [mm]. NEN-EN 1992-1-1+C2:2011 art.4.4.1.2 (2) - formula (4.2) Parameters ---------- c_min_b: MM - [:math:`c_{min,b}`] The minimum concrete cover based on the adhesion requirements based on art. 4.4.1.2 (3) [:math:`mm`]. + [cminb] The minimum concrete cover based on the adhesion requirements based on art. 4.4.1.2 (3) [mm]. c_min_dur: MM - [:math:`c_{min,dur}`] The minimum concrete cover based on environmental conditions based on art. 4.4.1.2 (5) [:math:`mm`]. + [cmindur] The minimum concrete cover based on environmental conditions based on art. 4.4.1.2 (5) [mm]. delta_c_dur_gamma: MM - [:math:`Δc_{dur,γ}`] An additional safety requirement based on art. 4.4.1.2 (6) [:math:`mm`]. + [Δcdurgamma] An additional safety requirement based on art. 4.4.1.2 (6) [mm]. delta_c_dur_st: MM - [:math:`Δc_{dur,st}`] A reduction of minimum concrete cover when using stainless steel based on art. 4.4.1.2 (7) [:math:`mm`]. + [Δcdurst] A reduction of minimum concrete cover when using stainless steel based on art. 4.4.1.2 (7) [mm]. delta_c_dur_add: MM - [:math:`Δc_{dur,add}`] A reduction of minimum concrete cover when using additional protection based on art. 4.4.1.2 (8) [:math:`mm`]. + [Δcduradd] A reduction of minimum concrete cover when using additional protection based on art. 4.4.1.2 (8) [mm]. """ super().__init__() self.c_min_b = c_min_b @@ -53,35 +51,14 @@ def _evaluate( delta_c_dur_add: MM, ) -> MM: """For more detailed documentation see the class docstring.""" - raise_if_negative( - c_min_b=c_min_b, - c_min_dur=c_min_dur, - delta_c_dur_gamma=delta_c_dur_gamma, - delta_c_dur_st=delta_c_dur_st, - delta_c_dur_add=delta_c_dur_add, - ) + if c_min_b < 0: + raise ValueError(f"Negative c_min_b: {c_min_b}. c_min_b cannot be negative") + if c_min_dur < 0: + raise ValueError(f"Negative c_min_dur: {c_min_dur}. c_min_dur cannot be negative") + if delta_c_dur_gamma < 0: + raise ValueError(f"Negative delta_c_dur_gamma: {delta_c_dur_gamma}. delta_c_dur_gamma cannot be negative") + if delta_c_dur_st < 0: + raise ValueError(f"Negative delta_c_dur_st: {delta_c_dur_st}. delta_c_dur_st cannot be negative") + if delta_c_dur_add < 0: + raise ValueError(f"Negative delta_c_dur_add: {delta_c_dur_add}. delta_c_dur_add cannot be negative") return max(c_min_b, c_min_dur + delta_c_dur_gamma - delta_c_dur_st - delta_c_dur_add, 10) - - def latex(self) -> LatexFormula: - """Returns LatexFormula object for formula 4.2.""" - arg_1 = self.c_min_b - arg_2 = ( - f"{to_text(self.c_min_dur)}+{to_text(self.delta_c_dur_gamma)}-{to_text(self.delta_c_dur_st)}" - f"-{to_text(self.delta_c_dur_add)}" - ) - arg_3 = 10 - - latex_c_min_dur = variable_with_subscript("c", "min,dur") - latex_delta_c_dur_gamma = variable_with_subscript(r"\Delta c", r"dur,\gamma") - latex_delta_c_dur_st = variable_with_subscript(r"\Delta c", "dur,st") - latex_delta_c_dur_add = variable_with_subscript(r"\Delta c", "dur,add") - - return LatexFormula( - return_symbol=variable_with_subscript("c", "min"), - result=to_text(self), - equation=max_curly_brackets(variable_with_subscript("c", "min,b"), - f"{latex_c_min_dur}+{latex_delta_c_dur_gamma}-{latex_delta_c_dur_st}-{latex_delta_c_dur_add}", - to_text("10 mm")), - numeric_equation=max_curly_brackets(arg_1, arg_2, arg_3), - comparison_operator_label="=", - ) diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_1.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_1.py index c4211843..f246d130 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_1.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_1.py @@ -2,7 +2,6 @@ import pytest from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011.chapter_4_durability_and_cover.formula_4_1 import Form4Dot1NominalConcreteCover -from blueprints.validations import NegativeValueError class TestForm4Dot1NominalConcreteCover: @@ -26,7 +25,7 @@ def test_raise_error_when_negative_c_min_is_given(self) -> None: c_min = -60 # mm delta_c_dev = 5 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot1NominalConcreteCover(c_min=c_min, delta_c_dev=delta_c_dev) def test_raise_error_when_negative_delta_c_dev_is_given(self) -> None: @@ -35,16 +34,5 @@ def test_raise_error_when_negative_delta_c_dev_is_given(self) -> None: c_min = 60 # mm delta_c_dev = -5 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot1NominalConcreteCover(c_min=c_min, delta_c_dev=delta_c_dev) - - -def test_latex() -> None: - """Test the latex implementation.""" - c_min = 60 # mm - delta_c_dev = 5 # mm - form = Form4Dot1NominalConcreteCover(c_min=c_min, delta_c_dev=delta_c_dev) - - assert form.latex().complete == r"c_{\text{nom}} = c_{\text{min}}+\Delta c_{\text{dev}} = \text{60}+\text{5} = \text{65.0}" - assert form.latex().short == r"c_{\text{nom}} = \text{65.0}" - assert str(form.latex()) == r"c_{\text{nom}} = c_{\text{min}}+\Delta c_{\text{dev}} = \text{60}+\text{5} = \text{65.0}" diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py index 01c8440a..8b2016d7 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_4_durability_and_cover/test_formula_4_2.py @@ -2,7 +2,6 @@ import pytest from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011.chapter_4_durability_and_cover.formula_4_2 import Form4Dot2MinimumConcreteCover -from blueprints.validations import NegativeValueError class TestForm4Dot1NominalConcreteCover: @@ -38,7 +37,7 @@ def test_raise_error_when_negative_c_min_b_is_given(self) -> None: delta_c_dur_st = 5 # mm delta_c_dur_add = 0 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot2MinimumConcreteCover( c_min_b=c_min_b, c_min_dur=c_min_dur, @@ -56,7 +55,7 @@ def test_raise_error_when_negative_c_min_dur_is_given(self) -> None: delta_c_dur_st = 5 # mm delta_c_dur_add = 0 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot2MinimumConcreteCover( c_min_b=c_min_b, c_min_dur=c_min_dur, @@ -74,7 +73,7 @@ def test_raise_error_when_negative_delta_c_dur_gamma_is_given(self) -> None: delta_c_dur_st = 5 # mm delta_c_dur_add = 0 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot2MinimumConcreteCover( c_min_b=c_min_b, c_min_dur=c_min_dur, @@ -92,7 +91,7 @@ def test_raise_error_when_negative_delta_c_dur_st_is_given(self) -> None: delta_c_dur_st = -5 # mm delta_c_dur_add = 0 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot2MinimumConcreteCover( c_min_b=c_min_b, c_min_dur=c_min_dur, @@ -110,7 +109,7 @@ def test_raise_error_when_negative_delta_c_dur_add_is_given(self) -> None: delta_c_dur_st = 5 # mm delta_c_dur_add = -5 # mm - with pytest.raises(NegativeValueError): + with pytest.raises(ValueError): Form4Dot2MinimumConcreteCover( c_min_b=c_min_b, c_min_dur=c_min_dur, @@ -118,25 +117,3 @@ def test_raise_error_when_negative_delta_c_dur_add_is_given(self) -> None: delta_c_dur_st=delta_c_dur_st, delta_c_dur_add=delta_c_dur_add, ) - - def test_latex(self) -> None: - """Test the latex implementation.""" - c_min_b = 15 # mm - c_min_dur = 10 # mm - delta_c_dur_gamma = 5 # mm - delta_c_dur_st = 5 # mm - delta_c_dur_add = 0 # mm - form = Form4Dot2MinimumConcreteCover( - c_min_b=c_min_b, - c_min_dur=c_min_dur, - delta_c_dur_gamma=delta_c_dur_gamma, - delta_c_dur_st=delta_c_dur_st, - delta_c_dur_add=delta_c_dur_add, - ) - expected_latex_formula = (r"c_{\text{min}} = \max \left\{c_{\text{min,b}}; c_{\text{min,dur}}+\Delta c_{\text{dur,\gamma}}-\Delta " - r"c_{\text{dur,st}}-\Delta c_{\text{dur,add}}; \text{10 mm}\right\} = \max \left\{\text{15}; \text{10}+\text{5}-" - r"\text{5}-\text{0}; \text{10}\right\} = \text{15.0}") - - assert form.latex().complete == expected_latex_formula - - assert form.latex().short == r"c_{\text{min}} = \text{15.0}" From 02f29380d8ff1a8aa00cd0ee3bf767a8612038e8 Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Thu, 25 Jan 2024 14:49:26 +0100 Subject: [PATCH 06/13] (#109) added test for __str__ method --- .../test_formula_9_1n.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py index b024f62a..89ef1148 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py @@ -97,3 +97,8 @@ def test_latex(self) -> None: r"\text{50} \cdot \text{150}\right\} = \text{10.985915492957748}" ) assert form.latex().short == r"A_{\text{s,min}} = \text{10.985915492957748}" + assert str(form.latex()) == ( + r"A_{\text{s,min}} = \max \left\{\text{0.26} \cdot \frac{f_{\text{ctm}}}{f_{\text{yk}}} \cdot b_{\text{t}} \cdot d; \text{0.0013} \cdot " + r"b_{\text{t}} \cdot d\right\} = \max \left\{\text{0.26} \cdot \frac{2}{355} \cdot \text{50} \cdot \text{150}; \text{0.0013} \cdot " + r"\text{50} \cdot \text{150}\right\} = \text{10.985915492957748}" + ) From 945090f9cfef30fe72bbd276d939f1ceb303b169 Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Thu, 25 Jan 2024 17:48:25 +0100 Subject: [PATCH 07/13] (#109) fixed formatting mistake --- .../chapter_9_detailling_and_specific_rules/formula_9_1n.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py index 57ca6f44..22ce6f8b 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py @@ -62,6 +62,7 @@ def latex(self) -> LatexFormula: latex_f_ctm = variable_with_subscript(variable="f", subscript="ctm") latex_f_yk = variable_with_subscript(variable="f", subscript="yk") latex_b_t = variable_with_subscript(variable="b", subscript="t") + return LatexFormula( return_symbol=variable_with_subscript(variable="A", subscript="s,min"), result=to_text(self), @@ -70,8 +71,7 @@ def latex(self) -> LatexFormula: rf"{to_text(0.0013)} \cdot {latex_b_t} \cdot d", ), numeric_equation=max_curly_brackets( - rf"{to_text(0.26)} \cdot {fraction(self.f_ctm, self.f_yk)} \cdot {to_text(self.b_t)} \cdot " - f"{to_text(self.d)}", + rf"{to_text(0.26)} \cdot {fraction(self.f_ctm, self.f_yk)} \cdot {to_text(self.b_t)} \cdot {to_text(self.d)}", rf"{to_text(0.0013)} \cdot {to_text(self.b_t)} \cdot {to_text(self.d)}", ), comparison_operator_label="=", From b92fef0a6d9193f81a581d54a2fc4b13b512653f Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Fri, 26 Jan 2024 10:29:00 +0100 Subject: [PATCH 08/13] (#109) updated to simplify latex representation --- .../formula_9_1n.py | 18 +++---- blueprints/codes/latex_formula.py | 48 +------------------ .../test_formula_9_1n.py | 20 ++++---- tests/codes/test_latex_formula.py | 45 ++--------------- 4 files changed, 23 insertions(+), 108 deletions(-) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py index 22ce6f8b..3a118769 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py @@ -1,7 +1,7 @@ """Formula 9.1N from NEN-EN 1992-1-1+C2:2011: Chapter 9 - Detailing of members and particular rules.""" from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula -from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets, to_text, variable_with_subscript +from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets from blueprints.type_alias import MM, MM2, MPA from blueprints.validations import raise_if_negative @@ -59,20 +59,16 @@ def _evaluate( def latex(self) -> LatexFormula: """Returns LatexFormula object for formula 9.1N.""" - latex_f_ctm = variable_with_subscript(variable="f", subscript="ctm") - latex_f_yk = variable_with_subscript(variable="f", subscript="yk") - latex_b_t = variable_with_subscript(variable="b", subscript="t") - return LatexFormula( - return_symbol=variable_with_subscript(variable="A", subscript="s,min"), - result=to_text(self), + return_symbol=r"A_{s,min}", + result=f"{self:.2f}", equation=max_curly_brackets( - rf"{to_text(0.26)} \cdot {fraction(latex_f_ctm, latex_f_yk)} \cdot {latex_b_t} \cdot d", - rf"{to_text(0.0013)} \cdot {latex_b_t} \cdot d", + rf"0.26 \cdot {fraction(r'f_{ctm}', r'f_{yk}')} \cdot b_t \cdot d", + r"0.0013 \cdot b_t \cdot d", ), numeric_equation=max_curly_brackets( - rf"{to_text(0.26)} \cdot {fraction(self.f_ctm, self.f_yk)} \cdot {to_text(self.b_t)} \cdot {to_text(self.d)}", - rf"{to_text(0.0013)} \cdot {to_text(self.b_t)} \cdot {to_text(self.d)}", + rf"0.26 \cdot {fraction(f'{self.f_ctm:.2f}', f'{self.f_yk:.2f}')} \cdot {self.b_t:.2f} \cdot {self.d:.2f}", + rf"0.0013 \cdot {self.b_t:.2f} \cdot {self.d:.2f}", ), comparison_operator_label="=", ) diff --git a/blueprints/codes/latex_formula.py b/blueprints/codes/latex_formula.py index d630fa81..592b9806 100644 --- a/blueprints/codes/latex_formula.py +++ b/blueprints/codes/latex_formula.py @@ -2,43 +2,6 @@ from dataclasses import dataclass -def to_text(value: str | float) -> str: - r"""Convert string to a latex text string (\text{variable}). All characters that are not used as a variable in the corresponding documents - should be text strings. - - Parameters - ---------- - value: str | float - The float, int or string to be converted to latex text string. - - Returns - ------- - str - The latex text - - """ - return f"\\text{{{value}}}" - - -def variable_with_subscript(variable: str, subscript: str) -> str: - r"""Return a string which will output: variable_{\text{subscript}} in latex. - - Parameters - ---------- - variable: str - The variable name. - subscript: str - The subscript of the variable. - - Returns - ------- - str - The latex representation of the variable with subscript. - - """ - return f"{variable}_{{\\text{{{subscript}}}}}" - - def max_curly_brackets(*args: str | float) -> str: """Return a string which will output: max{arg_1, arg_2, ..., arg_N} in latex and it will also automatically ensure floats are converted to latex text. @@ -54,12 +17,7 @@ def max_curly_brackets(*args: str | float) -> str: The latex representation of the max operator. """ - arguments = [] - for arg in args: - max_operation_argument = arg - if isinstance(arg, (int, float)): # check if arg is float or int, so it can be converted to latex text - max_operation_argument = to_text(arg) - arguments.append(str(max_operation_argument)) + arguments = [str(arg) for arg in args] return f"\\max \\left\\{{{'; '.join(arguments)}\\right\\}}" @@ -79,10 +37,6 @@ def fraction(numerator: str | float, denominator: str | float) -> str: The latex string """ - if isinstance(numerator, float): - numerator = to_text(numerator) - if isinstance(denominator, float): - denominator = to_text(denominator) return f"\\frac{{{numerator}}}{{{denominator}}}" diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py index 89ef1148..798ed92d 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py @@ -89,16 +89,16 @@ def test_latex(self) -> None: f_yk = 355 # MPa b_t = 50 # mm d = 150 # mm - form = Form9Dot1NMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d) + latex = Form9Dot1NMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d).latex() - assert form.latex().complete == ( - r"A_{\text{s,min}} = \max \left\{\text{0.26} \cdot \frac{f_{\text{ctm}}}{f_{\text{yk}}} \cdot b_{\text{t}} \cdot d; \text{0.0013} \cdot " - r"b_{\text{t}} \cdot d\right\} = \max \left\{\text{0.26} \cdot \frac{2}{355} \cdot \text{50} \cdot \text{150}; \text{0.0013} \cdot " - r"\text{50} \cdot \text{150}\right\} = \text{10.985915492957748}" + assert latex.complete == ( + r"A_{s,min} = \max \left\{0.26 \cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; 0.0013 \cdot b_t \cdot d\right\} " + r"= \max \left\{0.26 \cdot \frac{2.00}{355.00} \cdot 50.00 \cdot 150.00; 0.0013 \cdot 50.00 \cdot 150.00\right\} " + r"= 10.99" ) - assert form.latex().short == r"A_{\text{s,min}} = \text{10.985915492957748}" - assert str(form.latex()) == ( - r"A_{\text{s,min}} = \max \left\{\text{0.26} \cdot \frac{f_{\text{ctm}}}{f_{\text{yk}}} \cdot b_{\text{t}} \cdot d; \text{0.0013} \cdot " - r"b_{\text{t}} \cdot d\right\} = \max \left\{\text{0.26} \cdot \frac{2}{355} \cdot \text{50} \cdot \text{150}; \text{0.0013} \cdot " - r"\text{50} \cdot \text{150}\right\} = \text{10.985915492957748}" + assert latex.short == r"A_{s,min} = 10.99" + assert str(latex) == ( + r"A_{s,min} = \max \left\{0.26 \cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; 0.0013 \cdot b_t \cdot d\right\} " + r"= \max \left\{0.26 \cdot \frac{2.00}{355.00} \cdot 50.00 \cdot 150.00; 0.0013 \cdot 50.00 \cdot 150.00\right\} " + r"= 10.99" ) diff --git a/tests/codes/test_latex_formula.py b/tests/codes/test_latex_formula.py index 8ac4805f..2c40189b 100644 --- a/tests/codes/test_latex_formula.py +++ b/tests/codes/test_latex_formula.py @@ -1,7 +1,7 @@ """Tests for the LatexFormula class.""" import pytest -from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets, to_text, variable_with_subscript +from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets @pytest.fixture() @@ -33,33 +33,10 @@ def test_str(self, fixture_latex_formula: LatexFormula) -> None: assert str(fixture_latex_formula) == expected_result -def test_latex_value_to_text() -> None: - """Test the latex_value_to_text function.""" - # Example values - value = 5.0 - - # Expected result - expected_result = r"\text{5.0}" - - assert to_text(value=value) == expected_result - - -@pytest.mark.parametrize( - ("arg_1", "arg_2", "arg_3", "expected_output"), - [ - (r"a+b", r"\text{500}", r"c-d", r"\max \left\{a+b; \text{500}; c-d\right\}"), - (r"a+b", 500, r"c-d", r"\max \left\{a+b; \text{500}; c-d\right\}"), - ], -) -def test_latex_max_curly_brackets( - arg_1: str | float, - arg_2: str | float, - arg_3: str | float, - expected_output: str, -) -> None: +def test_latex_max_curly_brackets() -> None: """Test the latex_max_curly_brackets function.""" - result = max_curly_brackets(arg_1, arg_2, arg_3) - assert result == expected_output + result = max_curly_brackets(r"a+b", r"500", r"c-d") + assert result == r"\max \left\{a+b; 500; c-d\right\}" def test_latex_fraction() -> None: @@ -69,18 +46,6 @@ def test_latex_fraction() -> None: denominator = 10.0 # Expected result - expected_result = r"\frac{\text{5.0}}{\text{10.0}}" + expected_result = r"\frac{5.0}{10.0}" assert fraction(numerator=numerator, denominator=denominator) == expected_result - - -def test_latex_variable_with_subscript() -> None: - """Test the latex_subscript function.""" - # Example values - base = "a" - subscript = "b" - - # Expected result - expected_result = r"a_{\text{b}}" - - assert variable_with_subscript(variable=base, subscript=subscript) == expected_result From 84ea238c83b19a03429b6658d7cfdbd9387f7d11 Mon Sep 17 00:00:00 2001 From: PetervWestrienen Date: Fri, 26 Jan 2024 11:58:03 +0100 Subject: [PATCH 09/13] (#109) fixed latex formula --- blueprints/codes/latex_formula.py | 66 ++++++++++++++++++++++++++++++- tests/codes/test_latex_formula.py | 13 +++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/blueprints/codes/latex_formula.py b/blueprints/codes/latex_formula.py index 592b9806..5417d4b8 100644 --- a/blueprints/codes/latex_formula.py +++ b/blueprints/codes/latex_formula.py @@ -3,9 +3,14 @@ def max_curly_brackets(*args: str | float) -> str: - """Return a string which will output: max{arg_1, arg_2, ..., arg_N} in latex and it will also automatically ensure floats are converted to latex + r"""Return a string which will output: max{arg_1; arg_2; ...; arg_N} in latex and it will also automatically ensure floats are converted to latex text. + Examples + -------- + >>> max_curly_brackets(1, 2) + str(\max \left\{1; 2\right\}) + Parameters ---------- args: str @@ -14,16 +19,45 @@ def max_curly_brackets(*args: str | float) -> str: Returns ------- str - The latex representation of the max operator. + The latex representation of the max function. """ arguments = [str(arg) for arg in args] return f"\\max \\left\\{{{'; '.join(arguments)}\\right\\}}" +def min_curly_brackets(*args: str | float) -> str: + r"""Return a string which will output: min{arg_1; arg_2; ...; arg_N} in latex and it will also automatically ensure floats are converted to latex + text. + + Examples + -------- + >>> min_curly_brackets(1, 2) + str(\min \left\{1; 2\right\}) + + Parameters + ---------- + args: str + The arguments of the min function. + + Returns + ------- + str + The latex representation of the min function. + + """ + arguments = [str(arg) for arg in args] + return f"\\min \\left\\{{{'; '.join(arguments)}\\right\\}}" + + def fraction(numerator: str | float, denominator: str | float) -> str: r"""Return a string which will output: \frac{numerator}{denominator} in latex. + Examples + -------- + >>> fraction(1, 2) + str(\frac{1}{2}) + Parameters ---------- numerator: str | float @@ -40,6 +74,34 @@ def fraction(numerator: str | float, denominator: str | float) -> str: return f"\\frac{{{numerator}}}{{{denominator}}}" +def conditional(*args: list[float | str]) -> str: + r"""Return a string which will output a conditional statement with curly brackets with the given arguments and conditions in latex. + + Examples + -------- + >>> conditional([1, "a > 0"], [2, "a < 0"] + str(\left{\matrix{1 & \text{voor }a > 0 \\ 2 & \text{voor }a < 0 }\right.) + + Parameters + ---------- + args: list[float|str] + A list of length 2, where the first element is the value and the second value is the condition. + + Returns + ------- + str + The latex string + """ + string_parts = [] + for value, condition in args: + string_parts.append(f"{value} & \\text{{voor }}{condition} \\\\ ") + + output = r"\left{\matrix{" + output += "".join(string_parts) + output = output[:-3] + return output + r"}\right." + + @dataclass(frozen=True) class LatexFormula: """Latex formula representation. diff --git a/tests/codes/test_latex_formula.py b/tests/codes/test_latex_formula.py index 2c40189b..319639d3 100644 --- a/tests/codes/test_latex_formula.py +++ b/tests/codes/test_latex_formula.py @@ -1,7 +1,7 @@ """Tests for the LatexFormula class.""" import pytest -from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets +from blueprints.codes.latex_formula import LatexFormula, conditional, fraction, max_curly_brackets, min_curly_brackets @pytest.fixture() @@ -39,6 +39,12 @@ def test_latex_max_curly_brackets() -> None: assert result == r"\max \left\{a+b; 500; c-d\right\}" +def test_latex_min_curly_brackets() -> None: + """Test the latex_max_curly_brackets function.""" + result = min_curly_brackets(r"a+b", r"500", r"c-d") + assert result == r"\min \left\{a+b; 500; c-d\right\}" + + def test_latex_fraction() -> None: """Test the latex_fraction function.""" # Example values @@ -49,3 +55,8 @@ def test_latex_fraction() -> None: expected_result = r"\frac{5.0}{10.0}" assert fraction(numerator=numerator, denominator=denominator) == expected_result + + +def test_conditional() -> None: + """Test the conditional function.""" + assert conditional([1, "a > 0"], [2, "a < 0"]) == r"\left{\matrix{1 & \text{voor }a > 0 \\ 2 & \text{voor }a < 0 }\right." From 534b680a450bf30e6b681398d346c1c61536deea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20M=C3=A9ndez?= <44614728+egarciamendez@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:41:38 +0100 Subject: [PATCH 10/13] (#109) unused formulas deleted --- blueprints/codes/latex_formula.py | 84 ------------------------------- 1 file changed, 84 deletions(-) diff --git a/blueprints/codes/latex_formula.py b/blueprints/codes/latex_formula.py index 77941eff..98bfa12e 100644 --- a/blueprints/codes/latex_formula.py +++ b/blueprints/codes/latex_formula.py @@ -127,87 +127,3 @@ def latex_max_curly_brackets(*args: str | float) -> str: """ arguments = [str(arg) for arg in args] return f"\\max \\left\\{{{'; '.join(arguments)}\\right\\}}" - - -def to_text(value: str | float) -> str: - r"""Convert string to a latex text string (\text{variable}). All characters that are not used as a variable in the corresponding documents - should be text strings. - - Parameters - ---------- - value: str | float - The float, int or string to be converted to latex text string. - - Returns - ------- - str - The latex text - - """ - return f"\\text{{{value}}}" - - -def variable_with_subscript(variable: str, subscript: str) -> str: - r"""Return a string which will output: variable_{\text{subscript}} in latex. - - Parameters - ---------- - variable: str - The variable name. - subscript: str - The subscript of the variable. - - Returns - ------- - str - The latex representation of the variable with subscript. - - """ - return f"{variable}_{{\\text{{{subscript}}}}}" - - -def max_curly_brackets(*args: str | float) -> str: - """Return a string which will output: max{arg_1, arg_2, ..., arg_N} in latex and it will also automatically ensure floats are converted to latex - text. - - Parameters - ---------- - args: str - The arguments of the max function. - - Returns - ------- - str - The latex representation of the max operator. - - """ - arguments = [] - for arg in args: - max_operation_argument = arg - if isinstance(arg, (int, float)): # check if arg is float or int, so it can be converted to latex text - max_operation_argument = to_text(arg) - arguments.append(str(max_operation_argument)) - return f"\\max \\left\\{{{'; '.join(arguments)}\\right\\}}" - - -def fraction(numerator: str | float, denominator: str | float) -> str: - r"""Return a string which will output: \frac{numerator}{denominator} in latex. - - Parameters - ---------- - numerator: str | float - The numerator of the fraction. - denominator: str | float - The denominator of the fraction. - - Returns - ------- - str - The latex string - - """ - if isinstance(numerator, float): - numerator = to_text(numerator) - if isinstance(denominator, float): - denominator = to_text(denominator) - return f"\\frac{{{numerator}}}{{{denominator}}}" From 71df53fbb342fceacec1581bcf75afa395fafcd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20M=C3=A9ndez?= <44614728+egarciamendez@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:42:52 +0100 Subject: [PATCH 11/13] (#109) test added --- tests/codes/test_latex_formula.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/codes/test_latex_formula.py b/tests/codes/test_latex_formula.py index 54f2246f..b64c8586 100644 --- a/tests/codes/test_latex_formula.py +++ b/tests/codes/test_latex_formula.py @@ -2,13 +2,19 @@ import pytest -from blueprints.codes.latex_formula import LatexFormula, latex_fraction, latex_max_curly_brackets +from blueprints.codes.latex_formula import LatexFormula, latex_fraction, latex_max_curly_brackets, latex_min_curly_brackets @pytest.fixture() def fixture_latex_formula() -> LatexFormula: """Fixture for testing.""" - return LatexFormula(return_symbol="E", result="500", equation="mc^2", numeric_equation="5*10^2", comparison_operator_label="=") + return LatexFormula( + return_symbol="E", + result="500", + equation="mc^2", + numeric_equation="5*10^2", + comparison_operator_label="=", + ) class TestLatexFormula: @@ -50,3 +56,9 @@ def test_latex_max_curly_brackets() -> None: """Test the latex_max_curly_brackets function.""" result = latex_max_curly_brackets(r"a+b", r"500", r"c-d") assert result == r"\max \left\{a+b; 500; c-d\right\}" + + +def test_min_curly_brackets() -> None: + """Test the latex_min_curly_brackets function.""" + result = latex_min_curly_brackets(r"a+b", r"500", r"c-d") + assert result == r"\min \left\{a+b; 500; c-d\right\}" From ec8890eb2f986dfee5c2ea1ab95c281aeabfe69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20M=C3=A9ndez?= <44614728+egarciamendez@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:48:02 +0100 Subject: [PATCH 12/13] (#109) wrong imports fixed --- .../formula_9_1n.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py index db0f76e7..78f4167d 100644 --- a/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py +++ b/blueprints/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/formula_9_1n.py @@ -2,7 +2,7 @@ from blueprints.codes.eurocode.nen_en_1992_1_1_c2_2011 import NEN_EN_1992_1_1_C2_2011 from blueprints.codes.formula import Formula -from blueprints.codes.latex_formula import LatexFormula, fraction, max_curly_brackets +from blueprints.codes.latex_formula import LatexFormula, latex_fraction, latex_max_curly_brackets from blueprints.type_alias import MM, MM2, MPA from blueprints.validations import raise_if_negative @@ -63,12 +63,12 @@ def latex(self) -> LatexFormula: return LatexFormula( return_symbol=r"A_{s,min}", result=f"{self:.2f}", - equation=max_curly_brackets( - rf"0.26 \cdot {fraction(r'f_{ctm}', r'f_{yk}')} \cdot b_t \cdot d", + equation=latex_max_curly_brackets( + rf"0.26 \cdot {latex_fraction(r'f_{ctm}', r'f_{yk}')} \cdot b_t \cdot d", r"0.0013 \cdot b_t \cdot d", ), - numeric_equation=max_curly_brackets( - rf"0.26 \cdot {fraction(f'{self.f_ctm:.2f}', f'{self.f_yk:.2f}')} \cdot {self.b_t:.2f} \cdot {self.d:.2f}", + numeric_equation=latex_max_curly_brackets( + rf"0.26 \cdot {latex_fraction(f'{self.f_ctm:.2f}', f'{self.f_yk:.2f}')} \cdot {self.b_t:.2f} \cdot {self.d:.2f}", rf"0.0013 \cdot {self.b_t:.2f} \cdot {self.d:.2f}", ), comparison_operator_label="=", From 5511faa7e2b25e855d0ad4bb1491237455df2e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20M=C3=A9ndez?= <44614728+egarciamendez@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:23:47 +0100 Subject: [PATCH 13/13] (#109) Latex parametrization --- .../test_formula_9_1n.py | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py index a342c370..cc722690 100644 --- a/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py +++ b/tests/codes/eurocode/nen_en_1992_1_1_c2_2011/chapter_9_detailling_and_specific_rules/test_formula_9_1n.py @@ -83,23 +83,30 @@ def test_raise_error_when_negative_d_is_given(self) -> None: with pytest.raises(NegativeValueError): Form9Dot1nMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d) - def test_latex(self) -> None: + @pytest.mark.parametrize( + ("representation", "expected_result"), + [ + ( + "complete", + r"A_{s,min} = \max \left\{0.26 \cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; 0.0013 \cdot b_t \cdot d\right\} = " + r"\max \left\{0.26 \cdot \frac{2.00}{355.00} \cdot 50.00 \cdot 150.00; 0.0013 \cdot 50.00 \cdot 150.00\right\} = 10.99", + ), + ("short", "A_{s,min} = 10.99"), + ], + ) + def test_latex(self, representation: str, expected_result: str) -> None: """Test the latex representation.""" # Example values f_ctm = 2 # MPa f_yk = 355 # MPa b_t = 50 # mm d = 150 # mm + latex = Form9Dot1nMinimumTensileReinforcementBeam(f_ctm=f_ctm, f_yk=f_yk, b_t=b_t, d=d).latex() - assert latex.complete == ( - r"A_{s,min} = \max \left\{0.26 \cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; 0.0013 \cdot b_t \cdot d\right\} " - r"= \max \left\{0.26 \cdot \frac{2.00}{355.00} \cdot 50.00 \cdot 150.00; 0.0013 \cdot 50.00 \cdot 150.00\right\} " - r"= 10.99" - ) - assert latex.short == r"A_{s,min} = 10.99" - assert str(latex) == ( - r"A_{s,min} = \max \left\{0.26 \cdot \frac{f_{ctm}}{f_{yk}} \cdot b_t \cdot d; 0.0013 \cdot b_t \cdot d\right\} " - r"= \max \left\{0.26 \cdot \frac{2.00}{355.00} \cdot 50.00 \cdot 150.00; 0.0013 \cdot 50.00 \cdot 150.00\right\} " - r"= 10.99" - ) + actual = { + "complete": latex.complete, + "short": latex.short, + } + + assert actual[representation] == expected_result, f"{representation} representation failed."