💻
Algorithm
  • README
  • Array
    • At Most To Equal
    • Count Inversions In An Array
    • Interleaving Placement
    • Kadane
    • Left To Right State Transition
    • Permutation
    • Quick Select
    • Sliding Window
    • Two Pointers
  • Binary Tree
    • Avl Tree
    • Binary Search Tree
    • Serialization And Deserialization
    • Traversal
  • Company
    • Facebook
  • Cpp
    • Array
    • Memset 3 F
    • Overflow
  • Data Structure
    • Binary Indexed Tree
    • Segment Tree And Binary Index Tree
    • Segment Tree
    • Stack
    • Trie
    • Union Find
  • Dynamic Programming
    • Knapsack
      • 0 1 Knapsack
      • Bounded Knapsack
      • Unbounded Knapsack
    • Bitmask Dp
    • Dp On Subsets
    • Dp On Tree
    • Dp With Sorting
    • Selective State Dp
    • Travelling Salesperson
  • Graph
    • Minimum Spanning Tree
      • Kruskal
      • Prim
    • Shortest Path
      • Bellman Ford
      • Dijkstra
      • Floyd Warshall
      • Johnson
      • Shortest Path Faster Algorithm
    • Bi Directional Breadth First Search
    • Bipartite
    • Breadth First Search
    • Component Coloring
    • Component Count
    • Depth First Search
    • Eulerian Path
    • Maximum Bipartite Matching
    • Tarjan
    • Topological Sort
    • Tree Diameter
    • Tree Ring Order Traversal
  • Greedy
    • Greedy Scheduling
    • Regret Greedy
  • Math
    • Catalan Number
    • Combinatorics
    • Factorial
    • Factorization
    • Fast Pow
    • Gcd
    • Geometry
    • Get Digits
    • Lcm
    • Median Minimizes Sum Of Absolute Deviations
    • Mode
    • Modular Multiplicative Inverse
    • Palindrome
    • Prime Number
    • Round Up
    • Sieve Of Eratosthenes
    • Stars And Bars
    • Sum Of Sequence
  • Miscellaneous
    • Bin Packing
    • Floyds Tortoise And Hare
    • Hungarian
    • Palindrome
  • Sort
    • Bubble Sort
    • Cycle Sort
    • Heap Sort
    • Merge Sort
    • Quick Sort
    • Sorting
  • Stl
    • Cpp Stl
    • Istringstream
    • Lower Bound Upper Bound
    • Priority Queue
  • String
    • Kmp
    • Manacher
    • Rabin Karp
    • String Processing
    • Z
  • Backtracking
  • Binary Answer
  • Binary Lifting
  • Binary Search
  • Bit Manipulation
  • Date
  • Difference Array
  • Discretization
  • Divide And Conquer
  • Gray Code
  • Great Problems For Practice
  • Interval Scheduling Maximization
  • Io Optimization
  • K Subset Partitioning
  • Line Sweep
  • Longest Common Subsequence
  • Longest Increasing Subsequence
  • Meet In The Middle
  • Minmax
  • Mono Deque
  • Monotonic Stack
  • Offline Query
  • P And Np
  • Prefix State Map
  • Prefix Sum
  • Random
  • Reservoir Sampling
  • Reverse Polish Notation
  • Sqrt Decomposition
Powered by GitBook
On this page
  • Dot Product of Vectors
  • Usage

Was this helpful?

  1. Math

Geometry

PreviousGcdNextGet Digits

Last updated 1 year ago

Was this helpful?

Dot Product of Vectors

Dot product is also known as scalar product or inner product.

Algebraically, the dot product is the sum of the products of the corresponding entries of the two sequences of numbers.

a⋅b=∑i=1naibi\bm{a}\cdot \bm{b} = \sum_{i=1}^na_ib_ia⋅b=i=1∑n​ai​bi​

Geometrically, it is the product of the Euclidean magnitudes of the two vectors and the cosine of the angle between them.

a⋅b=∣a∣∣b∣cos⁡θ\bm{a}\cdot \bm{b}=\lvert \bm{a}\rvert\lvert \bm{b}\rvert\cos\thetaa⋅b=∣a∣∣b∣cosθ

Usage

  1. Calculate the angle between two vectors.

\cos\theta =\frac{\bm{a}\cdot \bm{b}}{\lvert \bm{a}\rvert\lvert \bm{b}\rvert}$$ 2. If two vectors are perpendicular, the dot product is `0`. ### Problem * [963. Minimum Area Rectangle II (Medium)](https://leetcode.com/problems/minimum-area-rectangle-ii/) ## Cross Product of Vectors Cross product is also known as **vector product** ((occasionally **directed area product**, to emphasize its geometric significance) ) The cross product $\bm{a}\times\bm{b}$ is defined as $\bm{a}$ vector $\bm{c}$ that is perpendicular (orthogonal) to both $\bm{a}$ and $\bm{b}$, with $\bm{a}$ direction given by the right-hand rule and a magnitude equal to the area of the parallelogram that the vectors span.

\bm{a}\times\bm{b}=\lvert\bm{a}\rvert\lvert\bm{b}\rvert\sin(\theta)\bm{n}

where $\bm{n}$ is a unit vector perpendicular to the plane containing $\bm{a}$ and $\bm{b}$. ![Finding the direction of the cross product by the right-hand rule.](../.gitbook/assets/cross-product-right-hand-rule.png) ## Check if three points are one te same line Say we have 3 points, `(x1, y1)`, `(x2, y2)` and `(x3, y3)`. Check if they are on the same line. Normally we can check if slopes are the same.

\frac{y_2-y_1}{x_2-x_1} = \frac{y_3-y_2}{x_3-x_2}

(y_2-y_1)\cdot(x_3-x_2) = (y_3-y_2)\cdot(x_2-x_1)

Example question: [2280. Minimum Lines to Represent a Line Chart (Medium)](https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart) ## Get angle between two points Assume we have two points `(x1, y1)` and `(x2, y2)`, we want to get the angle between these two points ```cpp if ((x1 > x2) || (x1 == x2 && y1 < y2)) swap(x1, x2), swap(y1, y2); // make sure these two points are ordered atan2(x2 - x1, y2 - y1) ``` Example question: [149. Max Points on a Line (Hard)](https://leetcode.com/problems/max-points-on-a-line)
Toavoid:1.Theprecisionerrorwemightgetfromthedivisions2.DividebyzeroissueWecanusethefollowingequation:To avoid: 1. The precision error we might get from the divisions 2. Divide by zero issue We can use the following equation:Toavoid:1.Theprecisionerrorwemightgetfromthedivisions2.DividebyzeroissueWecanusethefollowingequation: