Skip to content

Commit

Permalink
#515 - Area Units
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Mar 28, 2022
1 parent a6f8b4a commit 8b09b7a
Show file tree
Hide file tree
Showing 79 changed files with 2,242 additions and 4 deletions.
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public virtual TResult Analyze(Number exp, TContext context)
public virtual TResult Analyze(Angle exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <inheritdoc />
public virtual TResult Analyze(Area exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <inheritdoc />
public virtual TResult Analyze(Power exp, TContext context)
=> Analyze(exp as IExpression, context);
Expand Down
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public virtual TResult Analyze(Number exp)
public virtual TResult Analyze(Angle exp)
=> Analyze(exp as IExpression);

/// <inheritdoc />
public virtual TResult Analyze(Area exp)
=> Analyze(exp as IExpression);

/// <inheritdoc />
public virtual TResult Analyze(Power exp)
=> Analyze(exp as IExpression);
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Differentiator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ public override IExpression Analyze(Angle exp, DifferentiatorContext context)
return Number.Zero;
}

/// <inheritdoc />
public override IExpression Analyze(Area exp, DifferentiatorContext context)
{
ValidateArguments(exp, context);

return Number.Zero;
}

/// <inheritdoc />
public override IExpression Analyze(Power exp, DifferentiatorContext context)
{
Expand Down
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public virtual string Analyze(Number exp)
public virtual string Analyze(Angle exp)
=> exp.Value.ToString();

/// <inheritdoc />
public virtual string Analyze(Area exp)
=> exp.Value.ToString();

/// <inheritdoc />
public virtual string Analyze(Power exp)
=> exp.Value.ToString();
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ public interface IAnalyzer<out TResult, in TContext>
/// <returns>The result of analysis.</returns>
TResult Analyze(Angle exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <param name="context">The context.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(Area exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ public interface IAnalyzer<out TResult>
/// <returns>The result of analysis.</returns>
TResult Analyze(Angle exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(Area exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions xFunc.Maths/Analyzers/Simplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ public override IExpression Analyze(Add exp)
(Time left, Time right)
=> (left.Value + right.Value).AsExpression(),

(Number left, Area right)
=> (left.Value + right.Value).AsExpression(),
(Area left, Number right)
=> (left.Value + right.Value).AsExpression(),
(Area left, Area right)
=> (left.Value + right.Value).AsExpression(),

// x + x
(Variable left, Variable right) when left.Name == right.Name
=> new Mul(Number.Two, left),
Expand Down Expand Up @@ -341,6 +348,8 @@ public override IExpression Analyze(Div exp)
=> (left.Value / right.Value).AsExpression(),
(Time left, Number right)
=> (left.Value / right.Value).AsExpression(),
(Area left, Number right)
=> (left.Value / right.Value).AsExpression(),

// x / x
(Variable left, Variable right) when left.Equals(right)
Expand Down Expand Up @@ -548,6 +557,11 @@ public override IExpression Analyze(Mul exp)
(Time left, Number right)
=> (left.Value * right.Value).AsExpression(),

(Number left, Area right)
=> (left.Value * right.Value).AsExpression(),
(Area left, Number right)
=> (left.Value * right.Value).AsExpression(),

// x * -y
(var left, UnaryMinus minus)
=> new UnaryMinus(new Mul(left, minus.Argument)),
Expand Down Expand Up @@ -679,6 +693,7 @@ public override IExpression Analyze(ToNumber exp)
Mass(var mass) => new Number(mass.Value),
Length(var length) => new Number(length.Value),
Time(var time) => new Number(time.Value),
Area(var area) => new Number(area.Value),
var arg when IsChanged(exp, arg) => new ToNumber(arg),
_ => exp,
};
Expand Down Expand Up @@ -815,6 +830,13 @@ public override IExpression Analyze(Sub exp)
(Time left, Time right)
=> (left.Value - right.Value).AsExpression(),

(Number left, Area right)
=> (left.Value - right.Value).AsExpression(),
(Area left, Number right)
=> (left.Value - right.Value).AsExpression(),
(Area left, Area right)
=> (left.Value - right.Value).AsExpression(),

// x + x
(Variable left, Variable right) when left.Name == right.Name
=> Number.Zero,
Expand Down
7 changes: 6 additions & 1 deletion xFunc.Maths/Analyzers/TypeAnalyzers/ResultTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public enum ResultTypes
/// </summary>
TimeNumber = 1 << 13,

/// <summary>
/// The expression returns a area number.
/// </summary>
AreaNumber = 1 << 14,

/// <summary>
/// The expression returns a number or a complex number.
/// </summary>
Expand All @@ -109,7 +114,7 @@ public enum ResultTypes
/// <summary>
/// The expression returns any type of number.
/// </summary>
Numbers = Number | AngleNumber | PowerNumber | TemperatureNumber | MassNumber | LengthNumber | TimeNumber,
Numbers = Number | AngleNumber | PowerNumber | TemperatureNumber | MassNumber | LengthNumber | TimeNumber | AreaNumber,

/// <summary>
/// The expression returns a number or a angle number or a complex number or a vector.
Expand Down
35 changes: 33 additions & 2 deletions xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ ResultTypes.Number or ResultTypes.ComplexNumber or ResultTypes.Vector
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
_ => ResultTypes.NumbersOrComplexOrVector.ThrowFor(result),
};
}
Expand Down Expand Up @@ -363,6 +364,11 @@ public virtual ResultTypes Analyze(Add exp)
(ResultTypes.TimeNumber, ResultTypes.TimeNumber)
=> ResultTypes.TimeNumber,

(ResultTypes.Number, ResultTypes.AreaNumber) or
(ResultTypes.AreaNumber, ResultTypes.Number) or
(ResultTypes.AreaNumber, ResultTypes.AreaNumber)
=> ResultTypes.AreaNumber,

(ResultTypes.Number, ResultTypes.ComplexNumber) or
(ResultTypes.ComplexNumber, ResultTypes.Number) or
(ResultTypes.ComplexNumber, ResultTypes.ComplexNumber)
Expand Down Expand Up @@ -409,6 +415,7 @@ public virtual ResultTypes Analyze(Ceil exp)
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
_ => ResultTypes.Numbers.ThrowFor(result),
};
}
Expand Down Expand Up @@ -474,6 +481,9 @@ public virtual ResultTypes Analyze(Div exp)
(ResultTypes.TimeNumber, ResultTypes.Number)
=> ResultTypes.TimeNumber,

(ResultTypes.AreaNumber, ResultTypes.Number)
=> ResultTypes.AreaNumber,

(ResultTypes.Number, ResultTypes.ComplexNumber) or
(ResultTypes.ComplexNumber, ResultTypes.Number) or
(ResultTypes.ComplexNumber, ResultTypes.ComplexNumber)
Expand Down Expand Up @@ -537,6 +547,7 @@ public virtual ResultTypes Analyze(Floor exp)
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
_ => ResultTypes.NumberOrAngle.ThrowFor(result),
};
}
Expand All @@ -559,6 +570,7 @@ public virtual ResultTypes Analyze(Trunc exp)
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
_ => ResultTypes.NumberOrAngle.ThrowFor(result),
};
}
Expand All @@ -581,6 +593,7 @@ public virtual ResultTypes Analyze(Frac exp)
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
_ => ResultTypes.NumberOrAngle.ThrowFor(result),
};
}
Expand Down Expand Up @@ -761,6 +774,10 @@ public virtual ResultTypes Analyze(Mul exp)
(ResultTypes.TimeNumber, ResultTypes.Number)
=> ResultTypes.TimeNumber,

