dot

The dot tool returns the dot product of two arrays.

import numpy

A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])

print numpy.dot(A, B)       #Output : 11

cross

The cross tool returns the cross product of two arrays.

import numpy

A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])

print numpy.cross(A, B)     #Output : -2

Task

You are given two arrays $\mbox{A}$ and $\mbox{B}$. Both have dimensions of $N$X$N$. 

Your task is to compute their matrix product.

Input Format

The first line contains the integer $N$. 

The next $N$ lines contains $N$ space separated integers of array $\mbox{A}$. 

The following $N$ lines contains $N$ space separated integers of array $\mbox{B}$.

Output Format

Print the matrix multiplication of $\mbox{A}$ and $\mbox{B}$.

Sample Input
2
1 2
3 4
1 2
3 4

Sample Output
[[ 7 10]
 [15 22]]

Source: hackerrank | Difficulty: EASY


Solutions:
import numpy
n = int(input())
A = [[int(i) for i in input().strip().split()] for j in range(n)]
B = [[int(i) for i in input().strip().split()] for j in range(n)]
print(numpy.dot(A, B))


import numpy
m = int(input())
l = []
p = []
for i in range(m):
	l.append(list(map(int, input().split())))
for i in range(m):
	p.append(list(map(int, input().split())))
l = numpy.matrix(l)
p = numpy.matrix(p)
print(numpy.dot(l, p))


import numpy
n = int(input())
arra = numpy.array([list(map(int, input().split())) for allvals in range(0, n)])
arrb = numpy.array([list(map(int, input().split())) for allvals in range(0, n)])
print(numpy.dot(arra, arrb))


import numpy
N = range(int(input()))
print(numpy.dot(*[numpy.array([input().split() for _ in N], int) for _ in range(2)]))


import numpy as np
n = int(input())
a = np.array([input().split(' ') for i in range(n)], int)
b = np.array([input().split(' ') for i in range(n)], int)
print(np.dot(a, b))


import numpy
n = int(input())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
for i in range(n):
	b.append(list(map(int, input().split())))
print(numpy.dot(a, b))


import numpy as np
N = int(input())
(L1, L2) = ([], [])
for i in range(N):
	L1.append(list(map(int, input().split())))
for i in range(N):
	L2.append(list(map(int, input().split())))
npArr1 = np.array(L1)
npArr2 = np.array(L2)
npArr3 = np.array([[np.dot(npArr1[i], npArr2.transpose()[j]) for j in range(N)] for i in range(N)])
print(npArr3)


import numpy
n = int(input())
array_a = []
array_b = []
for _ in range(n):
	array_a.append([int(x) for x in input().split()])
for _ in range(n):
	array_b.append([int(x) for x in input().split()])
print(numpy.dot(array_a, array_b))


import numpy
n = int(input().strip())
a = numpy.array([list(map(int, input().strip().split())) for line in range(n)])
b = numpy.array([list(map(int, input().strip().split())) for line in range(n)])
print(numpy.dot(a, b))


import numpy

def col(matrix1, N, j):
	list1 = []
	for i in range(N):
		list1.append(int(matrix1[i][j]))
	return list1

def main():
	N = int(input())
	A = []
	B = []
	for i in range(N):
		A.append(list(map(int, input().split())))
	for i in range(N):
		B.append(list(map(int, input().split())))
	results = numpy.zeros((N, N))
	for i in range(N):
		for j in range(N):
			results[i][j] = numpy.dot(A[i], col(B, N, j))
	print(results.astype(int))
main()


import numpy
x = numpy.array(input().split(), int)
a = [numpy.array(input().split(), int) for i in range(x[0])]
b = [numpy.array(input().split(), int) for i in range(x[0])]
b = numpy.transpose(b)
c = numpy.array([], int)
c = [numpy.dot(a[i], b[j]) for j in range(x[0]) for i in range(x[0])]
print(numpy.transpose(numpy.reshape(c, (x[0], x[0]))))


import numpy
N = int(input())
a = []
b = []
for i in range(N):
	a.append(list(map(int, input().split())))
for i in range(N):
	b.append(list(map(int, input().split())))
a = numpy.array(a)
b = numpy.array(b)
print(numpy.dot(a, b))


import operator
import numpy

def read_f(f):
	return f(input())

def read_f_words(f):
	return mapl(f, input().split())

def mapl(f, x):
	return list(map(f, list(x)))

def read_a(f, n):
	ns = []
	for x in range(n):
		ns.append(read_f_words(f))
	return numpy.array(ns, f)
n = read_f(int)
a = read_a(int, n)
b = read_a(int, n)
print(numpy.dot(a, b))


import numpy
arr1 = []
arr2 = []
d = int(input().split()[0])
for i in range(d):
	arr1.append(list(map(int, input().split())))
for i in range(d):
	arr2.append(list(map(int, input().split())))
a1 = numpy.array(arr1, int)
a2 = numpy.array(arr2, int)
print(numpy.dot(a1, a2))


import numpy
n = int(input())

def get_array(n):
	return numpy.array(tuple((input().split() for _ in range(n))), int)
(a, b) = (get_array(n), get_array(n))
print(numpy.dot(a, b))


import numpy as np
i = int(input())
arr1 = np.array([input().split() for _ in range(i)], dtype=int)
arr2 = np.array([input().split() for _ in range(i)], dtype=int)
print(np.dot(arr1, arr2))


import sys
import numpy as np
n = int(input())
a_list = []
b_list = []
for _ in range(n):
	a_list.append([int(x) for x in input().split()])
for _ in range(n):
	b_list.append([int(x) for x in input().split()])
a_array = np.array(a_list)
b_array = np.array(b_list)
print(np.dot(a_array, b_array))


import numpy
N = int(input())
(a, b) = ([], [])
for _ in range(N):
	a.append(input().split())
for _ in range(N):
	b.append(input().split())
(A, B) = list(map(lambda x: numpy.array(x, int), [a, b]))
B = B.transpose()
c = [[0 for i in range(N)] for j in range(N)]
for i in range(N):
	for j in range(N):
		c[i][j] = numpy.dot(A[i], B[j])
C = numpy.array(c)
print(C)


import numpy as np
N = int(input())
a = np.array([input().split() for _ in range(N)], int)
b = np.array([input().split() for _ in range(N)], int)
print(np.matmul(a, b))


import numpy
N = int(input())
A = numpy.array([list(map(int, input().split())) for i in range(N)])
B = numpy.array([list(map(int, input().split())) for i in range(N)])
print(numpy.dot(A, B))


import numpy
n = int(input())
a = numpy.array([input().split() for i in range(n)], int)
b = numpy.array([input().split() for i in range(n)], int)
print(numpy.dot(a, b))


import numpy
import numpy
n = int(input())
m = n
arr1 = []
arr2 = []
while n > 0:
	n = n - 1
	arr1.append([int(x) for x in input().split()])
while m > 0:
	m = m - 1
	arr2.append([int(x) for x in input().split()])
my_array1 = numpy.array(arr1)
my_array2 = numpy.array(arr2)
print(numpy.dot(my_array1, my_array2))


import numpy as np
n = int(input())
(a, b) = ([], [])
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
(a, b) = (np.array(a), np.array(b))
print(a.dot(b))


import numpy
n = int(input())
a = []
for i in range(n):
	a.extend(list(map(int, input().split())))
a = numpy.array(a)
a.shape = (n, n)
b = []
for i in range(n):
	b.extend(list(map(int, input().split())))
b = numpy.array(b)
b.shape = (n, n)
print(numpy.dot(a, b))


import numpy as np
N = int(input())
a = np.array([list(map(lambda x: int(x), input().split())) for _ in range(N)])
b = np.array([list(map(lambda x: int(x), input().split())) for _ in range(N)])
print(np.dot(a, b))


import numpy
N = int(input().strip())
arrA = numpy.array([[int(x) for x in input().strip().split()] for _ in range(N)])
arrB = numpy.array([[int(x) for x in input().strip().split()] for _ in range(N)])
print(numpy.dot(arrA, arrB))


