[Proposal] Math Expression Converters
See original GitHub issueMathExpressionConverter
- Proposed
- Prototype
- Implementation
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Sample
- Documentation: https://github.com/MicrosoftDocs/CommunityToolkit/pull/89
Summary
The MathExpressionConverter
is a converter that allows users to calculate an expression at runtime from supplied arguments.
Detailed Design
MathExpression.shared.cs
sealed class MathExpression
{
internal MathExpression(string expression, IEnumerable<double>? arguments = null);
public double Calculate();
}
MathExpressionConverter.shared.cs
public class MathExpressionConverter : ValueConverterExtension, IValueConverter
{
public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo culture)
{
if (parameter is not string expression)
throw new ArgumentException("The parameter should be of type String.");
if (value == null || !double.TryParse(value.ToString(), out var xValue))
return null;
var math = new MathExpression(expression, new[] { xValue });
var result = math.Calculate();
return result;
}
public object ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}
MathOperator.shared.cs
sealed class MathOperator
{
public string Name { get; }
public int NumericCount { get; }
public MathOperatorPrecedence Precedence { get; }
public Func<double[], double> CalculateFunc { get; }
public MathOperator(
string name,
int numericCount,
MathOperatorPrecedence precedence,
Func<double[], double> calculateFunc)
{
Name = name;
CalculateFunc = calculateFunc;
Precedence = precedence;
NumericCount = numericCount;
}
}
MultiMathExpressionConverter.shared.cs
public class MultiMathExpressionConverter : MultiValueConverterExtension, IMultiValueConverter
{
public object? Convert(object[]? values, Type? targetType, object? parameter, CultureInfo culture)
{
if (parameter is not string expression)
throw new ArgumentException("The parameter should be of type String.");
if (values == null)
return null;
var args = new List<double>();
foreach (var value in values)
{
if (value == null)
return null;
if (double.TryParse(value.ToString(), out var xValue))
{
args.Add(xValue);
}
}
var math = new MathExpression(expression, args);
var result = math.Calculate();
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
Usage Syntax
XAML Usage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<xct:MathExpressionConverter x:Key="MathExpressionConverter" />
<xct:MultiMathExpressionConverter x:Key="MultiMathExpressionConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Frame
x:Name="CalculatedFrame"
HeightRequest="120"
CornerRadius="{Binding Source={x:Reference CalculatedFrame}, Path=HeightRequest, Converter={StaticResource MathExpressionConverter}, ConverterParameter='x/2'}">
<Label TextColor="Black">
<Label.Text>
<MultiBinding Converter="{StaticResource MultiMathExpressionConverter}"
ConverterParameter="x0 * x1"
StringFormat="The area of Frame = {0}">
<Binding Path="{Binding Source={x:Reference CalculatedFrame}, Path=HeightRequest}" />
<Binding Path="{Binding Source={x:Reference CalculatedFrame}, Path=WidthRequest}" />
</MultiBinding>
</Label.Text>
</Label>
</StackLayout>
</ContentPage>
C# Usage
new Frame().Size(120, -1).Assign(out var frame)
.Bind(Frame.CornerRadiusProperty, nameof(Frame.HeightRequest), converter = new MathExpressionConverter(), converterParameter: "x/2", source: frame);
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Improving the representation and conversion of ...
We propose a novel approach to mathematical format conversion (cf. section 5). The approach imitates the human sense-making process for mathematical content ...
Read more >Equation Editor for online mathematics - create, integrate and ...
LaTeX equation editor that creates lightweight graphical equations (gif, png, swf, pdf, emf) and produces code to quickly and directly embedding equations ......
Read more >Improving the Representation and Conversion of ...
A variety of tools exist to convert format representations of math- ematical formulae. However, to our knowledge, Kohlhase et al. presented the ...
Read more >The Application of Image Processing for Conversion ...
The proposed method was able to segment out handwritten mathematical equations from lined papers as well as extract out and identify each character...
Read more >Equation converter
Welcome to our step-by-step math solver! Solve · Simplify · Factor · Expand · Graph · GCF · LCM. equation converter. Related topics:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@russnash It’s yours. Thank you!
Reopening Proposal.
Only Proposals moved to the
Closed
Project Column andCompleted
Project Column can be closed.