Friday, August 16, 2024

Real-Life Example of Using numpy.fromfunction to Calculate Euclidean Distance from the Origin

Euclidean Distance Grid using NumPy fromfunction

๐Ÿ“ Euclidean Distance Grid using NumPy

This tutorial demonstrates how to compute a 2D grid where each value represents the Euclidean distance from the origin (0, 0). This is a foundational concept used in mathematics, physics, and computer graphics.


๐Ÿ“š Table of Contents


๐Ÿ“ What is Euclidean Distance?

Euclidean distance is the straight-line distance between two points in space. It is the most intuitive notion of distance.

๐Ÿ’ก Think of it as the shortest path between two points on a plane.

๐Ÿ“ Mathematical Formula

The Euclidean distance from the origin is:

\[ d = \sqrt{x^2 + y^2} \]

This formula comes from the Pythagorean theorem.

๐Ÿ“˜ Expand: Why this formula works

If you imagine a right triangle:

  • Horizontal side = x
  • Vertical side = y
  • Hypotenuse = distance
Then:

\[ d^2 = x^2 + y^2 \]

Taking square root gives the distance.


๐Ÿง  Step-by-Step Approach

  1. Define a function that computes distance
  2. Use NumPy to generate coordinates automatically
  3. Apply the function to every grid point

๐Ÿ’ป Code Example

import numpy as np

def euclidean_distance(x, y):
    return np.sqrt(x**2 + y**2)

distance_grid = np.fromfunction(euclidean_distance, (5, 5))

print(distance_grid)

๐Ÿ–ฅ Output

[[0. 1. 2. 3. 4. ]
 [1. 1.41421356 2.23606798 3.16227766 4.12310563]
 [2. 2.23606798 2.82842712 3.60555128 4.47213595]
 [3. 3.16227766 3.60555128 4.24264069 5. ]
 [4. 4.12310563 4.47213595 5. 5.65685425]]

๐Ÿ” Deep Explanation

1. How np.fromfunction Works

It creates arrays by calling a function with coordinate indices.

Internally:

\[ (x, y) = (0,0), (0,1), (0,2), ... \]

Each coordinate is passed into the function.

2. Vectorization

NumPy computes all values at once using vectorized operations.

\[ \text{Result} = \sqrt{X^2 + Y^2} \]

3. Grid Interpretation

Each value represents radial distance from center.

๐Ÿ“˜ Expand: Visualization Insight

If plotted as an image:

  • Center = dark (low values)
  • Edges = bright (high values)


๐Ÿ“Š Additional Math Insights

Distance in 3D

\[ d = \sqrt{x^2 + y^2 + z^2} \]

General n-Dimensional Distance

\[ d = \sqrt{\sum_{i=1}^{n} x_i^2} \]

Gradient of Distance Field

\[ \nabla d = \left(\frac{x}{\sqrt{x^2+y^2}}, \frac{y}{\sqrt{x^2+y^2}}\right) \]

This is useful in physics and graphics.


๐ŸŒ Real-Life Applications

  • Geography: Distance heatmaps
  • Physics: Potential fields
  • Computer Graphics: Distance fields
  • Machine Learning: Feature distances
๐Ÿ“˜ Expand: Distance Fields in Graphics

Distance fields are used for:

  • Font rendering
  • Shadow effects
  • Collision detection


๐ŸŽฏ Key Takeaways

  • Euclidean distance measures straight-line distance
  • NumPy enables efficient computation
  • fromfunction maps math directly to grids
  • Used in science, graphics, and AI

๐Ÿ Conclusion

This example demonstrates how mathematical concepts can be translated directly into code using NumPy. Understanding this bridge between math and programming is essential for fields like data science, physics, and computer vision.

Once you grasp this, you can extend it to more complex spatial problems and multidimensional computations.

No comments:

Post a Comment

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts