Jump to content

Matrix-exponentiation operator: Difference between revisions

m
Line 1,868:
<syntaxhighlight lang="j">pow=: pow1=: 4 : 'mp/ mp~^:(I.|.#:y) x'</syntaxhighlight>
 
This implements an optimization where the exponent is represented in base 2, and repeated squaring is used to create a list of relevant powers of the base matrix, which are then combined using matrix multiplication. Note, however, that these two definitions treat a zero exponent differently (<code>m pow0 0</code> gives an identity matrix whose shape matches m, while <code>m pow1 0</code> gives a scalar 1).
 
Example use:
 
<syntaxhighlight lang=J>
(3 2,:2 1) pow 3
55 34
34 21
</syntaxhighlight>
 
=={{header|JavaScript}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.