question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Math Expression Converters

See original GitHub issue

MathExpressionConverter

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
VladislavAntonyukcommented, May 9, 2022

@russnash It’s yours. Thank you!

0reactions
msftbot[bot]commented, May 19, 2022

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found