# Multi-Dimensional array multiplication in fortran

• Fortran

## Main Question or Discussion Point

Hi;

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

such as;

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

thanks

Related Programming and Computer Science News on Phys.org
rcgldr
Homework Helper
Is there anyone who knows multi-dimensional array multiplication in fotran?
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.

I mean more than 2 dimensions.

rcgldr
Homework Helper
I mean more than 2 dimensions.
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.

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?

Germán

SteamKing
Staff Emeritus
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.

rcgldr
Homework Helper
More importantly, how do you define mathematically such a multiplication?
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: