Fortran element by element array multiplication

Click For Summary

Discussion Overview

The discussion revolves around the problem of performing element-wise multiplication of two vectors in Fortran, as opposed to Matlab where this operation is straightforward using the syntax A.*B. Participants explore various methods to achieve this in Fortran, including both loop-based and array operations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their difficulty in performing element-wise multiplication in Fortran, contrasting it with the ease of doing so in Matlab.
  • Another participant suggests a loop-based approach to achieve the desired multiplication, providing a code snippet that multiplies corresponding elements of two arrays.
  • A third participant expresses surprise at the lack of a built-in function for element-wise multiplication in Fortran.
  • Another participant mentions that in Fortran 90 and later, element-wise multiplication can be performed using array syntax, suggesting two different forms of the operation.

Areas of Agreement / Disagreement

Participants generally agree on the methods to perform element-wise multiplication, but there is no consensus on the existence of a built-in function for this operation in Fortran.

Contextual Notes

Some participants assume that the vectors are of the same dimension, and there are mentions of potential issues with array initialization and declarations that remain unaddressed.

Kwetla
Messages
2
Reaction score
0

Homework Statement



Hello,

I'm having a problem in multiplying two vectors together in a specific way in Fortran. I can do it in Matlab, but can't work out how to do it in Fortran.

The problem is that i want to multiply two vectors together, but only each element by it's corresponding element in the other vector. In Matlab you simply use A.*B which works perfectly.



Homework Equations



For example if A = [1 2 3]'

and B = [4 5 6]'

Then A.*B = [4 10 18]



The Attempt at a Solution



Any attempt i make in Fortran tries to multiply all the elements together (usually by dot product etc), but i just want the first element multiplied by the first element, and then the second mulitplied by the second etc.

Sorry about the strange formatting, i tried to crowbar this question into the template...
 
Last edited by a moderator:
Physics news on Phys.org
Kwetla said:

Homework Statement



Hello,

I'm having a problem in multiplying two vectors together in a specific way in Fortran. I can do it in Matlab, but can't work out how to do it in Fortran.

The problem is that i want to multiply two vectors together, but only each element by it's corresponding element in the other vector. In Matlab you simply use A.*B which works perfectly.



Homework Equations



For example if A = [1 2 3]'

and B = [4 5 6]'

Then A.*B = [4 10 18]



The Attempt at a Solution



Any attempt i make in Fortran tries to multiply all the elements together (usually by dot product etc), but i just want the first element multiplied by the first element, and then the second mulitplied by the second etc.

Sorry about the strange formatting, i tried to crowbar this question into the template...
Assuming that all variables and the three arrays are declared and that A and B are initialized, this should work.
Code:
N = 3
DO I = 1, N
  C(I) = A(I) * B(I)
END DO
 
Ah yes, that would work. Although I'm surprised there isn't a function for it.

Thanks a lot though!
 
Vector times vector can also be a scalar, 32.
 
If you are using at least Fortran 90, I believe the following will work:

C = A*B

or

C(1:N) = A(1:N)*B(1:N)

The 1st version assumes A,B,C have the same dimension. The 2nd version is safer.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
6K
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K