Multi-Dimensional array multiplication in fortran

  • Fortran
  • Thread starter selmayda
  • Start date
  • #1
9
0

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
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,697
528
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.
 
  • #3
9
0
I mean more than 2 dimensions.
 
  • #4
rcgldr
Homework Helper
8,697
528
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.
 
  • #5
1,065
53
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
 
  • #6
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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.
 
  • #7
rcgldr
Homework Helper
8,697
528
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:

Related Threads on Multi-Dimensional array multiplication in fortran

Replies
4
Views
8K
Replies
5
Views
2K
Replies
3
Views
3K
Replies
1
Views
3K
  • Last Post
Replies
1
Views
4K
Replies
7
Views
7K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
20
Views
2K
Top