Addition-chain exponentiation: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 112: Line 112:
addch 27182*31415
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>
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>

I tried to compute the expression given by addch 31415 using matrix multiplication as my multiplier, and using

<lang j>A=: ".;._2]0 :0
4 _3 4r3 _1r4
_13r3 19r4 _7r3 11r24
3r2 _2 7r6 _1r4
_1r6 1r4 _1r6 1r24
)</lang>

However, I ran out of memory needed to represent the arbitrary precision intermediate results.

When I use floating point values instead of arbitrary precision, the result I got was:

<lang j> mul=: +/ .*
do addch 31415
_ __ _ __
__ _ __ _
_ __ _ __
__ _ __ _</lang>

Here a single underline represents floating point infinity and a double underline represents negative floating point infinity.

So I have elected to not try to compute the other requested results.

That said, note that this approach exceeds the values given in the A003313 by 1 in 22 cases, when considering the first 100 values in the sequence.

Revision as of 17:02, 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>

I tried to compute the expression given by addch 31415 using matrix multiplication as my multiplier, and using

<lang j>A=: ".;._2]0 :0

 4   _3    4r3  _1r4

_13r3 19r4 _7r3 11r24

 3r2 _2    7r6  _1r4
_1r6  1r4 _1r6   1r24

)</lang>

However, I ran out of memory needed to represent the arbitrary precision intermediate results.

When I use floating point values instead of arbitrary precision, the result I got was:

<lang j> mul=: +/ .*

  do addch 31415
_ __  _ __

__ _ __ _

_ __  _ __

__ _ __ _</lang>

Here a single underline represents floating point infinity and a double underline represents negative floating point infinity.

So I have elected to not try to compute the other requested results.

That said, note that this approach exceeds the values given in the A003313 by 1 in 22 cases, when considering the first 100 values in the sequence.