Skip to content

Commit c60b253

Browse files
committed
More handling of powers and factorial number juxtapositions
1 parent bb727f4 commit c60b253

File tree

3 files changed

+138
-41
lines changed

3 files changed

+138
-41
lines changed

Sources/AngouriMath/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Complex.Definition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private protected Complex(Real? real, Real? imaginary) =>
4646
internal override Priority Priority =>
4747
(RealPart, ImaginaryPart) switch
4848
{
49-
({ IsZero: false }, { IsZero: false }) => Priority.Sum,
49+
({ IsZero: false }, { IsZero: false }) or ({ IsZero: true }, { IsNegative: true }) => Priority.Sum,
5050
({ IsZero: true }, Integer(1)) => Priority.Leaf,
5151
_ => Priority.Mul
5252
};

Sources/AngouriMath/AngouriMath/Functions/Output/Latex/Latex.Arithmetics.Classes.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,50 @@ public partial record Mulf
3333
/// <inheritdoc/>
3434
public override string Latexise()
3535
{
36-
return GatherProducts(this).ToArray() switch
37-
{
38-
[Integer(-1), Complex n] => (-n).Latexise(parenthesesRequired: false),
39-
[Integer(-1), var other] => $"-{other.Latexise(other.Priority < Priority)}",
40-
var longArray =>
41-
longArray.AggregateIndexed("",
42-
(prevOut, index, currIn) =>
43-
{
44-
if (index == 0)
45-
return currIn.Latexise(currIn.Priority < Priority);
46-
var currOut = currIn.Latexise(currIn.Priority < Priority);
47-
return (longArray[index - 1], currIn) switch // whether we use juxtaposition and omit \cdot
36+
var longArray = GatherProducts(this).ToArray();
37+
return longArray.AggregateIndexed("",
38+
(prevOut, index, currIn) =>
39+
{
40+
switch (index)
41+
{
42+
case 0:
43+
return currIn switch
4844
{
49-
// NOTE: upright text are to be interpreted as a whole while italic text are to be interpreted as individual characters.
50-
// Therefore, constants formatted as upright text, and multi-character variables are not considered for juxtaposition.
45+
// -1, -2, 2i, i, -i, -2i etc. in the front and not (1+i) etc.
46+
Number { Priority: Priority.Sum } and not Complex { RealPart.IsZero: false, ImaginaryPart.IsZero: false } =>
47+
currIn.Latexise(false),
48+
_ => currIn.Latexise(currIn.Priority < Priority)
49+
};
50+
case 1:
51+
if (longArray[index - 1] is Integer(-1))
52+
return $"-{currIn.Latexise(currIn.Priority < Priority)}"; // display "-1 * x * y" as "-x \cdot y", only for the first -1
53+
break;
54+
}
55+
var currOut = currIn.Latexise(currIn.Priority < Priority);
56+
57+
return (longArray[index - 1], currIn) switch // whether we use juxtaposition and omit \cdot
58+
{
59+
// NOTE: upright text are to be interpreted as a whole while italic text are to be interpreted as individual characters.
60+
// Therefore, constants formatted as upright text, and multi-character variables are not considered for juxtaposition.
5161

52-
// Don't juxtapose upright variables with numbers like displaying "var2" for "var*2" since "var2" may be interpreted as one variable.
53-
// Also, don't produce upright "ei" (one variable with two chars) for e*i, or "ei^2" for e*i^2.
54-
// but "e (2+i)" and "e (2+i)^2" are fine with the parentheses - so we have the priority check.
55-
(Variable { IsLatexUprightFormatted: true }
56-
or Complex { ImaginaryPart.IsZero: false, Priority: Priority.Leaf } /* don't combine upright "i" with an upright variable*/,
57-
Variable { IsLatexUprightFormatted: true } or Number { Priority: Priority.Leaf } or Powf(Number { Priority: Priority.Leaf } or Variable { IsLatexUprightFormatted: true }, _)) => false,
58-
// 2 * 3 instead of 2 3 (= 23), 2 * 3^4 instead of 2 3^4 (= 23^4), 2 * (3/4) instead of 2 (3/4) which is a mixed number (= 2 + 3/4)
59-
// but "(2+i) 2", "2 (2+i)" and "2 (2+i)^2" are fine with the parentheses - so we have the priority check.
60-
(Number { Priority: Priority.Leaf }, Number { Priority: >= Priority.Div } or Powf(Number { Priority: Priority.Leaf }, _) or Divf) => false,
61-
(var left, var right) => left.Priority >= right.Priority &&
62-
!(left.Priority == Priority.Div && right.Priority == Priority.Div) // Without \cdot, the fraction lines may appear too closely together.
63-
} ? $@"{prevOut} {currOut}" : $@"{prevOut} \cdot {currOut}";
64-
})
65-
};
62+
// Don't juxtapose upright variables with numbers like displaying "var2" for "var*2" since "var2" may be interpreted as one variable.
63+
// Also, don't produce upright "ei" (one variable with two chars) for e*i, or "ei^2" for e*i^2.
64+
// but "e (2+i)" and "e (2+i)^2" are fine with the parentheses - so we have the priority check.
65+
(Variable { IsLatexUprightFormatted: true }
66+
or Complex { ImaginaryPart.IsZero: false, Priority: >= Priority.Mul } /* don't combine upright "i" with an upright variable*/,
67+
Variable { IsLatexUprightFormatted: true } or Number { Priority: >= Priority.Mul }
68+
or Factorialf(Number { Priority: Priority.Leaf } or Variable { IsLatexUprightFormatted: true })
69+
or Powf(Number { Priority: Priority.Leaf } or Variable { IsLatexUprightFormatted: true }
70+
or Factorialf(Number { Priority: Priority.Leaf } or Variable { IsLatexUprightFormatted: true }), _)) => false,
71+
// 2 * (3/4) instead of 2 (3/4) which is a mixed number (= 2 + 3/4)
72+
(Number { Priority: Priority.Leaf }, { Priority: Priority.Div }) => false,
73+
// 2 * 3 instead of 2 3 (= 23), 2 * 3^4 instead of 2 3^4 (= 23^4), but "(2+i) 2", "2 (2+i)" and "2 (2+i)^2" are fine with the parentheses - so we have the priority check.
74+
(_, Number { Priority: >= Priority.Mul } or Factorialf(Number { Priority: Priority.Leaf })
75+
or Powf(Number { Priority: Priority.Leaf } or Factorialf(Number { Priority: Priority.Leaf }), _)) => false, // Keep the \cdot in "f(x) \cdot -2" "f(x) \cdot 2i" "f(x) \cdot -2i"
76+
(var left, var right) => left.Priority >= right.Priority &&
77+
!(left.Priority == Priority.Div && right.Priority == Priority.Div) // Without \cdot, the fraction lines may appear too closely together.
78+
} ? $@"{prevOut} {currOut}" : $@"{prevOut} \cdot {currOut}";
79+
});
6680

6781
static IEnumerable<Entity> GatherProducts(Entity expr)
6882
=> expr switch

0 commit comments

Comments
 (0)