Multi-Dimensional array multiplication in fortran

In summary, the conversation discusses the possibility of multiplying multi-dimensional arrays in Fortran, specifically using the programming language APL as an example. APL is able to perform matrix multiplication with arrays of any number of dimensions, as long as the last dimension of the first array matches the first dimension of the second array. This can also be used for comparing strings in a more generic way.
  • #1
selmayda
9
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
  • #2
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.
 
  • #3
I mean more than 2 dimensions.
 
  • #4
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.
 
  • #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?


Germán
 
  • #6
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
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:

1. What is a multi-dimensional array in Fortran?

A multi-dimensional array in Fortran is a data structure that can hold multiple values of the same data type in a grid-like format. It is defined by two or more indices, allowing for data to be accessed using two or more dimensions.

2. How do you declare a multi-dimensional array in Fortran?

To declare a multi-dimensional array in Fortran, you can use the DIMENSION statement followed by the array name and the size of each dimension enclosed in parentheses. For example, DIMENSION A(3,4) declares an array named A with 3 rows and 4 columns.

3. How do you multiply two multi-dimensional arrays in Fortran?

To multiply two multi-dimensional arrays in Fortran, you can use the MATMUL function. This function takes in two arrays of the same size and performs matrix multiplication, returning a new array with the multiplied values.

4. Can a multi-dimensional array have more than two dimensions in Fortran?

Yes, a multi-dimensional array in Fortran can have more than two dimensions. In fact, it can have up to seven dimensions. However, it is not recommended to use arrays with more than three dimensions as it can become difficult to manage and understand.

5. How do you access individual elements in a multi-dimensional array in Fortran?

To access individual elements in a multi-dimensional array in Fortran, you can use the indices of the element you want to access. For example, in a 2-dimensional array A(3,4), A(2,3) will give you the element in the second row and third column.

Similar threads

  • Programming and Computer Science
2
Replies
62
Views
4K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
2
Views
929
  • Programming and Computer Science
Replies
4
Views
4K
Replies
7
Views
723
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
1
Views
507
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top