import numpy as np
n = int(input())
a = np.array([list(map(int, input().split())) for _ in range(n)])
b = np.array([list(map(int, input().split())) for _ in range(n)])
print(np.dot(a, b))


import numpy as np
repeat = int(input())
A = np.array([list(map(int, input().split(' '))) for i in range(repeat)])
B = np.array([list(map(int, input().split(' '))) for i in range(repeat)])
print(A.dot(B))


import numpy
N = int(input())
A = numpy.array([list(map(int, input().split())) for n in range(N)])
B = numpy.transpose(numpy.array([list(map(int, input().split())) for n in range(N)]))
AB = numpy.array([[numpy.dot(A[i], B[j]) for j in range(N)] for i in range(N)])
print(AB)


import numpy
N = int(input())
a = numpy.array([[i for i in map(int, input().split())] for i in range(N)])
b = numpy.array([[i for i in map(int, input().split())] for i in range(N)])
print(numpy.dot(a, b))


import numpy
n = int(input())
array_a = numpy.array([list(map(int, input().split())) for i in range(n)])
array_b = numpy.array([list(map(int, input().split())) for i in range(n)])
print(numpy.dot(array_a, array_b))


import numpy as np
n = int(input())
A = []
for i in range(n):
	A.append(list(map(int, input().split())))
arrA = np.array(A)
B = []
for i in range(n):
	B.append(list(map(int, input().split())))
arrB = np.array(B)
print(np.dot(arrA, arrB))


import numpy
n = int(input())
arr1 = numpy.array([input().split() for a in range(n)], int)
arr2 = numpy.array([input().split() for a in range(n)], int)
print(numpy.dot(arr1, arr2))


import numpy
n = int(input())
arrays = list()
for i in range(n):
	arrays.append(list(map(int, input().split())))
A = numpy.array(arrays)
arrays = list()
for i in range(n):
	arrays.append(list(map(int, input().split())))
B = numpy.array(arrays)
print(numpy.dot(A, B))


import numpy as np
n = int(input())
a = np.matrix(np.array([[int(i) for i in input().split()] for j in range(n)]))
b = np.matrix(np.array([[int(i) for i in input().split()] for j in range(n)]))
print(a * b)


import numpy
n = int(input())
A = [0] * n
B = [0] * n
for i in range(n):
	A[i] = list(map(int, input().split()))
for i in range(n):
	B[i] = list(map(int, input().split()))
A = numpy.array(A, int)
B = numpy.array(B, int)
C = numpy.dot(A, B)
print(C)


import numpy
n = int(input())
arrs = [numpy.array([list(map(int, input().strip().split())) for _ in range(n)]) for _ in range(2)]
print(numpy.dot(*arrs))


import numpy
n = int(input().strip())
(a, b) = ([], [])
for i in range(n):
	a.append(list(map(int, input().strip().split())))
for i in range(n):
	b.append(list(map(int, input().strip().split())))
print(numpy.dot(a, b))


import numpy
n = int(input())
A = numpy.array([input().split() for i in range(n)], int)
B = numpy.array([input().split() for j in range(n)], int)
b = numpy.transpose(B)
c = [[numpy.dot(A[i], b[j]) for j in range(n)] for i in range(n)]
print(numpy.array(c))


import numpy as np
N = int(input())
arr1 = []
arr2 = []
for _ in range(N):
	arr1.append([int(x) for x in input().split(' ')])
for _ in range(N):
	arr2.append([int(x) for x in input().split(' ')])
a1 = np.array(arr1).reshape(N, N)
a2 = np.array(arr2).reshape(N, N)
print(np.dot(a1, a2))


import numpy
N = int(input())
A = numpy.array([list(map(int, input().split())) for _ in range(N)])
B = numpy.array([list(map(int, input().split())) for _ in range(N)])
print(numpy.dot(A, B))


import numpy
N = int(input())
ll = []
for n in range(N):
	ll += [int(x) for x in input().split()]
