Skip to content

Commit 840075a

Browse files
Make dot and cross nodes that apply to a name for a vector (#2455)
dot(a, b) and cross(a, b) of a name threw InvalidArgumentParseException at parse time, so a vector could not be substituted later. They are now the nodes DotProductf and CrossProductf, computed once both operands are vectors, by substitution, InnerSimplified, Evaled and a compiled delegate over double[], double[,] or Complex[] (dot to a scalar, cross to a column). Vectors written out are still computed when parsed and print as before. Errors follow det: a number, a statement, a set, a written matrix that is not a vector, or a written vector that cannot fit (cross(a, [1, 2])) is refused at parse time; operands that turn out not to fit after substitution leave the node unevaluated; a compiled delegate throws InvalidMatrixOperationException at the call, or at Compile where the shapes are written out. cross is not commutative: a product with an unevaluated cross in it is kept in its order, as one with a transpose is, and nothing reorders its operands. A name beside a written vector, dot(x * [1, 2], [3, 4]), is left as a node, as det leaves det(x * [[1, 2], [3, 4]]); it was multiplied in as a number. Recorded in BREAKING-CHANGES.md. MatrixFunctionsInAnExpressionTest's dot(A, [1, 2]) row moved from refused to parsed, since that is the change. Part of #105 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3fd84eb commit 840075a

29 files changed

Lines changed: 844 additions & 53 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ Euler's totient is `phi(n)`, and `φ(n)` and `ϕ(n)` are refused. Each name with
8383
| `"divergence(x)".ToEntity()` | `divergence * x` | throws `UnrecognizedFunctionParseException` |
8484
| `"gradient(x^2*y, [x, y])".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
8585

86+
### `dot` and `cross` of a name for a vector are nodes
87+
88+
**Not silent: what was refused now parses.** `dot(a, b)` and `cross(a, b)` threw `InvalidArgumentParseException`
89+
where an operand was not a vector written out. Now they are the nodes `DotProductf` and `CrossProductf`. Each is
90+
computed once both operands are vectors: when they are substituted, by `Simplify`, `Evaled`, or a compiled delegate
91+
taking `double[]`, `double[,]` or `Complex[]`. Of vectors written out each is still computed as it is parsed, and prints
92+
as before. Vectors of different lengths, or a `cross` of vectors without three components, leave the node as it is.
93+
A number, or a written vector that cannot fit, is refused as before. The operands of `cross` are kept in their order, and
94+
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
95+
matrix, so the operand is no longer multiplied into the components as a number
96+
([#105](https://github.com/asc-community/AngouriMath/issues/105)).
97+
98+
| Input | Was (2.7.0) | Now |
99+
|---|---|---|
100+
| `"dot(a, b)".ToEntity()` | throws `InvalidArgumentParseException` | `dot(a, b)`, a `DotProductf` |
101+
| `"cross(W, r)".ToEntity()` | throws `InvalidArgumentParseException` | `cross(W, r)`, a `CrossProductf` |
102+
| `"dot([1, 2, 3], b)".ToEntity()` | throws `InvalidArgumentParseException` | `dot([1, 2, 3], b)` |
103+
| `"dot(a, b)"` with `a = [1, 2, 3]`, `b = [4, 5, 6]` substituted, `.Simplify()` | throws `InvalidArgumentParseException` | `32` |
104+
| `"cross(W, r)"` with `W = [0, 0, w]`, `r = [x, y, z]` substituted, `.Simplify()` | throws `InvalidArgumentParseException` | `[-w * y, w * x, 0]` |
105+
| `"dot(a, b)"` with `a = [1, 2, 3]`, `b = [4, 5]` substituted, `.Evaled` | throws `InvalidArgumentParseException` | `dot([1, 2, 3], [4, 5])`, left |
106+
| `"dot(x * [1, 2], [3, 4])".ToEntity()` | `x * 3 + x * 2 * 4` | `dot(x * [1, 2], [3, 4])`: `x` may stand for a matrix |
107+
| `"dot(5, a)".ToEntity()` | throws `InvalidArgumentParseException` | throws `InvalidArgumentParseException`, saying `dot` is taken of vectors |
108+
| `"cross(a, [1, 2])".ToEntity()` | throws `InvalidArgumentParseException` | throws `InvalidArgumentParseException`, naming `[1, 2]` |
109+
| `"dot([1, 2, 3], [4, 5, 6])".ToEntity()` | `1 * 4 + 2 * 5 + 3 * 6` | unchanged |
110+
| `"cross([0, 0, w], [x, y, z])".ToEntity()` | `[0 * z - w * y, w * x - 0 * z, 0 * y - 0 * x]` | unchanged |
111+
112+
`cross(a)` of one vector in the plane has no node, and still takes a vector written out.
113+
86114
---
87115

88116
### A system row that is not a solution declines the system

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,3 +1096,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10961096

10971097
[AngouriMath/Convenience/MathS.Equality.cs]
10981098
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
1099+
1100+
[Tests/UnitTests/Common/DotAndCrossNodesTest.cs]
1101+
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

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6050,6 +6050,44 @@ public static Matrix Matrix(int rows, int columns, params Entity[] values) =>
60506050
/// https://github.com/asc-community/AngouriMath/issues/2147
60516051
/// </remarks>
60526052
public static Entity Entry(Entity matrix, Entity row, Entity column) => new MatrixEntryf(matrix, row, column);
6053+
6054+
/// <summary>
6055+
/// The scalar product <c>dot(a, b)</c> of two vectors of one length, as a node: the sum of the products of
6056+
/// their components, neither side conjugated.
6057+
/// </summary>
6058+
/// <param name="left">The first vector, written out or a name that stands for one.</param>
6059+
/// <param name="right">The second vector, written out or a name that stands for one.</param>
6060+
/// <returns>The <see cref="Entity.DotProductf"/> node, computed once both operands are vectors</returns>
6061+
/// <remarks>
6062+
/// Vectors of different lengths have no scalar product, and the node is left as it is.
6063+
/// https://github.com/asc-community/AngouriMath/issues/105
6064+
/// </remarks>
6065+
/// <example>
6066+
/// <code>
6067+
/// var dot = MathS.Matrices.DotProduct("a", "b");
6068+
/// Console.WriteLine(dot);
6069+
/// Console.WriteLine(dot.Substitute("a", "[1, 2, 3]").Substitute("b", "[4, 5, 6]").Evaled);
6070+
/// </code>
6071+
/// Prints
6072+
/// <code>
6073+
/// dot(a, b)
6074+
/// 32
6075+
/// </code>
6076+
/// </example>
6077+
public static Entity DotProduct(Entity left, Entity right) => new DotProductf(left, right);
6078+
6079+
/// <summary>
6080+
/// The vector product <c>cross(a, b)</c> of two vectors in three dimensions, as a node. It is not
6081+
/// commutative, and its operands are kept in their order.
6082+
/// </summary>
6083+
/// <param name="left">The first vector, written out or a name that stands for one.</param>
6084+
/// <param name="right">The second vector, written out or a name that stands for one.</param>
6085+
/// <returns>The <see cref="Entity.CrossProductf"/> node, computed once both operands are vectors</returns>
6086+
/// <remarks>
6087+
/// Anything but two vectors of three components has no vector product, and the node is left as it is.
6088+
/// https://github.com/asc-community/AngouriMath/issues/105
6089+
/// </remarks>
6090+
public static Entity CrossProduct(Entity left, Entity right) => new CrossProductf(left, right);
60536091
}
60546092

60556093
/// <summary>

‎Sources/AngouriMath/Core/Antlr/ParsingHelpers.cs‎

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ internal static Matrix TryBuildingMatrix(List<Entity> elements)
3131
return tb.ToMatrix() ?? throw new AngouriBugException("Should've been checked already");
3232
}
3333

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

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

57+
// A vector written out, a column or a row, or null where the operand may yet stand for one, as
58+
// MatrixOrUnknown reads it. A matrix written out that is not a vector is refused.
59+
// https://github.com/asc-community/AngouriMath/issues/105
60+
private static Matrix? VectorOrUnknown(string function, Entity operand)
61+
{
62+
if (MatrixOrUnknown(function, operand, $"{function}([1, 2, 3], [4, 5, 6])", "vectors") is not { } vector)
63+
return null;
64+
if (VectorComponents(vector) is null)
65+
throw new InvalidArgumentParseException(
66+
$"{function} is taken of vectors, and {vector} is a {vector.RowCount} by {vector.ColumnCount} matrix");
67+
return vector;
68+
}
69+
5770
/// <summary>The entries of a vector, written as a column or as a row.</summary>
5871
private static Entity[] Components(string function, Entity argument)
5972
{
@@ -120,20 +133,21 @@ internal static Entity Entry(Entity argument, List<Entity> indices)
120133

121134
/// <summary>
122135
/// <c>dot(a, b)</c>: the sum of the products of the components, without conjugating either
123-
/// side, as SymPy's <c>dot</c> and Mathematica's <c>Dot</c> are.
136+
/// side, as SymPy's <c>dot</c> and Mathematica's <c>Dot</c> are; or the node, where an operand
137+
/// is a name that may stand for a vector.
124138
/// </summary>
125139
internal static Entity Dot(Entity left, Entity right)
126140
{
127-
var a = Components("dot", left);
128-
var b = Components("dot", right);
129-
if (a.Length != b.Length)
130-
throw new InvalidArgumentParseException(
131-
$"dot is taken of two vectors of one length, and these have {a.Length} and {b.Length} components");
132-
return Enumerable.Range(0, a.Length).Select(i => a[i] * b[i]).Aggregate((sum, term) => sum + term);
141+
var (a, b) = (VectorOrUnknown("dot", left), VectorOrUnknown("dot", right));
142+
if (a is null || b is null)
143+
return new DotProductf(left, right);
144+
return DotProductf.Of(a, b) ?? throw new InvalidArgumentParseException(
145+
$"dot is taken of two vectors of one length, and these have {VectorComponents(a)!.Length} and {VectorComponents(b)!.Length} components");
133146
}
134147

135148
/// <summary>
136-
/// <c>cross(a, b)</c>, the vector product in three dimensions, and <c>cross(a)</c>, the vector
149+
/// <c>cross(a, b)</c>, the vector product in three dimensions, or the node where an operand is a
150+
/// name that may stand for a vector; and <c>cross(a)</c> of a vector written out, the vector
137151
/// in the plane that <c>a</c> turns into through a right angle anticlockwise: <c>(-a_2, a_1)</c>,
138152
/// as Mathematica's <c>Cross</c> of one vector is. So <c>dot(cross(a), b)</c> is the plane's
139153
/// pseudo-scalar product <c>a_1 b_2 - a_2 b_1</c>.
@@ -148,15 +162,17 @@ internal static Entity Cross(List<Entity> arguments)
148162
$"cross of one vector is its turn through a right angle in the plane, and this vector has {a.Length} components");
149163
return MathS.Vector(-a[1], a[0]);
150164
}
151-
var u = Components("cross", arguments[0]);
152-
var v = Components("cross", arguments[1]);
153-
if (u.Length != 3 || v.Length != 3)
154-
throw new InvalidArgumentParseException(
155-
$"cross of two vectors is the vector product in three dimensions, and these have {u.Length} and {v.Length} components");
156-
return MathS.Vector(
157-
u[1] * v[2] - u[2] * v[1],
158-
u[2] * v[0] - u[0] * v[2],
159-
u[0] * v[1] - u[1] * v[0]);
165+
var (left, right) = (VectorOrUnknown("cross", arguments[0]), VectorOrUnknown("cross", arguments[1]));
166+
if (left is null || right is null)
167+
{
168+
// A vector written out has no vector product unless it has three components, whatever the other one is.
169+
if ((left ?? right) is { } written && VectorComponents(written)!.Length != 3)
170+
throw new InvalidArgumentParseException(
171+
$"cross of two vectors is the vector product in three dimensions, and {written} has {VectorComponents(written)!.Length} components");
172+
return new CrossProductf(arguments[0], arguments[1]);
173+
}
174+
return CrossProductf.Of(left, right) ?? throw new InvalidArgumentParseException(
175+
$"cross of two vectors is the vector product in three dimensions, and these have {VectorComponents(left)!.Length} and {VectorComponents(right)!.Length} components");
160176
}
161177

162178
/// <summary><c>resultant(p, q, x)</c>, eliminating <c>x</c> between two polynomials.</summary>

‎Sources/AngouriMath/Core/Domains.Classes.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ partial record MatrixEntryf
8989
internal override Domain DefaultCodomain => Domain.Complex;
9090
}
9191

92+
// The scalar product is a number, and the vector product a vector, which has no domain of numbers.
93+
// https://github.com/asc-community/AngouriMath/issues/105
94+
partial record DotProductf
95+
{
96+
/// <inheritdoc/>
97+
public override Domain Codomain { get; protected init; } = Domain.Complex;
98+
/// <inheritdoc/>
99+
internal override Domain DefaultCodomain => Domain.Complex;
100+
}
101+
102+
partial record CrossProductf
103+
{
104+
/// <inheritdoc/>
105+
public override Domain Codomain { get; protected init; } = Domain.Any;
106+
/// <inheritdoc/>
107+
internal override Domain DefaultCodomain => Domain.Any;
108+
}
109+
92110
partial record Matrix
93111
{
94112
/// <inheritdoc/>

‎Sources/AngouriMath/Core/Entity/Omni/Entity.MatrixOperations.Classes.cs‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
//
77

88
using System;
9+
using System.Linq;
910

1011
namespace AngouriMath
1112
{
12-
// The four operations on a matrix that an expression can name before the matrix is known. A
13+
// The operations on a matrix or on vectors that an expression can name before the matrix is known. A
1314
// matrix written out is computed from its entries as soon as it is parsed, as it was without
1415
// these nodes; a node is what is left where the argument is not yet a matrix -- a name that
1516
// stands for one, det(A) -- and it is computed once the argument becomes a matrix, by
@@ -88,5 +89,71 @@ public override Entity Replace(Func<Entity, Entity> func)
8889
/// <inheritdoc/>
8990
protected override Entity[] InitDirectChildren() => new[] { Argument, Row, Column };
9091
}
92+
93+
/// <summary>
94+
/// The scalar product <c>dot(a, b)</c> of two vectors of one length: the sum of the products of their components,
95+
/// neither side conjugated, as SymPy's <c>dot</c> and Mathematica's <c>Dot</c> are.
96+
/// </summary>
97+
/// <remarks>
98+
/// Computed when both operands are vectors, each a column or a row. Vectors of different lengths, or a matrix that
99+
/// is not a vector, have no scalar product, and the node is left as it is rather than given a value.
100+
/// https://github.com/asc-community/AngouriMath/issues/105
101+
/// </remarks>
102+
public sealed partial record DotProductf(Entity Left, Entity Right) : Function
103+
{
104+
private DotProductf New(Entity left, Entity right) =>
105+
ReferenceEquals(Left, left) && ReferenceEquals(Right, right) ? this : new(left, right) { Codomain = Codomain };
106+
/// <inheritdoc/>
107+
public override Entity Replace(Func<Entity, Entity> func) => func(New(Left.Replace(func), Right.Replace(func)));
108+
/// <inheritdoc/>
109+
protected override Entity[] InitDirectChildren() => new[] { Left, Right };
110+
111+
/// <summary>
112+
/// The sum of the products of the components, or null where the two are not vectors of one length. Written as
113+
/// it was computed when parsed: <c>dot([1, 2], [3, 4])</c> is <c>1 * 3 + 2 * 4</c>.
114+
/// </summary>
115+
internal static Entity? Of(Matrix left, Matrix right)
116+
=> VectorComponents(left) is { } a && VectorComponents(right) is { } b && a.Length == b.Length
117+
? Enumerable.Range(0, a.Length).Select(i => a[i] * b[i]).Aggregate((sum, term) => sum + term)
118+
: null;
119+
}
120+
121+
/// <summary>
122+
/// The vector product <c>cross(a, b)</c> of two vectors in three dimensions, a column. It is not commutative:
123+
/// <c>cross(b, a)</c> is <c>-cross(a, b)</c>, and the operands are never put in another order.
124+
/// </summary>
125+
/// <remarks>
126+
/// Computed when both operands are vectors of three components, each a column or a row. Anything else has no vector
127+
/// 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
128+
/// computed from a vector written out only, and has no node.
129+
/// https://github.com/asc-community/AngouriMath/issues/105
130+
/// </remarks>
131+
public sealed partial record CrossProductf(Entity Left, Entity Right) : Function
132+
{
133+
private CrossProductf New(Entity left, Entity right) =>
134+
ReferenceEquals(Left, left) && ReferenceEquals(Right, right) ? this : new(left, right) { Codomain = Codomain };
135+
/// <inheritdoc/>
136+
public override Entity Replace(Func<Entity, Entity> func) => func(New(Left.Replace(func), Right.Replace(func)));
137+
/// <inheritdoc/>
138+
protected override Entity[] InitDirectChildren() => new[] { Left, Right };
139+
140+
/// <summary>
141+
/// The vector product, a column, or null where the two are not vectors of three components. Written as it was
142+
/// 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>.
143+
/// </summary>
144+
internal static Entity? Of(Matrix left, Matrix right)
145+
=> VectorComponents(left) is { Length: 3 } u && VectorComponents(right) is { Length: 3 } v
146+
? MathS.Vector(
147+
u[1] * v[2] - u[2] * v[1],
148+
u[2] * v[0] - u[0] * v[2],
149+
u[0] * v[1] - u[1] * v[0])
150+
: null;
151+
}
152+
153+
/// <summary>The components of a vector, written as a column or as a row, or null where the matrix is neither.</summary>
154+
internal static Entity[]? VectorComponents(Matrix vector)
155+
=> vector.ColumnCount == 1 ? Enumerable.Range(0, vector.RowCount).Select(i => vector[i, 0]).ToArray()
156+
: vector.RowCount == 1 ? Enumerable.Range(0, vector.ColumnCount).Select(j => vector[0, j]).ToArray()
157+
: null;
91158
}
92159
}

‎Sources/AngouriMath/Core/Serialization/Entity.Serialization.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ [EntityJsonConverter] partial record Entity
6868
[EntityJsonConverter] partial record Transposef;
6969
[EntityJsonConverter] partial record Tracef;
7070
[EntityJsonConverter] partial record MatrixEntryf;
71+
[EntityJsonConverter] partial record DotProductf;
72+
[EntityJsonConverter] partial record CrossProductf;
7173
[EntityJsonConverter] partial record Maxf;
7274
[EntityJsonConverter] partial record Minf;
7375
[EntityJsonConverter] partial record Minusf;

0 commit comments

Comments
 (0)