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
42 changes: 39 additions & 3 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,40 @@ checked by exact division. `Entity.Factorize` asks the same layer.
| `MathS.Polynomials.Factor("(x ^ 3 - 5 * x ^ 2 * y ^ 2) * (3 * x ^ 2 + x * y ^ 3 * z ^ 3 - 3 * y ^ 2) * (x - y ^ 3 * z ^ 2 - 5 * y * z ^ 2)", "x")` | `null` | `x ^ 2 * (x - 5 * y ^ 2) * (x - y ^ 3 * z ^ 2 - 5 * y * z ^ 2) * (3 * x ^ 2 + x * y ^ 3 * z ^ 3 - 3 * y ^ 2)` |
| `"3 * x ^ 6 + (-3) * x ^ 5 * y ^ 3 * z ^ 2 + (-15) * x ^ 5 * y * z ^ 2 + x ^ 5 * y ^ 3 * z ^ 3 + -x ^ 4 * y ^ 6 * z ^ 5 + (-5) * x ^ 4 * y ^ 4 * z ^ 5 + (-3) * x ^ 4 * y ^ 2 + 3 * x ^ 3 * y ^ 5 * z ^ 2 + 15 * x ^ 3 * y ^ 3 * z ^ 2 + (-15) * x ^ 5 * y ^ 2 + 15 * x ^ 4 * y ^ 5 * z ^ 2 + 75 * x ^ 4 * y ^ 3 * z ^ 2 + (-5) * x ^ 4 * y ^ 5 * z ^ 3 + 5 * x ^ 3 * y ^ 8 * z ^ 5 + 25 * x ^ 3 * y ^ 6 * z ^ 5 + 15 * x ^ 3 * y ^ 4 + (-15) * x ^ 2 * y ^ 7 * z ^ 2 + (-75) * x ^ 2 * y ^ 5 * z ^ 2".Factorize()` | `x ^ 2 * z ^ 2 * ((-15) * y ^ 7 + (-75) * y ^ 5) + x ^ 3 * (z ^ 5 * (5 * y ^ 8 + 25 * y ^ 6) + z ^ 2 * (3 * y ^ 5 + 15 * y ^ 3) + 15 * y ^ 4) + x ^ 4 * (z ^ 2 * (15 * y ^ 5 + 75 * y ^ 3) + z ^ 5 * (-y ^ 6 + (-5) * y ^ 4) + (-3) * y ^ 2 + (-5) * y ^ 5 * z ^ 3) + x ^ 5 * (z ^ 2 * ((-3) * y ^ 3 + (-15) * y) + (y * z) ^ 3 + (-15) * y ^ 2) + 3 * x ^ 6` | `x ^ 2 * (x - 5 * y ^ 2) * (x - y ^ 3 * z ^ 2 - 5 * y * z ^ 2) * (3 * x ^ 2 + x * y ^ 3 * z ^ 3 - 3 * y ^ 2)` |

### A scalar product, a determinant or a transpose of a name compiled as a number is compiled