a = numpy.array(ll).reshape(N, N)
ll = []
for n in range(N):
	ll += [int(x) for x in input().split()]
b = numpy.array(ll).reshape(N, N)
print(numpy.dot(a, b))


import numpy
n = int(input().strip())
a = []
for _ in range(n):
	a.append(list(map(int, input().strip().split())))
b = []
for _ in range(n):
	b.append(list(map(int, input().strip().split())))
print(numpy.dot(a, b))


import numpy
n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
B = [list(map(int, input().split())) for i in range(n)]
print(numpy.dot(A, B))


import numpy
N = int(input())
A = [[int(x) for x in input().split()] for i in range(N)]
B = [[int(x) for x in input().split()] for i in range(N)]
print(numpy.matmul(A, B))


import numpy
N = int(input())
(A, B) = [numpy.array([numpy.array(input().split(), int) for _ in range(N)]) for _ in range(2)]
print(numpy.dot(A, B))


import numpy
n = int(input())
arr1 = []
for _ in range(n):
	arr1.append([int(e) for e in input().split()])
arr2 = []
for _ in range(n):
	arr2.append([int(e) for e in input().split()])
arr1 = numpy.matrix(arr1, dtype=int)
arr2 = numpy.matrix(arr2, dtype=int)
print(arr1 * arr2)


import numpy
m = int(input())
a = numpy.array([input().split(' ') for _ in range(m)], int)
b = numpy.array([input().split(' ') for _ in range(m)], int)
print(numpy.dot(a, b))


import numpy
n = int(input())
ar1 = []
ar2 = []
for i in range(n):
	tmp = list(map(int, input().split()))
	ar1.append(tmp)
np_ar1 = numpy.array(ar1)
for i in range(n):
	tmp = list(map(int, input().split()))
	ar2.append(tmp)
np_ar2 = numpy.array(ar2)
print(numpy.dot(np_ar1, np_ar2))


import numpy
N = int(input())
a = numpy.array([list(map(int, input().split())) for i in range(N)])
b = numpy.array([list(map(int, input().split())) for i in range(N)])
print(numpy.dot(a, b))


import numpy
n = int(input())
a = numpy.array([list(map(int, input().split(' '))) for _ in range(n)])
b = numpy.array([list(map(int, input().split(' '))) for _ in range(n)])
print(numpy.dot(a, b))


import numpy as np
n = int(input().strip())
arr1 = np.array([[int(x) for x in input().strip().split()] for _ in range(n)])
arr2 = np.array([[int(x) for x in input().strip().split()] for _ in range(n)])
print(np.dot(arr1, arr2))


import numpy as np
n = int(input())
a = np.fromstring(' '.join([input() for _ in range(n)]), sep=' ', dtype=int).reshape(n, n)
b = np.fromstring(' '.join([input() for _ in range(n)]), sep=' ', dtype=int).reshape(n, n)
print(a.dot(b))


import sys
import numpy as np

def main():
	f = open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin
	N = int(f.readline())
	A = [[int(i) for i in f.readline().split()] for _ in range(N)]
	B = [[int(i) for i in f.readline().split()] for _ in range(N)]
	print(np.dot(A, B))
main()


import numpy
n = int(input())
print(numpy.dot(numpy.array([input().split() for i in range(n)], int), numpy.array([input().split() for i in range(n)], int)))


import numpy
N = int(input())
A = numpy.array([input().split() for i in range(N)], int)
B = numpy.array([input().split() for i in range(N)], int)
print(numpy.dot(A, B))


import numpy as np
import sys
N = int(sys.stdin.readline())
A = np.array([sys.stdin.readline().split() for _ in range(N)], int)
B = np.array([sys.stdin.readline().split() for _ in range(N)], int)
print(np.dot(A, B))


import numpy
n = int(input())
(A, B) = ([], [])
for _ in range(n):
	A.append([int(x) for x in input().split()])
for _ in range(n):
	B.append([int(x) for x in input().split()])
(A, B) = (numpy.array(A, int), numpy.array(B, int))
print(numpy.dot(A, B))


import numpy
n = int(input())
a = numpy.array([list(map(int, input().split())) for i in range(n)])
b = numpy.array([list(map(int, input().split())) for i in range(n)])
print(numpy.dot(a, b))


from numpy import array, dot, cross
N = int(input())
(A, B) = (array([[int(i) for i in input().strip().split()] for _ in range(N)]) for _ in range(2))
print(dot(A, B))


import numpy
n = int(input())
l = list()
for _ in range(n):
	l.append(list(map(int, input().split())))
a = numpy.array(l)
l.clear()
for _ in range(n):
	l.append(list(map(int, input().split())))
b = numpy.array(l)
print(numpy.dot(a, b))


import numpy
nb = int(input())
A = [numpy.array(list(map(int, input().split()))) for i in range(nb)]
B = list(map(numpy.array, list(zip(*[list(map(int, input().split())) for i in range(nb)]))))
C = numpy.array([[numpy.dot(a, b) for b in B] for a in A])
print(C)


import numpy
N = int(input())
A_list = []
B_list = []
for i in range(N):
	A_list += [int(x) for x in input().strip().split()]
for i in range(N):
	B_list += [int(x) for x in input().strip().split()]
A = numpy.array(A_list).reshape(N, N)
B = numpy.array(B_list).reshape(N, N)
print(A.dot(B))


import numpy as np
n = int(input())
a = [[int(x) for x in input().split()] for _ in range(n)]
b = [[int(x) for x in input().split()] for _ in range(n)]
print(np.dot(a, b))


import numpy as np
N = int(input())
arr1 = np.array([list(map(int, input().split())) for i in range(N)])
arr2 = np.array([list(map(int, input().split())) for i in range(N)])
arr1.reshape(N, N)
arr2.reshape(N, N)
print(np.dot(arr1, arr2))


import numpy
N = int(input())
motoarra = []
for idx in range(0, N, 1):
	mojir = input()
	hyo = mojir.split(' ')
	yokovec = [int(mono) for mono in hyo]
	motoarra.append(yokovec)
arra = numpy.array(motoarra)
motoarrb = []
for idx in range(0, N, 1):
	mojir = input()
	hyo = mojir.split(' ')
	yokovec = [int(mono) for mono in hyo]
	motoarrb.append(yokovec)
arrb = numpy.array(motoarrb)
print(numpy.dot(arra, arrb))


import numpy as np
n = int(input())
(a, b) = (np.array([list(map(int, input().split())) for _ in range(n)], int), np.array([list(map(int, input().split())) for _ in range(n)], int))
print(np.array([[np.dot(a[x], np.array([b_[y] for b_ in b])) for y in range(len(b[0]))] for x in range(len(a))]))


import numpy
n = int(input())
a = numpy.array([[int(e) for e in input().split()] for i in range(n)])
b = numpy.array([[int(e) for e in input().split()] for i in range(n)])
bt = numpy.transpose(b)
c = numpy.array([[numpy.dot(row, rowb) for rowb in bt] for row in a])
print(c)


import numpy
n = int(input())
a = numpy.array([input().split() for _ in range(n)], int)
b = numpy.array([input().split() for _ in range(n)], int)
print(numpy.dot(a, b))


import numpy
n = int(input())
a = [numpy.array(input().split(' '), int) for i in range(n)]
b = [numpy.array(input().split(' '), int) for i in range(n)]
print(numpy.dot(a, b))


import numpy
n = int(input())
arr1 = numpy.array([list(map(int, input().split())) for i in range(n)])
arr2 = numpy.array([list(map(int, input().split())) for i in range(n)])
print(numpy.dot(arr1, arr2))


import numpy
n = int(input())
arr1 = numpy.array([list(map(int, input().strip().split())) for i in range(n)])
arr2 = numpy.array([list(map(int, input().strip().split())) for i in range(n)])
print(numpy.dot(arr1, arr2))


