Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector multiplication using MATMUL in Fortran

I am trying to multiply part of a column vector (n,1) by a part of another row vector (1,n). Both parts have the same length. So I should get a matrix (n,n).

Here is my simple code:

PROGRAM test_pack_1
REAL :: m(1,10), x(10,1), y(10,10)

m = reshape( (/ 1, -1, 3, 2, 1, 2, -2, -2, 1, 0 /), (/ 1, 10 /))
x = reshape( (/ 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 /), (/ 10, 1 /))

y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))


DO j = 1,10
PRINT* ;WRITE(*,*) y(:,j)
ENDDO
print *

END PROGRAM

I'm Using:

ifort -g -debug -traceback -check all -ftrapuv test_cshift.f90

And I'm getting:

test_cshift.f90(7): error #6241: The shapes of the arguments are inconsistent or nonconformable.   [MATMUL]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))
-------------^
test_cshift.f90(7): error #6366: The shapes of the array expressions do not conform.   [Y]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))
like image 489
user3203131 Avatar asked Mar 10 '14 16:03

user3203131


People also ask

What is Matmul operation?

MatMul operation takes two tensors and performs usual matrix-matrix multiplication, matrix-vector multiplication or vector-matrix multiplication depending on argument shapes. Input tensors can have any rank >= 1.

What is NP Matmul?

The numpy. matmul() function returns the matrix product of two arrays. While it returns a normal product for 2-D arrays, if dimensions of either argument is >2, it is treated as a stack of matrices residing in the last two indexes and is broadcast accordingly.

What is vector multiplication used for?

In mathematics, Vector multiplication refers to one of several techniques for the multiplication of two (or more) vectors with themselves. It may concern any of the following articles: Dot product – also known as the "scalar product", a binary operation that takes two vectors and returns a scalar quantity.

How do you multiply matrices in Julia?

In Julia, we can do matrix multiplication on two variables with a Matrix data type. We can use the asterisk ( * ) operator for this purpose.


1 Answers

The problem is that x(1:9,1) isn't of shape [9 1] but [9]. You need to use x(1:9, 1:1). The same goes for m.

like image 193
francescalus Avatar answered Sep 18 '22 01:09

francescalus