Matrix-exponentiation operator: Difference between revisions

add RPL
m (syntax highlighting fixup automation)
(add RPL)
Line 3,060:
4221 31322 9580</pre>
 
=={{header|RPL}}==
Operators can not be overloaded, but we can easily create a new word, with same syntax as the classical exponentiation operator. the power must be a signed integer.
{| class="wikitable"
! RPL code
! Comment
|-
|
SWAP '''IF''' OVER 0 < '''THEN''' INV '''END '''
DUP IDN → m id
≪ ABS id
'''WHILE''' OVER '''REPEAT''' m * SWAP 1 - SWAP '''END '''
SWAP DROP
≫ ≫ ''''MATXP'''' STO
|
'''MATXP''' ''( [[m]] n -- [[m^n]] ) ''
inverse matrix if n<0
store matrix and identity
initialize stack with abs(n) and identity
multiply n times
clean stack
return m^n
|}
 
[[3 2][2 1]] 0 '''MATXP'''
[[3 2][2 1]] 1 '''MATXP'''
[[3 2][2 1]] 2 '''MATXP'''
[[3 2][2 1]] 5 '''MATXP'''
[[3 2][2 1]] -5 '''MATXP'''
{{out}
<pre>
5: [[ 1 0 ]
[ 0 1 ]]
4: [[ 3 2 ]
[ 2 1 ]]
3: [[ 13 8 ]
[ 8 5 ]]
2: [[ 987 610 ]
[ 610 377 ]]
1: [[ -377 610 ]
[ 610 -987]]
</pre>
=={{header|Ruby}}==
Ruby's standard library already provides the matrix-exponentiation operator. It is <code>Matrix#**</code> from package 'matrix' of the standard library. [[MRI]] 1.9.x implements the matrix-exponentiation operator in file [http://redmine.ruby-lang.org/projects/ruby-19/repository/entry/lib/matrix.rb matrix.rb], <code>def **</code> (around [http://redmine.ruby-lang.org/projects/ruby-19/repository/entry/lib/matrix.rb#L961 line 961]).
1,150

edits