(ResultTypes.Number, ResultTypes.AreaNumber) or
(ResultTypes.AreaNumber, ResultTypes.Number)
=> ResultTypes.AreaNumber,

(ResultTypes.Number, ResultTypes.ComplexNumber) or
(ResultTypes.ComplexNumber, ResultTypes.Number) or
(ResultTypes.ComplexNumber, ResultTypes.ComplexNumber)
Expand Down Expand Up @@ -802,6 +819,10 @@ public virtual ResultTypes Analyze(Number exp)
public virtual ResultTypes Analyze(Angle exp)
=> CheckArgument(exp, ResultTypes.AngleNumber);

/// <inheritdoc />
public virtual ResultTypes Analyze(Area exp)
=> CheckArgument(exp, ResultTypes.AreaNumber);

/// <inheritdoc />
public virtual ResultTypes Analyze(Power exp)
=> CheckArgument(exp, ResultTypes.PowerNumber);
Expand Down Expand Up @@ -850,7 +871,8 @@ ResultTypes.PowerNumber or
ResultTypes.TemperatureNumber or
ResultTypes.MassNumber or
ResultTypes.LengthNumber or
ResultTypes.TimeNumber
ResultTypes.TimeNumber or
ResultTypes.AreaNumber
=> ResultTypes.Number,
_ => ResultTypes.AngleNumber.ThrowFor(result),
};
Expand Down Expand Up @@ -931,6 +953,7 @@ public virtual ResultTypes Analyze(Round exp)
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
_ => throw new DifferentParameterTypeMismatchException(
ResultTypes.Undefined | ResultTypes.Numbers,
number,
Expand Down Expand Up @@ -999,6 +1022,11 @@ public virtual ResultTypes Analyze(Sub exp)
(ResultTypes.TimeNumber, ResultTypes.TimeNumber)
=> ResultTypes.TimeNumber,

(ResultTypes.Number, ResultTypes.AreaNumber) or
(ResultTypes.AreaNumber, ResultTypes.Number) or
(ResultTypes.AreaNumber, ResultTypes.AreaNumber)
=> ResultTypes.AreaNumber,

(ResultTypes.Number, ResultTypes.ComplexNumber) or
(ResultTypes.ComplexNumber, ResultTypes.Number) or
(ResultTypes.ComplexNumber, ResultTypes.ComplexNumber)
Expand Down Expand Up @@ -1041,6 +1069,7 @@ public virtual ResultTypes Analyze(UnaryMinus exp)
ResultTypes.MassNumber => ResultTypes.MassNumber,
ResultTypes.LengthNumber => ResultTypes.LengthNumber,
ResultTypes.TimeNumber => ResultTypes.TimeNumber,
ResultTypes.AreaNumber => ResultTypes.AreaNumber,
ResultTypes.ComplexNumber => ResultTypes.ComplexNumber,
_ => ResultTypes.NumberOrComplex.ThrowFor(result),
};
Expand Down Expand Up @@ -1079,7 +1108,9 @@ ResultTypes.PowerNumber or
ResultTypes.TemperatureNumber or
ResultTypes.MassNumber or
ResultTypes.LengthNumber or
ResultTypes.TimeNumber => ResultTypes.Number,
ResultTypes.TimeNumber or
ResultTypes.AreaNumber
=> ResultTypes.Number,

