2.2 Multiply matrix with a vector

How to perform the following matrix/vector multiplication?

[ 1 2 3 ]   [ 1 ] 
[ 4 5 6 ] * [ 2 ] 
[ 7 8 8 ]   [ 3 ]
 

In Mathematica the dot "." is used for Matrix multiplication. In Matlab the "*" is used. In Fortran, MATMUL is used.

Mathematica

a={{1,2,3},{4,5,6},{7,8,8}}; 
x={1,2,3}; 
 
a.x
 

Out[7]= {14,32,47}
 

 

Matlab

A=[1 2 3;4 5 6;7 8 9]; 
x=[1; 2; 3]; 
c=A*x
 

    14 
    32 
    50
 

 

Julia

Same exact code as Matlab.

julia> A=[1 2 3;4 5 6;7 8 9] 
3Œ3 Matrix{Int64}: 
 1  2  3 
 4  5  6 
 7  8  9 
 
julia> x=[1;2;3] 
3-element Vector{Int64}: 
 1 
 2 
 3 
 
julia> c=a*x 
3-element Vector{Int64}: 
 14 
 32 
 50
 

Ada

with Ada.Text_Io; use Ada.Text_Io; 
with Ada.Numerics.Real_Arrays; 
     use Ada.Numerics.Real_Arrays; 
 
procedure t1 is 
  A : constant real_matrix := 
          (( 1.0,  2.0,  3.0), 
          ( 4.0,  5.0,  6.0), 
          ( 7.0,  8.0,  9.0)); 
 
  V : constant real_vector := (1.0,2.0,3.0); 
  procedure Put (X : real_vector) is 
    begin 
        FOR e of X LOOP 
            put_line(float'image(e)); 
        END LOOP; 
  end put; 
begin 
    put(A*v); 
end t1;
 

compile and run

>gnatmake -gnat2012 t1.adb 
gcc -c -gnat2012 t1.adb 
gnatbind -x t1.ali 
gnatlink t1.ali 
>./t1 
 1.40000E+01 
 3.20000E+01 
 5.00000E+01
 

Fortran

PROGRAM mainIMPLICIT NONE 
integer, parameter :: dp = kind(1.D0) 
integer, parameter :: N=3 
real(kind=dp), parameter, DIMENSION(N, N) :: & 
  A=DBLE(reshape([ 1.0, 2.0, 3.0,& 
                   4.0, 5.0, 6.0,& 
                   7.0, 8.0, 9.0], shape(A), & 
                                     order=[2,1])) 
 
real(kind=dp), parameter, DIMENSION(N, 1) :: & 
           x=DBLE(reshape([1.0, 2.0, 3.0], shape(x))) 
 
real(kind=dp), DIMENSION(N, 1) :: result 
integer, DIMENSION(2)    :: siz 
 
result = MATMUL(A,x) 
siz    = SHAPE(result) 
Write(*,*) result 
 
print *, "number of rows = ",siz(1) 
print *, "number of columns = ",siz(2) 
 
end program
 

compile and run

>gfortran  -fcheck=all -Wall -Wconversion 
   -Wextra -Wconversion-extra -pedantic test.f90 
>./a.out 
14.000000000000000  32.000000000000000 50.000000000000000 
 number of rows =            3 
 number of columns =            1
 

Maple

A:=Matrix([[1,2,3],[4,5,6],[7,8,9]]): 
x:=Vector([1,2,3]): #default is column vector 
c:=A.x;
 

\[ \left [ {\begin {array}{c} 14\\ 32\\ 50 \end {array}} \right ] \]

 

Python

import numpy as np 
mat=np.array([[1,2,3],[4,5,6],[7,8,8]]) 
b=np.array([1,2,3]) 
b.shape=(3,1) 
r=dot(mat,b)
 

array([[14], 
       [32], 
       [47]])