Fortran Multi-Dimensional array multiplication in fortran

AI Thread Summary
The discussion centers around multi-dimensional array multiplication in Fortran, specifically the multiplication of arrays with more than two dimensions. Participants clarify that traditional matrix multiplication typically involves two dimensions, while the inquiry pertains to higher-dimensional arrays. A comparison is made to APL, a programming language that allows for generic matrix-like operations across multiple dimensions, emphasizing that the last dimension of the first array must match the first dimension of the second for multiplication to occur. The output dimensions depend on the other dimensions of the input arrays. Fortran provides functions like dot_product() and matmul() for two-dimensional matrices, but participants suggest that for higher dimensions, one might need to implement looping to handle the multiplication manually. The conversation also touches on the mathematical definition of such multiplications, indicating a need for clarity in how these operations are defined and executed in programming contexts.
selmayda
Messages
9
Reaction score
0
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
 
Technology news on Phys.org
selmayda said:
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.
 
selmayda said:
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
 
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.
 
SteamKing said:
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:

Similar threads

Replies
20
Views
3K
Replies
2
Views
1K
Replies
14
Views
5K
Replies
3
Views
2K
Replies
4
Views
1K
Back
Top