_ => ResultTypes.Numbers.ThrowFor(result),
};
Expand Down
1 change: 1 addition & 0 deletions xFunc.Maths/Expressions/Abs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public override object Execute(ExpressionParameters? parameters)
MassValue mass => MassValue.Abs(mass),
LengthValue length => LengthValue.Abs(length),
TimeValue time => TimeValue.Abs(time),
AreaValue area => AreaValue.Abs(area),
Complex complex => Complex.Abs(complex),
Vector vector => vector.Abs(parameters),
_ => throw new ResultIsNotSupportedException(this, result),
Expand Down
4 changes: 4 additions & 0 deletions xFunc.Maths/Expressions/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public override object Execute(ExpressionParameters? parameters)
(TimeValue left, NumberValue right) => left + right,
(TimeValue left, TimeValue right) => left + right,

(NumberValue left, AreaValue right) => left + right,
(AreaValue left, NumberValue right) => left + right,
(AreaValue left, AreaValue right) => left + right,

(NumberValue left, Complex right) => left + right,
(Complex left, NumberValue right) => left + right,
(Complex left, Complex right) => left + right,
Expand Down
1 change: 1 addition & 0 deletions xFunc.Maths/Expressions/Ceil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public override object Execute(ExpressionParameters? parameters)
MassValue mass => MassValue.Ceiling(mass),
LengthValue length => LengthValue.Ceiling(length),
TimeValue time => TimeValue.Ceiling(time),
AreaValue area => AreaValue.Ceiling(area),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
22 changes: 22 additions & 0 deletions xFunc.Maths/Expressions/Collections/ParameterValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public ParameterValue(LengthValue value)
{
}