**Not silent where it was refused: it compiles, or is refused saying why. Silent where it compiled: a truncated value
or a 0 is the value.** `dot`, `cross`, `det`, `transpose`, `trace` and an entry `A[k, l]` of an expression with a name
beside a written matrix, as in `dot([0, 0, 1], cos(p) * [x, y, z])`, are nodes that simplification leaves as written,
since the name may stand for a matrix. A variable compiled as a number is one, so both compilers now carry each of them
out on the vector or the matrix the expression is with its names as numbers. `Compile(params Variable[])`, whose
variables are all numbers, had no form for these nodes and refused them, asking for a bug report. `Compile<...>`
converted a `double` beside a written matrix of whole numbers, or beside a matrix argument over `long`, to `long`, and
so truncated it; it now widens the matrix instead, as it already widened the narrower of two matrices, and `dot` and
`cross` of vectors over two element types widen the same way. Both compilers read a transpose beside a written matrix
as a number, and multiplied it out to 0, and refused a vector product beside one; a transpose or a product taken of a
number has no value, and is now refused for that reason. A name
compiled as a matrix is not read as a number: the operation is compiled on the matrix passed in, as before
([#105](https://github.com/asc-community/AngouriMath/issues/105)).

| Input | Was (2.9.0) | Now |
|---|---|---|
| `"dot([0, 0, 1], cos(p) * [x, y, z] + sin(p) / abs([a, b, c]) * cross([a, b, c], [x, y, z]))".Compile("a", "b", "c", "p", "x", "y", "z")` | throws `UncompilableNodeException`: `DotProductf` does not support compilation | the third component, `cos(p) z + sin(p) (a y - b x) / abs([a, b, c])` |
| the same, `.Compile<double, double, double, double, double, double, double, double>` | throws `UncompilableNodeException`: vectors over `Int64` and `Double` | the same; `-3.9818867534572675` at `(0.3, 1, 1.7, 2.4, 3.1, 3.8, 4.5)` |
| `"dot(x * [1, 2], [3, 4])".Compile("x")` | throws `UncompilableNodeException` | `11 x` |
| `"dot(x * [1, 2], [3, 4])".Compile<double, double>("x")` at `x = 0.5` | `0` | `5.5` |
| `"det(x * [[1, 2], [3, 4]])".Compile("x")` | throws `UncompilableNodeException` | `-2 x ^ 2` |
| `"det(x * [[1, 2], [3, 4]])".Compile<double, double>("x")` at `x = 0.5` | `0` | `-0.5` |
| `"[1, 0]T * transpose(x * [[1, 2], [3, 4]]) * [0, 1]"`, either compiler, at `x = 0.3` | `0` | `3 x`: `0.8999999999999999` |
| `"[1, 0]T * transpose(A) * [0, 1]"` over a number `A`, either compiler | `0` | throws `UncompilableNodeException`: the transpose is taken of a number |
| `"[0, 0, 1]T * cross(x * [1, 0, 0], [0, y, 0])"`, either compiler | throws `UncompilableNodeException` | `x y` |
| `"det(x * A)".Compile<double, long[,], double>("x", "A")` at `x = 0.5`, `A = [[1, 2], [3, 4]]` | `0` | `-0.5` |
| `"trace(A / x)".Compile<double, long[,], double>("x", "A")` at `x = 2`, the same `A` | `2` | `2.5` |
| `"x * A".Compile<double, long[,], double[,]>("x", "A")` at `x = 0.5`, the same `A` | throws `UncompilableNodeException`: a matrix over `Int64` | `[[0.5, 1], [1.5, 2]]` |
| `"dot(z * a, [1, 2])".Compile<Complex, double[], Complex>("z", "a")` at `z = i`, `a = [1, 2]` | throws `UncompilableNodeException`: no coercion from `Complex` to `Double` | `5i` |
| `"dot(a, b)".Compile<double[], Complex[], Complex>("a", "b")` | throws `UncompilableNodeException`: vectors over `Double` and `Complex` | the scalar product |
| `"dot(a, b)".Compile("a", "b")` | throws `UncompilableNodeException`, asking for a bug report | throws `UncompilableNodeException`: an operand is a number, and `Compile<..., double[]>` takes vectors |

---

## 2.9.0 — since 2.8.0
Expand Down Expand Up @@ -999,9 +1033,11 @@ In the other order each value computed at the defaults came back inside the scop

**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.
computed once both operands are vectors, by `Simplify`, `Evaled`, or a compiled delegate taking `double[]`, `double[,]`
or `Complex[]`. Substituting vectors for the names puts them in and computes nothing, as in every node:
`dot([0, 0, 1], v)` with `v = [x, y, z]` substituted is `dot([0, 0, 1], [x, y, z])` until it is simplified or
evaluated. 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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1216,3 +1216,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed

[Tests/UnitTests/Convenience/InverseHyperbolicCosinePrincipalValueTest.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

[AngouriMath/Functions/Compilation/MatrixOperationsOverNumbers.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/MatrixOperationOverNumbersCompilationTest.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
5 changes: 4 additions & 1 deletion Sources/AngouriMath/Docs/Usage/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ numbers and it is `x ^ 2 - 2`
`dot(a, b)` and `cross(a, b)` of a name are nodes too, carried out once both operands are vectors:
`dot(a, b)` with `a` and `b` substituted by `[1, 2, 3]` and `[4, 5, 6]` simplifies to `32`, and
`cross(W, r)` with `[0, 0, w]` and `[x, y, z]` to `[-w * y, w * x, 0]`. A compiled delegate takes
the vectors as `double[]` or `double[,]`, and `cross` returns a column. `cross` is not commutative,
the vectors as `double[]` or `double[,]`, and `cross` returns a column. A name compiled as a number is
one, so these and the matrix nodes above are carried out on it when compiling: over a number `x`,
`dot(x * [1, 2], [3, 4])` compiles to `11 x` and `det(x * [[1, 2], [3, 4]])` to `-2 x ^ 2`, the same
in `Compile(params Variable[])`, whose variables are all numbers. `cross` is not commutative,
and its operands are kept in their order, as is a product with a `cross` in it: `cross(a, b) + cross(b, a)`
is left as it is. Vectors of different lengths, or a `cross` of vectors that do not have three
components, have no value, and the node is left as it is; where the vector is written out, as in
Expand Down
6 changes: 6 additions & 0 deletions Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,12 @@ internal static FastExpression Compile(Entity func, IEnumerable<Variable> variab
varNamespace[varName] = id++;
foreach (var constant in Variable.NamedConstants.Values)
func = func.Substitute(constant, constant.Value);
// Every variable here is a number, so a scalar product, a determinant and the rest of a name beside a
// written matrix are carried out, and one still left cannot be a number and is refused. Left as nodes,
// they were refused for want of a compiled form, or read as numbers by the simplification below.
// https://github.com/asc-community/AngouriMath/issues/105
func = Compilation.MatrixOperationsOverNumbers.Rewritten(func, varNamespace.ContainsKey);
Compilation.MatrixOperationsOverNumbers.Check(func, varNamespace.ContainsKey, anyLeft: true);
// An expression built out of matrices often has a number for its value -- [1, 0]T * [[a, b], [c, d]] * [0, 1]
// is b -- and the LINQ compiler simplifies such an expression first, for that reason. This one did not, and
// refused it. https://github.com/asc-community/AngouriMath/issues/425
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ internal static TDelegate Compile<TDelegate>(
// before compiling, so those failed along with the ones that genuinely cannot
// be compiled (https://github.com/asc-community/AngouriMath/issues/425).
// Only expressions that mention a matrix pay for this.
// A scalar product, a determinant and the rest of a name compiled as a number beside a written matrix are
// carried out first, as simplification will not, and one taken of a number is refused before simplification
// reads it as a number. https://github.com/asc-community/AngouriMath/issues/105
bool IsNumberArgument(Variable variable) => typesAndNames.Any(c => c.variable == variable && !TensorCompilation.IsMatrixType(c.type));
expr = MatrixOperationsOverNumbers.Rewritten(expr, IsNumberArgument);
MatrixOperationsOverNumbers.Check(expr, IsNumberArgument, anyLeft: false);
if (expr.Nodes.Any(node => node is Entity.Matrix))
expr = TensorCompilation.SimplifiedAroundMatrixArguments(expr, IsMatrixArgument);
expr = CompiledMembership.Rewritten(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,19 @@ internal static (Expression Left, Expression Right) Widened(Expression left, Exp
return (left, right);
}

/// <summary>
/// A matrix beside a number of a wider type, widened to that type, as <see cref="Widened"/> widens the narrower of
/// two matrices. The number was converted to the entries' type instead, which truncated it: over a double
/// <c>x</c> and a matrix argument <c>A</c> over long, <c>det(x * A)</c> was 0 at <c>x = 0.5</c>, and a complex
/// number beside a matrix over double was refused. Unchanged where the number is not wider than the entries.
/// https://github.com/asc-community/AngouriMath/issues/105
/// </summary>
internal static Expression WidenedBeside(Expression tensor, Type number, Func<Expression, Type, Expression> convert)
{
var element = ElementOf(tensor.Type);
return element != number && mapKits.ContainsKey((element, number)) ? Widen(tensor, number, convert) : tensor;
}

private static Expression Widen(Expression tensor, Type to, Func<Expression, Type, Expression> convert)
{
var element = ElementOf(tensor.Type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,13 @@ public static int WholeIndex(Complex index)
if (!kitsByTensor.ContainsKey(operand.Type))
throw new UncompilableNodeException(
$"`{node.Stringize()}` is taken of vectors, and an operand compiles to {operand.Type.Name}: compile it with a vector or a matrix type, such as double[] or double[,], for the operands");
if (operands[0].Type != operands[1].Type)
throw new UncompilableNodeException($"Two vectors over different element types, {ElementOf(operands[0].Type)} and {ElementOf(operands[1].Type)}, cannot be combined");
var vectors = kitsByTensor[operands[0].Type];
return Expression.Call(node is Entity.DotProductf ? vectors.Dot : vectors.Cross, operands[0], operands[1]);
// The narrower of two vectors widened to the other, as a sum of two matrices is: a written vector of whole
// numbers beside one over double was refused. https://github.com/asc-community/AngouriMath/issues/105
var (left, right) = Widened(operands[0], operands[1], convert);
if (left.Type != right.Type)
throw new UncompilableNodeException($"Two vectors over different element types, {ElementOf(left.Type)} and {ElementOf(right.Type)}, cannot be combined");
var vectors = kitsByTensor[left.Type];
return Expression.Call(node is Entity.DotProductf ? vectors.Dot : vectors.Cross, left, right);
}
if (node is not (Entity.Transposef or Entity.Determinantf or Entity.Tracef or Entity.MatrixEntryf))
return null;
Expand Down Expand Up @@ -826,6 +829,8 @@ internal static Expression Build(Entity.Matrix matrix, IReadOnlyList<Expression>
{
if (node is Entity.Powf)
return Expression.Call(leftKit!.Power, left, Exponent(right, convert));
left = WidenedBeside(left, right.Type, convert);
leftKit = kitsByTensor[left.Type];
var scalar = convert(right, ElementOf(left.Type));
return node switch
{
Expand All @@ -836,6 +841,8 @@ internal static Expression Build(Entity.Matrix matrix, IReadOnlyList<Expression>
_ => null,
};
}
right = WidenedBeside(right, left.Type, convert);
rightKit = kitsByTensor[right.Type];
var value = convert(left, ElementOf(right.Type));
return node switch
{
Expand Down
Loading
Loading