import numpy
m = int(input())
my_array = []
my_arrayy = []
for i in range(m):
	my_array.append(list(map(int, input().split())))
for i in range(m):
	my_arrayy.append(list(map(int, input().split())))
my_array = numpy.array(my_array)
my_arrayy = numpy.array(my_arrayy)
print(numpy.dot(my_array, my_arrayy))


import numpy as np
import sys
k = int(next(sys.stdin))
a = np.array([list(map(int, next(sys.stdin).strip().split(' '))) for line_no in range(k)])
b = np.array([list(map(int, next(sys.stdin).strip().split(' '))) for line_no in range(k)])
print(a.dot(b))


import numpy
n = int(input())
a = []
for _ in range(n):
	a.append(list(map(int, input().split())))
a = numpy.matrix(a)
b = []
for _ in range(n):
	b.append(list(map(int, input().split())))
b = numpy.matrix(b)
print(a * b)


import numpy as np
N = int(input())
matrix1 = np.empty((N, N), dtype=int)
matrix2 = np.empty((N, N), dtype=int)
for i in range(N):
	inp = input()
	matrix1[i] = np.array(list(map(int, inp.split())))
for i in range(N):
	inp = input()
	matrix2[i] = np.array(list(map(int, inp.split())))
result = np.matmul(matrix1, matrix2)
print(result)


import numpy
N = int(input())
a = numpy.array([list(map(int, input().split())) for _ in range(N)])
b = numpy.array([list(map(int, input().split())) for _ in range(N)])
print(numpy.dot(a, b))


import numpy

def mult(a, b, n):
	ans = [[sum([a[i][j] * b[j][k] for j in range(n)]) for k in range(n)] for i in range(n)]
	return ans
n = int(input())
a = [[int(x) for x in input().split()] for _ in range(n)]
b = [[int(x) for x in input().split()] for _ in range(n)]
print(numpy.array(mult(a, b, n)))


import numpy
N = int(input())
A = numpy.array([input().split() for _ in range(N)], int)
B = numpy.array([input().split() for _ in range(N)], int)
print(A.dot(B))


import numpy as np

def main():
	n = int(input())
	a = [np.array(input().split(), int) for i in range(n)]
	b = [np.array(input().split(), int) for i in range(n)]
	print(np.dot(a, b))
main()


import numpy as np
n = int(input())
la = [input().split() for i in range(n)]
lb = [input().split() for i in range(n)]
a = np.array(la, int)
b = np.array(lb, int)
print(np.dot(a, b))


from numpy import array, dot, zeros, set_printoptions
N = int(input())
A = array([list(map(int, input().split())) for _ in range(N)])
B = array([list(map(int, input().split())) for _ in range(N)])
AB = zeros(shape=(N, N), dtype=int)
for i in range(N):
	for j in range(N):
		AB[i, j] = dot(A[i, :], B[:, j])
print(AB)


import numpy
N = int(input())
A = numpy.array([input().split() for _ in range(N)], int)
B = numpy.array([input().split() for _ in range(N)], int)
C = numpy.dot(A, B)
print(C)


import numpy
N = int(input())
A = numpy.array([input().split() for _ in range(N)], int)
B = numpy.array([input().split() for _ in range(N)], int)
print(numpy.dot(A, B))


import numpy
size = int(input())
arr1 = numpy.array([input().split() for _ in range(size)], int)
arr2 = numpy.array([input().split() for _ in range(size)], int)
arr2t = numpy.transpose(arr2)
prod = numpy.array([[numpy.dot(arr1[i], arr2t[j]) for j in range(size)] for i in range(size)], int)
print(prod)


import numpy
N = int(input())
A = numpy.array([list(map(int, input().strip().split())) for x in range(N)], int)
B = numpy.array([list(map(int, input().strip().split())) for x in range(N)], int)
print(numpy.dot(A, B))


import numpy
n = int(input())
a = numpy.array([list(map(int, input().split())) for _ in range(n)])
b = numpy.array([list(map(int, input().split())) for _ in range(n)])
print(numpy.dot(a, b))