How to resolve the algorithm Matrix chain multiplication step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Matrix chain multiplication step by step in the J programming language

Table of Contents

Problem Statement

Using the most straightfoward algorithm (which we assume here), computing the product of two matrices of dimensions (n1,n2) and (n2,n3) requires n1n2n3 FMA operations. The number of operations required to compute the product of matrices A1, A2... An depends on the order of matrix multiplications, hence on where parens are put. Remember that the matrix product is associative, but not commutative, hence only the parens can be moved. For instance, with four matrices, one can compute A(B(CD)), A((BC)D), (AB)(CD), (A(BC))D, (AB)C)D. The number of different ways to put the parens is a Catalan number, and grows exponentially with the number of factors. Here is an example of computation of the total cost, for matrices A(5,6), B(6,3), C(3,1): In this case, computing (AB)C requires more than twice as many operations as A(BC). The difference can be much more dramatic in real cases. Write a function which, given a list of the successive dimensions of matrices A1, A2... An, of arbitrary length, returns the optimal way to compute the matrix product, and the total cost. Any sensible way to describe the optimal solution is accepted. The input list does not duplicate shared dimensions: for the previous example of matrices A,B,C, one will only pass the list [5,6,3,1] (and not [5,6,6,3,3,1]) to mean the matrix dimensions are respectively (5,6), (6,3) and (3,1). Hence, a product of n matrices is represented by a list of n+1 dimensions. Try this function on the following two lists: To solve the task, it's possible, but not required, to write a function that enumerates all possible ways to parenthesize the product. This is not optimal because of the many duplicated computations, and this task is a classic application of dynamic programming. See also Matrix chain multiplication on Wikipedia.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Matrix chain multiplication step by step in the J programming language

Source code in the j programming language

moo =: verb define
  s =. m =. 0 $~ ,~ n=._1+#y
  for_lmo. 1+i.<:n do.
    for_i. i. n-lmo do.
      j =. i + lmo
      m =. _ (<i;j)} m
      for_k. i+i.j-i do.
        cost =. ((<i;k){m) + ((<(k+1);j){m) + */ y {~ i,(k+1),(j+1)
        if. cost < ((<i;j){m) do.
          m =. cost (<i;j)} m
          s =. k (<i;j)} s
        end.
      end.
    end.
  end.

  m;s
)

poco =: dyad define
  'i j' =. y
   if. i=j do.
     a. {~ 65 + i      NB. 65 = a.i.'A'
   else.
     k =. x {~ <y      NB. y = i,j
     '(' , (x poco i,k) , (x poco j ,~ 1+k) , ')'
   end.
)

optMM =: verb define
  'M S' =. moo y
  smoutput 'Cost: ' , ": x: M {~ <0;_1
  smoutput 'Order: ', S poco 0 , <:#M
)


   optMM 5 6 3 1
Cost: 48
Order: (A(BC))

   optMM 1 5 25 30 100 70 2 1 100 250 1 1000 2
Cost: 38120
Order: ((((((((AB)C)D)E)F)G)(H(IJ)))(KL))

   optMM 1000 1 500 12 1 700 2500 3 2 5 14 10
Cost: 1773740
Order: (A((((((BC)D)(((EF)G)H))I)J)K))


  

You may also check:How to resolve the algorithm Babbage problem step by step in the C++ programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Groovy programming language
You may also check:How to resolve the algorithm Vector products step by step in the Fortran programming language
You may also check:How to resolve the algorithm Comments step by step in the Pike programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the XPL0 programming language