Skip to content

Commit

Permalink
#641 - Introduce VectorValue/MatrixValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Jul 9, 2023
1 parent c2e2cb8 commit 586d547
Show file tree
Hide file tree
Showing 44 changed files with 1,258 additions and 873 deletions.
2 changes: 1 addition & 1 deletion xFunc.Maths/Analyzers/Simplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ public override IExpression Analyze(Matrix exp)
var arguments = exp.Vectors;
var isExpChanged = false;

for (var i = 0; i < exp.Rows; i++)
for (var i = 0; i < exp.Vectors.Length; i++)
{
var expression = exp[i].Analyze(this);
if (IsChanged(exp[i], expression))
Expand Down
3 changes: 1 addition & 2 deletions xFunc.Maths/Expressions/Abs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Immutable;
using System.Numerics;
using Vector = xFunc.Maths.Expressions.Matrices.Vector;

namespace xFunc.Maths.Expressions;

Expand Down Expand Up @@ -54,7 +53,7 @@ public override object Execute(ExpressionParameters? parameters)
AreaValue area => AreaValue.Abs(area),
VolumeValue volume => VolumeValue.Abs(volume),
Complex complex => Complex.Abs(complex),
Vector vector => vector.Abs(parameters),
VectorValue vector => VectorValue.Abs(vector),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
5 changes: 2 additions & 3 deletions xFunc.Maths/Expressions/Add.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Immutable;
using System.Numerics;
using Vector = xFunc.Maths.Expressions.Matrices.Vector;

namespace xFunc.Maths.Expressions;

Expand Down Expand Up @@ -86,8 +85,8 @@ public override object Execute(ExpressionParameters? parameters)
(Complex left, NumberValue right) => left + right,
(Complex left, Complex right) => left + right,

(Vector left, Vector right) => left.Add(right, parameters),
(Matrix left, Matrix right) => left.Add(right, parameters),
(VectorValue left, VectorValue right) => left + right,
(MatrixValue left, MatrixValue right) => left + right,

(string left, var right) => left + right,
(var left, string right) => left + right,
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Del.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override object Execute(ExpressionParameters? parameters)
.Analyze(simplifier));
}

return new Vector(vector.ToImmutableArray());
return new Vector(vector.ToImmutableArray()); // TODO: type analyzer -> expression
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Matrices/CrossProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override object Execute(ExpressionParameters? parameters)

return (left, right) switch
{
(Vector leftVector, Vector rightVector) => leftVector.Cross(rightVector, parameters),
(VectorValue leftVector, VectorValue rightVector) => VectorValue.Cross(leftVector, rightVector),
_ => throw new ResultIsNotSupportedException(this, left, right),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Matrices/Determinant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
Matrix matrix => matrix.Determinant(parameters),
MatrixValue matrix => MatrixValue.Determinant(matrix),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Matrices/DotProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override object Execute(ExpressionParameters? parameters)

return (left, right) switch
{
(Vector leftVector, Vector rightVector) => leftVector.Mul(rightVector, parameters),
(VectorValue leftVector, VectorValue rightVector) => leftVector * rightVector,
_ => throw new ResultIsNotSupportedException(this, left, right),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ namespace xFunc.Maths.Expressions.Matrices;
/// Thrown in matrix building.
/// </summary>
[Serializable]
public class MatrixIsInvalidException : Exception
public class InvalidMatrixException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="MatrixIsInvalidException"/> class.
/// Initializes a new instance of the <see cref="InvalidMatrixException"/> class.
/// </summary>
public MatrixIsInvalidException()
public InvalidMatrixException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MatrixIsInvalidException"/> class.
/// Initializes a new instance of the <see cref="InvalidMatrixException"/> class.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public MatrixIsInvalidException(string message)
public InvalidMatrixException(string message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MatrixIsInvalidException"/> class.
/// Initializes a new instance of the <see cref="InvalidMatrixException"/> class.
/// </summary>
/// <param name="message">A <see cref="string"/> that describes the error.</param>
/// <param name="inner">The exception that is the cause of the current exception.</param>
public MatrixIsInvalidException(string message, Exception inner)
public InvalidMatrixException(string message, Exception inner)
: base(message, inner)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MatrixIsInvalidException"/> class.
/// Initializes a new instance of the <see cref="InvalidMatrixException"/> class.
/// </summary>
/// <param name="info">The object that holds the serialized object data.</param>
/// <param name="context">The contextual information about the source or destination.</param>
protected MatrixIsInvalidException(SerializationInfo info, StreamingContext context)
protected InvalidMatrixException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Matrices/Inverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
Matrix matrix => matrix.Inverse(parameters),
MatrixValue matrix => MatrixValue.Inverse(matrix),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
46 changes: 11 additions & 35 deletions xFunc.Maths/Expressions/Matrices/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace xFunc.Maths.Expressions.Matrices;

/// <summary>
/// Represents a matrix.
/// Represents a matrix expression.
/// </summary>
public class Matrix : IExpression
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public Matrix(ImmutableArray<Vector> vectors)
var size = vectors[0].ParametersCount;
foreach (var vector in vectors)
if (vector.ParametersCount != size)
throw new MatrixIsInvalidException();
throw new InvalidMatrixException();

Vectors = vectors;
}
Expand Down Expand Up @@ -83,12 +83,18 @@ public override bool Equals(object? obj)
/// <inheritdoc />
public object Execute(ExpressionParameters? parameters)
{
var args = new Vector[Rows];
var vectors = new VectorValue[Vectors.Length];

for (var i = 0; i < Vectors.Length; i++)
args[i] = (Vector)Vectors[i].Execute(parameters);
{
var result = Vectors[i].Execute(parameters);
if (result is not VectorValue vectorValue)
throw new ResultIsNotSupportedException(this, result);

return new Matrix(Unsafe.As<Vector[], ImmutableArray<Vector>>(ref args));
vectors[i] = vectorValue;
}

return Unsafe.As<VectorValue[], MatrixValue>(ref vectors);
}

/// <inheritdoc />
Expand Down Expand Up @@ -121,38 +127,8 @@ public TResult Analyze<TResult>(IAnalyzer<TResult> analyzer)
public IExpression Clone(ImmutableArray<Vector>? vectors = null)
=> new Matrix(vectors ?? Vectors);

/// <summary>
/// Calculates current matrix and returns it as an two dimensional array.
/// </summary>
/// <param name="parameters">An object that contains all parameters and functions for expressions.</param>
/// <returns>The two dimensional array which represents current vector.</returns>
internal NumberValue[][] ToCalculatedArray(ExpressionParameters? parameters)
{
var results = new NumberValue[Rows][];

for (var i = 0; i < Rows; i++)
results[i] = Vectors[i].ToCalculatedArray(parameters);

return results;
}

/// <summary>
/// Gets the vectors.
/// </summary>
public ImmutableArray<Vector> Vectors { get; }

/// <summary>
/// Gets the count of rows.
/// </summary>
public int Rows => Vectors.Length;

/// <summary>
/// Gets the count of columns.
/// </summary>
public int Columns => Vectors[0].ParametersCount;

/// <summary>
/// Gets a value indicating whether matrix is square.
/// </summary>
public bool IsSquare => Rows == Columns;
}

0 comments on commit 586d547

Please sign in to comment.