/// <inheritdoc cref="ParameterValue(object)"/>
public ParameterValue(TimeValue value)
: this(value as object)
{
}

/// <inheritdoc cref="ParameterValue(object)"/>
public ParameterValue(AreaValue value)
: this(value as object)
{
}

/// <inheritdoc cref="ParameterValue(object)"/>
public ParameterValue(Complex value)
: this(value as object)
Expand Down Expand Up @@ -100,6 +112,8 @@ or PowerValue
or TemperatureValue
or MassValue
or LengthValue
or TimeValue
or AreaValue
or Complex
or bool
or Vector
Expand Down Expand Up @@ -142,6 +156,14 @@ public static implicit operator ParameterValue(MassValue value)
public static implicit operator ParameterValue(LengthValue value)
=> new ParameterValue(value);

/// <inheritdoc cref="ParameterValue.op_Implicit(double)"/>
public static implicit operator ParameterValue(TimeValue value)
=> new ParameterValue(value);

/// <inheritdoc cref="ParameterValue.op_Implicit(double)"/>
public static implicit operator ParameterValue(AreaValue value)
=> new ParameterValue(value);

/// <inheritdoc cref="ParameterValue.op_Implicit(double)"/>
public static implicit operator ParameterValue(Complex value)
=> new ParameterValue(value);
Expand Down
1 change: 1 addition & 0 deletions xFunc.Maths/Expressions/Div.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public override object Execute(ExpressionParameters? parameters)
(MassValue left, NumberValue right) => left / right,
(LengthValue left, NumberValue right) => left / right,
(TimeValue left, NumberValue right) => left / right,
(AreaValue left, NumberValue right) => left / right,

(NumberValue left, Complex right) => left / right,
(Complex left, NumberValue right) => left / right,
Expand Down
1 change: 1 addition & 0 deletions xFunc.Maths/Expressions/Floor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public override object Execute(ExpressionParameters? parameters)
MassValue mass => MassValue.Floor(mass),
LengthValue length => LengthValue.Floor(length),
TimeValue time => TimeValue.Floor(time),
AreaValue area => AreaValue.Floor(area),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
1 change: 1 addition & 0 deletions xFunc.Maths/Expressions/Frac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public override object Execute(ExpressionParameters? parameters)
MassValue mass => MassValue.Frac(mass),
LengthValue length => LengthValue.Frac(length),
TimeValue time => TimeValue.Frac(time),
AreaValue area => AreaValue.Frac(area),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
3 changes: 3 additions & 0 deletions xFunc.Maths/Expressions/Mul.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public override object Execute(ExpressionParameters? parameters)
(NumberValue left, TimeValue right) => left * right,
(TimeValue left, NumberValue right) => left * right,

(NumberValue left, AreaValue right) => left * right,
(AreaValue left, NumberValue right) => left * right,

(NumberValue left, Complex right) => left * right,
(Complex left, NumberValue right) => left * right,
(Complex left, Complex right) => left * right,
Expand Down
Loading

0 comments on commit 8b09b7a

Please sign in to comment.