Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ Euler's totient is `phi(n)`, and `φ(n)` and `ϕ(n)` are refused. Each name with
| `"divergence(x)".ToEntity()` | `divergence * x` | throws `UnrecognizedFunctionParseException` |
| `"gradient(x^2*y, [x, y])".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |

### `dot` and `cross` of a name for a vector are nodes

**Not silent: what was refused now parses.** `dot(a, b)` and `cross(a, b)` threw `InvalidArgumentParseException`
where an operand was not a vector written out. Now they are the nodes `DotProductf` and `CrossProductf`. Each is
computed once both operands are vectors: when they are substituted, by `Simplify`, `Evaled`, or a compiled delegate
taking `double[]`, `double[,]` or `Complex[]`. Of vectors written out each is still computed as it is parsed, and prints
as before. Vectors of different lengths, or a `cross` of vectors without three components, leave the node as it is.
A number, or a written vector that cannot fit, is refused as before. The operands of `cross` are kept in their order, and
so is a product with a `cross` in it. A name beside a written vector, as in `dot(x * [1, 2], [3, 4])`, may stand for a
matrix, so the operand is no longer multiplied into the components as a number
([#105](https://github.com/asc-community/AngouriMath/issues/105)).

| Input | Was (2.7.0) | Now |
|---|---|---|
| `"dot(a, b)".ToEntity()` | throws `InvalidArgumentParseException` | `dot(a, b)`, a `DotProductf` |
| `"cross(W, r)".ToEntity()` | throws `InvalidArgumentParseException` | `cross(W, r)`, a `CrossProductf` |
| `"dot([1, 2, 3], b)".ToEntity()` | throws `InvalidArgumentParseException` | `dot([1, 2, 3], b)` |
| `"dot(a, b)"` with `a = [1, 2, 3]`, `b = [4, 5, 6]` substituted, `.Simplify()` | throws `InvalidArgumentParseException` | `32` |
| `"cross(W, r)"` with `W = [0, 0, w]`, `r = [x, y, z]` substituted, `.Simplify()` | throws `InvalidArgumentParseException` | `[-w * y, w * x, 0]` |
| `"dot(a, b)"` with `a = [1, 2, 3]`, `b = [4, 5]` substituted, `.Evaled` | throws `InvalidArgumentParseException` | `dot([1, 2, 3], [4, 5])`, left |
| `"dot(x * [1, 2], [3, 4])".ToEntity()` | `x * 3 + x * 2 * 4` | `dot(x * [1, 2], [3, 4])`: `x` may stand for a matrix |
| `"dot(5, a)".ToEntity()` | throws `InvalidArgumentParseException` | throws `InvalidArgumentParseException`, saying `dot` is taken of vectors |
| `"cross(a, [1, 2])".ToEntity()` | throws `InvalidArgumentParseException` | throws `InvalidArgumentParseException`, naming `[1, 2]` |
| `"dot([1, 2, 3], [4, 5, 6])".ToEntity()` | `1 * 4 + 2 * 5 + 3 * 6` | unchanged |
| `"cross([0, 0, w], [x, y, z])".ToEntity()` | `[0 * z - w * y, w * x - 0 * z, 0 * y - 0 * x]` | unchanged |

`cross(a)` of one vector in the plane has no node, and still takes a vector written out.

---

### A system row that is not a solution declines the system
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1096,3 +1096,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed

[AngouriMath/Convenience/MathS.Equality.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Common/DotAndCrossNodesTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
38 changes: 38 additions & 0 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6050,6 +6050,44 @@ public static Matrix Matrix(int rows, int columns, params Entity[] values) =>
/// https://github.com/asc-community/AngouriMath/issues/2147
/// </remarks>
public static Entity Entry(Entity matrix, Entity row, Entity column) => new MatrixEntryf(matrix, row, column);

/// <summary>
/// The scalar product <c>dot(a, b)</c> of two vectors of one length, as a node: the sum of the products of
/// their components, neither side conjugated.
/// </summary>
/// <param name="left">The first vector, written out or a name that stands for one.</param>
/// <param name="right">The second vector, written out or a name that stands for one.</param>
/// <returns>The <see cref="Entity.DotProductf"/> node, computed once both operands are vectors</returns>
/// <remarks>
/// Vectors of different lengths have no scalar product, and the node is left as it is.
/// https://github.com/asc-community/AngouriMath/issues/105
/// </remarks>
/// <example>
/// <code>
/// var dot = MathS.Matrices.DotProduct("a", "b");
/// Console.WriteLine(dot);
/// Console.WriteLine(dot.Substitute("a", "[1, 2, 3]").Substitute("b", "[4, 5, 6]").Evaled);
/// </code>
/// Prints
/// <code>
/// dot(a, b)
/// 32
/// </code>
/// </example>
public static Entity DotProduct(Entity left, Entity right) => new DotProductf(left, right);

/// <summary>
/// The vector product <c>cross(a, b)</c> of two vectors in three dimensions, as a node. It is not
/// commutative, and its operands are kept in their order.
/// </summary>
/// <param name="left">The first vector, written out or a name that stands for one.</param>
/// <param name="right">The second vector, written out or a name that stands for one.</param>
/// <returns>The <see cref="Entity.CrossProductf"/> node, computed once both operands are vectors</returns>
/// <remarks>
/// Anything but two vectors of three components has no vector product, and the node is left as it is.
/// https://github.com/asc-community/AngouriMath/issues/105
/// </remarks>
public static Entity CrossProduct(Entity left, Entity right) => new CrossProductf(left, right);
}

/// <summary>
Expand Down
64 changes: 40 additions & 24 deletions Sources/AngouriMath/Core/Antlr/ParsingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ internal static Matrix TryBuildingMatrix(List<Entity> elements)
return tb.ToMatrix() ?? throw new AngouriBugException("Should've been checked already");
}

// det, transpose, dot, cross and resultant are computed from what they are applied to where it
// is a matrix written out, or an expression that is one once its operations are carried out,
// as 2 [[1, 2], [3, 4]] is. https://github.com/asc-community/AngouriMath/issues/2147
// cross of one vector and resultant are computed from what they are applied to where it is a
// matrix written out, or an expression that is one once its operations are carried out, as
// 2 [[1, 2], [3, 4]] is. https://github.com/asc-community/AngouriMath/issues/2147
private static Matrix MatrixArgument(string function, Entity argument, string example)
=> argument as Matrix ?? argument.InnerSimplified as Matrix
?? throw new InvalidArgumentParseException(
Expand All @@ -42,18 +42,31 @@ private static Matrix MatrixArgument(string function, Entity argument, string ex

// A matrix written out, or null where the argument may yet stand for one -- a name, or an
// expression with a name in it beside a matrix, as A [[1, 2], [3, 4]] is, which simplifying
// would read as a number -- and det, transpose, trace and an entry are left as nodes over
// it. A number, a statement or a set is no matrix, and is refused as it was.
private static Matrix? MatrixOrUnknown(string function, Entity argument, string example)
// would read as a number -- and det, transpose, trace, an entry, dot and cross are left as
// nodes over it. A number, a statement or a set is no matrix, and is refused as it was.
private static Matrix? MatrixOrUnknown(string function, Entity argument, string example, string what = "a matrix")
=> (argument as Matrix ?? argument.InnerSimplified) switch
{
Matrix when argument is not Matrix && HasANameOutsideAWrittenMatrix(argument) => null,
Matrix matrix => matrix,
Number or Statement or Set => throw new InvalidArgumentParseException(
$"{function} is taken of a matrix, as in {example}, and {argument} is not one"),
$"{function} is taken of {what}, as in {example}, and {argument} is not one"),
_ => null,
};

// A vector written out, a column or a row, or null where the operand may yet stand for one, as
// MatrixOrUnknown reads it. A matrix written out that is not a vector is refused.
// https://github.com/asc-community/AngouriMath/issues/105
private static Matrix? VectorOrUnknown(string function, Entity operand)
{
if (MatrixOrUnknown(function, operand, $"{function}([1, 2, 3], [4, 5, 6])", "vectors") is not { } vector)
return null;
if (VectorComponents(vector) is null)
throw new InvalidArgumentParseException(
$"{function} is taken of vectors, and {vector} is a {vector.RowCount} by {vector.ColumnCount} matrix");
return vector;
}

/// <summary>The entries of a vector, written as a column or as a row.</summary>
private static Entity[] Components(string function, Entity argument)
{
Expand Down Expand Up @@ -120,20 +133,21 @@ internal static Entity Entry(Entity argument, List<Entity> indices)

/// <summary>
/// <c>dot(a, b)</c>: the sum of the products of the components, without conjugating either
/// side, as SymPy's <c>dot</c> and Mathematica's <c>Dot</c> are.
/// side, as SymPy's <c>dot</c> and Mathematica's <c>Dot</c> are; or the node, where an operand
/// is a name that may stand for a vector.
/// </summary>
internal static Entity Dot(Entity left, Entity right)
{
var a = Components("dot", left);
var b = Components("dot", right);
if (a.Length != b.Length)
throw new InvalidArgumentParseException(
$"dot is taken of two vectors of one length, and these have {a.Length} and {b.Length} components");
return Enumerable.Range(0, a.Length).Select(i => a[i] * b[i]).Aggregate((sum, term) => sum + term);
var (a, b) = (VectorOrUnknown("dot", left), VectorOrUnknown("dot", right));
if (a is null || b is null)
return new DotProductf(left, right);
return DotProductf.Of(a, b) ?? throw new InvalidArgumentParseException(
$"dot is taken of two vectors of one length, and these have {VectorComponents(a)!.Length} and {VectorComponents(b)!.Length} components");
}

/// <summary>
/// <c>cross(a, b)</c>, the vector product in three dimensions, and <c>cross(a)</c>, the vector
/// <c>cross(a, b)</c>, the vector product in three dimensions, or the node where an operand is a
/// name that may stand for a vector; and <c>cross(a)</c> of a vector written out, the vector
/// in the plane that <c>a</c> turns into through a right angle anticlockwise: <c>(-a_2, a_1)</c>,
/// as Mathematica's <c>Cross</c> of one vector is. So <c>dot(cross(a), b)</c> is the plane's
/// pseudo-scalar product <c>a_1 b_2 - a_2 b_1</c>.
Expand All @@ -148,15 +162,17 @@ internal static Entity Cross(List<Entity> arguments)
$"cross of one vector is its turn through a right angle in the plane, and this vector has {a.Length} components");
return MathS.Vector(-a[1], a[0]);
}
var u = Components("cross", arguments[0]);
var v = Components("cross", arguments[1]);
if (u.Length != 3 || v.Length != 3)
throw new InvalidArgumentParseException(
$"cross of two vectors is the vector product in three dimensions, and these have {u.Length} and {v.Length} components");
return MathS.Vector(
u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0]);
var (left, right) = (VectorOrUnknown("cross", arguments[0]), VectorOrUnknown("cross", arguments[1]));
if (left is null || right is null)
{
// A vector written out has no vector product unless it has three components, whatever the other one is.
if ((left ?? right) is { } written && VectorComponents(written)!.Length != 3)
throw new InvalidArgumentParseException(
$"cross of two vectors is the vector product in three dimensions, and {written} has {VectorComponents(written)!.Length} components");
return new CrossProductf(arguments[0], arguments[1]);
}
return CrossProductf.Of(left, right) ?? throw new InvalidArgumentParseException(
$"cross of two vectors is the vector product in three dimensions, and these have {VectorComponents(left)!.Length} and {VectorComponents(right)!.Length} components");
}

/// <summary><c>resultant(p, q, x)</c>, eliminating <c>x</c> between two polynomials.</summary>
Expand Down
18 changes: 18 additions & 0 deletions Sources/AngouriMath/Core/Domains.Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ partial record MatrixEntryf
internal override Domain DefaultCodomain => Domain.Complex;
}

// The scalar product is a number, and the vector product a vector, which has no domain of numbers.
// https://github.com/asc-community/AngouriMath/issues/105
partial record DotProductf
{
/// <inheritdoc/>
public override Domain Codomain { get; protected init; } = Domain.Complex;
/// <inheritdoc/>
internal override Domain DefaultCodomain => Domain.Complex;
}

partial record CrossProductf
{
/// <inheritdoc/>
public override Domain Codomain { get; protected init; } = Domain.Any;
/// <inheritdoc/>
internal override Domain DefaultCodomain => Domain.Any;
}

partial record Matrix
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
//

using System;
using System.Linq;

namespace AngouriMath
{
// The four operations on a matrix that an expression can name before the matrix is known. A
// The operations on a matrix or on vectors that an expression can name before the matrix is known. A
// matrix written out is computed from its entries as soon as it is parsed, as it was without
// these nodes; a node is what is left where the argument is not yet a matrix -- a name that
// stands for one, det(A) -- and it is computed once the argument becomes a matrix, by
Expand Down Expand Up @@ -88,5 +89,71 @@ public override Entity Replace(Func<Entity, Entity> func)
/// <inheritdoc/>
protected override Entity[] InitDirectChildren() => new[] { Argument, Row, Column };
}

/// <summary>
/// The scalar product <c>dot(a, b)</c> of two vectors of one length: the sum of the products of their components,
/// neither side conjugated, as SymPy's <c>dot</c> and Mathematica's <c>Dot</c> are.
/// </summary>
/// <remarks>
/// Computed when both operands are vectors, each a column or a row. Vectors of different lengths, or a matrix that
/// is not a vector, have no scalar product, and the node is left as it is rather than given a value.
/// https://github.com/asc-community/AngouriMath/issues/105
/// </remarks>
public sealed partial record DotProductf(Entity Left, Entity Right) : Function
{
private DotProductf New(Entity left, Entity right) =>
ReferenceEquals(Left, left) && ReferenceEquals(Right, right) ? this : new(left, right) { Codomain = Codomain };
/// <inheritdoc/>
public override Entity Replace(Func<Entity, Entity> func) => func(New(Left.Replace(func), Right.Replace(func)));
/// <inheritdoc/>
protected override Entity[] InitDirectChildren() => new[] { Left, Right };

/// <summary>
/// The sum of the products of the components, or null where the two are not vectors of one length. Written as
/// it was computed when parsed: <c>dot([1, 2], [3, 4])</c> is <c>1 * 3 + 2 * 4</c>.
/// </summary>
internal static Entity? Of(Matrix left, Matrix right)
=> VectorComponents(left) is { } a && VectorComponents(right) is { } b && a.Length == b.Length
? Enumerable.Range(0, a.Length).Select(i => a[i] * b[i]).Aggregate((sum, term) => sum + term)
: null;
}

/// <summary>
/// The vector product <c>cross(a, b)</c> of two vectors in three dimensions, a column. It is not commutative:
/// <c>cross(b, a)</c> is <c>-cross(a, b)</c>, and the operands are never put in another order.
/// </summary>
/// <remarks>
/// Computed when both operands are vectors of three components, each a column or a row. Anything else has no vector
/// product, and the node is left as it is rather than given a value. <c>cross(a)</c> of one vector in the plane is
/// computed from a vector written out only, and has no node.
/// https://github.com/asc-community/AngouriMath/issues/105
/// </remarks>
public sealed partial record CrossProductf(Entity Left, Entity Right) : Function
{
private CrossProductf New(Entity left, Entity right) =>
ReferenceEquals(Left, left) && ReferenceEquals(Right, right) ? this : new(left, right) { Codomain = Codomain };
/// <inheritdoc/>
public override Entity Replace(Func<Entity, Entity> func) => func(New(Left.Replace(func), Right.Replace(func)));
/// <inheritdoc/>
protected override Entity[] InitDirectChildren() => new[] { Left, Right };

/// <summary>
/// The vector product, a column, or null where the two are not vectors of three components. Written as it was
/// computed when parsed: <c>cross([1, 2, 3], [4, 5, 6])</c> is <c>[2 * 6 - 3 * 5, 3 * 4 - 1 * 6, 1 * 5 - 2 * 4]</c>.
/// </summary>
internal static Entity? Of(Matrix left, Matrix right)
=> VectorComponents(left) is { Length: 3 } u && VectorComponents(right) is { Length: 3 } v
? MathS.Vector(
u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0])
: null;
}

/// <summary>The components of a vector, written as a column or as a row, or null where the matrix is neither.</summary>
internal static Entity[]? VectorComponents(Matrix vector)
=> vector.ColumnCount == 1 ? Enumerable.Range(0, vector.RowCount).Select(i => vector[i, 0]).ToArray()
: vector.RowCount == 1 ? Enumerable.Range(0, vector.ColumnCount).Select(j => vector[0, j]).ToArray()
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ [EntityJsonConverter] partial record Entity
[EntityJsonConverter] partial record Transposef;
[EntityJsonConverter] partial record Tracef;
[EntityJsonConverter] partial record MatrixEntryf;
[EntityJsonConverter] partial record DotProductf;
[EntityJsonConverter] partial record CrossProductf;
[EntityJsonConverter] partial record Maxf;
[EntityJsonConverter] partial record Minf;
[EntityJsonConverter] partial record Minusf;
Expand Down
Loading
Loading