Addition-chain exponentiation: Difference between revisions

From Rosetta Code
Content added Content deleted
(J: partial example -- still working on the matrix multiplies -- the result seems to be incredibly huge.)
Line 103: Line 103:
Example use:
Example use:


<lang j>mul=: '*'
<lang j> mul=: '*'
addch 15
addch 15
C*C*C =:A*B*B=:A*A
C*C*C =:A*B*B=:A*A

Revision as of 16:45, 27 August 2011

Addition-chain exponentiation is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at Addition-chain exponentiation. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

In cases of special objects (such as with matrices) the operation of multiplication can be excessively expensive. In these cases the operation of multiplication should be avoided or reduced to a minimum.

In mathematics and computer science, optimal addition-chain exponentiation is a method of exponentiation by positive integer powers that requires a minimal number of multiplications. It works by creating a shortest addition chain that generates the desired exponent. Each exponentiation in the chain can be evaluated by multiplying two of the earlier exponentiation results. More generally, addition-chain exponentiation may also refer to exponentiation by non-minimal addition chains constructed by a variety of algorithms (since a shortest addition chain is very difficult to find).

The shortest addition-chain algorithm requires no more multiplications than binary exponentiation and usually less. The first example of where it does better is for , where the binary method needs six multiplies but a shortest addition chain requires only five:

(binary, 6 multiplications)
(shortest addition chain, 5 multiplications)
Table demonstrating how to do Exponentiation using Addition Chains
#Multiplications Exponentiation Specific implementation of Addition Chains to do Exponentiation
0 a ↑ 1 a
1 a ↑ 2 a × a
2 a ↑ 3 a × a × a
2 a ↑ 4 (b←a × a ) × b
3 a ↑ 5 (b←a × a ) × b × a
3 a ↑ 6 (b←a × a ) × b × b
4 a ↑ 7 (b←a × a ) × b × b × a
3 a ↑ 8 (d←(b←a × a ) × b ) × d
4 a ↑ 9 (c←a × a × a ) × c × c
4 a ↑ 10 (d←(b←a × a ) × b ) × d × b
5 a ↑ 11 (d←(b←a × a ) × b ) × d × b × a
4 a ↑ 12 (d←(b←a × a ) × b ) × d × d
5 a ↑ 13 (d←(b←a × a ) × b ) × d × d × a
5 a ↑ 14 (d←(b←a × a ) × b ) × d × d × b
5 a ↑ 15 (d←(b←a × a ) × b ) × d × d × a
4 a ↑ 16 (h←(d←(b←a × a ) × b ) × d ) × h

The number of multiplications required follows this sequence: 0, 1, 2, 2, 3, 3, 4, 3, 4, 4, 5, 4, 5, 5, 5, 4, 5, 5, 6, 5, 6, 6, 6, 5, 6, 6, 6, 6, 7, 6, 7, 5, 6, 6, 7, 6, 7, 7, 7, 6, 7, 7, 7, 7, 7, 7, 8, 6, 7, 7, 7, 7, 8, 7, 8, 7, 8, 8, 8, 7, 8, 8, 8, 6, 7, 7, 8, 7, 8, 8, 9, 7, 8, 8, 8, 8, 8, 8, 9, 7, 8, 8, 8, 8, 8, 8, 9, 8, 9, 8, 9, 8, 9, 9, 9, 7, 8, 8, 8, 8...

This sequence can be found at: http://oeis.org/A003313

Task requirements: Using the following values:

and

Repeat task Matrix-exponentiation operator, except use addition-chain exponentiation to better calculate:

, and .

Also: Display a count of how many multiplications were done in each case.

Note: There are two ways to approach this task:

  • Brute force - try every permutation possible and pick one with the least number of multiplications. If the brute force is a simpler algorithm, then present it as a subtask under the subtitle "Brute force", eg ===Brute Force===.
  • Some clever algorithm - the wikipedia page has some hints, subtitle the code with the name of algorithm.

Note: Binary exponentiation does not usually produce the best solution. Provide only optimal solutions.

Kudos (κῦδος) for providing a routine that generate sequence A003313 in the output.

J

<lang j>vars=: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' mul=: '+/ .*' eq=: '=:' b2e=:3 :0 NB. express binary chain as equation

 (|.x{.~##:y) b2e y
 b=. }.#: y 
 v=. (-#b) {. x
 ({.x),eq,(,(mul,~])"0 |.b#v), ([,eq,])/(],mul,])"0 v

)

addch=:3 :0

 p=. q: y
 N=: +/n=. #@#:"0 p
 assert N <: 1+#vars NB. enough variables?
 v=. (|.N{.vars) <@(' ',])/.~ (+/\n) I. 1+i.N
 3}.;v b2e&.> p

)</lang>

This implementation finds an expression which calculates the given exponent. b2e finds the binary chain expression for that exponent. addch finds the addition chain expression.

This implementation of addch uses binary chain for each prime factor of the exponent and combines them.

Example use:

<lang j> mul=: '*'

  addch 15

C*C*C =:A*B*B=:A*A

  addch 31415

L*M*M=:L*L =:G*I*J*K*K*K=:J*J=:I*I=:H*H=:G*G =:A*B*C*F*F*F=:E*E=:D*D=:C*C=:B*B=:A*A

  addch 27182

N*N =:A*B*C*E*I*K*M*M*M=:L*L=:K*K=:J*J=:I*I=:H*H=:G*G=:F*F=:E*E=:D*D=:C*C=:B*B=:A*A

  addch 27182*31415

a*a =:Y*Z*Z=:Y*Y =:T*V*W*X*X*X=:W*W=:V*V=:U*U=:T*T =:N*O*P*S*S*S=:R*R=:Q*Q=:P*P=:O*O=:N*N =:A*B*C*E*I*K*M*M*M=:L*L=:K*K=:J*J=:I*I=:H*H=:G*G=:F*F=:E*E=:D*D=:C*C=:B*B=:A*A</lang>