Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Multi-Dimensional array multiplication in fortran

  1. Mar 25, 2013 #1

    Is there anyone who knows multi-dimensional array multiplication in fortan ?

    such as;

    A(5,5,3,3) * B(5,5,3,3)

  2. jcsd
  3. Mar 25, 2013 #2


    User Avatar
    Homework Helper

    Do you mean matrix multiplication, which is 2 dimensions for each variable, or more than 2 dimensions? In the case of a programming language called APL, a generic form of matrix like operations is performed on the last dimension of the left variable and the first dimension of the second variable.
  4. Mar 25, 2013 #3
    I mean more than 2 dimensions.
  5. Mar 25, 2013 #4


    User Avatar
    Homework Helper

    Using APL as an example, and using the names "table" to mean the first dimension, "column" to mean the second dimension, and "row" to mean the last dimension, then "rows" of numbers from the left variable are multiplied with "tables" of numbers from the right variable, and the products summed to produce the output variable. The last dimension of the left variable and the first dimension of the right variable must be the same size, the sizes of the other dimensions don't matter. For example, you could "multiply" A(2,3,4) with B(4,5,6), and the resulting output would have size (2,3,5,6). I'm not sure why you'd want to do this.
  6. Mar 25, 2013 #5
    I have never known how to multiply matrices of more than 2 dimensions each...how do you that?

    For two 2-d matrices at a time, Fortran does provide dot_product() and matmul(), read up.

    Other than that, maybe you are trying to take too much of a shortcut for what you want to do...is ...how about you do some looping and reduce the problem?

  7. Mar 25, 2013 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    More importantly, how do you define mathematically such a multiplication? It sounds like you want to throw a bunch of numbers at a program and are hoping the program will properly sort everything out.
  8. Mar 25, 2013 #7


    User Avatar
    Homework Helper

    This was done in the programming language APL, which treats matrix multplies as a specific case of it's "inner product" operator. For a matrix multiply, the syntax is:

    A +.× B

    but this is allowed for A and B of any number of dimensions as long as the size of the last dimension of A equals the size of the first dimension of B. If A and B are vectors, then the result is the dot product (inner product) of two vectors. APL's inner product operator is more generic, to compare strings, you could use:

    A ^.= B

    Where ^ means binary AND, and = means compare (outputs 0 or 1). If A and B are 2 dimensional matrices, then the result produces a 1 for each row of A that equals a column of B (otherwise it produces a 0), but this will also work for vectors or for A and B of greater than 2 dimensions. B needs to be transposed so that it's first dimension contains the strings. There's a unary operator to do this.
    Last edited: Mar 25, 2013
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook