Dot and Cross

  • + 7 comments

    For those having difficulty understanding problem as I had, matrices multiplication is dot product of them.

    • + 1 comment

      Which is helpful to know, because the link provided doesn't say that.

      • + 3 comments

        it actually does if you look at the matrices.

        • + 5 comments

          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))

          • + 1 comment

            Copy and Paste my code on python compiler if it not execute add comment :|

            • + 0 comments

              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

          • + 0 comments

            thanks

          • + 0 comments

            code is working why this much of dislikes??

          • + 0 comments

            brother without using numpy how can i read matrix

          • + 0 comments

            in place of input() in 3rd and 4th line it should be raw_input()

        • + 0 comments

          here is problem solution in python programming. https://programs.programmingoneonone.com/2021/02/hackerrank-dot-and-cross-solution-python.html

    • + 2 comments

      Be careful with terminology. "Matrix multiplication" and "Matrix product" doesn't refer to the same thing.

      • + 1 comment

        Matrix product is the result of matrix multiplication?

        • + 0 comments

          nope

      • + 0 comments

        UPDATED SOLUTION IS HERE

        https://www.thecscience.com/2021/08/hackerrank-dot-and-cross-in-python-problem-solution.html

    • + 2 comments

      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))

      • + 1 comment

        test case 2 not runnning itseems

        • + 0 comments

          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))

      • + 0 comments

        no pa it didnt execute i want cross so i changed acc to that but still error

    • + 0 comments

      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)

    • + 0 comments

      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