We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.matmul(A,B))
Why did NumPy's cross product not give me a vector?
Good question imaginary person asking this question. The cross product of 2 vectors produces another vector (which are what NumPy calls "1d arrays"). So you might reasonably wonder, "Why did the example only produce a scalar (ie, just one number)?"
The reason the example gives the result as just "-2" is that when two vectors lie on the x-y plane, then their cross product vector is orthogonal (basically perpendicular) to the x-y plane.
What does that mean in plane English?
That the x and y components/values become 0. It's easier to see with an animation, so here's a link to the gif on the wiki page for cross products (can't seem to add it, so go to the wiki page for cross products).
If you give NumPy only a 2d vector (ie, a 1d NumPy array that has only 2 rows), it still treats it as a 3d vector (ie, a 1d NumPy array that has 3 rows where the 3rd value is assumed to be 0). The calculation of the cross product itself also shows this, so I've included it for those who are interested.
For some beautiful animations by 3blue1brown, check out a great explanation called "Cross products | Chapter 10, Essence of linear algebra"
The step-by-step calculation of the cross product
Note that you can think of a 3d vector (ie, a 1d NumPy array with 3 rows) as having values for the coordinates x, y, and z. Symbolically,
[ x, y, z].
If any of the following steps seem mysterious, then the section titled "Matrix notation" in the cross product wiki article might help:
Dot and Cross
You are viewing a single comment's thread. Return to all comments →
For those having difficulty understanding problem as I had, matrices multiplication is dot product of them.
Which is helpful to know, because the link provided doesn't say that.
it actually does if you look at the matrices.
undestand this question by this code
import numpy
a=int(input())
arr1=numpy.array([list(map(int,input().split())) for _ in range(a)])
arr2=numpy.array([list(map(int,input().split())) for _ in range(a)])
print(numpy.dot(arr1,arr2))
Copy and Paste my code on python compiler if it not execute add comment :|
here is solution of problem Dot and Cross in python 2 and 3 https://solution.programmingoneonone.com/2020/06/hackerrank-dot-and-cross-problem-solution-python.html
thanks
code is working why this much of dislikes??
brother without using numpy how can i read matrix
in place of input() in 3rd and 4th line it should be raw_input()
here is problem solution in python programming. https://programs.programmingoneonone.com/2021/02/hackerrank-dot-and-cross-solution-python.html
Be careful with terminology. "Matrix multiplication" and "Matrix product" doesn't refer to the same thing.
Matrix product is the result of matrix multiplication?
nope
UPDATED SOLUTION IS HERE
https://www.thecscience.com/2021/08/hackerrank-dot-and-cross-in-python-problem-solution.html
Copy and Paste my code on python3 compiler if it not execute add comment :|
import numpy
a=int(input())
arr1=numpy.array([list(map(int,input().split())) for _ in range(a)])
arr2=numpy.array([list(map(int,input().split())) for _ in range(a)])
print(numpy.dot(arr1,arr2))
test case 2 not runnning itseems
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.matmul(A,B))
no pa it didnt execute i want cross so i changed acc to that but still error
from numpy import array,dot
a = int(input())
arr1, arr2=array([[list(map(int,input().split())) for _ in range(a)] for _ in range(2)])
print(dot(arr1,arr2))
easy to understand i think x)
Why did NumPy's cross product not give me a vector?
Good question imaginary person asking this question. The cross product of 2 vectors produces another vector (which are what NumPy calls "1d arrays"). So you might reasonably wonder, "Why did the example only produce a scalar (ie, just one number)?"
The reason the example gives the result as just "-2" is that when two vectors lie on the x-y plane, then their cross product vector is orthogonal (basically perpendicular) to the x-y plane.
What does that mean in plane English?
That the x and y components/values become 0. It's easier to see with an animation, so here's a link to the gif on the wiki page for cross products (can't seem to add it, so go to the wiki page for cross products).
If you give NumPy only a 2d vector (ie, a 1d NumPy array that has only 2 rows), it still treats it as a 3d vector (ie, a 1d NumPy array that has 3 rows where the 3rd value is assumed to be 0). The calculation of the cross product itself also shows this, so I've included it for those who are interested.
For some beautiful animations by 3blue1brown, check out a great explanation called "Cross products | Chapter 10, Essence of linear algebra"
The step-by-step calculation of the cross product
Note that you can think of a 3d vector (ie, a 1d NumPy array with 3 rows) as having values for the coordinates x, y, and z. Symbolically,
[ x, y, z].
If any of the following steps seem mysterious, then the section titled "Matrix notation" in the cross product wiki article might help:
[ 1, 2, 0] x [ 3, 4, 0 ]
= determinant( [ [ 2, 0 ], [ 4, 0 ] ] ) i
- determinant( [ [ 1, 0 ], [ 3, 0 ] ] ) j
+ determinant( [ [ 1, 2 ], [ 3, 4 ] ] ) k
So this becomes
= (2 · 0 - 0 · 4) i
- (1 · 0 - 0 · 3) j
+ (1 · 3 - 2 · 4) k
Which simplifies to
= 0 i
- 0 j
+ (-2) k