Repository navigation
Compile a function of a matrix argument as the function of each entry, and vectors as arrays - #2435
Merged
Conversation
…, and vectors as arrays Evaluation applies a function to a matrix entry by entry: ExpandOnOneArgument maps a one-argument function over it, and ExpandOnTwoArguments a two-argument one over a matrix and a number or two matrices of one shape. Measured on master and on 2.6.0 for sin, cos, exp, ln, sgn, conj, log, max, min, mod, binomial and a number to the power of a matrix. The exceptions are a power with a matrix base, which is the matrix power and is left unevaluated where the exponent is not whole (sqrt(A) included), and abs of a column vector, which is its norm. The LINQ compiler refused all of these for a matrix argument. It now compiles each the same way: - A function of one argument maps over the entries, through a lambda nested in the tree, and a listed function of two maps over a matrix and a number, or zips two matrices of one shape. The entry type widens where the function's value does, and a complex matrix keeps a real value as complex. A matrix of statements, as A < 0 is, is refused, and so are equality of two matrices and a matrix to the power of a matrix. - abs of something known when compiling to be a vector, a column or a row, is its norm, sqrt(sum |v_k|^2), scaled against overflow, as #2423 makes evaluation take it over the complex numbers and for a row as for a column. abs of a two-dimensional array is entry by entry, and a column or a row of more than one entry throws InvalidMatrixOperationException at the call, its value being a number. - A one-dimensional array of any of the six element types is a column vector, as an argument and as the result. A result known not to be a column is refused at Compile, otherwise at the call. - Two matrices over different element types widen the narrower to the other's, as a number does, so a written matrix of whole numbers combines with a matrix argument whatever the constants are compiled as. CompileMatrixTest's sin(A) refusal now asks for sin(A) as a number, which is still refused; sin(A) as a matrix is what this change compiles. Measured: every new test compares the compiled value with Substitute then Evaled, entry by entry, over double and Complex at negative and non-real points; over double an entry evaluation puts off the real line must be NaN. The norm of a row is checked against sqrt(sum |v_k|^2) of the evaluated row, written out, as #2423 is not on this branch. work/compilecheck's matrix-argument rows, with these functions added, all agree with evaluation or throw where it has no answer. Compile, matrix and tensor tests: 1318 passed. FSharpWrapperUnitTests, same filter: 60 passed. The NativeAOT smoke test with a map over Complex[,] and a norm over double[] added: 76 of 76. The 2.6.0 values in BREAKING-CHANGES.md are measured on the 2.6.0 package. Part of #526. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2435 +/- ##
==========================================
+ Coverage 86.85% 86.87% +0.02%
==========================================
Files 312 313 +1
Lines 69709 69928 +219
Branches 20378 20440 +62
==========================================
+ Hits 60543 60750 +207
- Misses 4923 4934 +11
- Partials 4243 4244 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follows #2429 (#2422). A function of a matrix argument now compiles under the LINQ compiler, as evaluation has it, and a
T[]passes at the delegate's boundary as a column vector.What evaluation does, measured on master and on the 2.6.0 package, and so what compiling does now:
sin,cos,exp,ln,sgn,arcsin,conj,floor,!,gamma,2^Aandx^A.log(2, A),log(A, 3),max,min,mod,binomial,polygamma,lambertw, the falling and rising factorials,divisorsigma,gcdandlcm.absof a column or a row vector is its normsqrt(sum |v_k|^2), as The norm of a vector with complex entries is NaN when evaluated and wrong when simplified #2423 makes it in evaluation.absof any other matrix is entry by entry. AT[,]argument that turns out at the call to be a vector of more than one entry throwsInvalidMatrixOperationException, because its value would be a number while the delegate returns a matrix. A 1×1 array passes.sqrt(A)andA^(1/2)stay unevaluated in evaluation, so compiled they throw at the call.T[], for all six element types, is an n×1 column, as an argument and as a result. A result known not to be a column is refused atCompile.A * sin(x) + B,A + [[1, 2], [3, 4]],sin(x) * [[…]] * A. Element types widen where the value does: whole numbers to double, and real values to complex in a complex matrix.sin(A),exp(A),abs(A),2 ^ A,log(2, A),max(A, B)UncompilableNodeExceptiondouble[]argumentAngouriBugExceptionorKeyNotFoundExceptionThese rows are in BREAKING-CHANGES.md.
Still refused:
A^B, comparisons of matrices,A = B,not A, and a matrix used as a number unless it is known to be 1×1. Matrix functions in the power-series sense,exp(A)as a series orA^(1/2), are computed neither by evaluation nor by compilation. Whetherf(A)of a square matrix should mean the matrix function is a question for v3 (#1019).Checked on this branch:
doubleandComplex, forT[,],T[]andGenTensorarguments.🤖 Generated with Claude Code